MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
elements.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <bsoncxx/vector/elements-fwd.hpp> // IWYU pragma: export
18
19//
20
22
23#include <cstdint>
24#include <cstring>
25
27
29
30namespace bsoncxx {
31namespace v_noabi {
32namespace vector {
33namespace elements {
34
36class float32 {
37 template <bsoncxx::detail::endian = bsoncxx::detail::endian::native>
38 void construct(float value);
39
40 template <bsoncxx::detail::endian = bsoncxx::detail::endian::native>
41 float convert() const;
42
43 public:
46 float32(float value) noexcept;
47
49 operator float() const noexcept;
50
52 float32& operator+=(float const& other) noexcept {
53 return *this = *this + other;
54 }
55
57 float32& operator-=(float const& other) noexcept {
58 return *this = *this - other;
59 }
60
62 float32& operator*=(float const& other) noexcept {
63 return *this = *this * other;
64 }
65
67 float32& operator/=(float const& other) noexcept {
68 return *this = *this / other;
69 }
70
71 private:
72 std::uint8_t bytes[4];
73};
74
75template <>
76inline void float32::construct<bsoncxx::detail::endian::little>(float value) {
77 std::memcpy(bytes, &value, sizeof value);
78}
79
80template <>
81inline void float32::construct<bsoncxx::detail::endian::big>(float value) {
82 auto const ptr = reinterpret_cast<unsigned char const*>(&value);
83 bytes[0] = ptr[3];
84 bytes[1] = ptr[2];
85 bytes[2] = ptr[1];
86 bytes[3] = ptr[0];
87}
88
89template <>
90inline float float32::convert<bsoncxx::detail::endian::little>() const {
91 float value;
92 std::memcpy(&value, bytes, sizeof value);
93 return value;
94}
95
96template <>
97inline float float32::convert<bsoncxx::detail::endian::big>() const {
98 float value;
99 auto const ptr = reinterpret_cast<unsigned char*>(&value);
100 ptr[0] = bytes[3];
101 ptr[1] = bytes[2];
102 ptr[2] = bytes[1];
103 ptr[3] = bytes[0];
104 return value;
105}
106
107inline float32::float32(float value) noexcept {
108 this->construct(value);
109}
110
111inline float32::operator float() const noexcept {
112 return this->convert();
113}
114
117template <typename Iterator>
118class packed_bit_element {
119 public:
121 using value_type = bool;
122
124 constexpr operator value_type() const {
125 return ((*byte & mask) == UINT8_C(0)) ? value_type(0) : value_type(1);
126 }
127
129 packed_bit_element const& operator=(value_type const& v) const {
130 if (v == 0) {
131 *byte &= std::uint8_t(~mask);
132 } else {
133 *byte |= mask;
134 }
135 return *this;
136 }
137
139 packed_bit_element const& operator=(packed_bit_element const& v) const noexcept {
140 return *this = value_type{v};
141 }
142
144 packed_bit_element const& operator^=(value_type const& other) const noexcept {
145 return *this = *this ^ other;
146 }
147
149 packed_bit_element const& operator&=(value_type const& other) const noexcept {
150 return *this = *this & other;
151 }
152
154 packed_bit_element const& operator|=(value_type const& other) const noexcept {
155 return *this = *this | other;
156 }
157
158 constexpr packed_bit_element(packed_bit_element const& other) : byte(other.byte), mask(other.mask) {}
159
160 private:
161 friend class iterators::packed_bit_element<Iterator>;
162
163 constexpr packed_bit_element(Iterator byte_iter, uint8_t bit_index) noexcept
164 : byte{byte_iter}, mask{uint8_t(0x80u >> bit_index)} {}
165
166 Iterator byte;
167 std::uint8_t mask;
168};
169
171template <typename Iterator>
173 bool a_value = a;
174 bool b_value = b;
175 a = b_value;
176 b = a_value;
177}
178
182template <typename Iterator>
183class packed_bit_byte {
184 public:
186 constexpr operator std::uint8_t() const noexcept {
187 return *byte;
188 }
189
193 packed_bit_byte const& operator=(std::uint8_t const& v) const noexcept {
194 *byte = v & mask;
195 return *this;
196 }
197
199 packed_bit_byte const& operator=(packed_bit_byte const& v) const noexcept {
200 return *this = std::uint8_t(v);
201 }
202
204 packed_bit_byte const& operator+=(std::uint8_t const& other) const noexcept {
205 return *this = std::uint8_t(*this + other);
206 }
207
209 packed_bit_byte const& operator-=(std::uint8_t const& other) const noexcept {
210 return *this = std::uint8_t(*this - other);
211 }
212
214 packed_bit_byte const& operator*=(std::uint8_t const& other) const noexcept {
215 return *this = std::uint8_t(*this * other);
216 }
217
219 packed_bit_byte const& operator/=(std::uint8_t const& other) const noexcept {
220 return *this = *this / other;
221 }
222
224 packed_bit_byte const& operator%=(std::uint8_t const& other) const noexcept {
225 return *this = *this % other;
226 }
227
229 packed_bit_byte const& operator^=(std::uint8_t const& other) const noexcept {
230 return *this = *this ^ other;
231 }
232
234 packed_bit_byte const& operator&=(std::uint8_t const& other) const noexcept {
235 return *this = *this & other;
236 }
237
239 packed_bit_byte const& operator|=(std::uint8_t const& other) const noexcept {
240 return *this = *this | other;
241 }
242
244 packed_bit_byte const& operator<<=(unsigned other) const noexcept {
245 return *this = *this << other;
246 }
247
249 packed_bit_byte const& operator>>=(unsigned other) const noexcept {
250 return *this = *this >> other;
251 }
252
253 constexpr packed_bit_byte(packed_bit_byte const& other) : byte{other.byte}, mask{other.mask} {}
254
255 private:
256 friend class iterators::packed_bit_byte<Iterator>;
257
258 constexpr packed_bit_byte(Iterator byte_iter, uint8_t byte_mask) noexcept : byte{byte_iter}, mask{byte_mask} {}
259
260 Iterator byte;
261 std::uint8_t mask;
262};
263
267template <typename Iterator>
269 std::uint8_t a_value = a;
270 std::uint8_t b_value = b;
271 a = b_value;
272 b = a_value;
273}
274
275} // namespace elements
276} // namespace vector
277} // namespace v_noabi
278} // namespace bsoncxx
279
281
For internal use only!
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
float32 & operator*=(float const &other) noexcept
Operator *=, emulating normal float behavior.
Definition elements.hpp:62
float32 & operator-=(float const &other) noexcept
Operator -=, emulating normal float behavior.
Definition elements.hpp:57
float32 & operator+=(float const &other) noexcept
Operator +=, emulating normal float behavior.
Definition elements.hpp:52
float32 & operator/=(float const &other) noexcept
Operator /=, emulating normal float behavior.
Definition elements.hpp:67
float32(float value) noexcept
Construct a packed little-endian value from a float input in the local byte order.
Definition elements.hpp:107
Reference to a byte or partial byte within a vector of packed_bit elements. Allows access to each byt...
Definition elements.hpp:183
packed_bit_byte const & operator*=(std::uint8_t const &other) const noexcept
Operator *=, emulating number reference behavior.
Definition elements.hpp:214
packed_bit_byte const & operator+=(std::uint8_t const &other) const noexcept
Operator +=, emulating number reference behavior.
Definition elements.hpp:204
packed_bit_byte const & operator/=(std::uint8_t const &other) const noexcept
Operator /=, emulating number reference behavior.
Definition elements.hpp:219
packed_bit_byte const & operator=(packed_bit_byte const &v) const noexcept
Copy the referenced value from another reference of the same type.
Definition elements.hpp:199
packed_bit_byte const & operator^=(std::uint8_t const &other) const noexcept
Operator ^=, emulating number reference behavior.
Definition elements.hpp:229
packed_bit_byte const & operator&=(std::uint8_t const &other) const noexcept
Operator &=, emulating number reference behavior.
Definition elements.hpp:234
packed_bit_byte const & operator-=(std::uint8_t const &other) const noexcept
Operator -=, emulating number reference behavior.
Definition elements.hpp:209
packed_bit_byte const & operator|=(std::uint8_t const &other) const noexcept
Operator |=, emulating number reference behavior.
Definition elements.hpp:239
packed_bit_byte const & operator%=(std::uint8_t const &other) const noexcept
Operator %=, emulating number reference behavior.
Definition elements.hpp:224
packed_bit_byte const & operator<<=(unsigned other) const noexcept
Operator <<=, emulating number reference behavior.
Definition elements.hpp:244
packed_bit_byte const & operator>>=(unsigned other) const noexcept
Operator >>=, emulating number reference behavior.
Definition elements.hpp:249
packed_bit_byte const & operator=(std::uint8_t const &v) const noexcept
Overwrite the usable portion of the byte, and set reserved bits to zero.
Definition elements.hpp:193
Reference to a single element in a packed_bit vector.
Definition elements.hpp:118
packed_bit_element const & operator^=(value_type const &other) const noexcept
Operator ^=, emulating bool reference behavior.
Definition elements.hpp:144
packed_bit_element const & operator=(value_type const &v) const
Set the value of the element referenced.
Definition elements.hpp:129
packed_bit_element const & operator=(packed_bit_element const &v) const noexcept
Copy the referenced value from another reference of the same type.
Definition elements.hpp:139
packed_bit_element const & operator&=(value_type const &other) const noexcept
Operator &=, emulating bool reference behavior.
Definition elements.hpp:149
bool value_type
Value type chosen to represent a single-bit element.
Definition elements.hpp:121
packed_bit_element const & operator|=(value_type const &other) const noexcept
Operator |=, emulating bool reference behavior.
Definition elements.hpp:154
Iterator for bytes within a packed_bit vector.
Definition iterators.hpp:165
Iterator for elements within a packed_bit vector.
Definition iterators.hpp:40
Forward declarations for bsoncxx::v_noabi::vector::elements.
Forward declarations for bsoncxx::v_noabi::vector::iterators.
Declares element accessor types for BSON Binary Vector.
void swap(packed_bit_element< Iterator > a, packed_bit_element< Iterator > b) noexcept
packed_bit_element is Swappable even when it's not an lvalue reference
Definition elements.hpp:172
Declarations related to the BSON Binary Vector subtype.
Declares entities whose ABI stability is NOT guaranteed.
The top-level namespace within which all bsoncxx library entities are declared.