Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
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
7namespace zserio
8{
9
10// forward declarations
11template <typename, std::size_t>
12class Span;
13
14template <typename T>
15class View;
16
17template <typename T, typename ARRAY_TRAITS>
18class ArrayView;
19
20template <typename ALLOC, typename T>
21class BasicOptional;
22
23template <typename T>
24class Extended;
25
27
28namespace detail
29{
30
31// Useful as a false-substitute in static_assert(false) which is buggy in gcc.
32// At the same time it should print the type name if static_assert is triggered.
33template <typename T>
34struct always_false : std::false_type
35{};
36
37template <typename VALUE_TYPE>
38class NumericTypeWrapper;
39
40template <typename VALUE_TYPE>
41class DynIntWrapper;
42
43template <typename T, typename = void>
44struct is_allocator_impl : std::false_type
45{};
46
47template <typename T>
48struct is_allocator_impl<T,
49 std::void_t<typename T::value_type, decltype(std::declval<T>().allocate(0)),
50 decltype(std::declval<T>().deallocate(std::declval<typename T::value_type*>(), 0))>>
51 : std::true_type
52{};
53
54template <typename T>
55struct is_array_view : std::false_type
56{};
57
58template <typename T, typename ARRAY_TRAITS>
59struct is_array_view<ArrayView<T, ARRAY_TRAITS>> : std::true_type
60{};
61
62template <typename T>
63constexpr bool is_array_view_v = is_array_view<T>::value;
64
65template <typename T>
66struct is_view : std::false_type
67{};
68
69template <typename T>
70struct is_view<View<T>> : std::true_type
71{};
72
73template <typename T>
74constexpr bool is_view_v = is_view<T>::value;
75
76template <typename T>
77struct needs_offset_reference : std::negation<is_view<T>>
78{};
79
80template <typename T>
81struct needs_offset_reference<Extended<T>>
82{
83 static constexpr bool value = needs_offset_reference<T>::value;
84};
85
86template <typename ALLOC, typename T>
87struct needs_offset_reference<BasicOptional<ALLOC, T>>
88{
89 static constexpr bool value = needs_offset_reference<T>::value;
90};
91
92template <typename T>
93constexpr bool needs_offset_reference_v = needs_offset_reference<T>::value;
94
95} // namespace detail
96
101template <typename T>
102struct is_allocator : detail::is_allocator_impl<std::decay_t<T>>
103{};
104
105template <typename T>
113template <typename... ARGS>
114struct is_first_allocator : std::false_type
115{};
116
117template <typename T, typename... ARGS>
119{};
120
121template <typename... ARGS>
122inline constexpr bool is_first_allocator_v = is_first_allocator<ARGS...>::value;
129template <typename T, typename = void>
130struct has_allocator : std::false_type
131{};
132
133template <typename T>
134struct has_allocator<T, std::void_t<typename T::allocator_type>> : std::true_type
135{};
136
137template <typename T, typename V = void>
145template <typename... ARGS>
146struct is_first_array_preallocation : std::false_type
147{};
148
149template <typename T, typename... ARGS>
150struct is_first_array_preallocation<T, ARGS...> : std::is_same<std::decay_t<T>, ArrayPreallocation>
151{};
152
153template <typename... ARGS>
161template <typename T, typename = void>
162struct is_bitmask : std::false_type
163{};
164
165template <typename T>
166struct is_bitmask<T, std::void_t<decltype(std::declval<T>().getValue()), typename T::ZserioType>>
167 : std::true_type
168{};
169
170template <typename T, typename V = void>
171inline constexpr bool is_bitmask_v = is_bitmask<T, V>::value;
180template <typename>
181struct is_span : std::false_type
182{};
183
184template <typename T, size_t Extent>
185struct is_span<Span<T, Extent>> : std::true_type
186{};
187
188template <typename T>
189inline constexpr bool is_span_v = is_span<T>::value;
197template <typename T, typename = void>
198struct is_numeric_wrapper : std::false_type
199{};
200
201template <typename T>
202struct is_numeric_wrapper<T,
203 std::enable_if_t<std::is_base_of_v<detail::NumericTypeWrapper<typename T::ValueType>, T>>>
204 : std::true_type
205{};
206
207template <typename T, typename V = void>
209
213template <typename T, typename = void>
214struct is_dyn_int_wrapper : std::false_type
215{};
216
217template <typename T>
218struct is_dyn_int_wrapper<T,
219 std::enable_if_t<std::is_base_of_v<detail::DynIntWrapper<typename T::ValueType>, T>>> : std::true_type
220{};
221
222template <typename T, typename V = void>
224
229template <typename T, typename = void>
230struct is_complete : std::false_type
231{};
232
233template <typename T>
234struct is_complete<T, std::void_t<decltype(sizeof(T))>> : std::true_type
235{};
236
237template <typename T>
246template <typename T, typename = void>
248{
249 using type = T;
250};
251
252template <typename T>
253struct view_type<T, std::enable_if_t<is_complete_v<View<T>>>>
254{
255 using type = View<T>;
256};
257
258template <typename T, typename V = void>
267template <typename T, typename = void>
269{
270 using type = T;
271};
272
273template <typename T>
274struct offset_field_reference<T, std::enable_if_t<detail::needs_offset_reference_v<T>>>
275{
276 using type = T&;
277};
278
279template <typename T, typename V = void>
290template <typename T, typename ALLOC, typename... ARGS>
292{
293 if constexpr (std::is_constructible_v<T, ARGS..., ALLOC>)
294 {
295 return T(std::forward<ARGS>(args)..., allocator);
296 }
297 else
298 {
299 return T(std::forward<ARGS>(args)...);
300 }
301}
302
309template <typename T, typename ALLOC, typename U, std::enable_if_t<is_numeric_wrapper_v<T>, int> = 0>
310constexpr T constructWithAllocator(const ALLOC&, U value)
311{
312 return T(static_cast<typename T::ValueType>(value));
313}
314
315} // namespace zserio
316
317#endif // ifndef ZSERIO_TRAITS_H_INC
constexpr bool is_complete_v
Definition Traits.h:238
typename offset_field_reference< T, V >::type offset_field_reference_t
Definition Traits.h:280
constexpr bool is_allocator_v
Definition Traits.h:106
constexpr bool is_dyn_int_wrapper_v
Definition Traits.h:223
constexpr bool is_span_v
Definition Traits.h:189
constexpr T constructWithAllocator(const ALLOC &allocator, ARGS &&... args)
Definition Traits.h:291
constexpr bool is_numeric_wrapper_v
Definition Traits.h:208
constexpr bool is_bitmask_v
Definition Traits.h:171
View(T, ARGS &&...) -> View< T >
constexpr bool has_allocator_v
Definition Traits.h:138
typename view_type< T, V >::type view_type_t
Definition Traits.h:259
constexpr bool is_first_allocator_v
Definition Traits.h:122
constexpr bool is_first_array_preallocation_v
Definition Traits.h:154