Sign in to follow this  
Diventurer

Storing class with a template in an array?

Recommended Posts

Diventurer    1631

I've always had a resource manager in my client software that has different resource containers.

Basically this:

 

in library:

template<class _Type>
class Container {
   // code dealing with loading resources
};

in client:

class Resources {
public:
    Container<Sprite> sprite;
    Container<Music> music;
    Container<Font> font;
};

So in the client code, you just use it like this:

Sprite* player_idle = res->sprite["Objects"]["Characters"]["Player"]("idle");

I won't get into any more detail, but you now understand how it works, I presume.

 

So what I want is to have the "Resources" class in the library.

 

I want it to be like this:

class Resources {
    std::vector<Container<?>> _containers;
public:
    template<class _Type>
    void add_type() {
        _containers.push_back(Container<_Type>());
    }
};

But that won't work, as the std::vector class only takes one specific type.

 

I've thought of making a wrapper class of every resource type, and just base them off a class, but that seems like a very unelegant solution, both for the library and client.

 

I could also just use a switch, but what would be the point - the client would not be able to create new types that way.

 

Thanks in advance! :)

Share this post


Link to post
Share on other sites
Waterlimon    4398

You can use the typeid keyword, get the hash of the type, and store multiple types (pointers to them?) in a map (c++11 unordered_map might be fastest out of standard containers).

 

So when you add a type you allocate the object of that type, and put the pointer in the map with the key as the hash of the type (from the thing returned by typeid)

 

Same with access.

 

But that adds a run time cost (if you use a hash map as the container it should be nearly O(1) though), even though you should know what types you will have at compile time.

So you need to pick between convenience and a unnecessary run time cost.

Share this post


Link to post
Share on other sites
Diventurer    1631

Maybe I misunderstood you, but this means that I can only have one container per type, right?

What if I want two different containers of music. It sounds a bit weird with music, but there could be a different class in the future that makes sense to have two containers for.

 

Also, do you mean that the usage should look like this?

res->container("Sprite")["Objects"]["Characters"]["Player"]("idle")

I apologise if I have misunderstood you.

 

I am thinking a bit clearer about it with your post though, so +rep from me.

I might use your idea if nothing better comes up.

Edited by diventurer

Share this post


Link to post
Share on other sites
MrCodeSushi    1066

See this: ResourcePool.h.

 

What I did was to create a base, pure interface, resource container class (Abstract Base Class) then derive a templated class from it which the application can use.

 

Basically it would look like this:

// ResourcePool to be created by the res pool will inherit from this class
class IResourceItem
{
	...
};

class ResourcePool
{
public:

	virtual ~ResourcePool( void ) { }

	virtual IResourceItem * Create( void ) = 0;

	virtual Delete( IResourceItem *pItem ) = 0;

	...
};


template < typename T >
class ResourcePoolT : public ResourcePool
{
public:

	ResourcePoolT( void ) { }

	virtual ~ResourcePoolT( void ) { }

	virtual IResourceItem * Create( void ) { /* create resource */ }

	virtual Delete( IResourceItem *pItem ) { /* Delete resource */ }

	// Ulitily functions to do away with type casting every time we create the resource
	T * CreateResource( void ) { return (T*)Create(); }

	...
};


// Your resources manager class
class ResourcePoolManager
{
	vector<ResourcePool> m_resources;  // Or you could use a map to associate each resource to a certain type(enum, key, id...)
};

So your manager doesn't have to keep track different kind of resource container types. It just manages the base, pure-virtual, ResourcePool class.

 

I hope this helps :)

Share this post


Link to post
Share on other sites
Waterlimon    4398

If you want it to support multiple Container objects of the same type, you can always make the map point to potentially multiple objects when you access it with the hash of the type (unordered_multimap?)

 

Now, i was assuming that you wanted to be able to get your container for objects of type T from the resources class, assuming it was added in elsewhere. But i dont see how that would work with multiple containers of the same type, unless you give the containers ID's (so you would have a sprite container 1 and sprite container 2 for example)

 

Anyways, if you only need a single container per type in the resources class, the access would look like:

 

res->container<Sprite>()["blah"]["blah"]("blah")

 

where container method looks like:

template<T>

Container<T> container()

{

return static_cast<T*>(m_containerMap.find(typeid(T).hash_code())); //Might want to check if the container actually exists for that type first

}

 

hash_code() seems to be a C++11 feature though, according to the internets. If you dont have C++11 you can get the name of the type and use that instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this