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 {
public:
template < class ReturnType >
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 >
......
......@@ -47,7 +47,8 @@ class RawWriterRegistry {
public:
template < class ParamType >
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 >
......
......@@ -14,8 +14,8 @@ std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getA
while ( isspace ( ss.peek ( ) ) )
ss.get ( );
 
auto lambda = [ & ] ( const std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > & entry ) {
return entry.first ( ss );
auto lambda = [ & ] ( const std::pair < const std::string, std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > & entry ) {
return entry.second.first ( ss );
};
 
const auto & entryIterator = getEntries ( ).find ( group );
......@@ -26,14 +26,14 @@ std::shared_ptr < abstraction::OperationAbstraction > StringReaderRegistry::getA
 
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 ( ) )
throw exception::CommonException ( "No callback handling input found." );
 
if ( pos != ss.tellg ( ) )
throw exception::CommonException ( "First function of registered callback moved the stream." );
 
return callback->second->getAbstraction ( );
return callback->second.second->getAbstraction ( );
}
 
} /* namespace abstraction */
......@@ -39,15 +39,19 @@ class StringReaderRegistry {
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::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 > > > > & getEntries ( ) {
static ext::map < std::string, ext::map < std::string, std::pair < std::function < bool ( std::istream & ) >, std::unique_ptr < Entry > > > > readers;
return readers;
}
 
public:
template < class Group, class ReturnType >
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 );
......
......@@ -48,7 +48,8 @@ class StringWriterRegistry {
public:
template < class ParamType >
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 >
......
......@@ -48,7 +48,8 @@ class XmlComposerRegistry {
public:
template < class ParamType >
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 >
......
......@@ -47,7 +47,8 @@ class XmlParserRegistry {
public:
template < class ReturnType >
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 >
......
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