我想创建一个插件架构。我有一个DLL作为接口,然后我会为每个实现有多个DLL。
class INormalizer {
virtual ~INormalizer() { }
virtual std::string Normalize(const std::string& input) = 0;
virtual void Destroy() = 0;
}
template<typename T> INormalizer* CreateT() { return new T; }
class NormalizerFactory {
public:
typedef std::map<std::string, INormalizer*(*)()> map_type;
static INormalizer* CreateInstance(const std::string& name) {
map_type::iterator it = getMap()->find(name);
if(it == getMap()->end()) {
return NULL;
}
return it->second();
}
protected:
static map_type* getMap() {
if(!_map) {
_map = new map_type;
}
return _map;
}
private:
static map_type* _map;
}
NormalizerFactory::map_type* NormalizerFactory::_map = NULL;
template<typename T>
class DerivedRegister : NormalizerFactory {
public:
DerivedRegister(const std::string& name) {
getMap()->insert(std::make_pair(name, &createT<T>));
}
}
extern "C" __declspec(dllexport) INormalizer* __cdecl CreateNormalizer(const std::string& name) {
return NormalizerFactory::CreateInstance(name);
}
class AlphaNumericOnly : public INormalizer {
public:
~AlphaNumericOnly();
std::string Normalize(const std::string& input) override;
void Destroy() override;
private:
static DerivedRegister<AlphaNumericOnly> _reg;
}
...
DerivedRegister<AlphaNumericOnly> AlphaNUmericOnly::_reg("AlphaNumericOnly");
问如何在不添加DerivedRegister<T> _reg
界面的情况下强制每个实施者也包含INormalizer
成员。
问构建AlphaNumericOnly.dll时,链接器会向NormalizerFactory::_map
投诉NormalizerFactory::_map
。我不想在每个实现DLL中定义静态成员LoadLibrary
。
实现DLL将由客户端应用程序中的GetProcAddress
/ LoadLibrary
使用。如果我使用GetProcAddress(hDll, "CreateNormalizer")
加载AlphaNumericOnly.dll,则对CreateNormalizer
的调用失败,因为此DLL中未导出{{1}}函数。