Jump to content
  • Advertisement
Sign in to follow this  
HexDump

Design question about storing template objects in a stl::container.

This topic is 2804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Sorry for the dumb topic, didn't really know how to categorize my question.

There's a problem I have found several times and hasn't been able to solve it in a good way. Now I'm building a Component System, and this Systems contain components (1 system, 1 type). so I have created a tempalte like this:


template <typename T>
class ComponentSystem
{
public:
typedef std::map<int,T*> TCompMap;
virtual ~ComponentSystem();

virtual T* createComponent(int id);
virtual void removeComponent(int id);
private:
std::map<int,T*> _components;
};

template<typename T>
ComponentSystem<T>::~ComponentSystem()
{
TCompMap::iterator itB=_components.begin();
TCompMap::iterator itE=_components.end();
while(itB!=itE)
{
delete(itB->second);
++itB;
}
_components.clear();
}

template<typename T>
T* ComponentSystem<T>::createComponent(int id)
{
assert(_components.find(id)!=_components.end());
_components[id]=new T;
}

template<typename T>
void ComponentSystem<T>::removeComponent(int id)
{
TCompMap::iterator it=_components.find(id)!=_components.end();
delete(it->second);
_components.erase(it);
}


Pretty simple. My problem is that I need to register this systems to a Manager, but hey! they are templatized... I could create a base dumb interface to have starting point for all this templates, but again, the createMethod uses the template so, the base interface can't have it. I could, of course, give away the tempalte thing and create concrete classes, but then I will have to do a lot of Copy and Paste and it is something I don't like. Has anybody found a better solution?.

Thanks in advance,
HexDump.

Share this post


Link to post
Share on other sites
Advertisement
Hi!,

They just are there for lookup. When I have to create a set of components to build an entity, it provides a list of systems that will add its natural components to the entity. So I need a way to look for this systems based on name.

HexDump.

Share this post


Link to post
Share on other sites
I'd suggest an abstract base class ComponentSystemBase (or whatever) that provides a single pure virtual method, PopulateEntity(). All ComponentSystem<T> classes should derive from this base. Then you store the base in your "manager" list, e.g. using a std::map<std::string, boost::shared_ptr<ComponentSystemBase> > and invoke PopulateEntity() to create the components needed in a given entity.

Make sense?

Share this post


Link to post
Share on other sites
Hi!,

Thanks for the answer ApochPiQ.

If you check the source code I pasted, there's a T* createComponent(int id); that is the one creating all the trouble. In my system there's no a class entity on its own, an entity is just an id (number).

I can't make a base class and derive from it, because createComponent is needed in the base class and it is a templatized method to be called when retrieving the system, something like:

SystemMgr::getInstance()->getSystem["Spatial"]->createComponent( entity id);

I guess I'm doing something worng here, and I think there must be another way to do it, without creating concrete system clases and copy and pasting all code again and again referencing the concrete component class it holds.

Thanks in advance,
hexDump.

Share this post


Link to post
Share on other sites
Erm... what exactly is "T" a parameter for, then? What class/type is the template going to be specialized on?

Share this post


Link to post
Share on other sites
Hi!,

ApochPiQ, I have changed my design becuase I thik what I wanted to do is something odd. Now it is working ok. Thanks for your time and aswers.

HexDump.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!