MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
18
19//
20
21#ifndef _WIN32
22#include <sys/param.h> // Endian detection
23#endif
24
25#include <cstdint>
26#include <cstring>
27
29
31
32namespace bsoncxx {
33namespace v_noabi {
34namespace vector {
35namespace elements {
36
38class float32 {
39 public:
42 float32(float value) noexcept {
43#if defined(_WIN32) || \
44 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
45 defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)
46 std::memcpy(bytes, &value, sizeof value);
47#elif (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || \
48 defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
49 std::uint32_t u32;
50 std::memcpy(&u32, &value, sizeof value);
51 u32 = __builtin_bswap32(u32);
52 std::memcpy(bytes, &u32, sizeof value);
53#else
54#error No implementation for storing 32-bit little endian unaligned float
55#endif
56 }
57
59 operator float() const noexcept {
60 float value;
61#if defined(_WIN32) || \
62 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
63 defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)
64 std::memcpy(&value, bytes, sizeof value);
65#elif (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || \
66 defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
67 std::uint32_t u32;
68 std::memcpy(&u32, bytes, sizeof value);
69 u32 = __builtin_bswap32(u32);
70 std::memcpy(&value, &u32, sizeof value);
71#else
72#error No implementation for loading 32-bit little endian unaligned float
73#endif
74 return value;
75 }
76
78 float32& operator+=(float const& other) noexcept {
79 return *this = *this + other;
80 }
81
83 float32& operator-=(float const& other) noexcept {
84 return *this = *this - other;
85 }
86
88 float32& operator*=(float const& other) noexcept {
89 return *this = *this * other;
90 }
91
93 float32& operator/=(float const& other) noexcept {
94 return *this = *this / other;
95 }
96
97 private:
98 std::uint8_t bytes[4];
99};
100
103template <typename Iterator>
104class packed_bit_element {
105 public:
107 using value_type = bool;
108
110 constexpr operator value_type() const {
111 return ((*byte & mask) == UINT8_C(0)) ? value_type(0) : value_type(1);
112 }
113
115 packed_bit_element const& operator=(value_type const& v) const {
116 if (v == 0) {
117 *byte &= std::uint8_t(~mask);
118 } else {
119 *byte |= mask;
120 }
121 return *this;
122 }
123
125 packed_bit_element const& operator=(packed_bit_element const& v) const noexcept {
126 return *this = value_type{v};
127 }
128
130 packed_bit_element const& operator^=(value_type const& other) const noexcept {
131 return *this = *this ^ other;
132 }
133
135 packed_bit_element const& operator&=(value_type const& other) const noexcept {
136 return *this = *this & other;
137 }
138
140 packed_bit_element const& operator|=(value_type const& other) const noexcept {
141 return *this = *this | other;
142 }
143
144 constexpr packed_bit_element(packed_bit_element const& other) : byte(other.byte), mask(other.mask) {}
145
146 private:
147 friend class iterators::packed_bit_element<Iterator>;
148
149 constexpr packed_bit_element(Iterator byte_iter, uint8_t bit_index) noexcept
150 : byte{byte_iter}, mask{uint8_t(0x80u >> bit_index)} {}
151
152 Iterator byte;
153 std::uint8_t mask;
154};
155
157template <typename Iterator>
159 bool a_value = a;
160 bool b_value = b;
161 a = b_value;
162 b = a_value;
163}
164
168template <typename Iterator>
169class packed_bit_byte {
170 public:
172 constexpr operator std::uint8_t() const noexcept {
173 return *byte;
174 }
175
179 packed_bit_byte const& operator=(std::uint8_t const& v) const noexcept {
180 *byte = v & mask;
181 return *this;
182 }
183
185 packed_bit_byte const& operator=(packed_bit_byte const& v) const noexcept {
186 return *this = std::uint8_t(v);
187 }
188
190 packed_bit_byte const& operator+=(std::uint8_t const& other) const noexcept {
191 return *this = std::uint8_t(*this + other);
192 }
193
195 packed_bit_byte const& operator-=(std::uint8_t const& other) const noexcept {
196 return *this = std::uint8_t(*this - other);
197 }
198
200 packed_bit_byte const& operator*=(std::uint8_t const& other) const noexcept {
201 return *this = std::uint8_t(*this * other);
202 }
203
205 packed_bit_byte const& operator/=(std::uint8_t const& other) const noexcept {
206 return *this = *this / other;
207 }
208
210 packed_bit_byte const& operator%=(std::uint8_t const& other) const noexcept {
211 return *this = *this % other;
212 }
213
215 packed_bit_byte const& operator^=(std::uint8_t const& other) const noexcept {
216 return *this = *this ^ other;
217 }
218
220 packed_bit_byte const& operator&=(std::uint8_t const& other) const noexcept {
221 return *this = *this & other;
222 }
223
225 packed_bit_byte const& operator|=(std::uint8_t const& other) const noexcept {
226 return *this = *this | other;
227 }
228
230 packed_bit_byte const& operator<<=(unsigned other) const noexcept {
231 return *this = *this << other;
232 }
233
235 packed_bit_byte const& operator>>=(unsigned other) const noexcept {
236 return *this = *this >> other;
237 }
238
239 constexpr packed_bit_byte(packed_bit_byte const& other) : byte{other.byte}, mask{other.mask} {}
240
241 private:
242 friend class iterators::packed_bit_byte<Iterator>;
243
244 constexpr packed_bit_byte(Iterator byte_iter, uint8_t byte_mask) noexcept : byte{byte_iter}, mask{byte_mask} {}
245
246 Iterator byte;
247 std::uint8_t mask;
248};
249
253template <typename Iterator>
255 std::uint8_t a_value = a;
256 std::uint8_t b_value = b;
257 a = b_value;
258 b = a_value;
259}
260
261} // namespace elements
262} // namespace vector
263} // namespace v_noabi
264} // namespace bsoncxx
265
267
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:88
float32 & operator-=(float const &other) noexcept
Operator -=, emulating normal float behavior.
Definition elements.hpp:83
float32 & operator+=(float const &other) noexcept
Operator +=, emulating normal float behavior.
Definition elements.hpp:78
float32 & operator/=(float const &other) noexcept
Operator /=, emulating normal float behavior.
Definition elements.hpp:93
float32(float value) noexcept
Construct a packed little-endian value from a float input in the local byte order.
Definition elements.hpp:42
Reference to a byte or partial byte within a vector of packed_bit elements. Allows access to each byt...
Definition elements.hpp:169
packed_bit_byte const & operator*=(std::uint8_t const &other) const noexcept
Operator *=, emulating number reference behavior.
Definition elements.hpp:200
packed_bit_byte const & operator+=(std::uint8_t const &other) const noexcept
Operator +=, emulating number reference behavior.
Definition elements.hpp:190
packed_bit_byte const & operator/=(std::uint8_t const &other) const noexcept
Operator /=, emulating number reference behavior.
Definition elements.hpp:205
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:185
packed_bit_byte const & operator^=(std::uint8_t const &other) const noexcept
Operator ^=, emulating number reference behavior.
Definition elements.hpp:215
packed_bit_byte const & operator&=(std::uint8_t const &other) const noexcept
Operator &=, emulating number reference behavior.
Definition elements.hpp:220
packed_bit_byte const & operator-=(std::uint8_t const &other) const noexcept
Operator -=, emulating number reference behavior.
Definition elements.hpp:195
packed_bit_byte const & operator|=(std::uint8_t const &other) const noexcept
Operator |=, emulating number reference behavior.
Definition elements.hpp:225
constexpr operator std::uint8_t() const noexcept
Read the entire byte, as a std::uint8_t.
Definition elements.hpp:172
packed_bit_byte const & operator%=(std::uint8_t const &other) const noexcept
Operator %=, emulating number reference behavior.
Definition elements.hpp:210
packed_bit_byte const & operator<<=(unsigned other) const noexcept
Operator <<=, emulating number reference behavior.
Definition elements.hpp:230
packed_bit_byte const & operator>>=(unsigned other) const noexcept
Operator >>=, emulating number reference behavior.
Definition elements.hpp:235
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:179
Reference to a single element in a packed_bit vector.
Definition elements.hpp:104
packed_bit_element const & operator^=(value_type const &other) const noexcept
Operator ^=, emulating bool reference behavior.
Definition elements.hpp:130
packed_bit_element const & operator=(value_type const &v) const
Set the value of the element referenced.
Definition elements.hpp:115
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:125
packed_bit_element const & operator&=(value_type const &other) const noexcept
Operator &=, emulating bool reference behavior.
Definition elements.hpp:135
bool value_type
Value type chosen to represent a single-bit element.
Definition elements.hpp:107
packed_bit_element const & operator|=(value_type const &other) const noexcept
Operator |=, emulating bool reference behavior.
Definition elements.hpp:140
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:158
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.