通过c++模板访问不同的数据

access to different data via c++ templates

本文关键字:数据 访问 c++ 通过      更新时间:2023-10-16

我试图使一个管理器,它通过模板提供访问存储不同类型的数据的容器。

首先,我做了一个基本的管理器类来存储不同类型的数据

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种类型的baseMapManagers:

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:programminggithub projectsbossbossnewresourcemanagerresourcemanager.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:programminggithub projectsbossbossnewresourcemanagerresourcemanager.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:programminggithub projectsbossbossnewmain.cpp(17) : see reference to function template instantiation 'void ResourceManager::load_resource<AnimationResource>(std::string,std::string)' being compiled
1>d:programminggithub projectsbossbossnewresourcemanagerresourcemanager.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)//得到它或manager->load_resource<FontResource>(itsPath, itsName)//add it

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

edit问题的根源可能是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;
}