使用CRTP替代C ++ 11中的抽象静态方法

时间:2016-04-25 20:27:59

标签: c++ c++11 crtp

我试图实现一个通用资源管理器,它确保每个资源只用C ++ 11加载一次。

我的第一次尝试:

resourcemanager.h

#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H

#include <map>
#include <memory>


template<typename T>
class ResourceManager {

public:
    static std::shared_ptr<T> load(std::string filePath);

private:
    static map<std::string, std::weak_ptr<T>> resources;
    virtual static std::shared_ptr<T> loadResource(std::string filePath) = 0;
};

#endif // RESOURCEMANAGER_H

#include "resourcemanager.h"

resourcemanager.cpp

using namespace std;

template<typename T>
map<string, weak_ptr<T>>  ResourceManager<T>::resources;

template<typename T>
shared_ptr<T> ResourceManager<T>::load(std::string filePath) {
    auto search = resources.find(filePath);
    if (search != resources.end()) {
        auto ptr = search->second.lock();
        if (ptr) {
            return ptr;
        }
    }
    auto ptr = loadResource(filePath);
    resources[filePath] = ptr;
    return ptr;
}

然而,由于抽象的静态方法显然是禁止黑魔法,我试图使用CRTP:

resourcemanager.h

#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H

#include <map>
#include <memory>

template<typename T, class Derived>
class ResourceManager {

public:
    static std::shared_ptr<T> load(std::string filePath);

private:
    static std::map<std::string, std::weak_ptr<T>> resources;
    static std::shared_ptr<T> loadResource(std::string filePath);
};

#endif // RESOURCEMANAGER_H

resourcemanager.cpp

#include "resourcemanager.h"

using namespace std;

template<typename T, class Derived>
map<string, weak_ptr<T>>  ResourceManager<T, Derived>::resources;

template<typename T, class Derived>
shared_ptr<T> ResourceManager<T, Derived>::load(string filePath) {
    auto search = resources.find(filePath);
    if (search != resources.end()) {
        auto ptr = search->second.lock();
        if (ptr) {
            return ptr;
        }
    }
    auto ptr = ResourceManager::loadResource(filePath);
    resources[filePath] = ptr;
    return ptr;
}

template<typename T, class Derived>
shared_ptr<T> ResourceManager<T, Derived>::loadResource(string filePath) {
    return Derived::loadResource(filePath);
}

这看起来应该做我想做的事。但是当我尝试使用它时,它在链接阶段失败了:

managedstring.h

#ifndef MANAGEDSTRING_H
#define MANAGEDSTRING_H

#include "resourcemanager.h"

class ManagedString {

public:
    ManagedString(std::string filePath);
    std::string get();

private:
    std::shared_ptr<std::string> ptr;

    class StringManager : public ResourceManager<std::string, StringManager> {
    private:
        static std::shared_ptr<std::string> loadResource(std::string filePath);
    };
};

#endif // MANAGEDSTRING_H

managedstring.cpp

#include "managedstring.h"

using namespace std;

ManagedString::ManagedString(string filePath) {
    ptr = StringManager::load(filePath);
}

string ManagedString::get() {
    return *ptr;
}

shared_ptr<string> ManagedString::StringManager::loadResource(string filePath) {
    // dummy implementation
    return make_shared<string>("foo");
}

的main.cpp

#include <iostream>
#include "managedstring.h"

using namespace std;

int main() {
    ManagedString string1 = ManagedString("bar");
    ManagedString string2 = ManagedString("foobar");
    cout << string1.get() << endl; 
    cout << string2.get() << endl; 
}

当我尝试使用g++ -std=c++11 -o bin -Wall main.cpp managedstring.cpp resourcemanager.cpp(使用gcc版本5.3.0)编译时,我收到此错误消息:

/tmp/ccgqljOQ.o: In function `ManagedString::ManagedString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
managedstring.cpp:(.text+0xdd): undefined reference to `ResourceManager<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,
 ManagedString::StringManager>::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

这应该有用吗?这是编译器的缺点吗?或者我正在尝试做一些我不应该做的事情。

我也考虑过改变我的设计,不过我认为它并没有那么糟糕。请随意与我不同意。

1 个答案:

答案 0 :(得分:1)

resourcemanager.h 中,这一行:

#include "resourcemanager.h"

应该是:

#include "resourcemanager.cpp"

这似乎仅适用于您的第一个示例,但同样适用于所有其他示例 否则,作为替代方案,将模板类的声明和定义放在同一文件中。