From ee50d56fc98e7c7499a4bf591f5ca97d6bf72a77 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 12 Jun 2019 16:08:18 +0200 Subject: [PATCH] redesign register and unregister methods to save space --- .../src/registry/CastRegistry.cpp | 11 ++++++++ .../src/registry/CastRegistry.hpp | 19 +++++--------- .../src/registry/ContainerRegistry.cpp | 25 ++++++++++++++++++ .../src/registry/ContainerRegistry.hpp | 23 +++------------- .../src/registry/ImmediateRegistry.cpp | 11 ++++++++ .../src/registry/ImmediateRegistry.hpp | 11 +++----- .../src/registry/NormalizeRegistry.cpp | 14 ++++++++++ .../src/registry/NormalizeRegistry.hpp | 14 +++------- .../src/registry/ValuePrinterRegistry.cpp | 11 ++++++++ .../src/registry/ValuePrinterRegistry.hpp | 11 +++----- alib2raw/src/registry/RawReaderRegistry.cpp | 11 ++++++++ alib2raw/src/registry/RawReaderRegistry.hpp | 11 +++----- alib2raw/src/registry/RawWriterRegistry.cpp | 11 ++++++++ alib2raw/src/registry/RawWriterRegistry.hpp | 11 +++----- .../src/registry/StringReaderRegistry.cpp | 15 +++++++++++ .../src/registry/StringReaderRegistry.hpp | 18 +++++-------- .../src/registry/StringWriterRegistry.cpp | 11 ++++++++ .../src/registry/StringWriterRegistry.hpp | 11 +++----- alib2xml/src/registry/XmlComposerRegistry.cpp | 11 ++++++++ alib2xml/src/registry/XmlComposerRegistry.hpp | 11 +++----- .../registry/XmlContainerParserRegistry.cpp | 26 +++++++++++++++++++ .../registry/XmlContainerParserRegistry.hpp | 24 +++-------------- alib2xml/src/registry/XmlParserRegistry.cpp | 11 ++++++++ alib2xml/src/registry/XmlParserRegistry.hpp | 11 +++----- 24 files changed, 220 insertions(+), 123 deletions(-) diff --git a/alib2abstraction/src/registry/CastRegistry.cpp b/alib2abstraction/src/registry/CastRegistry.cpp index 07ae0efc88..774df44767 100644 --- a/alib2abstraction/src/registry/CastRegistry.cpp +++ b/alib2abstraction/src/registry/CastRegistry.cpp @@ -16,6 +16,17 @@ ext::map < ext::pair < std::string, std::string >, std::unique_ptr < CastRegistr return casts; } +void CastRegistry::unregisterCast ( const std::string & target, const std::string & param ) { + if ( getEntries ( ).erase ( ext::tie ( target, param ) ) == 0u ) + throw std::invalid_argument ( "Entry from " + param + " to " + target + " not registered." ); +} + +void CastRegistry::registerCast ( std::string target, std::string param, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( ext::make_pair ( std::move ( target ), std::move ( param ) ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry from " + iter.first->first.second + " to " + iter.first->first.first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > CastRegistry::getAbstraction ( const std::string & target, const std::string & param ) { auto entry = getEntries ( ).end ( ); for ( auto iter = getEntries ( ).begin ( ); iter != getEntries ( ).end ( ); ++ iter ) diff --git a/alib2abstraction/src/registry/CastRegistry.hpp b/alib2abstraction/src/registry/CastRegistry.hpp index 2a421c10c8..11655e85f3 100644 --- a/alib2abstraction/src/registry/CastRegistry.hpp +++ b/alib2abstraction/src/registry/CastRegistry.hpp @@ -64,10 +64,7 @@ private: static ext::map < ext::pair < std::string, std::string >, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterCast ( const std::string & target, const std::string & param ) { - if ( getEntries ( ).erase ( ext::tie ( target, param ) ) == 0u ) - throw std::invalid_argument ( "Entry from " + param + " to " + target + " not registered." ); - } + static void unregisterCast ( const std::string & target, const std::string & param ); template < class TargetType, class ParamType > static void unregisterCast ( ) { @@ -77,11 +74,11 @@ public: unregisterCast ( target, param ); } + static void registerCast ( std::string target, std::string param, std::unique_ptr < Entry > entry ); + template < class TargetType, class ParamType > static void registerCast ( std::string target, std::string param, bool isExplicit = false ) { - auto iter = getEntries ( ).insert ( std::make_pair ( ext::make_pair ( std::move ( target ), std::move ( param ) ), std::unique_ptr < Entry > ( new DefaultEntryImpl < TargetType, ParamType > ( isExplicit ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry from " + iter.first->first.second + " to " + iter.first->first.first + " already registered." ); + registerCast ( std::move ( target ), std::move ( param ), std::unique_ptr < Entry > ( new DefaultEntryImpl < TargetType, ParamType > ( isExplicit ) ) ); } template < class TargetType, class ParamType > @@ -94,9 +91,7 @@ public: template < class TargetType, class ParamType > static void registerCastAlgorithm ( std::string target, std::string param, TargetType ( * callback ) ( const ParamType & ), bool isExplicit = false ) { - auto iter = getEntries ( ).insert ( std::make_pair ( ext::make_pair ( std::move ( target ), std::move ( param ) ), std::unique_ptr < Entry > ( new AlgorithmEntryImpl < TargetType, const ParamType & > ( callback, isExplicit ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry from " + iter.first->first.second + " to " + iter.first->first.first + " already registered." ); + registerCast ( std::move ( target ), std::move ( param ), std::unique_ptr < Entry > ( new AlgorithmEntryImpl < TargetType, const ParamType & > ( callback, isExplicit ) ) ); } template < class TargetType, class ParamType > @@ -109,9 +104,7 @@ public: template < class TargetType, class ParamType > static void registerCastAlgorithm ( std::string target, std::string param, TargetType ( * callback ) ( ParamType ), bool isExplicit = false ) { - auto iter = getEntries ( ).insert ( std::make_pair ( ext::make_pair ( std::move ( target ), std::move ( param ) ), std::unique_ptr < Entry > ( new AlgorithmEntryImpl < TargetType, ParamType > ( callback, isExplicit ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry from " + iter.first->first.second + " to " + iter.first->first.first + " already registered." ); + registerCast ( std::move ( target ), std::move ( param ), std::unique_ptr < Entry > ( new AlgorithmEntryImpl < TargetType, ParamType > ( callback, isExplicit ) ) ); } template < class TargetType, class ParamType > diff --git a/alib2abstraction/src/registry/ContainerRegistry.cpp b/alib2abstraction/src/registry/ContainerRegistry.cpp index 5dcbc02f8d..0c481d1257 100644 --- a/alib2abstraction/src/registry/ContainerRegistry.cpp +++ b/alib2abstraction/src/registry/ContainerRegistry.cpp @@ -16,6 +16,31 @@ ext::map < std::string, ext::list < ext::pair < std::string, std::unique_ptr < C return containerGroups; } +void ContainerRegistry::unregisterSet ( const std::string & param ) { + std::string container = "Set"; + + auto & group = getEntries ( ) [ container ]; + auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const ext::pair < std::string, std::unique_ptr < Entry > > & entry ) { + return entry.first == param; + } ); + if ( iter == group.end ( ) ) + throw std::invalid_argument ( "Callback for " + param + " in contaier " + container + " not registered." ); + group.erase ( iter ); +} + +void ContainerRegistry::registerSet ( std::string param, std::unique_ptr < Entry > entry ) { + std::string container = "Set"; + + auto & group = getEntries ( ) [ container ]; + auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const ext::pair < std::string, std::unique_ptr < Entry > > & elem ) { + return elem.first == param; + } ); + if ( iter != group.end ( ) ) + throw std::invalid_argument ( "Callback for " + param + " in contaier " + container + " already registered." ); + + group.insert ( group.end ( ), ext::make_pair ( std::move ( param ), std::move ( entry ) ) ); +} + bool ContainerRegistry::hasAbstraction ( const std::string & container ) { return getEntries ( ).contains ( container ); } diff --git a/alib2abstraction/src/registry/ContainerRegistry.hpp b/alib2abstraction/src/registry/ContainerRegistry.hpp index cd336aee97..5550937033 100644 --- a/alib2abstraction/src/registry/ContainerRegistry.hpp +++ b/alib2abstraction/src/registry/ContainerRegistry.hpp @@ -40,17 +40,7 @@ private: static ext::map < std::string, ext::list < ext::pair < std::string, std::unique_ptr < Entry > > > > & getEntries ( ); public: - static void unregisterSet ( const std::string & param ) { - std::string container = "Set"; - - auto & group = getEntries ( ) [ container ]; - auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const ext::pair < std::string, std::unique_ptr < Entry > > & entry ) { - return entry.first == param; - } ); - if ( iter == group.end ( ) ) - throw std::invalid_argument ( "Callback for " + param + " in contaier " + container + " not registered." ); - group.erase ( iter ); - } + static void unregisterSet ( const std::string & param ); template < class ParamTypes > static void unregisterSet ( ) { @@ -58,16 +48,11 @@ public: unregisterSet ( param ); } + static void registerSet ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamTypes > static void registerSet ( std::string param ) { - std::string container = "Set"; - - auto & group = getEntries ( ) [ container ]; - for ( const ext::pair < std::string, std::unique_ptr < Entry > > & entry : group ) - if ( entry.first == param ) - throw std::invalid_argument ( "Callback for " + param + " in contaier " + container + " already registered." ); - - group.insert ( group.end ( ), ext::make_pair ( std::move ( param ), std::make_unique < SetEntryImpl < ParamTypes > > ( ) ) ); + registerSet ( std::move ( param ), std::make_unique < SetEntryImpl < ParamTypes > > ( ) ); } template < class ParamTypes > diff --git a/alib2abstraction/src/registry/ImmediateRegistry.cpp b/alib2abstraction/src/registry/ImmediateRegistry.cpp index ad26da2e80..0915015deb 100644 --- a/alib2abstraction/src/registry/ImmediateRegistry.cpp +++ b/alib2abstraction/src/registry/ImmediateRegistry.cpp @@ -14,6 +14,17 @@ ext::map < std::string, std::unique_ptr < ImmediateRegistry::Entry > > & Immedia return fileWriters; } +void ImmediateRegistry::unregisterImmediate ( const std::string & result ) { + if ( getEntries ( ).erase ( result ) == 0u ) + throw std::invalid_argument ( "Entry " + result + " not registered." ); +} + +void ImmediateRegistry::registerImmediate ( std::string result, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( result ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > ImmediateRegistry::getAbstraction ( const std::string & result, std::string value ) { auto res = getEntries ( ).find ( result ); if ( res == getEntries ( ).end ( ) ) diff --git a/alib2abstraction/src/registry/ImmediateRegistry.hpp b/alib2abstraction/src/registry/ImmediateRegistry.hpp index 68b4cc662d..35fd25efe8 100644 --- a/alib2abstraction/src/registry/ImmediateRegistry.hpp +++ b/alib2abstraction/src/registry/ImmediateRegistry.hpp @@ -39,10 +39,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterImmediate ( const std::string & result ) { - if ( getEntries ( ).erase ( result ) == 0u ) - throw std::invalid_argument ( "Entry " + result + " not registered." ); - } + static void unregisterImmediate ( const std::string & result ); template < class ResultType > static void unregisterImmediate ( ) { @@ -50,11 +47,11 @@ public: unregisterImmediate ( result ); } + static void registerImmediate ( std::string result, std::unique_ptr < Entry > entry ); + template < class ResultType > static void registerImmediate ( std::string result ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( result ), std::unique_ptr < Entry > ( new EntryImpl < ResultType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerImmediate ( std::move ( result ), std::unique_ptr < Entry > ( new EntryImpl < ResultType > ( ) ) ); } template < class ResultType > diff --git a/alib2abstraction/src/registry/NormalizeRegistry.cpp b/alib2abstraction/src/registry/NormalizeRegistry.cpp index 99bc4a61ad..ab70b42683 100644 --- a/alib2abstraction/src/registry/NormalizeRegistry.cpp +++ b/alib2abstraction/src/registry/NormalizeRegistry.cpp @@ -14,6 +14,20 @@ ext::map < std::string, std::list < std::unique_ptr < NormalizeRegistry::Entry > return entries; } +void NormalizeRegistry::unregisterNormalize ( const std::string & param, std::list < std::unique_ptr < Entry > >::const_iterator iter ) { + auto & entry = getEntries ( ) [ param ]; + + if ( ! ext::range_contains_iterator ( entry.begin ( ), entry.end ( ), iter ) ) + throw std::invalid_argument ( "Normalization entry not found in " + param + "." ); + + entry.erase ( iter ); +} + +std::list < std::unique_ptr < NormalizeRegistry::Entry > >::const_iterator NormalizeRegistry::registerNormalize ( std::string param, std::unique_ptr < Entry > entry ) { + auto & collection = getEntries ( ) [ std::move ( param ) ]; + return collection.insert ( collection.end ( ), std::move ( entry ) ); +} + std::shared_ptr < abstraction::OperationAbstraction > NormalizeRegistry::getAbstraction ( const std::string & param ) { auto res = getEntries ( ).find ( param ); if ( res == getEntries ( ).end ( ) || res->second.empty ( ) ) diff --git a/alib2abstraction/src/registry/NormalizeRegistry.hpp b/alib2abstraction/src/registry/NormalizeRegistry.hpp index 356da9ebdb..9706699e1b 100644 --- a/alib2abstraction/src/registry/NormalizeRegistry.hpp +++ b/alib2abstraction/src/registry/NormalizeRegistry.hpp @@ -43,14 +43,7 @@ private: static ext::map < std::string, std::list < std::unique_ptr < Entry > > > & getEntries ( ); public: - static void unregisterNormalize ( const std::string & param, std::list < std::unique_ptr < Entry > >::const_iterator iter ) { - auto & entry = getEntries ( ) [ param ]; - - if ( ! ext::range_contains_iterator ( entry.begin ( ), entry.end ( ), iter ) ) - throw std::invalid_argument ( "Normalization entry not found in " + param + "." ); - - entry.erase ( iter ); - } + static void unregisterNormalize ( const std::string & param, std::list < std::unique_ptr < Entry > >::const_iterator iter ); template < class ParamType > static void unregisterNormalize ( std::list < std::unique_ptr < Entry > >::const_iterator iter ) { @@ -58,10 +51,11 @@ public: unregisterNormalize ( param, iter ); } + static std::list < std::unique_ptr < Entry > >::const_iterator registerNormalize ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamType > static std::list < std::unique_ptr < Entry > >::const_iterator registerNormalize ( std::string param ) { - auto & entry = getEntries ( ) [ std::move ( param ) ]; - return entry.insert ( entry.end ( ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ); + return registerNormalize ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ); } template < class ParamType > diff --git a/alib2abstraction/src/registry/ValuePrinterRegistry.cpp b/alib2abstraction/src/registry/ValuePrinterRegistry.cpp index 93868482cb..fced34393e 100644 --- a/alib2abstraction/src/registry/ValuePrinterRegistry.cpp +++ b/alib2abstraction/src/registry/ValuePrinterRegistry.cpp @@ -22,6 +22,17 @@ std::shared_ptr < abstraction::OperationAbstraction > ValuePrinterRegistry::getA return res->second->getAbstraction ( os ); } +void ValuePrinterRegistry::unregisterValuePrinter ( const std::string & param ) { + if ( getEntries ( ).erase ( param ) == 0u ) + throw std::invalid_argument ( "Entry " + param + " not registered." ); +} + +void ValuePrinterRegistry::registerValuePrinter ( std::string param, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + template < > std::shared_ptr < abstraction::OperationAbstraction > ValuePrinterRegistry::EntryImpl < void >::getAbstraction ( std::ostream & os ) const { return std::make_shared < abstraction::ValuePrinterAbstraction < void > > ( os ); diff --git a/alib2abstraction/src/registry/ValuePrinterRegistry.hpp b/alib2abstraction/src/registry/ValuePrinterRegistry.hpp index 4eb5c7674b..a37c2129bf 100644 --- a/alib2abstraction/src/registry/ValuePrinterRegistry.hpp +++ b/alib2abstraction/src/registry/ValuePrinterRegistry.hpp @@ -39,10 +39,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterValuePrinter ( const std::string & param ) { - if ( getEntries ( ).erase ( param ) == 0u ) - throw std::invalid_argument ( "Entry " + param + " not registered." ); - } + static void unregisterValuePrinter ( const std::string & param ); template < class ParamType > static void unregisterValuePrinter ( ) { @@ -50,11 +47,11 @@ public: unregisterValuePrinter ( param ); } + static void registerValuePrinter ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamType > static void registerValuePrinter ( std::string param ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerValuePrinter ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ); } template < class ParamType > diff --git a/alib2raw/src/registry/RawReaderRegistry.cpp b/alib2raw/src/registry/RawReaderRegistry.cpp index 8044273553..68e8e70b76 100644 --- a/alib2raw/src/registry/RawReaderRegistry.cpp +++ b/alib2raw/src/registry/RawReaderRegistry.cpp @@ -16,6 +16,17 @@ ext::map < std::string, std::unique_ptr < RawReaderRegistry::Entry > > & RawRead return readers; } +void RawReaderRegistry::unregisterRawReader ( const std::string & type ) { + if ( getEntries ( ).erase ( type ) == 0u ) + throw std::invalid_argument ( "Entry " + type + " not registered." ); +} + +void RawReaderRegistry::registerRawReader ( std::string type, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( type ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > RawReaderRegistry::getAbstraction ( const std::string & type ) { for ( const std::pair < const std::string, std::unique_ptr < Entry > > & entry : getEntries ( ) ) { if ( ext::is_same_type ( entry.first, type ) ) diff --git a/alib2raw/src/registry/RawReaderRegistry.hpp b/alib2raw/src/registry/RawReaderRegistry.hpp index 23ca593a0b..2dec588b0a 100644 --- a/alib2raw/src/registry/RawReaderRegistry.hpp +++ b/alib2raw/src/registry/RawReaderRegistry.hpp @@ -37,10 +37,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterRawReader ( const std::string & type ) { - if ( getEntries ( ).erase ( type ) == 0u ) - throw std::invalid_argument ( "Entry " + type + " not registered." ); - } + static void unregisterRawReader ( const std::string & type ); template < class ReturnType > static void unregisterRawReader ( ) { @@ -48,11 +45,11 @@ public: unregisterRawReader ( type ); } + static void registerRawReader ( std::string type, std::unique_ptr < Entry > entry ); + template < class ReturnType > static void registerRawReader ( std::string type ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( type ), std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerRawReader ( std::move ( type ), std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ); } template < class ReturnType > diff --git a/alib2raw/src/registry/RawWriterRegistry.cpp b/alib2raw/src/registry/RawWriterRegistry.cpp index 94eb942eb1..69144b48ce 100644 --- a/alib2raw/src/registry/RawWriterRegistry.cpp +++ b/alib2raw/src/registry/RawWriterRegistry.cpp @@ -14,6 +14,17 @@ ext::map < std::string, std::unique_ptr < RawWriterRegistry::Entry > > & RawWrit return writers; } +void RawWriterRegistry::unregisterRawWriter ( const std::string & param ) { + if ( getEntries ( ).erase ( param ) == 0u ) + throw std::invalid_argument ( "Entry " + param + " not registered." ); +} + +void RawWriterRegistry::registerRawWriter ( std::string param, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > RawWriterRegistry::getAbstraction ( const std::string & param ) { auto type = getEntries ( ).find ( param ); if ( type == getEntries ( ).end ( ) ) diff --git a/alib2raw/src/registry/RawWriterRegistry.hpp b/alib2raw/src/registry/RawWriterRegistry.hpp index b782acc23f..2f8f71a906 100644 --- a/alib2raw/src/registry/RawWriterRegistry.hpp +++ b/alib2raw/src/registry/RawWriterRegistry.hpp @@ -39,10 +39,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterRawWriter ( const std::string & param ) { - if ( getEntries ( ).erase ( param ) == 0u ) - throw std::invalid_argument ( "Entry " + param + " not registered." ); - } + static void unregisterRawWriter ( const std::string & param ); template < class ParamType > static void unregisterRawWriter ( ) { @@ -50,11 +47,11 @@ public: unregisterRawWriter ( param ); } + static void registerRawWriter ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamType > static void registerRawWriter ( std::string param ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerRawWriter ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ); } template < class ParamType > diff --git a/alib2str/src/registry/StringReaderRegistry.cpp b/alib2str/src/registry/StringReaderRegistry.cpp index 4acfc5f4cb..e4ddef122d 100644 --- a/alib2str/src/registry/StringReaderRegistry.cpp +++ b/alib2str/src/registry/StringReaderRegistry.cpp @@ -14,6 +14,21 @@ ext::map < std::string, ext::list < std::pair < std::function < bool ( std::istr return readers; } +void StringReaderRegistry::unregisterStringReader ( const std::string & group, ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < StringReaderRegistry::Entry > > >::const_iterator iter ) { + auto & entry = getEntries ( ) [ group ]; + if ( ! ext::range_contains_iterator ( entry.begin ( ), entry.end ( ), iter ) ) + throw std::invalid_argument ( "Entry not found in " + group + "." ); + + entry.erase ( iter ); + if ( entry.empty ( ) ) + getEntries ( ).erase ( group ); +} + +ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < StringReaderRegistry::Entry > > >::const_iterator StringReaderRegistry::registerStringReader ( std::string group, std::function < bool ( std::istream & ) > first, std::unique_ptr < Entry > entry ) { + auto & collection = getEntries ( ) [ std::move ( group ) ]; + return collection.insert ( collection.end ( ), std::make_pair ( std::move ( first ), std::move ( entry ) ) ); +} + std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getAbstraction ( const std::string & group, const std::string & str ) { std::stringstream ss ( str ); while ( ext::isspace ( ss.peek ( ) ) ) diff --git a/alib2str/src/registry/StringReaderRegistry.hpp b/alib2str/src/registry/StringReaderRegistry.hpp index 47df792176..4bef65f81c 100644 --- a/alib2str/src/registry/StringReaderRegistry.hpp +++ b/alib2str/src/registry/StringReaderRegistry.hpp @@ -40,25 +40,19 @@ private: static ext::map < std::string, ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > > & getEntries ( ); public: + static void unregisterStringReader ( const std::string & group, ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > >::const_iterator iter ); + template < class Group > static void unregisterStringReader ( ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > >::const_iterator iter ) { std::string group = ext::to_string < Group > ( ); - - auto & entry = getEntries ( ) [ group ]; - if ( ! ext::range_contains_iterator ( entry.begin ( ), entry.end ( ), iter ) ) - throw std::invalid_argument ( "Entry not found in " + group + "." ); - - entry.erase ( iter ); - if ( entry.empty ( ) ) - getEntries ( ).erase ( group ); + unregisterStringReader ( group, iter ); } + static ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > >::const_iterator registerStringReader ( std::string group, std::function < bool ( std::istream & ) > first, std::unique_ptr < Entry > entry ); + template < class Group, class ReturnType > static ext::list < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > >::const_iterator registerStringReader ( ) { - std::string group = ext::to_string < Group > ( ); - - auto & entry = getEntries ( ) [ std::move ( group ) ]; - return entry.insert ( entry.end ( ), std::make_pair ( core::stringApi < ReturnType >::first, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ); + return registerStringReader ( ext::to_string < Group > ( ), core::stringApi < ReturnType >::first, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ); } static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & group, const std::string & str ); diff --git a/alib2str/src/registry/StringWriterRegistry.cpp b/alib2str/src/registry/StringWriterRegistry.cpp index 99ef936ad9..97cbd01b76 100644 --- a/alib2str/src/registry/StringWriterRegistry.cpp +++ b/alib2str/src/registry/StringWriterRegistry.cpp @@ -14,6 +14,17 @@ ext::map < std::string, std::unique_ptr < StringWriterRegistry::Entry > > & Stri return writers; } +void StringWriterRegistry::unregisterStringWriter ( const std::string & param ) { + if ( getEntries ( ).erase ( param ) == 0u ) + throw std::invalid_argument ( "Entry " + param + " not registered." ); +} + +void StringWriterRegistry::registerStringWriter ( std::string param, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > StringWriterRegistry::getAbstraction ( const std::string & param ) { auto type = getEntries ( ).find ( param ); if ( type == getEntries ( ).end ( ) ) diff --git a/alib2str/src/registry/StringWriterRegistry.hpp b/alib2str/src/registry/StringWriterRegistry.hpp index 3df5dffdef..376b3a0d51 100644 --- a/alib2str/src/registry/StringWriterRegistry.hpp +++ b/alib2str/src/registry/StringWriterRegistry.hpp @@ -40,10 +40,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterStringWriter ( const std::string & param ) { - if ( getEntries ( ).erase ( param ) == 0u ) - throw std::invalid_argument ( "Entry " + param + " not registered." ); - } + static void unregisterStringWriter ( const std::string & param ); template < class ParamType > static void unregisterStringWriter ( ) { @@ -51,11 +48,11 @@ public: unregisterStringWriter ( param ); } + static void registerStringWriter ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamType > static void registerStringWriter ( std::string param ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerStringWriter ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ); } template < class ParamType > diff --git a/alib2xml/src/registry/XmlComposerRegistry.cpp b/alib2xml/src/registry/XmlComposerRegistry.cpp index ad298fc61e..66db02f481 100644 --- a/alib2xml/src/registry/XmlComposerRegistry.cpp +++ b/alib2xml/src/registry/XmlComposerRegistry.cpp @@ -14,6 +14,17 @@ ext::map < std::string, std::unique_ptr < XmlComposerRegistry::Entry > > & XmlCo return fileWriters; } +void XmlComposerRegistry::unregisterXmlComposer ( const std::string & param ) { + if ( getEntries ( ).erase ( param ) == 0u ) + throw std::invalid_argument ( "Entry " + param + " not registered." ); +} + +void XmlComposerRegistry::registerXmlComposer ( std::string param, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > XmlComposerRegistry::getAbstraction ( const std::string & param ) { auto res = getEntries ( ).find ( param ); if ( res == getEntries ( ).end ( ) ) diff --git a/alib2xml/src/registry/XmlComposerRegistry.hpp b/alib2xml/src/registry/XmlComposerRegistry.hpp index 892d7b283c..2ae999bf9d 100644 --- a/alib2xml/src/registry/XmlComposerRegistry.hpp +++ b/alib2xml/src/registry/XmlComposerRegistry.hpp @@ -40,10 +40,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterXmlComposer ( const std::string & param ) { - if ( getEntries ( ).erase ( param ) == 0u ) - throw std::invalid_argument ( "Entry " + param + " not registered." ); - } + static void unregisterXmlComposer ( const std::string & param ); template < class ParamType > static void unregisterXmlComposer ( ) { @@ -51,11 +48,11 @@ public: unregisterXmlComposer ( param ); } + static void registerXmlComposer ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamType > static void registerXmlComposer ( std::string param ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerXmlComposer ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ); } template < class ParamType > diff --git a/alib2xml/src/registry/XmlContainerParserRegistry.cpp b/alib2xml/src/registry/XmlContainerParserRegistry.cpp index 661c46d7a1..a0eacda1bf 100644 --- a/alib2xml/src/registry/XmlContainerParserRegistry.cpp +++ b/alib2xml/src/registry/XmlContainerParserRegistry.cpp @@ -15,6 +15,32 @@ ext::map < std::string, ext::list < ext::pair < std::string, std::unique_ptr < X return containerGroups; } +void XmlContainerParserRegistry::unregisterSet ( const std::string & param ) { + std::string container = "Set"; + + auto & group = getEntries ( ) [ container ]; + auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const ext::pair < std::string, std::unique_ptr < Entry > > & entry ) { + return entry.first == param; + } ); + if ( iter == group.end ( ) ) + throw exception::CommonException ( "Callback for " + param + " in container " + container + " not registered." ); + + group.erase ( iter ); +} + +void XmlContainerParserRegistry::registerSet ( std::string param, std::unique_ptr < Entry > entry ) { + std::string container = "Set"; + + auto & group = getEntries ( ) [ container ]; + auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const ext::pair < std::string, std::unique_ptr < Entry > > & item ) { + return item.first == param; + } ); + if ( iter != group.end ( ) ) + throw exception::CommonException ( "Callback for " + param + " in container " + container + " already registered." ); + + group.insert ( group.end ( ), ext::make_pair ( std::move ( param ), std::move ( entry ) ) ); +} + bool XmlContainerParserRegistry::hasAbstraction ( const std::string & container ) { return getEntries ( ).contains ( container ); } diff --git a/alib2xml/src/registry/XmlContainerParserRegistry.hpp b/alib2xml/src/registry/XmlContainerParserRegistry.hpp index 0d08602c04..9872a54b14 100644 --- a/alib2xml/src/registry/XmlContainerParserRegistry.hpp +++ b/alib2xml/src/registry/XmlContainerParserRegistry.hpp @@ -40,18 +40,7 @@ private: static ext::map < std::string, ext::list < ext::pair < std::string, std::unique_ptr < Entry > > > > & getEntries ( ); public: - static void unregisterSet ( const std::string & param ) { - std::string container = "Set"; - - auto & group = getEntries ( ) [ container ]; - auto iter = find_if ( group.begin ( ), group.end ( ), [ & ] ( const ext::pair < std::string, std::unique_ptr < Entry > > & entry ) { - return entry.first == param; - } ); - if ( iter == group.end ( ) ) - throw exception::CommonException ( "Callback for " + param + " in container " + container + " not registered." ); - - group.erase ( iter ); - } + static void unregisterSet ( const std::string & param ); template < class ParamTypes > static void unregisterSet ( ) { @@ -59,16 +48,11 @@ public: unregisterSet ( param ); } + static void registerSet ( std::string param, std::unique_ptr < Entry > entry ); + template < class ParamTypes > static void registerSet ( std::string param ) { - std::string container = "Set"; - - auto & group = getEntries ( ) [ container ]; - for ( const ext::pair < std::string, std::unique_ptr < Entry > > & entry : group ) - if ( entry.first == param ) - throw exception::CommonException ( "Callback for " + param + " in container " + container + " already registered." ); - - group.insert ( group.end ( ), ext::make_pair ( std::move ( param ), std::make_unique < SetEntryImpl < ParamTypes > > ( ) ) ); + registerSet ( std::move ( param ), std::make_unique < SetEntryImpl < ParamTypes > > ( ) ); } template < class ParamTypes > diff --git a/alib2xml/src/registry/XmlParserRegistry.cpp b/alib2xml/src/registry/XmlParserRegistry.cpp index 5ecc00d1de..9e0034562f 100644 --- a/alib2xml/src/registry/XmlParserRegistry.cpp +++ b/alib2xml/src/registry/XmlParserRegistry.cpp @@ -14,6 +14,17 @@ ext::map < std::string, std::unique_ptr < XmlParserRegistry::Entry > > & XmlPars return parsers; } +void XmlParserRegistry::unregisterXmlParser ( const std::string & result ) { + if ( getEntries ( ).erase ( result ) == 0u ) + throw std::invalid_argument ( "Entry " + result + " not registered." ); +} + +void XmlParserRegistry::registerXmlParser ( std::string result, std::unique_ptr < Entry > entry ) { + auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( result ), std::move ( entry ) ) ); + if ( ! iter.second ) + throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); +} + std::shared_ptr < abstraction::OperationAbstraction > XmlParserRegistry::getAbstraction ( const std::string & typeName ) { auto type = getEntries ( ).find ( typeName ); if ( type == getEntries ( ).end ( ) ) diff --git a/alib2xml/src/registry/XmlParserRegistry.hpp b/alib2xml/src/registry/XmlParserRegistry.hpp index d1c1277658..63bd72326b 100644 --- a/alib2xml/src/registry/XmlParserRegistry.hpp +++ b/alib2xml/src/registry/XmlParserRegistry.hpp @@ -39,10 +39,7 @@ private: static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ); public: - static void unregisterXmlParser ( const std::string & result ) { - if ( getEntries ( ).erase ( result ) == 0u ) - throw std::invalid_argument ( "Entry " + result + " not registered." ); - } + static void unregisterXmlParser ( const std::string & result ); template < class ReturnType > static void unregisterXmlParser ( ) { @@ -50,11 +47,11 @@ public: unregisterXmlParser ( ret ); } + static void registerXmlParser ( std::string result, std::unique_ptr < Entry > entry ); + template < class ReturnType > static void registerXmlParser ( std::string result ) { - auto iter = getEntries ( ).insert ( std::make_pair ( std::move ( result ), std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ); - if ( ! iter.second ) - throw std::invalid_argument ( "Entry " + iter.first->first + " already registered." ); + registerXmlParser ( std::move ( result ), std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ; } template < class ReturnType > -- GitLab