通过c++模板访问不同的数据
access to different data via c++ templates
我试图使一个管理器,它通过模板提供访问存储不同类型的数据的容器。
首先,我做了一个基本的管理器类来存储不同类型的数据
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;
}
相关文章:
- 用于访问容器<T>数据成员的正确 API
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- OpenCV C++ 3 维垫数据访问错误值
- 用于随机数据访问的最有效文件类型
- 用于多维数据访问的重载 () 运算符
- MEX C++原始数据访问
- C++数据访问基准
- 随机 mmaped 内存访问比堆数据访问慢 16%
- 共享内存多线程和数据访问
- 如何使用常见的C 业务逻辑和数据访问层构建跨平台移动应用程序
- 链表数据访问
- OpenMP线程、数据访问延迟和STL数据容器
- C++ 矢量数据访问
- 我很难在C++类中实现我想要的数据访问
- 概括模型中的数据访问.避免宏观的方法
- 矢量化/优化循环,用于宽寄存器(特别是Xeon Phi)的未对齐数据访问
- 松类耦合和数据访问
- 在Linux上锁定对单个进程内数据访问的最快方法
- 如何使用数据访问对象进行序列化和关系数据库数据访问