Pergunta

I'm creating my first game. It will be Pacman or Snake. I'm going to use DirectX 11.

I'm writing resource manager at the moment. I want to create it the most simple to use but I think that it is not well designed. Here's what I wrote:

#pragma once

class Shader;
class Mesh;
class Texture;

typedef std::map<std::string, std::auto_ptr<Shader>>    shaders_map;
typedef std::map<std::string, std::auto_ptr<Mesh>>      meshes_map;
typedef std::map<std::string, std::auto_ptr<Texture>>   textures_map;

template<class C, class E>
inline int findElementInMap(const C& cont, E *ptr)
{
    C::const_iterator it = cont.begin();
    int i = 0;

    while(it != cont.end())
    {
        if(it->second.get() == ptr) // ERROR AT THIS LINE!!!!
            return i;

        i++;
        it++;
    }

    return -1;
}

class ResourceManager
{
public:
    ResourceManager(void);
    ~ResourceManager(void);

    template<class T>
    inline T* get(const std::string &name)
    {
        if(typeid(T) == typeid(Shader)) {
            return (T*)getShader(name);
        }
        else if(typeid(T) == typeid(Mesh)) {
            return (T*)getMesh(name);
        }
        else if(typeid(T) == typeid(Texture)) {
            return (T*)getTexture(name);
        }

        return nullptr;
    }

    Shader*     getShader(const std::string &name);
    Mesh*       getMesh(const std::string &name);
    Texture*    getTexture(const std::string &name);

    template<class T>
    inline bool add(T *ptr)
    {
        if(typeid(T) == typeid(Shader)) {
            return addShader((Shader*)((void*)ptr));
        }
        else if(typeid(T) == typeid(Mesh)) {
            return addMesh((Mesh*)((void*)ptr));
        }
        else if(typeid(T) == typeid(Texture)) {
            return addTexture((Texture*)((void*)ptr));
        }

        return false;
    }

    bool        addShader(Shader *ptr);
    bool        addMesh(Mesh *ptr);
    bool        addTexture(Texture *ptr);

    template<class E>
    inline void release(E *ptr)
    {
        if(typeid(E) == typeid(Shader)) {
            release<shaders_map, E>(shaders, (E*)((void*)ptr));
            return;
        }
        else if(typeid(E) == typeid(Mesh)) {
            release<meshes_map, E>(meshes, (E*)((void*)ptr));
            return;
        }
        else if(typeid(E) == typeid(Texture)) {
            release<textures_map, E>(textures, ptr);
        }
    }

        // THIS METHOD CAUSES PROBLEM
    template<class C, class E>
    void release(C &container, E *ptr)
    {
        assert(ptr != nullptr);

        int index = findElementInMap<C, E>(container, ptr);
        if(index < 0)
            return;

        C::iterator it = container.begin();
        it->second.release();
    }
private:
    shaders_map     shaders;
    meshes_map      meshes;
    textures_map    textures;
};

And now compile error:

error C2440: '==' : cannot convert from 'Shader *' to 'Mesh *'
int findElementInMap<C,E>(const C &,E *)' being compiled
      with
      [
          C=textures_map,
          E=Shader
      ]

So the container type and the element type don't match. Any ideas on how to set it work?

Or should I build new resource manager from scratch?

Edit: That's how I use this class:

    Shader *sh = new Shader();
    resourceManager.add<Shader>(sh);
    resourceManager.release<Shader>(sh);
Foi útil?

Solução

Many ways to do it .

You could do with simple overloading :

bool        add(Shader *ptr){
    return addShader(ptr);
}
bool        add(Mesh *ptr){
    return addMesh(ptr);
}

bool        add(Texture *ptr){
    return addTexture(ptr);
}

or if you want to use templates, just make the add and get methods template methods and specialize

template<class T>
void add(T *ptr){

}

and for each resource type

template<>
ResourceManager<Texture>::add(Texture *ptr){
    return addTexture(ptr);
}

template<>
ResourceManager<Shader>::add(Shader *ptr){
    return addShader(ptr);
}

template<>
ResourceManager<Mesh>::add(Mesh *ptr){
    return addMesh(ptr);
}

Another cleaner option would be to make a template method which would return the target container

template<typename T>
std::map<std::string, std::auto_ptr<typename T> > &getContainer(){


}

Specialize it to return the good container given the type

template<>
std::map<std::string, std::auto_ptr<Mesh> > &ResourceManager::getContainer<Mesh>(){

    return meshes_map;
}
template<>
std::map<std::string, std::auto_ptr<Texture> > &ResourceManager::getContainer<Texture>(){

    return textures_map;
}
template<>
std::map<std::string, std::auto_ptr<Shader> > &ResourceManager::getContainer<Shader>(){

    return shader_map;
}

This would boil down the get method for example to something like:

 template<typename T>
    T* get(const std::string &name){
        return getContainer<T>().get(name);
    }

Disclaimer: that is just a quickie, i did'nt compiled it.Ask if you have more questions

Edit concerning your compilation error: You call resourceManager.release<Shader>(sh); For the picture, replace E with Shader in the release method and you'll see that it cannot compile . release(textures, ptr) For your release method to compile you have to cast explicitely ie:

template<class E>
    inline void release(E *ptr)
    {
        if(typeid(E) == typeid(Shader)) {
            release<shaders_map, Shader>(shaders, (Shader*)((void*)ptr));
            return;
        }
        else if(typeid(E) == typeid(Mesh)) {
            release<meshes_map, Mesh>(meshes, (Mesh*)((void*)ptr));
            return;
        }
        else if(typeid(E) == typeid(Texture)) {
            release<textures_map, Texture>(textures, (Texture*)((void*)ptr));
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top