Zserio C++17 runtime library  1.2.0
Built for Zserio 2.18.1
View.h
Go to the documentation of this file.
1 #ifndef ZSERIO_VIEW_H_INC
2 #define ZSERIO_VIEW_H_INC
3 
4 #include <string_view>
5 #include <tuple>
6 
7 #include "zserio/BitSize.h"
10 #include "zserio/ParsingInfo.h"
11 #include "zserio/Traits.h"
12 
13 namespace zserio
14 {
15 
26 template <typename T>
27 class View;
28 
29 // template argument deduction guide for View constructor
30 template <typename T, typename... ARGS>
31 View(T, ARGS&&...) -> View<T>;
32 
33 /*
34  * Gets parsing information from a view
35  *
36  * \param view Zserio View to extract the parsing info from.
37  */
38 template <typename T>
39 const ParsingInfo& parsingInfo(const View<T>& view)
40 {
41  return view.zserioData().m_parsingInfo;
42 }
43 
44 namespace detail
45 {
46 
50 template <typename T>
51 struct ObjectTraits;
52 
53 // TODO[Mi-L@]: Do we need to have U&& here? It should take either simple type, or view (or string_view, etc.).
54 template <size_t I, typename T, typename U>
55 view_type_t<std::tuple_element_t<I, typename ObjectTraits<T>::Parameters>> makeParameter(U arg)
56 {
57  using ParamType = std::tuple_element_t<I, typename ObjectTraits<T>::Parameters>;
58  if constexpr (is_dyn_int_wrapper_v<ParamType>)
59  {
60  return View<ParamType>(ParamType(static_cast<typename ParamType::ValueType>(arg)), 64);
61  }
62  else if constexpr (is_numeric_wrapper_v<ParamType>)
63  {
64  return ParamType(static_cast<typename ParamType::ValueType>(arg));
65  }
66  else
67  {
68  return arg;
69  }
70 }
71 
79 template <typename T>
80 void validate(const View<T>& view, std::string_view fieldName = "")
81 {
82  ObjectTraits<T>::validate(view, fieldName);
83 }
84 
93 template <typename T>
94 BitSize bitSizeOf(const View<T>& view, BitSize bitPosition = 0)
95 {
96  return ObjectTraits<T>::bitSizeOf(view, bitPosition);
97 }
98 
99 template <typename T, typename = void>
100 struct has_initialize_offsets : std::false_type
101 {};
102 
103 template <typename T>
104 struct has_initialize_offsets<T,
105  std::void_t<decltype(ObjectTraits<T>().initializeOffsets(std::declval<const View<T>&>(), 0))>>
106  : std::true_type
107 {};
108 
109 template <typename T, typename V = void>
110 inline constexpr bool has_initialize_offsets_v = has_initialize_offsets<T, V>::value;
111 
112 template <typename T>
113 BitSize initializeOffsets(const View<T>& view, BitSize bitPosition)
114 {
115  if constexpr (has_initialize_offsets_v<T>)
116  {
117  return ObjectTraits<T>::initializeOffsets(view, bitPosition);
118  }
119  else
120  {
121  return ObjectTraits<T>::bitSizeOf(view, bitPosition);
122  }
123 }
124 
133 template <typename T>
134 void write(BitStreamWriter& writer, const View<T>& view)
135 {
136  ObjectTraits<T>::write(writer, view);
137 }
138 
150 template <typename T, typename... ARGS>
151 View<T> read(BitStreamReader& reader, T& data, ARGS&&... args)
152 {
153  return ObjectTraits<T>::read(reader, data, std::forward<ARGS>(args)...);
154 }
155 
156 } // namespace detail
157 
158 template <typename VALUE_TYPE>
159 class View<detail::DynIntWrapper<VALUE_TYPE>>
160 {
161 public:
162  using ValueType = VALUE_TYPE;
163 
164  View(detail::DynIntWrapper<VALUE_TYPE> value, uint8_t numBits) :
165  m_value(value),
166  m_numBits(numBits)
167  {}
168 
169  uint8_t numBits() const
170  {
171  return m_numBits;
172  }
173 
174  detail::DynIntWrapper<VALUE_TYPE> value() const
175  {
176  return m_value;
177  }
178 
179  operator VALUE_TYPE() const
180  {
181  return m_value;
182  }
183 
184 private:
185  detail::DynIntWrapper<VALUE_TYPE> m_value;
186  uint8_t m_numBits;
187 };
188 
189 template <typename VALUE_TYPE>
191  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
192 {
193  return lhs.numBits() == rhs.numBits() && lhs.value() == rhs.value();
194 }
195 
196 template <typename VALUE_TYPE>
198  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
199 {
200  return !(lhs == rhs);
201 }
202 
203 template <typename VALUE_TYPE>
205  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
206 {
207  if (lhs.numBits() != rhs.numBits())
208  {
209  return lhs.numBits() < rhs.numBits();
210  }
211  if (lhs.value() != rhs.value())
212  {
213  return lhs.value() < rhs.value();
214  }
215 
216  return false;
217 }
218 
219 template <typename VALUE_TYPE>
221  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
222 {
223  return rhs < lhs;
224 }
225 
226 template <typename VALUE_TYPE>
228  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
229 {
230  return !(rhs < lhs);
231 }
232 
233 template <typename VALUE_TYPE>
235  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
236 {
237  return !(lhs < rhs);
238 }
239 
240 namespace detail
241 {
242 
243 template <typename VALUE_TYPE>
244 void validate(const View<detail::DynIntWrapper<VALUE_TYPE>>& view, std::string_view fieldName)
245 {
246  return validate(view.value(), view.numBits(), fieldName);
247 }
248 
249 template <typename VALUE_TYPE>
250 BitSize bitSizeOf(const View<detail::DynIntWrapper<VALUE_TYPE>>& view, BitSize = 0)
251 {
252  return view.numBits();
253 }
254 
255 template <typename VALUE_TYPE>
256 void write(BitStreamWriter& writer, const View<detail::DynIntWrapper<VALUE_TYPE>>& view)
257 {
258  write(writer, view.value(), view.numBits());
259 }
260 
261 } // namespace detail
262 
263 } // namespace zserio
264 
265 namespace std
266 {
267 
268 template <typename VALUE_TYPE>
269 struct hash<zserio::View<zserio::detail::DynIntWrapper<VALUE_TYPE>>>
270 {
271  size_t operator()(const zserio::View<zserio::detail::DynIntWrapper<VALUE_TYPE>>& view) const
272  {
273  uint32_t result = zserio::HASH_SEED;
274  result = zserio::calcHashCode(result, view.numBits());
275  result = zserio::calcHashCode(result, view.value());
276  return result;
277  }
278 };
279 
280 } // namespace std
281 
282 #endif // ifndef ZSERIO_VIEW_H_INC
Definition: BitBuffer.h:602
uint8_t numBits(uint64_t numValues)
bool operator>(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition: BitBuffer.h:525
bool operator==(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition: BitBuffer.h:507
unsigned int BitSize
Definition: BitSize.h:8
const ParsingInfo & parsingInfo(const View< T > &view)
Definition: View.h:39
bool operator<(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition: BitBuffer.h:519
uint32_t calcHashCode(uint32_t seedValue, const ArrayView< T, ARRAY_TRAITS > &array)
Definition: ArrayView.h:882
View(T, ARGS &&...) -> View< T >
bool operator<=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition: BitBuffer.h:531
bool operator>=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition: BitBuffer.h:537
bool operator!=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition: BitBuffer.h:513