Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
Traits.h
Go to the documentation of this file.
1 #ifndef ZSERIO_TRAITS_H_INC
2 #define ZSERIO_TRAITS_H_INC
3 
4 #include <cstddef>
5 #include <type_traits>
6 
7 namespace zserio
8 {
9 
10 // forward declarations
11 template <typename, std::size_t>
12 class Span;
13 
14 template <typename T>
15 class View;
16 
17 template <typename T, typename ARRAY_TRAITS>
18 class ArrayView;
19 
20 template <typename ALLOC, typename T>
21 class BasicOptional;
22 
23 template <typename T>
24 class Extended;
25 
26 namespace detail
27 {
28 
29 template <typename VALUE_TYPE>
30 class NumericTypeWrapper;
31 
32 template <typename VALUE_TYPE>
33 class DynIntWrapper;
34 
35 template <typename T, typename = void>
36 struct is_allocator_impl : std::false_type
37 {};
38 
39 template <typename T>
40 struct is_allocator_impl<T,
41  std::void_t<typename T::value_type, decltype(std::declval<T>().allocate(0)),
42  decltype(std::declval<T>().deallocate(std::declval<typename T::value_type*>(), 0))>>
43  : std::true_type
44 {};
45 
46 template <typename T>
47 struct is_array_view : std::false_type
48 {};
49 
50 template <typename T, typename ARRAY_TRAITS>
51 struct is_array_view<ArrayView<T, ARRAY_TRAITS>> : std::true_type
52 {};
53 
54 template <typename T>
55 constexpr bool is_array_view_v = is_array_view<T>::value;
56 
57 template <typename T>
58 struct is_view : std::false_type
59 {};
60 
61 template <typename T>
62 struct is_view<View<T>> : std::true_type
63 {};
64 
65 template <typename T>
66 constexpr bool is_view_v = is_view<T>::value;
67 
68 template <typename T>
69 struct needs_offset_reference : std::negation<is_view<T>>
70 {};
71 
72 template <typename T>
73 struct needs_offset_reference<Extended<T>>
74 {
75  static constexpr bool value = needs_offset_reference<T>::value;
76 };
77 
78 template <typename ALLOC, typename T>
79 struct needs_offset_reference<BasicOptional<ALLOC, T>>
80 {
81  static constexpr bool value = needs_offset_reference<T>::value;
82 };
83 
84 template <typename T>
85 constexpr bool needs_offset_reference_v = needs_offset_reference<T>::value;
86 
87 } // namespace detail
88 
93 template <typename T>
94 struct is_allocator : detail::is_allocator_impl<std::decay_t<T>>
95 {};
96 
97 template <typename T>
98 inline constexpr bool is_allocator_v = is_allocator<T>::value;
105 template <typename... ARGS>
106 struct is_first_allocator : std::false_type
107 {};
108 
109 template <typename T, typename... ARGS>
110 struct is_first_allocator<T, ARGS...> : is_allocator<T>
111 {};
112 
113 template <typename... ARGS>
114 inline constexpr bool is_first_allocator_v = is_first_allocator<ARGS...>::value;
121 template <typename T, typename = void>
122 struct has_allocator : std::false_type
123 {};
124 
125 template <typename T>
126 struct has_allocator<T, std::void_t<typename T::allocator_type>> : std::true_type
127 {};
128 
129 template <typename T, typename V = void>
137 template <typename T, typename = void>
138 struct is_bitmask : std::false_type
139 {};
140 
141 template <typename T>
142 struct is_bitmask<T, std::void_t<decltype(std::declval<T>().getValue()), typename T::ZserioType>>
143  : std::true_type
144 {};
145 
146 template <typename T, typename V = void>
147 inline constexpr bool is_bitmask_v = is_bitmask<T, V>::value;
156 template <typename>
157 struct is_span : std::false_type
158 {};
159 
160 template <typename T, size_t Extent>
161 struct is_span<Span<T, Extent>> : std::true_type
162 {};
163 
164 template <typename T>
165 inline constexpr bool is_span_v = is_span<T>::value;
173 template <typename T, typename = void>
174 struct is_numeric_wrapper : std::false_type
175 {};
176 
177 template <typename T>
178 struct is_numeric_wrapper<T,
179  std::enable_if_t<std::is_base_of_v<detail::NumericTypeWrapper<typename T::ValueType>, T>>>
180  : std::true_type
181 {};
182 
183 template <typename T, typename V = void>
185 
189 template <typename T, typename = void>
190 struct is_dyn_int_wrapper : std::false_type
191 {};
192 
193 template <typename T>
194 struct is_dyn_int_wrapper<T,
195  std::enable_if_t<std::is_base_of_v<detail::DynIntWrapper<typename T::ValueType>, T>>> : std::true_type
196 {};
197 
198 template <typename T, typename V = void>
200 
205 template <typename T, typename = void>
206 struct is_complete : std::false_type
207 {};
208 
209 template <typename T>
210 struct is_complete<T, std::void_t<decltype(sizeof(T))>> : std::true_type
211 {};
212 
213 template <typename T>
222 template <typename T, typename = void>
223 struct view_type
224 {
225  using type = T;
226 };
227 
228 template <typename T>
229 struct view_type<T, std::enable_if_t<is_complete_v<View<T>>>>
230 {
231  using type = View<T>;
232 };
233 
234 template <typename T, typename V = void>
243 template <typename T, typename = void>
245 {
246  using type = T;
247 };
248 
249 template <typename T>
250 struct offset_field_reference<T, std::enable_if_t<detail::needs_offset_reference_v<T>>>
251 {
252  using type = T&;
253 };
254 
255 template <typename T, typename V = void>
266 template <typename T, typename ALLOC, typename... ARGS>
267 constexpr T constructWithAllocator(const ALLOC& allocator, ARGS&&... args)
268 {
269  if constexpr (std::is_constructible_v<T, ARGS..., ALLOC>)
270  {
271  return T(std::forward<ARGS>(args)..., allocator);
272  }
273  else
274  {
275  return T(std::forward<ARGS>(args)...);
276  }
277 }
278 
285 template <typename T, typename ALLOC, typename U, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
286 constexpr T constructWithAllocator(const ALLOC&, U value)
287 {
288  return T(static_cast<typename T::ValueType>(value));
289 }
290 
291 } // namespace zserio
292 
293 #endif // ifndef ZSERIO_TRAITS_H_INC
Definition: BitBuffer.h:602
constexpr bool is_complete_v
Definition: Traits.h:214
typename offset_field_reference< T, V >::type offset_field_reference_t
Definition: Traits.h:256
constexpr bool is_allocator_v
Definition: Traits.h:98
constexpr bool is_dyn_int_wrapper_v
Definition: Traits.h:199
constexpr bool is_span_v
Definition: Traits.h:165
constexpr T constructWithAllocator(const ALLOC &allocator, ARGS &&... args)
Definition: Traits.h:267
constexpr bool is_numeric_wrapper_v
Definition: Traits.h:184
constexpr bool is_bitmask_v
Definition: Traits.h:147
View(T, ARGS &&...) -> View< T >
constexpr bool has_allocator_v
Definition: Traits.h:130
typename view_type< T, V >::type view_type_t
Definition: Traits.h:235
constexpr bool is_first_allocator_v
Definition: Traits.h:114