开发者

Separation of Concerns: Create base library and remove dependencies on concrete classes

We have a situation where we develop DLL plugins for an application. All the plugins have a common set of source files. However, I wish to separate them into a base library. We have the following implementation which is all just part of one DLL project:

// dllentry class
BaseImporter* spImporter = NULL;
    extern开发者_C百科 BaseImporter* createImporterImplementation(const std::string& pDllFileName);

int __stdcall DllImportOpen(
            const char* pDllFileName,
            const char** ppImporterUniqueName,
            const char** ppImporterVersion)
{
    // method implementation in concrete class source file
    spImporter = createImporterImplementation(pDllFileName);

    (*ppImporterUniqueName) = spImporter->GetUniqueName();
    (*ppImporterVersion) = spImporter->GetImporterVersion();
    return (0);
}

// base importer class

class BaseImporter()
{
public:
    const char* GetUniqueName() const {return mUniqueName.c_str();}
    const char* GetImporterVersion() const {return mVersion.c_str();}
protected:
    // some pure virtual methods here
}

// concrete class
class XImporter : BaseImporter() 
{
    // implement base importer
}

// resides in XImporter source file
BaseImporter* createImporterImplementation(
            const std::string& dll_filename)
{
    return new XImporter (dll_filename);
}

This works fine when built together as 1 DLL. However, I would like to separate the dllentry class and BaseImporter into a base library. This will obviously create a linker error when compiling the base library as it will be searching for the implementation of createImporterImplementation. How can I nicely separate a base library that can be used with concrete libraries?


One solution would be to late-bind to createImporterImplementation. Try something like (NB, not tested at all, porbably wrong in detail)

// in base DLL:
BaseImporter* createImporterImplementation(
        const std::string& dll_filename)
{
   HMODULE h - LoadLIbrary (dll_filename.c_str());
   BaseImporter* (*fn)() = GetProcAddress(h, CreateImporterImplementation);
   return (*fn)();
}

then in the concrete importer:

__declspec(dllexport) BaseImporter* createImporterImplementation()
{
    return new XImporter;
}

Does that answer the question?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜