Modernise CalypModules factory
This commit is contained in:
@@ -24,9 +24,6 @@
|
||||
|
||||
#include "CalypModulesFactory.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
|
||||
#include "config.h"
|
||||
#ifdef USE_DYNLOAD
|
||||
#include <dlfcn.h>
|
||||
@@ -37,45 +34,50 @@
|
||||
// Generated by cmake
|
||||
#include "ModulesListHeader.h"
|
||||
|
||||
CalypModulesFactory::CalypModulesFactory(){ REGISTER_ALL_MODULES }
|
||||
CalypModulesFactory::CalypModulesFactory()
|
||||
{
|
||||
REGISTER_ALL_MODULES
|
||||
}
|
||||
|
||||
|
||||
void CalypModulesFactory::Register( const char* moduleName, CreateModuleFn pfnCreate )
|
||||
void CalypModulesFactory::Register( const std::string& moduleName, std::function<CalypModulePtr( void )> pfnCreate )
|
||||
{
|
||||
m_FactoryMap[moduleName] = pfnCreate;
|
||||
}
|
||||
|
||||
bool CalypModulesFactory::RegisterDl( const char* dlName )
|
||||
void CalypModulesFactory::Register( const char* moduleName, std::function<CalypModulePtr( void )> pfnCreate )
|
||||
{
|
||||
#ifdef USE_DYNLOAD
|
||||
void* pHndl = dlopen( dlName, RTLD_NOW );
|
||||
if( pHndl == NULL )
|
||||
{
|
||||
std::cerr << dlerror() << std::endl;
|
||||
return false;
|
||||
}
|
||||
CreateModuleFn pfnCreate = (CreateModuleFn)dlsym( pHndl, "Maker" );
|
||||
if( pfnCreate == NULL )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Register( dlName, pfnCreate );
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
Register( moduleName, pfnCreate );
|
||||
}
|
||||
|
||||
CalypModulePtr CalypModulesFactory::CreateModule( const char* moduleName )
|
||||
bool CalypModulesFactory::RegisterDl( const char* dlName )
|
||||
{
|
||||
CalypModulesFactoryMap& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
CalypModulesFactoryMap::iterator it = moduleFactoryMap.begin();
|
||||
for( ; it != moduleFactoryMap.end(); ++it )
|
||||
// #ifdef USE_DYNLOAD
|
||||
// void* pHndl = dlopen( dlName, RTLD_NOW );
|
||||
// if( pHndl == NULL )
|
||||
// {
|
||||
// std::cerr << dlerror() << std::endl;
|
||||
// return false;
|
||||
// }
|
||||
// CreateModuleFn pfnCreate = (CreateModuleFn)dlsym( pHndl, "Maker" );
|
||||
// if( pfnCreate == NULL )
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
// Register( dlName, pfnCreate );
|
||||
// return true;
|
||||
// #else
|
||||
return false;
|
||||
// #endif
|
||||
}
|
||||
|
||||
CalypModulePtr CalypModulesFactory::CreateModule( const std::string& moduleName ) const
|
||||
{
|
||||
const auto& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
|
||||
if( moduleFactoryMap.contains( moduleName ) )
|
||||
{
|
||||
if( !strcmp( it->first, moduleName ) )
|
||||
{
|
||||
return it->second();
|
||||
}
|
||||
auto createFct = moduleFactoryMap.at( moduleName );
|
||||
return createFct();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user