Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8745483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:01:16+00:00 2026-06-13T12:01:16+00:00

I’m creating my first game. It will be Pacman or Snake. I’m going to

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:01:17+00:00Added an answer on June 13, 2026 at 12:01 pm

    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));
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I want to show the soap response to UIWebview.. my soap response is, <p><img
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.