Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
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/Traits.h"
11 
12 namespace zserio
13 {
14 
25 template <typename T>
26 class View;
27 
28 // template argument deduction guide for View constructor
29 template <typename T, typename... ARGS>
30 View(T, ARGS&&...) -> View<T>;
31 
32 namespace detail
33 {
34 
38 template <typename T>
39 struct ObjectTraits;
40 
41 // TODO[Mi-L@]: Do we need to have U&& here? It should take either simple type, or view (or string_view, etc.).
42 template <size_t I, typename T, typename U>
44 {
45  using ParamType = std::tuple_element_t<I, typename ObjectTraits<T>::Parameters>;
46  if constexpr (is_dyn_int_wrapper_v<ParamType>)
47  {
48  return View<ParamType>(ParamType(static_cast<typename ParamType::ValueType>(arg)), 64);
49  }
50  else if constexpr (is_numeric_wrapper_v<ParamType>)
51  {
52  return ParamType(static_cast<typename ParamType::ValueType>(arg));
53  }
54  else
55  {
56  return arg;
57  }
58 }
59 
67 template <typename T>
68 void validate(const View<T>& view, std::string_view fieldName = "")
69 {
70  ObjectTraits<T>::validate(view, fieldName);
71 }
72 
81 template <typename T>
82 BitSize bitSizeOf(const View<T>& view, BitSize bitPosition = 0)
83 {
84  return ObjectTraits<T>::bitSizeOf(view, bitPosition);
85 }
86 
87 template <typename T, typename = void>
88 struct has_initialize_offsets : std::false_type
89 {};
90 
91 template <typename T>
92 struct has_initialize_offsets<T,
93  std::void_t<decltype(ObjectTraits<T>().initializeOffsets(std::declval<const View<T>&>(), 0))>>
94  : std::true_type
95 {};
96 
97 template <typename T, typename V = void>
98 inline constexpr bool has_initialize_offsets_v = has_initialize_offsets<T, V>::value;
99 
100 template <typename T>
101 BitSize initializeOffsets(const View<T>& view, BitSize bitPosition)
102 {
103  if constexpr (has_initialize_offsets_v<T>)
104  {
105  return ObjectTraits<T>::initializeOffsets(view, bitPosition);
106  }
107  else
108  {
109  return ObjectTraits<T>::bitSizeOf(view, bitPosition);
110  }
111 }
112 
121 template <typename T>
122 void write(BitStreamWriter& writer, const View<T>& view)
123 {
124  ObjectTraits<T>::write(writer, view);
125 }
126 
138 template <typename T, typename... ARGS>
139 View<T> read(BitStreamReader& reader, T& data, ARGS&&... args)
140 {
141  return ObjectTraits<T>::read(reader, data, std::forward<ARGS>(args)...);
142 }
143 
144 } // namespace detail
145 
146 template <typename VALUE_TYPE>
147 class View<detail::DynIntWrapper<VALUE_TYPE>>
148 {
149 public:
150  using ValueType = VALUE_TYPE;
151 
152  View(detail::DynIntWrapper<VALUE_TYPE> value, uint8_t numBits) :
153  m_value(value),
154  m_numBits(numBits)
155  {}
156 
157  uint8_t numBits() const
158  {
159  return m_numBits;
160  }
161 
162  detail::DynIntWrapper<VALUE_TYPE> value() const
163  {
164  return m_value;
165  }
166 
167  operator VALUE_TYPE() const
168  {
169  return m_value;
170  }
171 
172 private:
173  detail::DynIntWrapper<VALUE_TYPE> m_value;
174  uint8_t m_numBits;
175 };
176 
177 template <typename VALUE_TYPE>
179  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
180 {
181  return lhs.numBits() == rhs.numBits() && lhs.value() == rhs.value();
182 }
183 
184 template <typename VALUE_TYPE>
186  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
187 {
188  return !(lhs == rhs);
189 }
190 
191 template <typename VALUE_TYPE>
193  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
194 {
195  if (lhs.numBits() != rhs.numBits())
196  {
197  return lhs.numBits() < rhs.numBits();
198  }
199  if (lhs.value() != rhs.value())
200  {
201  return lhs.value() < rhs.value();
202  }
203 
204  return false;
205 }
206 
207 template <typename VALUE_TYPE>
209  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
210 {
211  return rhs < lhs;
212 }
213 
214 template <typename VALUE_TYPE>
216  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
217 {
218  return !(rhs < lhs);
219 }
220 
221 template <typename VALUE_TYPE>
223  const View<detail::DynIntWrapper<VALUE_TYPE>>& lhs, const View<detail::DynIntWrapper<VALUE_TYPE>>& rhs)
224 {
225  return !(lhs < rhs);
226 }
227 
228 namespace detail
229 {
230 
231 template <typename VALUE_TYPE>
232 void validate(const View<detail::DynIntWrapper<VALUE_TYPE>>& view, std::string_view fieldName)
233 {
234  return validate(view.value(), view.numBits(), fieldName);
235 }
236 
237 template <typename VALUE_TYPE>
238 BitSize bitSizeOf(const View<detail::DynIntWrapper<VALUE_TYPE>>& view, BitSize = 0)
239 {
240  return view.numBits();
241 }
242 
243 template <typename VALUE_TYPE>
244 void write(BitStreamWriter& writer, const View<detail::DynIntWrapper<VALUE_TYPE>>& view)
245 {
246  write(writer, view.value(), view.numBits());
247 }
248 
249 } // namespace detail
250 
251 } // namespace zserio
252 
253 namespace std
254 {
255 
256 template <typename VALUE_TYPE>
257 struct hash<zserio::View<zserio::detail::DynIntWrapper<VALUE_TYPE>>>
258 {
259  size_t operator()(const zserio::View<zserio::detail::DynIntWrapper<VALUE_TYPE>>& view) const
260  {
261  uint32_t result = zserio::HASH_SEED;
262  result = zserio::calcHashCode(result, view.numBits());
263  result = zserio::calcHashCode(result, view.value());
264  return result;
265  }
266 };
267 
268 } // namespace std
269 
270 #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
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:860
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
typename view_type< T, V >::type view_type_t
Definition: Traits.h:235