emlabcpp
modern opinionated embedded C++ library
convert.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../string_buffer.h"
27 #include "./base.h"
28 
29 #include <bitset>
30 
31 namespace emlabcpp::testing
32 {
33 
34 template < typename T >
36 
37 template < typename T >
39 {
40  static std::optional< T > from_value( value_type const& var )
41  {
42  T const* val_ptr = std::get_if< T >( &var );
43  if ( val_ptr )
44  return *val_ptr;
45  return std::nullopt;
46  }
47 
48  static value_type to_value( T const& item )
49  {
50  return { item };
51  }
52 };
53 
54 template < typename T >
56 {
57  static_assert( std::is_enum_v< T > );
58 
59  static value_type to_value( T m )
60  {
61  return static_cast< int64_t >( m );
62  }
63 
64  static std::optional< T > from_value( value_type const& var )
65  {
66  auto* val = std::get_if< int64_t >( &var );
67  if ( val == nullptr )
68  return std::nullopt;
69 
70  return static_cast< T >( *val );
71  }
72 };
73 
74 template <>
76 {
77 };
78 
79 template <>
80 struct value_type_converter< int64_t > : value_type_converter_getter< int64_t >
81 {
82 };
83 
84 template < std::size_t N >
87 {
88  static std::optional< emlabcpp::string_buffer< N > > from_value( value_type const& var )
89  {
90  auto const* val_ptr = std::get_if< string_buffer >( &var );
91  if ( val_ptr == nullptr )
92  return std::nullopt;
93 
94  if constexpr ( N == string_buffer::capacity )
95  return *val_ptr;
96  else
97  return emlabcpp::string_buffer< N >{ std::string_view{ *val_ptr } };
98  }
99 
100  static value_type to_value( emlabcpp::string_buffer< N > const& item )
101  {
102  return { string_buffer{ item } };
103  }
104 };
105 
106 template < typename T >
107 requires( alternative_of< T, value_type > )
108 struct value_type_converter< T > : value_type_converter_getter< T >
109 {
110 };
111 
112 template < typename T >
113 requires( !std::same_as< T, int64_t > && std::is_integral_v< T > && !std::same_as< T, bool > )
114 struct value_type_converter< T >
115 {
116  static std::optional< T > from_value( value_type const& var )
117  {
118  if ( std::holds_alternative< float >( var ) ) {
119  auto const v = *std::get_if< float >( &var );
120  if ( v == 0.f )
121  return T{ 0 };
122  }
123  std::optional< int64_t > opt_val =
125  if ( !opt_val )
126  return std::nullopt;
127  return static_cast< T >( *opt_val );
128  }
129 
130  static value_type to_value( T const& item )
131  {
132  return { static_cast< int64_t >( item ) };
133  }
134 };
135 
136 template < std::size_t N >
137 struct value_type_converter< std::bitset< N > >
138 {
139  static_assert( N < 64 );
140 
141  static std::optional< std::bitset< N > > from_value( value_type const& var )
142  {
143  if ( auto* val = std::get_if< int64_t >( &var ) )
144  return { *val };
145  return std::nullopt;
146  }
147 
148  static value_type to_value( std::bitset< N > const& item )
149  {
150  return static_cast< int64_t >( item.to_ulong() );
151  }
152 };
153 
154 template <>
155 struct value_type_converter< std::string_view >
156 {
157  static value_type to_value( std::string_view const& item )
158  {
159  return string_buffer( item );
160  }
161 };
162 
163 template <>
164 struct value_type_converter< char const* >
165 {
166  static value_type to_value( char const* item )
167  {
168  return string_buffer( item );
169  }
170 };
171 
172 template <>
174 {
175  static value_type to_value( std::byte const item )
176  {
177  return int64_t( item );
178  }
179 };
180 
181 template < typename Rep, typename Ratio >
182 struct value_type_converter< std::chrono::duration< Rep, Ratio > >
183 {
184  static value_type to_value( std::chrono::duration< Rep, Ratio > const& val )
185  {
186  return value_type_converter< Rep >::to_value( val.count() );
187  }
188 
189  static std::optional< std::chrono::duration< Rep, Ratio > >
190  from_value( value_type const& var )
191  {
192  std::optional< Rep > opt_raw = value_type_converter< Rep >::from_value( var );
193  if ( !opt_raw.has_value() )
194  return std::nullopt;
195  return std::chrono::duration< Rep, Ratio >{ *opt_raw };
196  }
197 };
198 
199 template < typename T >
200 concept value_type_convertible = requires( T const& item, value_type const& val ) {
201  { value_type_converter< T >::to_value( item ) } -> std::convertible_to< value_type >;
202  {
204  } -> std::convertible_to< std::optional< T > >;
205 };
206 
207 } // namespace emlabcpp::testing
MIT License.
Definition: base.h:37
string_buffer< 128 > string_buffer
Definition: base.h:46
requires(N<=string_buffer::capacity) struct value_type_converter< emlabcpp requires(alternative_of< T, value_type >) struct value_type_converter< T > requires(!std::same_as< T, int64_t > &&std::is_integral_v< T > &&!std::same_as< T, bool >) struct value_type_converter< T >
Definition: convert.h:113
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
concept value_type_convertible
Definition: convert.h:200
MIT License.
Definition: impl.h:31
N
Definition: static_storage.h:97
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118
physical_quantity represents all physical units defined using the International System of Units and m...
Definition: physical_quantity.h:56
Definition: string_buffer.h:38
static constexpr std::size_t capacity
Definition: string_buffer.h:39
static value_type to_value(char const *item)
Definition: convert.h:166
static value_type to_value(std::bitset< N > const &item)
Definition: convert.h:148
static std::optional< std::bitset< N > > from_value(value_type const &var)
Definition: convert.h:141
static value_type to_value(std::byte const item)
Definition: convert.h:175
static value_type to_value(std::chrono::duration< Rep, Ratio > const &val)
Definition: convert.h:184
static std::optional< std::chrono::duration< Rep, Ratio > > from_value(value_type const &var)
Definition: convert.h:190
static value_type to_value(std::string_view const &item)
Definition: convert.h:157
static value_type to_value(T m)
Definition: convert.h:59
static std::optional< T > from_value(value_type const &var)
Definition: convert.h:64
static std::optional< T > from_value(value_type const &var)
Definition: convert.h:40
static value_type to_value(T const &item)
Definition: convert.h:48