Horizon
Loading...
Searching...
No Matches
byte_container_with_subtype.hpp
1#pragma once
2
3#include <cstdint> // uint8_t, uint64_t
4#include <tuple> // tie
5#include <utility> // move
6
7namespace nlohmann
8{
9
23template<typename BinaryType>
24class byte_container_with_subtype : public BinaryType
25{
26 public:
28 using container_type = BinaryType;
30 using subtype_type = std::uint64_t;
31
32 byte_container_with_subtype() noexcept(noexcept(container_type()))
34 {}
35
36 byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
38 {}
39
40 byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
41 : container_type(std::move(b))
42 {}
43
44 byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
46 , m_subtype(subtype_)
47 , m_has_subtype(true)
48 {}
49
50 byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
51 : container_type(std::move(b))
52 , m_subtype(subtype_)
53 , m_has_subtype(true)
54 {}
55
56 bool operator==(const byte_container_with_subtype& rhs) const
57 {
58 return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
59 std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
60 }
61
62 bool operator!=(const byte_container_with_subtype& rhs) const
63 {
64 return !(rhs == *this);
65 }
66
85 void set_subtype(subtype_type subtype_) noexcept
86 {
87 m_subtype = subtype_;
88 m_has_subtype = true;
89 }
90
113 constexpr subtype_type subtype() const noexcept
114 {
115 return m_has_subtype ? m_subtype : subtype_type(-1);
116 }
117
134 constexpr bool has_subtype() const noexcept
135 {
136 return m_has_subtype;
137 }
138
158 void clear_subtype() noexcept
159 {
160 m_subtype = 0;
161 m_has_subtype = false;
162 }
163
164 private:
165 subtype_type m_subtype = 0;
166 bool m_has_subtype = false;
167};
168
169} // namespace nlohmann
an internal type for a backed binary type
Definition byte_container_with_subtype.hpp:25
BinaryType container_type
the type of the underlying container
Definition byte_container_with_subtype.hpp:28
void clear_subtype() noexcept
clears the binary subtype
Definition byte_container_with_subtype.hpp:158
constexpr bool has_subtype() const noexcept
return whether the value has a subtype
Definition byte_container_with_subtype.hpp:134
void set_subtype(subtype_type subtype_) noexcept
sets the binary subtype
Definition byte_container_with_subtype.hpp:85
constexpr subtype_type subtype() const noexcept
return the binary subtype
Definition byte_container_with_subtype.hpp:113
std::uint64_t subtype_type
the type of the subtype
Definition byte_container_with_subtype.hpp:30
namespace for Niels Lohmann
Definition adl_serializer.hpp:12