StringIOIndex.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "StringIO.hpp"
4 
5 #include <any>
6 #include <typeindex>
7 #include <unordered_map>
8 #include <string>
9 
10 namespace iv
11 {
12 
14 {
15 public:
16  static std::any Read( std::type_index, const char * source, Context const * context );
17  static std::string Write( std::any const & value, Context const * context );
18 
19  template< class T >
20  static void Register();
21 
22 private:
23  static void Register_Impl( std::any * ){}
24 
25  template< class T >
26  static void Register_Impl( T * );
27 
28  template< class Type >
29  static std::any DoRead( std::type_index, const char * source, Context const * context );
30 
31  template< class Type >
32  static std::string DoWrite( std::any const & value, Context const * context );
33 
34  struct Handler
35  {
36  std::any ( *read )( std::type_index, const char * source, Context const * context );
37  std::string ( *write )( std::any const & value, Context const * context );
38  };
39 
40  static std::unordered_map< std::type_index, Handler > & handlers();
41 
42 private:
43  static std::unordered_map< std::type_index, Handler > * _handlers;
44 };
45 
46 
47 // register it as StriongIO for std::any
48 template<>
49 struct StringIO< std::any >
50 {
51  std::any Read( const char * source, Context const * context ) const
52  {
53  // unimplemented
54  return std::any();
55  }
56 
57  std::string Write( std::any const & value, Context const * context ) const
58  {
59  return StringIOIndex::Write( value, context );
60  }
61 };
62 
63 }
64 
65 #include "StringIOIndex.inl"