0

I am trying to create an Asset Manager (Much like the one that is provided in the Libgdx library) for SFML in C++. But I am running into the age old problem of templates being one of the worst parts of C++.

I am trying to have a map object hold generic types, the key would be a simple string and the data would be any type I want it to be. Note, I don't want to template the map object to simply hold one generic type throughout the entire map (IE, the map being <string, int>). I want to have different types in the same map so I can load many different assets.

Is there any way I can do something like this?

Thank you for your help and consideration, any little tip goes a long way.

jww
  • 97,681
  • 90
  • 411
  • 885
BigBerger
  • 1,765
  • 4
  • 23
  • 43
  • Well there *is* [the Boost Any library](http://www.boost.org/doc/libs/1_57_0/doc/html/any.html), or of course using inheritance an pointers. But you could also rethink your design about having a single asset-manager to handle all assets, and instead use one manager for each type of asset (and have a map of the managers instead). – Some programmer dude Mar 23 '15 at 22:39
  • 3
    Templates are one of the best part of C++. As for your problem you may want to look at http://stackoverflow.com/questions/251403/how-do-you-make-a-heterogeneous-boostmap – Wojtek Surowka Mar 23 '15 at 22:40
  • Thanks for this! I will check it out sometime later tonight, and I guess I worded the template comment wrong. I meant to say that, in my opinion and the opinion of most of my friends, templates in C++ are one of the most troublesome and confusing features (Although they are very useful). – BigBerger Mar 23 '15 at 22:49
  • Can't you just use inheritance? ie. derive all your assets from a base class "Asset" and then your map can look like map – Brad Mar 23 '15 at 22:49
  • @Brad No I can't use inheritance, this is because of the fact that I am using the SFML library which has custom asset classes of it's own. And because of the fact that I want to load all different types of assets. The SFML audio and image asset do not share a parent class (Which is very frustrating). – BigBerger Mar 23 '15 at 22:57
  • Presumably when you grab the asset, you will want it to be a particular type. Therefore why is having different maps per type of asset, instead of one giant map which contains any assets, but you need to specify the type at use, so bad? – Neil Kirk Mar 24 '15 at 23:08
  • You can still use a parent type. This class would have a type member and a pointer to any type of the asset you can have. Of course then you would need to define a templated getter (or have one per type). If you are feeling adventurous then you could even define type conversion operators for it, but that is generally discouraged. – Jozef Legény Mar 24 '15 at 23:34

2 Answers2

0

I reiterate my comment about a redesign using a map of manager instead.

Then you could have e.g.

class basic_asset_manager { ... };
class image_asset_manager : public basic_asset_manager { ... };
...

std::unordered_map<std::string, basic_asset_manager*> asset_managers;
asset_managers["image"] = new image_asset_manager;
...

// Load an image from a file
asset_managers["image"]->load("some alias for image", "/some/file/name");
...

// Get image
image = asset_manager["image"]->get("some alias for image");

Maybe not exactly like this, but you hopefully get the point.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You could define a struct or in some cases possibly use a union to pass as the second parameter of the map. Might not be the most elegant solution, but can get the job done.

talon24
  • 36
  • 3