Sleeping Wombat GUI  0.100
swGUI
Converters.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <codecvt>
5 #include <type_traits>
6 
8 
9 
10 
11 class Convert
12 {
13 private:
14 
15  template< typename SrcType >
16  static inline std::string EnumToString ( SrcType defaultVal );
17 
18  template< typename SrcType >
19  static inline SrcType StringToEnum ( const std::string& value, SrcType defaultValue );
20 
21 public:
22 
23  template< typename SrcType >
24  static inline typename std::enable_if< std::is_enum< SrcType >::value, std::string >::type
25  ToString ( const SrcType& val );
26 
27  template< typename SrcType >
28  static inline typename std::enable_if< std::is_enum< SrcType >::value, SrcType >::type
29  FromString ( const std::string& val, const SrcType& defaultValue );
30 
31  template< typename SrcType >
32  static inline typename std::enable_if< !std::is_enum< SrcType >::value, std::string >::type
33  ToString ( const SrcType& val );
34 
35  template< typename SrcType >
36  static inline typename std::enable_if< !std::is_enum< SrcType >::value, SrcType >::type
37  FromString ( const std::string& val, const SrcType& defaultValue );
38 
39 
40 
41  template<>
42  static inline typename std::enable_if< !std::is_enum< std::wstring >::value, std::string >::type
43  ToString< std::wstring > ( const std::wstring& value );
44 
45  template<>
46  static inline typename std::enable_if< !std::is_enum< std::wstring >::value, std::wstring >::type
47  FromString< std::wstring > ( const std::string& value, const std::wstring& defaultValue );
48 
49 };
50 
51 
52 template< typename SrcType >
53 inline typename std::enable_if< !std::is_enum< SrcType >::value, std::string >::type
54  Convert::ToString ( const SrcType& val )
55 {
56  return std::to_string( val );
57 }
58 
59 template< typename SrcType >
60 inline typename std::enable_if< !std::is_enum< SrcType >::value, SrcType >::type
61  Convert::FromString ( const std::string& val, const SrcType& defaultValue )
62 {
63  static_assert( false, "Specialize template" );
64  return SrcType();
65 }
66 
67 
68 //====================================================================================//
69 // Public template specialization for enum types
70 //====================================================================================//
71 
73 template< typename SrcType >
74 inline typename std::enable_if< std::is_enum< SrcType >::value, std::string >::type
75  Convert::ToString ( const SrcType& val )
76 {
77  return EnumToString( val );
78 }
79 
81 template< typename SrcType >
82 inline typename std::enable_if< std::is_enum< SrcType >::value, SrcType >::type
83  Convert::FromString ( const std::string& val, const SrcType& defaultValue )
84 {
85  return StringToEnum( val, defaultValue );
86 }
87 
88 //====================================================================================//
89 // Wstring to string
90 //====================================================================================//
91 
92 template<>
93 inline typename std::enable_if< !std::is_enum< std::wstring >::value, std::string >::type
94  Convert::ToString< std::wstring > ( const std::wstring& value )
95 {
96  typedef std::codecvt_utf8<wchar_t> convert_type;
97  std::wstring_convert<convert_type, wchar_t> converter;
98  return converter.to_bytes( value );
99 }
100 
101 template<>
102 inline typename std::enable_if< !std::is_enum< std::wstring >::value, std::wstring >::type
103  Convert::FromString< std::wstring > ( const std::string& value, const std::wstring& defaultValue )
104 {
105  typedef std::codecvt_utf8<wchar_t> convert_type;
106  std::wstring_convert<convert_type, wchar_t> converter;
107  return converter.from_bytes( value );
108 }
109 
110 //====================================================================================//
111 // Private enum helpers
112 //====================================================================================//
113 
114 
115 template< typename SrcType >
116 std::string Convert::EnumToString ( SrcType value )
117 {
118  static_assert( std::is_enum< SrcType >::value, "Type is not enum" );
119 
120  auto type = rttr::type::get< SrcType >();
121 
122  assert( type.is_valid() );
123  assert( type.is_enumeration() );
124 
125  rttr::enumeration enumVal = type.get_enumeration();
126 
127  return enumVal.value_to_name( value );
128 }
129 
130 
131 template< typename SrcType >
132 SrcType Convert::StringToEnum ( const std::string& value, SrcType defaultValue )
133 {
134  static_assert( std::is_enum< SrcType >::value, "Type is not enum" );
135 
136  auto type = rttr::type::get< SrcType >();
137 
138  assert( type.is_valid() );
139  assert( type.is_enumeration() );
140 
141  rttr::enumeration enumVal = type.get_enumeration();
142 
143  rttr::variant result = enumVal.value_to_name( value );
144  if( !result.is_valid() )
145  return defaultValue;
146 
147  return result.get_value< SrcType >();
148 }
149 
150 
151 
152 
static std::enable_if< std::is_enum< SrcType >::value, std::string >::type ToString(const SrcType &val)
std::enable_if< !std::is_enum< std::wstring >::value, std::wstring >::type Convert::FromString< std::wstring >(const std::string &value, const std::wstring &defaultValue)
Definition: Converters.h:103
std::enable_if< !std::is_enum< std::wstring >::value, std::string >::type Convert::ToString< std::wstring >(const std::wstring &value)
Definition: Converters.h:94
static std::string EnumToString(SrcType defaultVal)
Definition: Converters.h:116
static SrcType StringToEnum(const std::string &value, SrcType defaultValue)
Definition: Converters.h:132
Definition: Converters.h:11
static std::enable_if< std::is_enum< SrcType >::value, SrcType >::type FromString(const std::string &val, const SrcType &defaultValue)