Plugable factory with template?

Started by
3 comments, last by Skizz 16 years, 5 months ago
Hi all, I didn´t know how to name the thread so I used that silly name. My problem is this: I have a entitymanger, my entites live in pools, and I want to register a pool for every entity type. I do something like this to register: pEntityMgr->RegisterPool("CAMERA",new CPool<CCamera*>(10)); This stores in the entity manager something like m_Pools["CAMERA"]=pPool; that is a map. so far so good. The problem is that to do things this way my map should be declared this way std::map <string,CPool*>; and this is not possible because CPool is a template. Did not come across any solution for this, is there any? any other better method to do what I want without having to hardcode pools per entity entitype? Thanks in advance, HexDump.
Advertisement
You could have an additional superclass that isn't templated:
class Pool{};template<class Type>class PoolTemplate : public Pool{};std::map <string,Pool*>;


This should work provided all your virtual functions signatures aren't dependant on the template parameter.
Create an interface class as a common base to all CPool<> classes:
class IPool{public:  virtual ~IPool ()  {  }  // various functions};template <class T>class CPool : public IPool{  // implementation of interface functions};

and then you can do:
  map <string, IPool *>    pool_map;  pool_map [identifier] = new CPool <type>;  pool_map [another_identifier] = new CPool <another_type>;

Skizz
Ohhh, thanks a lot.

By the way, I know I´m not at the correcto forum but don´t want to open another one in game dev forum.

I´m running into a little "problem". My aproach to the entity manager makes me register a pool for every type of entity, I mean, player, orctype1, orctype2, door, event, etc.... won´t this be a nightmare to mantain? Do people does this things this way?. Any better aproach?

Thanks in advance,
HexDump.
Quote:Original post by HexDump
Ohhh, thanks a lot.

By the way, I know I´m not at the correcto forum but don´t want to open another one in game dev forum.

I´m running into a little "problem". My aproach to the entity manager makes me register a pool for every type of entity, I mean, player, orctype1, orctype2, door, event, etc.... won´t this be a nightmare to mantain? Do people does this things this way?. Any better aproach?

Thanks in advance,
HexDump.


The pool idea itself is OK. However, I think you're being too specific in your pool creation. For example, orctype1, orctype2 and other NPC types are all more or less the same. So, instead of orctype1, orctype2, etc... have:
class NPC{private:  string m_npc_type;      // what type of NPC: orctype1, orctype2  graphic *m_graphic;     // the data required to draw the object  AI *m_ai;               // the AI controller for the objectpublic:  void Update (void)  {    m_ai->Update (this);  // update the NPC state using the AI controller  }  void Render (RenderContext *context)  {    m_graphic->Render (context);  }};

By making the differences between NPC types defined by the data the NPC holds, you only need one NPC type instead of a type specialised for each type of NPC, thus reducing the number of pools required.

Now you may be thinking "What if I want to find the closest NPC that is a goblin?" There's nothing stopping you from doing the following:
class NPC{public:  NPC (string npc_type) : m_npc_type (npc_type)  {    if (!m_npc_type_lists.contains (m_npc_type))    {      m_npc_type_lists [m_npc_type] = new list <NPC *>;    }      m_npc_type_lists [m_npc_type]->AddLast (this);  }  ~NPC ()  {    // remove from NPC type lists  }  static list <NPC *> GetListOfType (string npc_type)  {    return m_npc_type_lists [npc_type];  }private:  static map <string, NPC *> m_npc_type_lists;};

Skizz

This topic is closed to new replies.

Advertisement