c++sfmlasset-management

Generic map objects


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.


Solution

  • 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.