Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
Extended.h
Go to the documentation of this file.
1 #ifndef ZSERIO_EXTENDED_H_INC
2 #define ZSERIO_EXTENDED_H_INC
3 
4 #include "HashCodeUtil.h"
5 
6 namespace zserio
7 {
8 
9 namespace detail
10 {
11 
12 template <typename T, typename... ARGS>
13 struct is_single_type : std::false_type
14 {};
15 
16 template <typename T, typename A1>
17 struct is_single_type<T, A1> : std::is_same<T, std::decay_t<A1>>
18 {};
19 
20 template <typename T, typename... ARGS>
21 inline constexpr bool is_single_type_v = is_single_type<T, ARGS...>::value;
22 
23 } // namespace detail
24 
31 template <typename T>
32 class Extended
33 {
34 public:
35  Extended(Extended&& other) = default;
36 
37  template <typename ALLOC>
38  Extended(Extended&& other, const ALLOC& allocator) :
39  m_isPresent(other.m_isPresent),
40  m_value(std::move(other.m_value), allocator)
41  {}
42 
43  Extended(const Extended& other) = default;
44 
45  template <typename ALLOC>
46  Extended(const Extended& other, const ALLOC& allocator) :
47  m_isPresent(other.m_isPresent),
48  m_value(other.m_value, allocator)
49  {}
50 
54  template <typename... ARGS,
55  typename = std::enable_if_t<!detail::is_single_type_v<Extended, ARGS...> &&
56  std::is_constructible_v<T, ARGS...>>>
57  explicit constexpr Extended(ARGS&&... args) :
58  m_value(std::forward<ARGS>(args)...)
59  {}
60 
61  Extended& operator=(Extended&& other) = default;
62  Extended& operator=(const Extended& other) = default;
63 
64  ~Extended() = default;
65 
71  constexpr const T& value() const noexcept
72  {
73  return m_value;
74  }
75 
81  T& value() noexcept
82  {
83  return m_value;
84  }
85 
91  constexpr T& operator*() noexcept
92  {
93  return m_value;
94  }
95 
101  constexpr const T& operator*() const noexcept
102  {
103  return m_value;
104  }
105 
111  T* operator->() noexcept
112  {
113  return &m_value;
114  }
115 
121  const T* operator->() const noexcept
122  {
123  return &m_value;
124  }
125 
131  constexpr explicit operator bool() const noexcept
132  {
133  return m_isPresent;
134  }
135 
141  bool isPresent() const noexcept
142  {
143  return m_isPresent;
144  }
145 
159  void setPresent(bool present) noexcept
160  {
161  m_isPresent = present;
162  }
163 
164 private:
165  bool m_isPresent = true; // present by default
166  T m_value;
167 };
168 
169 template <typename T>
170 bool operator==(const Extended<T>& lhs, const Extended<T>& rhs)
171 {
172  if (lhs.isPresent() && rhs.isPresent())
173  {
174  return lhs.value() == rhs.value();
175  }
176  return lhs.isPresent() == rhs.isPresent();
177 }
178 
179 template <typename T>
180 bool operator!=(const Extended<T>& lhs, const Extended<T>& rhs)
181 {
182  return !(lhs == rhs);
183 }
184 
185 template <typename T>
186 bool operator<(const Extended<T>& lhs, const Extended<T>& rhs)
187 {
188  if (lhs.isPresent() && rhs.isPresent())
189  {
190  return lhs.value() < rhs.value();
191  }
192  return lhs.isPresent() < rhs.isPresent();
193 }
194 
195 template <typename T>
196 bool operator>(const Extended<T>& lhs, const Extended<T>& rhs)
197 {
198  if (lhs.isPresent() && rhs.isPresent())
199  {
200  return lhs.value() > rhs.value();
201  }
202  return lhs.isPresent() > rhs.isPresent();
203 }
204 
205 template <typename T>
206 bool operator<=(const Extended<T>& lhs, const Extended<T>& rhs)
207 {
208  return !(lhs > rhs);
209 }
210 
211 template <typename T>
212 bool operator>=(const Extended<T>& lhs, const Extended<T>& rhs)
213 {
214  return !(rhs > lhs);
215 }
216 
225 template <typename T>
226 uint32_t calcHashCode(uint32_t seed, const Extended<T>& extendedValue)
227 {
228  uint32_t result = seed;
229  if (extendedValue.isPresent())
230  {
231  result = calcHashCode(result, *extendedValue);
232  }
233  return result;
234 }
235 
236 } // namespace zserio
237 
238 namespace std
239 {
240 
241 template <typename T>
242 struct hash<zserio::Extended<T>>
243 {
244  size_t operator()(const zserio::Extended<T>& extendedValue) const
245  {
246  return zserio::calcHashCode(zserio::HASH_SEED, extendedValue);
247  }
248 };
249 
250 } // namespace std
251 
252 #endif // ZSERIO_EXTENDED_H_INC
bool isPresent() const noexcept
Definition: Extended.h:141
Extended & operator=(Extended &&other)=default
const T * operator->() const noexcept
Definition: Extended.h:121
~Extended()=default
T & value() noexcept
Definition: Extended.h:81
constexpr Extended(ARGS &&... args)
Definition: Extended.h:57
constexpr const T & value() const noexcept
Definition: Extended.h:71
constexpr const T & operator*() const noexcept
Definition: Extended.h:101
void setPresent(bool present) noexcept
Definition: Extended.h:159
T * operator->() noexcept
Definition: Extended.h:111
Extended & operator=(const Extended &other)=default
Extended(Extended &&other)=default
constexpr T & operator*() noexcept
Definition: Extended.h:91
Extended(Extended &&other, const ALLOC &allocator)
Definition: Extended.h:38
Extended(const Extended &other)=default
Extended(const Extended &other, const ALLOC &allocator)
Definition: Extended.h:46
Definition: BitBuffer.h:602
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
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
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
size_t operator()(const zserio::Extended< T > &extendedValue) const
Definition: Extended.h:244