Modernise CalypModules factory
This commit is contained in:
6 files changed
+60
-83
No files matched your search
@@ -100,12 +100,11 @@ void ModulesHandle::buildMenu()
|
||||
m_pcModulesMenu->clear();
|
||||
m_pcModulesSubMenuList.clear();
|
||||
|
||||
CalypModulesFactoryMap& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
CalypModulesFactoryMap::iterator it = moduleFactoryMap.begin();
|
||||
for( ; it != moduleFactoryMap.end(); ++it )
|
||||
const auto& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
for( auto& [name, create_fct] : moduleFactoryMap )
|
||||
{
|
||||
auto pcCurrModuleIf = it->second();
|
||||
QString ModuleIfinternalName = QString::fromLocal8Bit( it->first );
|
||||
auto pcCurrModuleIf = create_fct();
|
||||
QString moduleIfInternalName = QString::fromStdString( name );
|
||||
|
||||
QMenu* currSubMenu{ nullptr };
|
||||
if( pcCurrModuleIf->m_iModuleAPI <= CLP_MODULE_API_2 )
|
||||
@@ -134,7 +133,7 @@ void ModulesHandle::buildMenu()
|
||||
moduleTooltip += QString( " | Requires %1 frames" ).arg( pcCurrModuleIf->m_uiNumberOfFrames );
|
||||
|
||||
currAction->setStatusTip( moduleTooltip );
|
||||
currAction->setData( ModuleIfinternalName );
|
||||
currAction->setData( moduleIfInternalName );
|
||||
currAction->setCheckable( false );
|
||||
connect( currAction, &QAction::triggered, this, &ModulesHandle::activateModule );
|
||||
m_arrayModulesActions.append( currAction );
|
||||
|
||||
@@ -84,7 +84,7 @@ bool StreamHandlerPortableMap::openHandler( std::string strFilename, bool bInput
|
||||
}
|
||||
else
|
||||
{
|
||||
int colorSpace = CalypFrame::pelformatColorSpace( m_iPixelFormat );
|
||||
int colorSpace = CalypFrame::pelFormatColorSpace( m_iPixelFormat );
|
||||
m_iMaxValue = ( 1 << m_uiBitsPerPixel ) - 1;
|
||||
if( m_uiBitsPerPixel == 1 )
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -25,37 +25,33 @@
|
||||
#ifndef __CALYPMODULESFACTORY_H__
|
||||
#define __CALYPMODULESFACTORY_H__
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
||||
// CalypLib
|
||||
#include "lib/CalypModuleIf.h"
|
||||
|
||||
#define REGISTER_MODULE( X ) Register( "X", &( X::Create ) );
|
||||
|
||||
typedef CalypModulePtr ( *CreateModuleFn )( void );
|
||||
typedef std::map<const char*, CreateModuleFn> CalypModulesFactoryMap;
|
||||
|
||||
// Factory for creating instances of CalypModuleIf
|
||||
class CalypModulesFactory
|
||||
{
|
||||
using CalypModulesFactoryMap = std::map<std::string, std::function<CalypModulePtr( void )>>;
|
||||
|
||||
private:
|
||||
CalypModulesFactory();
|
||||
CalypModulesFactory& operator=( const CalypModulesFactory& ) { return *this; }
|
||||
CalypModulesFactoryMap m_FactoryMap;
|
||||
|
||||
public:
|
||||
~CalypModulesFactory() = default;
|
||||
|
||||
static CalypModulesFactory* Get()
|
||||
{
|
||||
static CalypModulesFactory instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
void Register( const char* moduleName, CreateModuleFn pfnCreate );
|
||||
void Register( const std::string& moduleName, std::function<CalypModulePtr( void )> pfnCreate );
|
||||
void Register( const char* moduleName, std::function<CalypModulePtr( void )> pfnCreate );
|
||||
bool RegisterDl( const char* dlName );
|
||||
|
||||
CalypModulePtr CreateModule( const char* moduleName );
|
||||
CalypModulePtr CreateModule( const std::string& moduleName ) const;
|
||||
CalypModulesFactoryMap& getMap() { return m_FactoryMap; }
|
||||
};
|
||||
|
||||
|
||||
+1
-10
@@ -303,16 +303,7 @@ int CalypTools::Open( int argc, char* argv[] )
|
||||
{
|
||||
std::string moduleName = m_strModule;
|
||||
|
||||
CalypModulesFactoryMap& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
CalypModulesFactoryMap::iterator it = moduleFactoryMap.begin();
|
||||
for( unsigned int i = 0; it != moduleFactoryMap.end(); ++it, i++ )
|
||||
{
|
||||
if( strcmp( it->first, moduleName.c_str() ) == 0 )
|
||||
{
|
||||
m_pcCurrModuleIf = it->second();
|
||||
break;
|
||||
}
|
||||
}
|
||||
m_pcCurrModuleIf = CalypModulesFactory::Get()->CreateModule( moduleName );
|
||||
if( !m_pcCurrModuleIf )
|
||||
{
|
||||
log( CLP_LOG_ERROR, "Invalid module! " );
|
||||
|
||||
@@ -118,20 +118,10 @@ int CalypToolsCmdParser::parseToolsArgs( int argc, char* argv[] )
|
||||
void CalypToolsCmdParser::listModuleHelp()
|
||||
{
|
||||
std::string moduleName = m_strModule;
|
||||
|
||||
CalypModulesFactoryMap& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
CalypModulesFactoryMap::iterator it = moduleFactoryMap.begin();
|
||||
for( unsigned int i = 0; it != moduleFactoryMap.end(); ++it, i++ )
|
||||
{
|
||||
if( strcmp( it->first, moduleName.c_str() ) == 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto pcCurrModuleIf = it->second();
|
||||
auto pcCurrModuleIf = CalypModulesFactory::Get()->CreateModule( moduleName );
|
||||
if( pcCurrModuleIf )
|
||||
{
|
||||
printf( "Usage: calypTools --module=%s options:\n", it->first );
|
||||
printf( "Usage: calypTools --module=%s options:\n", moduleName.c_str() );
|
||||
pcCurrModuleIf->m_cModuleOptions.doHelp( std::cout );
|
||||
}
|
||||
}
|
||||
@@ -143,27 +133,26 @@ void CalypToolsCmdParser::listModules()
|
||||
if( m_cOptions.hasOpt( "module_list_full" ) )
|
||||
bDetailed = true;
|
||||
|
||||
CalypModulesFactoryMap& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
CalypModulesFactoryMap::iterator it = moduleFactoryMap.begin();
|
||||
const auto& moduleFactoryMap = CalypModulesFactory::Get()->getMap();
|
||||
|
||||
printf( "Calyp available modules: \n" );
|
||||
printf( " [Internal Name] " );
|
||||
std::cout << "Calyp available modules:" << std::endl;
|
||||
std::cout << " [Internal Name] ";
|
||||
if( bDetailed )
|
||||
{
|
||||
printf( " [Full Name] " );
|
||||
printf( " [Type] " );
|
||||
printf( " [Description]" );
|
||||
std::cout << " [Full Name] ";
|
||||
std::cout << " [Type] ";
|
||||
std::cout << " [Description]";
|
||||
}
|
||||
printf( " \n" );
|
||||
std::cout << std::endl;
|
||||
|
||||
for( unsigned int i = 0; it != moduleFactoryMap.end(); ++it, i++ )
|
||||
for( const auto& [name, moduleFct] : moduleFactoryMap )
|
||||
{
|
||||
printf( " " );
|
||||
printf( "%-33s", it->first );
|
||||
printf( "%-33s", name.c_str() );
|
||||
if( bDetailed )
|
||||
{
|
||||
std::string ModuleNameString;
|
||||
auto pcCurrModuleIf = it->second();
|
||||
auto pcCurrModuleIf = moduleFct();
|
||||
|
||||
if( pcCurrModuleIf->m_pchModuleCategory )
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user