Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
ArrayTraits.h
Go to the documentation of this file.
1 #ifndef ZSERIO_ARRAY_TRAIT_H_INC
2 #define ZSERIO_ARRAY_TRAIT_H_INC
3 
4 #include <cstddef>
5 #include <string_view>
6 #include <type_traits>
7 
10 #include "zserio/Bitmasks.h"
11 #include "zserio/DeltaContext.h"
12 #include "zserio/Enums.h"
13 #include "zserio/Traits.h"
14 #include "zserio/Types.h"
15 #include "zserio/View.h"
16 
17 namespace zserio
18 {
19 
25 template <typename T, typename = void>
26 struct ArrayTraits;
27 
28 namespace detail
29 {
30 
31 struct DummyArrayOwner
32 {};
33 
34 template <typename ARRAY_TRAITS, typename = void>
35 struct array_owner_type
36 {
37  using type = DummyArrayOwner;
38 };
39 
40 template <typename ARRAY_TRAITS>
41 struct array_owner_type<ARRAY_TRAITS, std::void_t<typename ARRAY_TRAITS::OwnerType>>
42 {
43  using type = typename ARRAY_TRAITS::OwnerType;
44 };
45 
46 template <typename T, typename V = void>
47 using array_owner_type_t = typename array_owner_type<T, V>::type;
48 
49 template <typename T>
50 struct is_dummy_array_owner : std::is_same<DummyArrayOwner, T>
51 {};
52 
53 template <typename T>
54 inline constexpr bool is_dummy_array_owner_v = is_dummy_array_owner<T>::value;
55 
56 template <typename T, typename = void>
57 struct is_packable : std::false_type
58 {};
59 
60 template <typename T>
61 struct is_packable<T, std::enable_if_t<is_numeric_wrapper_v<T> || is_bitmask_v<T> || std::is_enum_v<T>>>
62  : std::true_type
63 {};
64 
65 template <typename T>
66 struct is_packable<T, std::enable_if_t<is_complete_v<typename ObjectTraits<std::decay_t<T>>::PackingContext>>>
67  : std::true_type
68 {};
69 
70 template <typename T, typename V = void>
71 inline constexpr bool is_packable_v = is_packable<T, V>::value;
72 
73 template <typename T>
74 struct is_delta_context : std::is_same<DeltaContext, T>
75 {};
76 
77 template <typename T>
78 inline constexpr bool is_delta_context_v = is_delta_context<T>::value;
79 
80 template <typename T>
81 struct NumericArrayTraits
82 {
83  static const T& at(const detail::DummyArrayOwner&, const T& element, size_t)
84  {
85  return element;
86  }
87 
88  static T& at(const detail::DummyArrayOwner&, T& element, size_t)
89  {
90  return element;
91  }
92 
93  static void read(BitStreamReader& reader, const detail::DummyArrayOwner&, T& element, size_t)
94  {
95  detail::read(reader, element);
96  }
97 
98  static BitSize bitSizeOf()
99  {
100  return detail::bitSizeOf(T{});
101  }
102 };
103 
104 template <typename T>
105 struct IntegralArrayTraits : NumericArrayTraits<T>
106 {
107  using NumericArrayTraits<T>::read;
108 
109  static void read(
110  DeltaContext& context, BitStreamReader& reader, const detail::DummyArrayOwner&, T& element, size_t)
111  {
112  detail::read(context, reader, element);
113  }
114 };
115 
116 } // namespace detail
117 
118 // for all generated objects which don't need an owner
119 template <typename OBJECT, typename>
121 {
122  template <typename OBJECT_ = OBJECT,
123  std::enable_if_t<std::is_constructible_v<View<OBJECT_>, const OBJECT&>, int> = 0>
124  static View<OBJECT> at(const detail::DummyArrayOwner&, const OBJECT& element, size_t)
125  {
126  return View<OBJECT>(element);
127  }
128 
129  template <typename OBJECT_ = OBJECT,
130  std::enable_if_t<std::is_constructible_v<View<OBJECT_>, OBJECT&>, int> = 0>
131  static View<OBJECT> at(const detail::DummyArrayOwner&, OBJECT& element, size_t)
132  {
133  return View<OBJECT>(element);
134  }
135 
136  static void read(BitStreamReader& reader, const detail::DummyArrayOwner&, OBJECT& element, size_t)
137  {
138  (void)detail::read(reader, element);
139  }
140 
141  template <typename OBJECT_ = OBJECT>
142  static std::enable_if_t<!detail::is_delta_context_v<detail::packing_context_type_t<OBJECT_>>> read(
143  typename detail::ObjectTraits<OBJECT_>::PackingContext& packingContext, BitStreamReader& reader,
144  const detail::DummyArrayOwner&, OBJECT& element, size_t)
145  {
146  detail::read(packingContext, reader, element);
147  }
148 };
149 
150 template <>
151 struct ArrayTraits<detail::BoolWrapper> : detail::IntegralArrayTraits<detail::BoolWrapper>
152 {};
153 
154 template <BitSize BIT_SIZE, bool IS_SIGNED>
155 struct ArrayTraits<detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>>
156  : detail::IntegralArrayTraits<detail::FixedIntWrapper<BIT_SIZE, IS_SIGNED>>
157 {};
158 
159 template <typename VALUE_TYPE, typename detail::VarIntType VAR_TYPE>
160 struct ArrayTraits<detail::VarIntWrapper<VALUE_TYPE, VAR_TYPE>>
161  : detail::IntegralArrayTraits<detail::VarIntWrapper<VALUE_TYPE, VAR_TYPE>>
162 {};
163 
164 template <typename VALUE_TYPE, detail::FloatType FLOAT_TYPE>
165 struct ArrayTraits<detail::FloatWrapper<VALUE_TYPE, FLOAT_TYPE>>
166  : detail::NumericArrayTraits<detail::FloatWrapper<VALUE_TYPE, FLOAT_TYPE>>
167 {};
168 
169 template <typename ALLOC>
170 struct ArrayTraits<BasicBytes<ALLOC>>
171 {
172  static constexpr BytesView at(const detail::DummyArrayOwner&, const BasicBytes<ALLOC>& element, size_t)
173  {
174  return element;
175  }
176 
177  static void read(
178  BitStreamReader& reader, const detail::DummyArrayOwner&, BasicBytes<ALLOC>& element, size_t)
179  {
180  detail::read(reader, element);
181  }
182 };
183 
184 template <typename ALLOC>
186 {
187  static constexpr BasicBitBufferView<ALLOC> at(
188  const detail::DummyArrayOwner&, const BasicBitBuffer<ALLOC>& element, size_t)
189  {
190  return element;
191  }
192 
193  static void read(
194  BitStreamReader& reader, const detail::DummyArrayOwner&, BasicBitBuffer<ALLOC>& element, size_t)
195  {
196  detail::read(reader, element);
197  }
198 };
199 
200 template <typename ALLOC>
201 struct ArrayTraits<BasicString<ALLOC>>
202 {
203  static constexpr std::string_view at(const detail::DummyArrayOwner&, std::string_view element, size_t)
204  {
205  return element;
206  }
207 
208  static void read(
209  BitStreamReader& reader, const detail::DummyArrayOwner&, BasicString<ALLOC>& element, size_t)
210  {
211  detail::read(reader, element);
212  }
213 };
214 
215 template <typename T>
216 struct ArrayTraits<T, std::enable_if_t<std::is_enum_v<T>>>
217 {
218  static T at(const detail::DummyArrayOwner&, T element, size_t)
219  {
220  return element;
221  }
222 
223  static void read(BitStreamReader& reader, const detail::DummyArrayOwner&, T& element, size_t)
224  {
225  detail::read(reader, element);
226  }
227 
228  static void read(detail::DeltaContext& context, BitStreamReader& reader, const detail::DummyArrayOwner&,
229  T& element, size_t)
230  {
231  detail::read(context, reader, element);
232  }
233 };
234 
235 template <typename T>
236 struct ArrayTraits<T, std::enable_if_t<zserio::is_bitmask_v<T>>>
237 {
238  static T at(const detail::DummyArrayOwner&, T element, size_t)
239  {
240  return element;
241  }
242 
243  static void read(BitStreamReader& reader, const detail::DummyArrayOwner&, T& element, size_t)
244  {
245  detail::read(reader, element);
246  }
247 
248  static void read(detail::DeltaContext& context, BitStreamReader& reader, const detail::DummyArrayOwner&,
249  T& element, size_t)
250  {
251  detail::read(context, reader, element);
252  }
253 };
254 
255 } // namespace zserio
256 
257 #endif // ZSERIO_ARRAY_TRAIT_H_INC
Definition: BitBuffer.h:602
unsigned int BitSize
Definition: BitSize.h:8
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
Definition: String.h:17
std::vector< uint8_t, ALLOC > BasicBytes
Definition: Bytes.h:20
std::reference_wrapper< const BasicBitBuffer< ALLOC > > BasicBitBufferView
Definition: BitBuffer.h:492
static constexpr BasicBitBufferView< ALLOC > at(const detail::DummyArrayOwner &, const BasicBitBuffer< ALLOC > &element, size_t)
Definition: ArrayTraits.h:187
static void read(BitStreamReader &reader, const detail::DummyArrayOwner &, BasicBitBuffer< ALLOC > &element, size_t)
Definition: ArrayTraits.h:193
static constexpr BytesView at(const detail::DummyArrayOwner &, const BasicBytes< ALLOC > &element, size_t)
Definition: ArrayTraits.h:172
static void read(BitStreamReader &reader, const detail::DummyArrayOwner &, BasicBytes< ALLOC > &element, size_t)
Definition: ArrayTraits.h:177
static constexpr std::string_view at(const detail::DummyArrayOwner &, std::string_view element, size_t)
Definition: ArrayTraits.h:203
static void read(BitStreamReader &reader, const detail::DummyArrayOwner &, BasicString< ALLOC > &element, size_t)
Definition: ArrayTraits.h:208
static void read(BitStreamReader &reader, const detail::DummyArrayOwner &, T &element, size_t)
Definition: ArrayTraits.h:223
static void read(detail::DeltaContext &context, BitStreamReader &reader, const detail::DummyArrayOwner &, T &element, size_t)
Definition: ArrayTraits.h:228
static T at(const detail::DummyArrayOwner &, T element, size_t)
Definition: ArrayTraits.h:218
static void read(BitStreamReader &reader, const detail::DummyArrayOwner &, T &element, size_t)
Definition: ArrayTraits.h:243
static void read(detail::DeltaContext &context, BitStreamReader &reader, const detail::DummyArrayOwner &, T &element, size_t)
Definition: ArrayTraits.h:248
static T at(const detail::DummyArrayOwner &, T element, size_t)
Definition: ArrayTraits.h:238
static View< OBJECT > at(const detail::DummyArrayOwner &, const OBJECT &element, size_t)
Definition: ArrayTraits.h:124
static std::enable_if_t<!detail::is_delta_context_v< detail::packing_context_type_t< OBJECT_ > > > read(typename detail::ObjectTraits< OBJECT_ >::PackingContext &packingContext, BitStreamReader &reader, const detail::DummyArrayOwner &, OBJECT &element, size_t)
Definition: ArrayTraits.h:142
static void read(BitStreamReader &reader, const detail::DummyArrayOwner &, OBJECT &element, size_t)
Definition: ArrayTraits.h:136
static View< OBJECT > at(const detail::DummyArrayOwner &, OBJECT &element, size_t)
Definition: ArrayTraits.h:131