0

我正在尝试创建一个管理器,它通过模板提供对存储不同类型数据的容器的访问。

首先,我制作了用于存储不同类型数据的基本管理器类

template <typename T>

class BaseMapManager{
public:
T add(T element, std::string name);

T remove(T element);
T remove(std::string name);
T remove(unsigned int id);

T get(std::string name);    
T get(unsigned int id);

BaseMapManager() { id = 0; };
~BaseMapManager() { nameMap.clear(); idMap.clear(); };

protected:
    std::map<std::string, T> nameMap;
    std::map<unsigned int, T> idMap;

//each element of type T gets new unique id
unsigned int id;

//hide it
BaseMapManager(const BaseMapManager&) {};
BaseMapManager& operator=(const BaseMapManager&) {};
};

然后我创建了我的具​​体管理器,它必须存储 3 种类型的 baseMapManager:

class ResourceManager{
public:
    //creates singleton
    static ResourceManager* init(){
        static ResourceManager singleton;
        return &singleton;
    }

    template <typename T>
    void load_resource(std::string path, std::string name){
        get_map<std::shared_ptr<T>>().add(std::shared_ptr<T>(new T(path, name), name);
    }

    template <typename T>
    std::shared_ptr<T> get_resource(std::string name){
        get_map<T>().get(name);
    }

    template <typename T>
    std::shared_ptr<T> get_resource(unsigned int id){
        get_map<T>().get(id);
    }


private:
    BaseMapManager<std::shared_ptr<AnimationResource> > animationMap;
    BaseMapManager<std::shared_ptr<ImageResource> > imageMap;
    BaseMapManager<std::shared_ptr<FontResource> > fontMap;

    template <typename T>
    BaseMapManager<T>& get_map(){
        if (std::is_same<T, std::shared_ptr<AnimationResource> >() == true) return animationMap;
        if (std::is_same<T, std::shared_ptr<ImageResource> >() == true) return imageMap;
        if (std::is_same<T, std::shared_ptr<FontResource> >() == true) return fontMap;
    };
};

现在我得到了这个:

1>------ Build started: Project: BOSS, Configuration: Debug Win32 ------
1>  main.cpp
1>d:\programming\github projects\boss\boss\new\resourcemanager\resourcemanager.h(43): 
error C2440: 'return' : cannot convert from 'BaseMapManager<T>' to 'BaseMapManager<T> &'
1>          with
1>          [
1>              T=std::shared_ptr<ImageResource>
1>          ]
1>          and
1>          [
1>              T=std::shared_ptr<AnimationResource>
1>          ]
1>          d:\programming\github projects\boss\boss\new\resourcemanager\resourcemanager.h(21) : see reference to function template instantiation 'BaseMapManager<T> &ResourceManager::get_map<std::shared_ptr<_Ty>>(void)' being compiled
1>          with
1>          [
1>              T=std::shared_ptr<AnimationResource>,
1>              _Ty=AnimationResource
1>          ]
1>          d:\programming\github projects\boss\boss\new\main.cpp(17) : see reference to function template instantiation 'void ResourceManager::load_resource<AnimationResource>(std::string,std::string)' being compiled
1>d:\programming\github projects\boss\boss\new\resourcemanager\resourcemanager.h(44): error C2440: 'return' : cannot convert from 'BaseMapManager<T>' to 'BaseMapManager<T> &'
1>          with
1>          [
1>              T=std::shared_ptr<FontResource>
1>          ]
1>          and
1>          [
1>              T=std::shared_ptr<AnimationResource>
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

目标是 - 使用模板函数提供对不同类型数据的访问,例如 manager->get_resource<AnimationResource>(itsName)//get it 或 manager->load_resource<FontResource>(itsPath, itsName)//add it

有没有更好的方法来处理这个问题?谢谢!

4

1 回答 1

1

编辑 问题接缝的来源是 is_same 函数在编译时无法正确解析。 /编辑

您始终可以拥有仅适用于指定类型的模板函数。

例如:

template<>
BaseMapManager<AnimationResource> get_map<AnimationResource>()
{
    return animationMap;
}

template<>
BaseMapManager<ImageResource> get_map<ImageResource>()
{
    return imageMap;
}

template<>
BaseMapManager<FontResource> get_map<FontResource>()
{
    return fontMap;
}
于 2013-05-24T05:43:08.447 回答