Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
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
6namespace zserio
7{
8
9namespace detail
10{
11
12template <typename T, typename... ARGS>
13struct is_single_type : std::false_type
14{};
15
16template <typename T, typename A1>
17struct is_single_type<T, A1> : std::is_same<T, std::decay_t<A1>>
18{};
19
20template <typename T, typename... ARGS>
21inline constexpr bool is_single_type_v = is_single_type<T, ARGS...>::value;
22
23} // namespace detail
24
31template <typename T>
33{
34public:
35 Extended(Extended&& other) = default;
36
37 template <typename ALLOC>
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>
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
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
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
112 {
113 return &m_value;
114 }
115
122 {
123 return &m_value;
124 }
125
131 constexpr explicit operator bool() const noexcept
132 {
133 return m_isPresent;
134 }
135
142 {
143 return m_isPresent;
144 }
145
159 void setPresent(bool present) noexcept
160 {
161 m_isPresent = present;
162 }
163
164private:
165 bool m_isPresent = true; // present by default
166 T m_value;
167};
168
169template <typename T>
171{
172 if (lhs.isPresent() && rhs.isPresent())
173 {
174 return lhs.value() == rhs.value();
175 }
176 return lhs.isPresent() == rhs.isPresent();
177}
178
179template <typename T>
181{
182 return !(lhs == rhs);
183}
184
185template <typename T>
187{
188 if (lhs.isPresent() && rhs.isPresent())
189 {
190 return lhs.value() < rhs.value();
191 }
192 return lhs.isPresent() < rhs.isPresent();
193}
194
195template <typename T>
197{
198 if (lhs.isPresent() && rhs.isPresent())
199 {
200 return lhs.value() > rhs.value();
201 }
202 return lhs.isPresent() > rhs.isPresent();
203}
204
205template <typename T>
207{
208 return !(lhs > rhs);
209}
210
211template <typename T>
213{
214 return !(rhs > lhs);
215}
216
225template <typename T>
227{
229 if (extendedValue.isPresent())
230 {
232 }
233 return result;
234}
235
236} // namespace zserio
237
238namespace std
239{
240
241template <typename T>
242struct 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
constexpr T & operator*() noexcept
Definition Extended.h:91
constexpr const T & operator*() const noexcept
Definition Extended.h:101
bool isPresent() const noexcept
Definition Extended.h:141
constexpr const T & value() const noexcept
Definition Extended.h:71
const T * operator->() const noexcept
Definition Extended.h:121
Extended & operator=(const Extended &other)=default
~Extended()=default
T * operator->() noexcept
Definition Extended.h:111
constexpr Extended(ARGS &&... args)
Definition Extended.h:57
void setPresent(bool present) noexcept
Definition Extended.h:159
T & value() noexcept
Definition Extended.h:81
Extended(Extended &&other)=default
Extended & operator=(Extended &&other)=default
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
bool operator>(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:526
bool operator==(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:508
bool operator<(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:520
uint32_t calcHashCode(uint32_t seedValue, const ArrayView< T, ARRAY_TRAITS > &array)
Definition ArrayView.h:882
bool operator<=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:532
bool operator>=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:538
bool operator!=(const BasicBitBufferView< ALLOC > &lhs, const BasicBitBufferView< ALLOC > &rhs)
Definition BitBuffer.h:514