emlabcpp
modern opinionated embedded C++ library
types.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "./concepts.h"
27 #include "./types/base.h"
28 
29 #include <string>
30 
31 #ifdef EMLABCPP_USE_DEMANGLING
32 #include <cxxabi.h>
33 #endif
34 
35 namespace emlabcpp
36 {
37 
40 
41 template < typename T >
42 constexpr bool has_static_size_v = static_sized< T >;
43 
47 
48 template < typename Container, typename UnaryCallable >
49 struct mapped;
50 
51 template < gettable_container Container, typename UnaryCallable >
52 requires( !range_container< Container > )
53 struct mapped< Container, UnaryCallable >
54 {
55  using type = decltype( std::declval< UnaryCallable >()(
56  std::get< 0 >( std::declval< Container >() ) ) );
57 };
58 
59 template < range_container Container, typename UnaryCallable >
60 struct mapped< Container, UnaryCallable >
61 {
62  using type = decltype( std::declval< UnaryCallable >()(
63  *std::begin( std::declval< Container >() ) ) );
64 };
65 
66 template < typename Container, typename UnaryCallable >
68 
71 template < auto V >
72 struct tag
73 {
74  using value_type = decltype( V );
75 
76  static constexpr value_type value = V;
77 
78  friend constexpr auto operator<=>( tag const&, tag const& ) = default;
79 };
80 
81 #ifdef EMLABCPP_USE_OSTREAM
82 template < auto ID >
83 std::ostream& operator<<( std::ostream& os, tag< ID > )
84 {
85  return os << ID;
86 }
87 #endif
88 
91 
92 template < typename T >
94 {
95 #ifdef EMLABCPP_USE_DEMANGLING
96  std::string res;
97  int tmp = 0;
98  char* const dname = abi::__cxa_demangle( typeid( T ).name(), nullptr, nullptr, &tmp );
99  res = dname;
100  // NOLINTNEXTLINE
101  free( dname );
102 #elif defined EMLABCPP_USE_TYPEID
103  std::string_view const res = typeid( T ).name();
104 #else
105  std::string_view const res = "type names not supported";
106 #endif
107  return res;
108 }
109 
111 
112 template < std::size_t >
114 
115 template < std::size_t N >
116 requires( sizeof( uint8_t ) == N )
117 struct select_utype< N >
118 {
119  using type = uint8_t;
120 };
121 
122 template < std::size_t N >
123 requires( sizeof( uint16_t ) == N )
124 struct select_utype< N >
125 {
126  using type = uint16_t;
127 };
128 
129 template < std::size_t N >
130 requires( sizeof( uint32_t ) == N )
131 struct select_utype< N >
132 {
133  using type = uint32_t;
134 };
135 
136 template < std::size_t N >
137 requires( sizeof( uint64_t ) == N )
138 struct select_utype< N >
139 {
140  using type = uint64_t;
141 };
142 
143 template < std::size_t N >
145 
147 
148 template < typename, template < typename > class >
149 struct type_map;
150 
151 template < typename... Ts, template < typename > class Fun >
152 struct type_map< std::tuple< Ts... >, Fun >
153 {
154  using type = std::tuple< Fun< Ts >... >;
155 };
156 
157 template < typename T, template < typename > class Fun >
159 
161 
162 template < typename T >
163 struct type_tag
164 {
165  using type = T;
166 };
167 
169 
170 template < typename T, typename Variant >
171 struct index_of;
172 
173 template < typename T, typename... Ts >
174 struct index_of< T, std::variant< Ts... > >
175 {
176  static constexpr std::size_t value =
177  std::variant< type_tag< Ts >... >( type_tag< T >() ).index();
178  // got the tip for this from:
179  // https://stackoverflow.com/questions/52303316/get-index-by-type-in-stdvariant
180 };
181 
182 template < typename T, typename Variant >
184 
185 } // namespace emlabcpp
MIT License.
Definition: impl.h:31
auto pretty_type_name()
Definition: types.h:93
std::tuple< Fun< Ts >... > type
Definition: types.h:154
T res
Definition: algorithm.h:505
T type
Definition: types.h:165
requires(!range_container< Container >) const expr std
Returns index of an element in tuple 't', for which call to predicate f(x) holds true,...
Definition: algorithm.h:127
typename mapped< Container, UnaryCallable >::type mapped_t
Definition: types.h:67
UnaryCallable
Definition: types.h:54
typename select_utype< N >::type select_utype_t
Definition: types.h:144
constexpr bool has_static_size_v
Definition: types.h:42
index_of< T, Variant >::value index_of_v
Definition: types.h:183
std::ostream & operator<<(std::ostream &os, string_buffer< N > const &sb)
Definition: string_buffer.h:112
N
Definition: static_storage.h:97
typename type_map< T, Fun >::type type_map_t
Definition: types.h:158
Definition: types.h:171
Definition: types.h:49
Definition: types.h:113
Definition: types.h:149
Definition: types.h:164
decltype(std::declval< UnaryCallable >()(*std::begin(std::declval< Container >()))) type
Definition: types.h:63
Definition: types.h:73
decltype(V) value_type
Definition: types.h:74
constexpr friend auto operator<=>(tag const &, tag const &)=default
static constexpr value_type value
Definition: types.h:76