StringIOIndex.cpp
Go to the documentation of this file.
1 #include "StringIOIndex.hpp"
2 
3 #include "Context.hpp"
4 
5 namespace iv
6 {
7 
8 std::unordered_map< std::type_index, StringIOIndex::Handler > * StringIOIndex::_handlers;
9 
10 std::unordered_map< std::type_index, StringIOIndex::Handler > & StringIOIndex::handlers()
11 {
12  if( !_handlers )
13  _handlers = new std::unordered_map< std::type_index, StringIOIndex::Handler >;
14  return *_handlers;
15 }
16 
17 std::any StringIOIndex::Read( std::type_index type, const char * source, Context const * context )
18 {
19  auto & h = handlers();
20  auto it = h.find( type );
21  if( it == h.end() )
22  {
23  context->log( SRC_INFO, Defs::Log::Warning, "StringIOIndex does not have StringIO handler registered for type '", type.name(), "'. Register it using StringIOIndex< T >::Register()." );
24  return std::any();
25  }
26 
27  return it->second.read( type, source, context );
28 }
29 
30 std::string StringIOIndex::Write( std::any const & value, Context const * context )
31 {
32  std::type_index type = value.type();
33  auto & h = handlers();
34  auto it = h.find( type );
35  if( it == h.end() )
36  {
37  context->log( SRC_INFO, Defs::Log::Warning, "StringIOIndex does not have r/w handler registered for type '", type.name(), "'." );
38  return std::string( "<" )+value.type().name()+( "/undecoded>" );
39  }
40 
41  return it->second.write( value, context );
42 }
43 
44 }