lundi 25 juillet 2016

C++ get the first argument value in template parameter pack

I have this class

class AssetManager
{
private:
    std::unordered_map<const std::type_info*, Asset*> assets;

public:
    AssetManager();
    virtual ~AssetManager();

    template <typename T, typename ...Args>
    bool newAsset(Args... args)
    {   
        //Get path
        if(cached(path)) { return true; }

        auto asset = new T{ std::forward<Args>(args)... };
        assets[&typeid(*asset)] = asset;
        return *static_cast<T*>(assets[&typeid(T)]);
    }

    bool cached(const std::string& path)
    {
        for(auto asset : assets) {
          if(asset.second->getPath() == path) {
             return true;
          }
        }

        return false;
    }
};

The first argument of every Asset will always be std::string path. I'm trying to get this value and see if it's already loaded in the list. Asset is an abstract class.

class Asset
{
private:
    std::string path;

public:
    Asset(const std::string& path);
    virtual ~Asset() = default;
    virtual bool load() = 0;
    std::string getPath();
};

Classes inheriting Asset may have different number of arguments and as such i'm trying to capture the value of the first argument because it will always be an std::string path as you can see in the Asset class constructor.

Aucun commentaire:

Enregistrer un commentaire