Zserio C++ runtime library  1.1.0
Built for Zserio 2.15.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 <type_traits>
5 
6 #include "zserio/NoInit.h"
7 #include "zserio/RebindAlloc.h"
8 
9 namespace zserio
10 {
11 
12 // forward declaration
13 class BitStreamReader;
14 struct PropagateAllocatorT;
15 template <typename>
17 template <typename, std::size_t>
18 class Span;
19 
20 namespace detail
21 {
22 
23 template <typename>
24 class inplace_optional_holder;
25 
26 template <typename, typename>
27 class heap_optional_holder;
28 
29 // These decltype's wrappers are needed because of old MSVC compiler 2015.
30 template <typename T, typename U = decltype(&T::initialize)>
31 struct decltype_initialize
32 {
33  using type = U;
34 };
35 
36 template <typename T, typename U = decltype(&T::initializeChildren)>
37 struct decltype_initialize_children
38 {
39  using type = U;
40 };
41 
42 template <typename T, typename U = decltype(std::declval<T>().reflectable())>
43 struct decltype_reflectable
44 {
45  using type = U;
46 };
47 
48 // declval is needed because otherwise MSVC 2015 states that std::allocator<int> does NOT have allocate method!
49 template <typename T, typename U = decltype(std::declval<T>().allocate(0))>
50 struct decltype_allocate
51 {
52  using type = U;
53 };
54 
55 template <typename T, typename U = decltype(std::declval<T>().deallocate(nullptr, 0))>
56 struct decltype_deallocate
57 {
58  using type = U;
59 };
60 
61 template <typename T, typename U = decltype(std::declval<T>().getValue())>
62 struct decltype_get_value
63 {
64  using type = U;
65 };
66 
67 template <typename T, typename U = decltype(&T::initializeOffset)>
68 struct decltype_initialize_offset
69 {
70  using type = U;
71 };
72 
73 template <typename T, typename U = decltype(&T::checkOffset)>
74 struct decltype_check_offset
75 {
76  using type = U;
77 };
78 
79 template <typename T, typename U = decltype(&T::initializeElement)>
80 struct decltype_initialize_element
81 {
82  using type = U;
83 };
84 
85 template <typename... T>
86 struct make_void
87 {
88  using type = void;
89 };
90 
91 template <typename... T>
92 using void_t = typename make_void<T...>::type;
93 
94 } // namespace detail
95 
100 template <typename T, typename = void>
101 struct is_allocator : std::false_type
102 {};
103 
104 template <typename T>
105 struct is_allocator<T,
106  detail::void_t<typename detail::decltype_allocate<T>::type,
107  typename detail::decltype_deallocate<T>::type>> : std::true_type
108 {};
115 template <typename... ARGS>
116 struct is_first_allocator : std::false_type
117 {};
118 
119 template <typename T, typename... ARGS>
120 struct is_first_allocator<T, ARGS...> : is_allocator<T>
121 {};
128 template <typename T, typename = void>
129 struct has_owner_type : std::false_type
130 {};
131 
132 template <typename T>
133 struct has_owner_type<T, detail::void_t<typename T::OwnerType>> : std::true_type
134 {};
141 template <typename T, typename = void>
142 struct has_zserio_packing_context : std::false_type
143 {};
144 
145 template <typename T>
146 struct has_zserio_packing_context<T, detail::void_t<typename T::ZserioPackingContext>> : std::true_type
147 {};
154 template <typename T, typename = void>
155 struct has_allocator : std::false_type
156 {};
157 
158 template <typename T>
159 struct has_allocator<T, detail::void_t<typename T::allocator_type>> : std::true_type
160 {};
167 template <typename T, typename = void>
168 struct has_initialize : std::false_type
169 {};
170 
171 template <typename T>
172 struct has_initialize<T, detail::void_t<typename detail::decltype_initialize<T>::type>> : std::true_type
173 {};
180 template <typename T, typename = void>
181 struct has_initialize_children : std::false_type
182 {};
183 
184 template <typename T>
185 struct has_initialize_children<T, detail::void_t<typename detail::decltype_initialize_children<T>::type>>
186  : std::true_type
187 {};
194 template <typename T, typename = void>
195 struct has_reflectable : std::false_type
196 {};
197 
198 template <typename T>
199 struct has_reflectable<T, detail::void_t<typename detail::decltype_reflectable<T>::type>> : std::true_type
200 {};
207 template <typename T, typename = void>
208 struct has_initialize_offset : std::false_type
209 {};
210 
211 template <typename T>
212 struct has_initialize_offset<T, detail::void_t<typename detail::decltype_initialize_offset<T>::type>>
213  : std::true_type
214 {};
223 template <typename T, typename = void>
224 struct has_check_offset : std::false_type
225 {};
226 
227 template <typename T>
228 struct has_check_offset<T, detail::void_t<typename detail::decltype_check_offset<T>::type>> : std::true_type
229 {};
238 template <typename T, typename = void>
239 struct has_initialize_element : std::false_type
240 {};
241 
242 template <typename T>
243 struct has_initialize_element<T, detail::void_t<typename detail::decltype_initialize_element<T>::type>>
244  : std::true_type
245 {};
254 template <typename T, typename = void>
255 struct is_bitmask : std::false_type
256 {};
257 
258 template <typename T>
259 struct is_bitmask<T, detail::void_t<typename detail::decltype_get_value<T>::type, typename T::underlying_type>>
260  : std::true_type
261 {};
270 template <typename>
271 struct is_span : std::false_type
272 {};
273 
274 template <typename T, size_t Extent>
275 struct is_span<Span<T, Extent>> : std::true_type
276 {};
285 template <typename FIELD_TYPE, typename COMPOUND_TYPE, typename ALLOCATOR_TYPE, typename = void>
287  : std::enable_if<!std::is_same<typename std::decay<FIELD_TYPE>::type, ALLOCATOR_TYPE>::value &&
288  !std::is_same<typename std::decay<FIELD_TYPE>::type, BitStreamReader>::value &&
289  !std::is_same<typename std::decay<FIELD_TYPE>::type, COMPOUND_TYPE>::value &&
290  !std::is_same<typename std::decay<FIELD_TYPE>::type, PropagateAllocatorT>::value &&
291  !std::is_same<typename std::decay<FIELD_TYPE>::type, NoInitT>::value,
292  int>
293 {};
294 
295 template <typename FIELD_TYPE, typename COMPOUND_TYPE, typename ALLOCATOR_TYPE>
296 struct is_field_constructor_enabled<FIELD_TYPE, COMPOUND_TYPE, ALLOCATOR_TYPE,
297  detail::void_t<typename COMPOUND_TYPE::ZserioPackingContext>>
298  : std::enable_if<!std::is_same<typename std::decay<FIELD_TYPE>::type, ALLOCATOR_TYPE>::value &&
299  !std::is_same<typename std::decay<FIELD_TYPE>::type, BitStreamReader>::value &&
300  !std::is_same<typename std::decay<FIELD_TYPE>::type, COMPOUND_TYPE>::value &&
301  !std::is_same<typename std::decay<FIELD_TYPE>::type, PropagateAllocatorT>::value &&
302  !std::is_same<typename std::decay<FIELD_TYPE>::type, NoInitT>::value &&
303  !std::is_same<typename std::decay<FIELD_TYPE>::type,
304  typename COMPOUND_TYPE::ZserioPackingContext>::value,
305  int>
306 {};
312 template <typename FIELD_TYPE, typename COMPOUND_TYPE, typename ALLOCATOR_TYPE>
315 
316 } // namespace zserio
317 
318 #endif // ifndef ZSERIO_TRAITS_H_INC
typename is_field_constructor_enabled< FIELD_TYPE, COMPOUND_TYPE, ALLOCATOR_TYPE >::type is_field_constructor_enabled_t
Definition: Traits.h:314