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 種類の 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:\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

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;
}
于 2013-05-24T05:43:08.447 に答える