Laurens Posted December 14, 2009 Share Posted December 14, 2009 Hello, I am currently working on a service provider which will hold instances of classes such as the renderer and the state manager which all derive from the Service class. To do so I though I could use templating. Broken header file: #include <map> #include "Service.h" class ServiceProvider { public: // Methods. template<class Type> void AddService(Type type, Service* service); template <class Type> Service* GetService(Type type); private: // Globals. map<class type, Service*> services; }; I want to be able to store all these services in the map with the type as the key so I can do GetService<StateManager> for example. Is it possible to template member attributes this way? Thanks! Quote Link to comment Share on other sites More sharing options...
Laurens Posted December 14, 2009 Author Share Posted December 14, 2009 On a side note: Class templating is not what I am looking for. This would force my service provider to handle only one type of class where I need one service provider to handle many. Quote Link to comment Share on other sites More sharing options...
Laurens Posted December 14, 2009 Author Share Posted December 14, 2009 Nevermind. C++ does not have typeof so it would never work anyway. I got to get out of my C# head Quote Link to comment Share on other sites More sharing options...
Rick Posted December 14, 2009 Share Posted December 14, 2009 A hack around typeof() would be to make a pure virtual method in your base class called string GetType(). Then every derived class needs to override it and return it's type. class Base { public: virtual string GetType()=0; }; class Child1 : public Base { public: virtual string GetType() { return "CHILD1"; } }; Quote Link to comment Share on other sites More sharing options...
Qbound Posted December 15, 2009 Share Posted December 15, 2009 Hi, I think you are more looking for a factroy pattern... i would do something like this: First define a virtual class called Service. class CService { public: float m_fMyVar; UINT8 m_uiMyType; public: CService(); virtual ~CService(); UINT8 GetType(){return m_uiType;}; virtual void Setup() = 0; virtual void Execute() = 0; virtual void Shutdown() = 0; }; typedef CService* LPSERVICE; This is your pure virutal BaseService class. Now you can use it as the base for all the service youll provide. Just do the following. class CMyNewService : public CService { public: CMyNewSerrvice(); ~CMyNewService(); void Setup(){ m_fMyVar = 42; uiMyType = 1; }; void Execute(){ m_fMyVar++; }; void Shutdown(){ m_fMyVar = 0; }; }; typedef CMyNewSerrvice* LPMYNEWSERVICE; Then you have to create a factory or a managerclass. class CServiceProvider{ protected: map<UINT8, LPCSERVICE> m_mapService; public: CServiceProvider(); ~CServiceProvider(); void Register( LPCSERVICE _pService ){ m_mapService[ _pService->GetType() ] = _pService; }; LPCSERVICE Get( UINT8 _uiService ){ return m_mapService[ _uiService ]; }; }; The usage looks like this LPMYNEWSERVICE m_pService = new CMyNewService(); m_pService->Setup(); CServiceProvider cServiceProvider; cServiceProvider.Register( m_pService ); LPSERVICE m_pCOMMON_SERVICE = cServiceProvider.Get( 1 ); Hopefully i understood you right!? this is right from brain to forum so no testing cu Oliver Quote Windows Vista 64 / Win7 64 | 12GB DDR3 1600 | I7 965 | 2 * 280GTX sli | 2 * 300GB Raptor | 2 * 1.5TB Link to comment Share on other sites More sharing options...
Laurens Posted December 15, 2009 Author Share Posted December 15, 2009 That looks pretty interesting. I will try that when I get home tonight. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.