Jump to content

Templating members


Laurens
 Share

Recommended Posts

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!

Link to comment
Share on other sites

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"; }

};

Link to comment
Share on other sites

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

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...