diff --git a/alib2cli/src/registration/InputFileTypeRegistration.hpp b/alib2cli/src/registration/InputFileTypeRegistration.hpp index 28cb35ef38523538813aa8fc66ae08eb2ca06ce6..1fd84aa119b50e2d2e0ac4fbb6b84db064f223ce 100644 --- a/alib2cli/src/registration/InputFileTypeRegistration.hpp +++ b/alib2cli/src/registration/InputFileTypeRegistration.hpp @@ -6,9 +6,15 @@ namespace registration { class InputFileRegister { + std::string m_FileType; + public: - InputFileRegister ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) { - abstraction::InputFileRegistry::registerInputFileHandler ( fileType, callback ); + InputFileRegister ( std::string fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) : m_FileType ( std::move ( fileType ) ) { + abstraction::InputFileRegistry::registerInputFileHandler ( m_FileType, callback ); + } + + ~InputFileRegister ( ) { + abstraction::InputFileRegistry::unregisterInputFileHandler ( m_FileType ); } }; diff --git a/alib2cli/src/registration/OutputFileTypeRegistration.hpp b/alib2cli/src/registration/OutputFileTypeRegistration.hpp index 9238b542213a95dd42b4bb812da4b5ba185d4e76..8f81cfbe5c272789291780dda2d36ae80061bd2a 100644 --- a/alib2cli/src/registration/OutputFileTypeRegistration.hpp +++ b/alib2cli/src/registration/OutputFileTypeRegistration.hpp @@ -6,9 +6,15 @@ namespace registration { class OutputFileRegister { + std::string m_FileType; + public: - OutputFileRegister ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & typehint ) ) { - abstraction::OutputFileRegistry::registerOutputFileHandler ( fileType, callback ); + OutputFileRegister ( std::string fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & typehint ) ) : m_FileType ( std::move ( fileType ) ) { + abstraction::OutputFileRegistry::registerOutputFileHandler ( m_FileType, callback ); + } + + ~OutputFileRegister ( ) { + abstraction::OutputFileRegistry::unregisterOutputFileHandler ( m_FileType ); } }; diff --git a/alib2cli/src/registry/InputFileRegistry.cpp b/alib2cli/src/registry/InputFileRegistry.cpp index e06f0be975050bd8488d9f4818d7977174c5b672..9260784782f81f100df8e80b52cd6bf06ff2c0e6 100644 --- a/alib2cli/src/registry/InputFileRegistry.cpp +++ b/alib2cli/src/registry/InputFileRegistry.cpp @@ -10,6 +10,22 @@ namespace abstraction { +ext::map < std::string, std::unique_ptr < InputFileRegistry::Entry > > & InputFileRegistry::getEntries ( ) { + static ext::map < std::string, std::unique_ptr < Entry > > inputFileHandlers; + return inputFileHandlers; +} + +void InputFileRegistry::registerInputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) { + auto iter = getEntries ( ).insert ( std::make_pair ( fileType, std::unique_ptr < Entry > ( new EntryImpl ( callback ) ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + +void InputFileRegistry::unregisterInputFileHandler ( const std::string & fileType ) { + if ( getEntries ( ).erase ( fileType ) == 0u ) + throw std::invalid_argument ( "Entry " + fileType + " not registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > InputFileRegistry::getAbstraction ( const std::string & fileType, const std::string & type, const ext::vector < std::string > & templateParams ) { auto res = getEntries ( ).find ( fileType ); if ( res == getEntries ( ).end ( ) ) diff --git a/alib2cli/src/registry/InputFileRegistry.hpp b/alib2cli/src/registry/InputFileRegistry.hpp index 796681df332fc3b635a724ae4185cf9b3d0c976b..312ae510b6a27ee71d639c1354459635f0c7637b 100644 --- a/alib2cli/src/registry/InputFileRegistry.hpp +++ b/alib2cli/src/registry/InputFileRegistry.hpp @@ -35,15 +35,12 @@ class InputFileRegistry { std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & type, const ext::vector < std::string > & templateParams ) const override; }; - static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ) { - static ext::map < std::string, std::unique_ptr < Entry > > inputFileHandlers; - return inputFileHandlers; - } + static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void registerInputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) { - getEntries ( ).insert ( std::make_pair ( fileType, std::unique_ptr < Entry > ( new EntryImpl ( callback ) ) ) ); - } + static void registerInputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ); + + static void unregisterInputFileHandler ( const std::string & fileType ); static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & fileType, const std::string & type, const ext::vector < std::string > & templateParams ); }; diff --git a/alib2cli/src/registry/OutputFileRegistry.cpp b/alib2cli/src/registry/OutputFileRegistry.cpp index 82cbb79331e6ed9be91a7670ef466d8adbb26223..3a2b2abf8cca915e3c765ad267010c7feb2cb77e 100644 --- a/alib2cli/src/registry/OutputFileRegistry.cpp +++ b/alib2cli/src/registry/OutputFileRegistry.cpp @@ -10,6 +10,22 @@ namespace abstraction { +ext::map < std::string, std::unique_ptr < OutputFileRegistry::Entry > > & OutputFileRegistry::getEntries ( ) { + static ext::map < std::string, std::unique_ptr < Entry > > inputFileHandlers; + return inputFileHandlers; +} + +void OutputFileRegistry::registerOutputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & typehint ) ) { + auto iter = getEntries ( ).insert ( std::make_pair ( fileType, std::unique_ptr < Entry > ( new EntryImpl ( callback ) ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + +void OutputFileRegistry::unregisterOutputFileHandler ( const std::string & fileType ) { + if ( getEntries ( ).erase ( fileType ) == 0u ) + throw std::invalid_argument ( "Entry " + fileType + " not registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > OutputFileRegistry::getAbstraction ( const std::string & fileType, const std::string & typehint ) { auto res = getEntries ( ).find ( fileType ); if ( res == getEntries ( ).end ( ) ) diff --git a/alib2cli/src/registry/OutputFileRegistry.hpp b/alib2cli/src/registry/OutputFileRegistry.hpp index c50d761532a00ccc5e43eb5dcc88085ef9176b40..4365f57f4c2557c0fd5a36a252df6b9a37e9c853 100644 --- a/alib2cli/src/registry/OutputFileRegistry.hpp +++ b/alib2cli/src/registry/OutputFileRegistry.hpp @@ -35,15 +35,12 @@ class OutputFileRegistry { std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & typehint ) const override; }; - static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ) { - static ext::map < std::string, std::unique_ptr < Entry > > inputFileHandlers; - return inputFileHandlers; - } + static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void registerOutputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & typehint ) ) { - getEntries ( ).insert ( std::make_pair ( fileType, std::unique_ptr < Entry > ( new EntryImpl ( callback ) ) ) ); - } + static void registerOutputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & typehint ) ); + + static void unregisterOutputFileHandler ( const std::string & fileType ); static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & fileType, const std::string & typehint ); };