Skip to content
Snippets Groups Projects
Commit ed2f45cf authored by Jan Trávníček's avatar Jan Trávníček
Browse files

force unique registration on raw, string. xml parsers/composers

parent bc5d3442
No related branches found
No related tags found
No related merge requests found
Pipeline #27654 passed
...@@ -44,7 +44,8 @@ class RawReaderRegistry { ...@@ -44,7 +44,8 @@ class RawReaderRegistry {
public: public:
template < class ReturnType > template < class ReturnType >
static void registerRawReader ( std::string type ) { static void registerRawReader ( std::string type ) {
getEntries ( ).insert ( std::make_pair ( std::move ( type ), std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ); if ( ! getEntries ( ).insert ( std::make_pair ( std::move ( type ), std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ).second )
throw std::invalid_argument ( "Entry " + type + " already registered." );
} }
   
template < class ReturnType > template < class ReturnType >
......
...@@ -47,7 +47,8 @@ class RawWriterRegistry { ...@@ -47,7 +47,8 @@ class RawWriterRegistry {
public: public:
template < class ParamType > template < class ParamType >
static void registerRawWriter ( std::string param ) { static void registerRawWriter ( std::string param ) {
getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); if ( ! getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ).second )
throw std::invalid_argument ( "Entry " + param + " already registered." );
} }
   
template < class ParamType > template < class ParamType >
......
...@@ -14,8 +14,8 @@ std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getA ...@@ -14,8 +14,8 @@ std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getA
while ( isspace ( ss.peek ( ) ) ) while ( isspace ( ss.peek ( ) ) )
ss.get ( ); ss.get ( );
   
auto lambda = [ & ] ( const std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > & entry ) { auto lambda = [ & ] ( const std::pair < const std::string, std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > & entry ) {
return entry.first ( ss ); return entry.second.first ( ss );
}; };
   
const auto & entryIterator = getEntries ( ).find ( group ); const auto & entryIterator = getEntries ( ).find ( group );
...@@ -26,14 +26,14 @@ std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getA ...@@ -26,14 +26,14 @@ std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getA
   
int pos = ss.tellg(); int pos = ss.tellg();
   
typename ext::deque < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > >::const_iterator callback = find_if ( entries.begin ( ), entries.end ( ), lambda ); typename ext::map < std::string, std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > >::const_iterator callback = find_if ( entries.begin ( ), entries.end ( ), lambda );
if ( callback == entries.end ( ) ) if ( callback == entries.end ( ) )
throw exception::CommonException ( "No callback handling input found." ); throw exception::CommonException ( "No callback handling input found." );
   
if ( pos != ss.tellg ( ) ) if ( pos != ss.tellg ( ) )
throw exception::CommonException ( "First function of registered callback moved the stream." ); throw exception::CommonException ( "First function of registered callback moved the stream." );
   
return callback->second->getAbstraction ( ); return callback->second.second->getAbstraction ( );
} }
   
} /* namespace abstraction */ } /* namespace abstraction */
...@@ -39,15 +39,19 @@ class StringReaderRegistry { ...@@ -39,15 +39,19 @@ class StringReaderRegistry {
virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( ) const override; virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( ) const override;
}; };
   
static ext::map < std::string, ext::deque < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > > & getEntries ( ) { static ext::map < std::string, ext::map < std::string, std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > > & getEntries ( ) {
static ext::map < std::string, ext::deque < std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > > readers; static ext::map < std::string, ext::map < std::string, std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > > readers;
return readers; return readers;
} }
   
public: public:
template < class Group, class ReturnType > template < class Group, class ReturnType >
static void registerStringReader ( ) { static void registerStringReader ( ) {
getEntries ( ) [ ext::to_string < Group > ( ) ].push_back ( std::make_pair ( core::stringApi < ReturnType >::first, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ); std::string group = ext::to_string < Group > ( );
std::string returnType = ext::to_string < ReturnType > ( );
if ( ! getEntries ( ) [ group ].insert ( std::make_pair ( std::move ( returnType ), std::make_pair ( core::stringApi < ReturnType >::first, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ) ).second )
throw std::invalid_argument ( "Entry " + returnType + " already registered in " + group + "." );
} }
   
static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & group, const std::string & data ); static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & group, const std::string & data );
......
...@@ -48,7 +48,8 @@ class StringWriterRegistry { ...@@ -48,7 +48,8 @@ class StringWriterRegistry {
public: public:
template < class ParamType > template < class ParamType >
static void registerStringWriter ( std::string param ) { static void registerStringWriter ( std::string param ) {
getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); if ( ! getEntries ( ).insert ( std::make_pair ( std::move ( param ), std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ).second )
throw std::invalid_argument ( "Entry " + param + " already registered." );
} }
   
template < class ParamType > template < class ParamType >
......
...@@ -48,7 +48,8 @@ class XmlComposerRegistry { ...@@ -48,7 +48,8 @@ class XmlComposerRegistry {
public: public:
template < class ParamType > template < class ParamType >
static void registerXmlComposer ( std::string param ) { static void registerXmlComposer ( std::string param ) {
getEntries ( ).insert ( std::make_pair ( param, std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ); if ( ! getEntries ( ).insert ( std::make_pair ( param, std::unique_ptr < Entry > ( new EntryImpl < ParamType > ( ) ) ) ).second )
throw std::invalid_argument ( "Entry " + param + " already registered." );
} }
   
template < class ParamType > template < class ParamType >
......
...@@ -47,7 +47,8 @@ class XmlParserRegistry { ...@@ -47,7 +47,8 @@ class XmlParserRegistry {
public: public:
template < class ReturnType > template < class ReturnType >
static void registerXmlParser ( std::string result ) { static void registerXmlParser ( std::string result ) {
getEntries ( ).insert ( std::make_pair ( result, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ); if ( ! getEntries ( ).insert ( std::make_pair ( result, std::unique_ptr < Entry > ( new EntryImpl < ReturnType > ( ) ) ) ).second )
throw std::invalid_argument ( "Entry " + result + " already registered." );
} }
   
template < class ReturnType > template < class ReturnType >
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment