我可以使用创建类型相同但不同类型的插件覆盖类类型吗?

时间:2014-09-21 00:27:13

标签: c++ plugins dll factory

Language: C++;
Platform: Win32;
Tool: Visual Studio 2012;

我创建了一个可以返回不同接口的工厂列表。也是.dll插件工厂的列表。如果插件工厂将接口返回到在其中创建的与主项目相同类名的.dll的具体类型,会发生什么?它会覆盖它/创建它吗?

确定请尝试设想这个伪代码

// Inside CFactoryGeometryPlugin_Sphere.dll
extern "C" __declspec(dllexport)
IFactory* CFactoryGeometryPlugin_Sphere::CreateClassInstance(void)
{
    return (new CGeometrySphere());
}

// Also Inside CFactoryGeometryPlugin_Sphere.dll
class CGeometrySphere : public IGeometry
{
    // stuff here
    void SomeDifferentFunction(void);
};

...然后

// Inside the main project:
class CGeometrySphere : IGeometry
{
    // stuff here
    void Function_A(void);
};

class CFactoryManager
{
    // stuff here
    template<typename FactoryT>
    void CreateAndRegisterTypes(int iType);

    unordered_map<int, IFactory> m_umapFactories;
};

template<typename FactoryT>
CFactoryManager::CreateAndRegisterType(int iType)
{
    umapFactories[iType] = new FactoryT;
}

所以现在......

// From main project this is ok I understand will create CGeometrySphere from main project
m_FactoryManager.CreateAndRegisterType<CGeometrySphere>(/*MyCodeHere*/);

但是这里发生了什么......?

// From CPluginManager in EXE.
CreateAndRegisterType<CFactoryGeometryPlugin_Sphere>();

// Then call the create function to create the class
umapFactories[iType] = /*Plugin*/->GetFunctionPtr<fpCreateFactoryObject_T>("CreateClassInstance");

// Basically the new is called from within the dll instead of the main project

**编辑*澄清: CFactoryManager中还包含一个CPluginManager。它采用CFactoryManager的this指针。它是如何访问umapFactories的。

我知道我原本可以将原始的CGeometrySphere作为.dll而只是将.dll替换成另一个来覆盖它 - 但这很简单!我可以这样做吗?非常感谢。

0 个答案:

没有答案