Skip to main content
gamedev.net
🔒 Locked

Templated creation

Started by Penance 4 replies 1.4k views
Original Post
Penance
Penance
I'm wondering if there is a way to create a templated class/method that will reliably return an instance of the parameter specified. My main problem is making it handle types as well as pointers to types. template T create() { //possible? } Is is possible to write the above such that both the following calls will work? MyClass x = create(); MyClass* y = create();
Enigma
Enigma
#include <boost/shared_ptr.hpp>template < typename Type >struct Creator{	Type operator()() const	{		return Type();	}};template < typename Type >struct Creator< Type * >{	boost::shared_ptr< Type > operator()() const	{		return boost::shared_ptr< Type >(new Type());	}};template < typename Type >Type create(){	return Creator< Type >()();}
I wouldn't exactly recommed this though. What are you wanting it for?

Σnigma
Penance
Penance
Basically I'm working on creating a templated Manager class.

I want to be able to just pass in a string to a createObject() method and have the Manager return the key of the object that was created and is now being managed.

So, I guess it's a hybrid manager-factory.

Something like this:

template <class Key, class T>class Manager{public:....	const Key createObject(const string& name)	{		Key newKey(name);		....		T obj = new T(newKey);		m_objects.insert( make_pair(newKey, obj) );		return obj->getID();                ....	}....private:	map<Key, T> m_objects;};


Problem is where I'm actually creating the object. Obviously it won't work if something like MyClass is passed to the template instead of MyClass*
dmail
dmail
I would also have to question the reason for this, normally something like this would be used in a abstract factory but then this requires two types the type and the base type.

Personally if I had a functor like this I would return an auto_ptr rather than a shared_ptr to make it clear who owns the pointer.
Penance
Penance
What do you guys recommend?

I want to be able to create an object using a human-readable name and get back an ID for it.

Manager manager;
ID myID = manager.createObject("testObj1");

Then I want to be able to use that ID to get that object back in order to manipulate it.

GameObject* myObj = manager.getObject(myID);
or
GameObject* myObj = manager.getObject("testObj1");

//Then access that object's components as needed (code not shown above)
Physics p = myObj.getComponent();

Is my design flawed?

Maybe it would make more sense to deal exclusively in ID's and avoid GameObjects/pointers all together?
Physics p = manager.getComponent(myID);
Penance
Penance
In case anyone else has the same question and comes across this thread, here's a really good article I found explaining the technique that Enigma showed.

It's called partial specialization (for those, like me, that aren't template masters yet)

http://www.cprogramming.com/tutorial/template_specialization.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.