StringIO_Table.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "StringIO.hpp"
4 #include "SS.hpp"
5 #include "Context.hpp"
6 
7 namespace iv
8 {
9 
10 //-------- table (eg enum) helper -----------------
40 template< class Enum >
42 {
43  using ValuesType = std::initializer_list< std::pair< Enum, const char * > >;
44 
45  Enum Read( const char * source, Context const * context ) const
46  {
47  for( std::pair< Enum, const char * > const & value : StringIO< Enum >::Values )
48  {
49  if( strcmp( source, value.second ) == 0 )
50  return value.first;
51  }
52 
53  SS ss;
54  ss<<"Can not read string '"<<source<<"' as type "<<typeid( Enum ).name()<<".\n";
55  ss<<" possible values: ";
56  for( std::pair< Enum, const char * > const & value : StringIO< Enum >::Values )
57  ss << value.second << " ";
58 
59  context->warning( SRC_INFO, ss<<SS::c_str() );
60 
61  return Enum();
62  }
63 
64  std::string Write( Enum const & value, Context const * context ) const
65  {
66  for( std::pair< Enum, const char * > const & val : StringIO< Enum >::Values )
67  {
68  if( value == val.first )
69  return val.second;
70  }
71 
72  //context->warning( SRC_INFO, Warning( "Can not write value of type '", typeid( Enum ).name(), "' to string." ) );
73  std::stringstream ss;
74  ss << (typename std::underlying_type< Enum >::type)value;
75  return ss.str();
76  }
77 };
78 
79 }