Jump to content

  • Log In with Google      Sign In   
  • Create Account

Storing class with a template in an array?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 irbaboon   Members   -  Reputation: 899

Like
0Likes
Like

Posted 26 July 2013 - 09:49 AM

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! :)



Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2638

Like
1Likes
Like

Posted 26 July 2013 - 10:15 AM

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.


o3o


#3 irbaboon   Members   -  Reputation: 899

Like
0Likes
Like

Posted 26 July 2013 - 10:46 AM

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, 26 July 2013 - 11:14 AM.


#4 BrentChua   Crossbones+   -  Reputation: 1066

Like
1Likes
Like

Posted 26 July 2013 - 10:57 AM

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 :)



#5 irbaboon   Members   -  Reputation: 899

Like
0Likes
Like

Posted 26 July 2013 - 11:23 AM

Thanks a lot! 

 

This definitely helped me. :)

 

I should have been able to think of this. (but I didn't!)

 

So thanks again.



#6 Waterlimon   Crossbones+   -  Reputation: 2638

Like
0Likes
Like

Posted 26 July 2013 - 11:26 AM

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.


o3o


#7 irbaboon   Members   -  Reputation: 899

Like
0Likes
Like

Posted 26 July 2013 - 11:48 AM

Yeah, I realised Waterlimon.

 

I realised BrentChua's method is a bit restricting, but I'll go with that one. It is more what I want to go for.

 

Thanks all for the help though!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS