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

xml, str: simplify implementation of registrations

parent c7adce78
No related branches found
No related tags found
1 merge request!221New normalization
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
   
namespace core { namespace core {
   
ext::map < std::string, std::unique_ptr < stringApi < object::Object >::GroupWriter > > & stringApi < object::Object >::composeFunctions ( ) { ext::map < core::type_details, std::unique_ptr < stringApi < object::Object >::GroupWriter > > & stringApi < object::Object >::composeFunctions ( ) {
static ext::map < std::string, std::unique_ptr < GroupWriter > > res; static ext::map < core::type_details, std::unique_ptr < GroupWriter > > res;
   
return res; return res;
} }
...@@ -27,20 +27,20 @@ ext::list < std::pair < std::function < bool ( ext::istream & ) >, std::unique_p ...@@ -27,20 +27,20 @@ ext::list < std::pair < std::function < bool ( ext::istream & ) >, std::unique_p
return entries.insert ( entries.end ( ), std::make_pair ( std::move ( first ), std::move ( entry ) ) ); return entries.insert ( entries.end ( ), std::make_pair ( std::move ( first ), std::move ( entry ) ) );
} }
   
void stringApi < object::Object >::unregisterStringWriter ( const std::string & type, const std::string & typeName ) { void stringApi < object::Object >::unregisterStringWriter ( const core::type_details & type ) {
if ( ! composeFunctions ( ).erase ( type ) ) { if ( ! composeFunctions ( ).erase ( type ) ) {
std::string groupName = ext::to_string < object::Object > ( ); std::string groupName = ext::to_string < object::Object > ( );
   
throw::exception::CommonException ( "Parse callback of " + typeName + " not registered in group " + groupName + "." ); throw::exception::CommonException ( "Parse callback of " + ext::to_string ( type ) + " not registered in group " + groupName + "." );
} }
} }
   
void stringApi < object::Object >::registerStringWriter ( std::string type, const std::string & typeName, std::unique_ptr < GroupWriter > entry ) { void stringApi < object::Object >::registerStringWriter ( const core::type_details & type, std::unique_ptr < GroupWriter > entry ) {
bool res = composeFunctions ( ).insert ( std::make_pair ( std::move ( type ), std::move ( entry ) ) ).second; bool res = composeFunctions ( ).insert ( std::make_pair ( std::move ( type ), std::move ( entry ) ) ).second;
if ( ! res ) { if ( ! res ) {
std::string groupName = ext::to_string < object::Object > ( ); std::string groupName = ext::to_string < object::Object > ( );
   
throw::exception::CommonException ( "Parse callback of " + typeName + " already registered in group " + groupName + "." ); throw::exception::CommonException ( "Parse callback of " + ext::to_string ( type ) + " already registered in group " + groupName + "." );
} }
} }
   
...@@ -80,11 +80,10 @@ bool stringApi < object::Object >::first ( ext::istream & input ) { ...@@ -80,11 +80,10 @@ bool stringApi < object::Object >::first ( ext::istream & input ) {
} }
   
void stringApi < object::Object >::compose ( ext::ostream & output, const object::Object & data ) { void stringApi < object::Object >::compose ( ext::ostream & output, const object::Object & data ) {
const auto & content = data.getData ( ); core::type_details type = core::type_details::get ( data );
std::string type = ext::to_string ( ext::type_index ( typeid ( content ) ) ); auto callback = std::find_if ( composeFunctions ( ).begin ( ), composeFunctions ( ).end ( ), [ & ] ( const std::pair < const core::type_details, std::unique_ptr < GroupWriter > > & entry ) { return entry.first.compatible_with ( type ); } );
auto callback = composeFunctions ( ).find ( type );
   
if ( callback == composeFunctions ( ).end ( ) ) throw exception::CommonException ( "Compose callback for " + type + " tag not registered." ); if ( callback == composeFunctions ( ).end ( ) ) throw exception::CommonException ( "Compose callback for " + ext::to_string ( type ) + " tag not registered." );
   
callback->second->compose ( output, data ); callback->second->compose ( output, data );
for ( unsigned i = 0; i < data.getId ( ); ++ i ) for ( unsigned i = 0; i < data.getId ( ); ++ i )
......
...@@ -52,7 +52,7 @@ public: ...@@ -52,7 +52,7 @@ public:
}; };
   
private: private:
static ext::map < std::string, std::unique_ptr < GroupWriter > > & composeFunctions ( ); static ext::map < core::type_details, std::unique_ptr < GroupWriter > > & composeFunctions ( );
   
template < class Type > template < class Type >
class WriterRegister : public GroupWriter { class WriterRegister : public GroupWriter {
...@@ -74,18 +74,18 @@ public: ...@@ -74,18 +74,18 @@ public:
return registerStringReader ( stringApi < Type >::first, std::unique_ptr < GroupReader > ( new ReaderRegister < Type > ( ) ) ); return registerStringReader ( stringApi < Type >::first, std::unique_ptr < GroupReader > ( new ReaderRegister < Type > ( ) ) );
} }
   
static void unregisterStringWriter ( const std::string & type, const std::string & typeName ); static void unregisterStringWriter ( const core::type_details & type );
   
template < class Type > template < class Type >
static void unregisterStringWriter ( ) { static void unregisterStringWriter ( ) {
unregisterStringWriter ( ext::to_string < object::AnyObject < Type > > ( ), ext::to_string < Type > ( ) ); unregisterStringWriter ( core::type_details::get < Type > ( ) );
} }
   
static void registerStringWriter ( std::string type, const std::string & typeName, std::unique_ptr < GroupWriter > entry ); static void registerStringWriter ( const core::type_details & type, std::unique_ptr < GroupWriter > entry );
   
template < class Type > template < class Type >
static void registerStringWriter ( ) { static void registerStringWriter ( ) {
registerStringWriter ( ext::to_string < object::AnyObject < Type > > ( ), ext::to_string < Type > ( ), std::unique_ptr < GroupWriter > ( new WriterRegister < Type > ( ) ) ); registerStringWriter ( core::type_details::get < Type > ( ), std::unique_ptr < GroupWriter > ( new WriterRegister < Type > ( ) ) );
} }
   
static object::Object parse ( ext::istream & input ); static object::Object parse ( ext::istream & input );
......
...@@ -8,8 +8,8 @@ ext::map < std::string, std::unique_ptr < xmlApi < object::Object >::GroupParser ...@@ -8,8 +8,8 @@ ext::map < std::string, std::unique_ptr < xmlApi < object::Object >::GroupParser
return res; return res;
} }
   
ext::map < std::string, std::unique_ptr < xmlApi < object::Object >::GroupComposer > > & xmlApi < object::Object >::composeFunctions ( ) { ext::map < core::type_details, std::unique_ptr < xmlApi < object::Object >::GroupComposer > > & xmlApi < object::Object >::composeFunctions ( ) {
static ext::map < std::string, std::unique_ptr < GroupComposer > > res; static ext::map < core::type_details, std::unique_ptr < GroupComposer > > res;
   
return res; return res;
} }
...@@ -31,20 +31,20 @@ void xmlApi < object::Object >::registerXmlReader ( std::string tagName, const c ...@@ -31,20 +31,20 @@ void xmlApi < object::Object >::registerXmlReader ( std::string tagName, const c
} }
} }
   
void xmlApi < object::Object >::unregisterXmlWriter ( const std::string & type, const core::type_details & typeName ) { void xmlApi < object::Object >::unregisterXmlWriter ( const core::type_details & type ) {
if ( ! composeFunctions ( ).erase ( type ) ) { if ( ! composeFunctions ( ).erase ( type ) ) {
std::string groupName = ext::to_string < object::Object > ( ); std::string groupName = ext::to_string < object::Object > ( );
   
throw::exception::CommonException ( "Compose callback of " + ext::to_string ( typeName ) + " not registered in group " + groupName + "." ); throw::exception::CommonException ( "Compose callback of " + ext::to_string ( type ) + " not registered in group " + groupName + "." );
} }
} }
   
void xmlApi < object::Object >::registerXmlWriter ( std::string type, const core::type_details & typeName, std::unique_ptr < GroupComposer > entry ) { void xmlApi < object::Object >::registerXmlWriter ( const core::type_details & type, std::unique_ptr < GroupComposer > entry ) {
bool res = composeFunctions ( ).insert ( std::make_pair ( std::move ( type ), std::move ( entry ) ) ).second; bool res = composeFunctions ( ).insert ( std::make_pair ( std::move ( type ), std::move ( entry ) ) ).second;
if ( ! res ) { if ( ! res ) {
std::string groupName = ext::to_string < object::Object > ( ); std::string groupName = ext::to_string < object::Object > ( );
   
throw::exception::CommonException ( "Compose callback of " + ext::to_string ( typeName ) + " already registered in group " + groupName + "." ); throw::exception::CommonException ( "Compose callback of " + ext::to_string ( type ) + " already registered in group " + groupName + "." );
} }
} }
   
...@@ -150,11 +150,11 @@ void xmlApi < object::Object >::composeUnique ( xmlApiOutputContext & output, co ...@@ -150,11 +150,11 @@ void xmlApi < object::Object >::composeUnique ( xmlApiOutputContext & output, co
} }
   
void xmlApi < object::Object >::composeObject ( xmlApiOutputContext & output, const object::Object & data ) { void xmlApi < object::Object >::composeObject ( xmlApiOutputContext & output, const object::Object & data ) {
const auto & content = data.getData ( ); core::type_details type = core::type_details::get ( data );
std::string type = ext::to_string ( ext::type_index ( typeid ( content ) ) ); auto callback = std::find_if ( composeFunctions ( ).begin ( ), composeFunctions ( ).end ( ), [ & ] ( const std::pair < const core::type_details, std::unique_ptr < GroupComposer > > & entry ) { return entry.first.compatible_with ( type ); } );
typename ext::map < std::string, std::unique_ptr < GroupComposer > >::iterator callback = composeFunctions ( ).find ( type );
   
if ( callback == composeFunctions ( ).end ( ) ) throw exception::CommonException ( "Compose callback for " + type + " tag not registered." ); if ( callback == composeFunctions ( ).end ( ) )
throw exception::CommonException ( "Compose callback for " + ext::to_string ( type ) + " tag not registered." );
   
/* encode referenced object */ /* encode referenced object */
callback->second->compose ( output, data ); callback->second->compose ( output, data );
......
...@@ -83,7 +83,7 @@ public: ...@@ -83,7 +83,7 @@ public:
}; };
   
private: private:
static ext::map < std::string, std::unique_ptr < GroupComposer > > & composeFunctions ( ); static ext::map < core::type_details, std::unique_ptr < GroupComposer > > & composeFunctions ( );
   
template < class Type > template < class Type >
class ComposerRegister : public GroupComposer { class ComposerRegister : public GroupComposer {
...@@ -109,18 +109,18 @@ public: ...@@ -109,18 +109,18 @@ public:
registerXmlReader ( xmlApi < Type >::xmlTagName ( ), core::type_details::get < Type > ( ), std::unique_ptr < GroupParser > ( new ParserRegister < Type > ( ) ) ); registerXmlReader ( xmlApi < Type >::xmlTagName ( ), core::type_details::get < Type > ( ), std::unique_ptr < GroupParser > ( new ParserRegister < Type > ( ) ) );
} }
   
static void unregisterXmlWriter ( const std::string & type, const core::type_details & typeName ); static void unregisterXmlWriter ( const core::type_details & type );
   
template < class Type > template < class Type >
static void unregisterXmlWriter ( ) { static void unregisterXmlWriter ( ) {
unregisterXmlWriter ( ext::to_string < object::AnyObject < Type > > ( ), core::type_details::get < Type > ( ) ); unregisterXmlWriter ( core::type_details::get < Type > ( ) );
} }
   
static void registerXmlWriter ( std::string type, const core::type_details & typeName, std::unique_ptr < GroupComposer > entry ); static void registerXmlWriter ( const core::type_details & type, std::unique_ptr < GroupComposer > entry );
   
template < class Type > template < class Type >
static void registerXmlWriter ( ) { static void registerXmlWriter ( ) {
registerXmlWriter ( ext::to_string < object::AnyObject < Type > > ( ), core::type_details::get < Type > ( ), std::unique_ptr < GroupComposer > ( new ComposerRegister < Type > ( ) ) ); registerXmlWriter ( core::type_details::get < Type > ( ), std::unique_ptr < GroupComposer > ( new ComposerRegister < Type > ( ) ) );
} }
   
static object::Object parseRef ( xmlApiInputContext & input ); static object::Object parseRef ( xmlApiInputContext & input );
......
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