Loading...
Searching...
No Matches
elements.hpp
Go to the documentation of this file.
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <bsoncxx/vector/elements-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/detail/bit.hpp>
#include <cstdint>
#include <cstring>
#include <bsoncxx/vector/iterators-fwd.hpp>
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
namespace vector {
namespace elements {
class float32 {
template <bsoncxx::detail::endian = bsoncxx::detail::endian::native>
void construct(float value);
template <bsoncxx::detail::endian = bsoncxx::detail::endian::native>
float convert() const;
public:
float32(float value) noexcept;
operator float() const noexcept;
float32& operator+=(float const& other) noexcept {
return *this = *this + other;
}
float32& operator-=(float const& other) noexcept {
return *this = *this - other;
}
float32& operator*=(float const& other) noexcept {
return *this = *this * other;
}
float32& operator/=(float const& other) noexcept {
return *this = *this / other;
}
private:
std::uint8_t bytes[4];
};
template <>
inline void float32::construct<bsoncxx::detail::endian::little>(float value) {
std::memcpy(bytes, &value, sizeof value);
}
template <>
inline void float32::construct<bsoncxx::detail::endian::big>(float value) {
auto const ptr = reinterpret_cast<unsigned char const*>(&value);
bytes[0] = ptr[3];
bytes[1] = ptr[2];
bytes[2] = ptr[1];
bytes[3] = ptr[0];
}
template <>
inline float float32::convert<bsoncxx::detail::endian::little>() const {
float value;
std::memcpy(&value, bytes, sizeof value);
return value;
}
template <>
inline float float32::convert<bsoncxx::detail::endian::big>() const {
float value;
auto const ptr = reinterpret_cast<unsigned char*>(&value);
ptr[0] = bytes[3];
ptr[1] = bytes[2];
ptr[2] = bytes[1];
ptr[3] = bytes[0];
return value;
}
inline float32::float32(float value) noexcept {
this->construct(value);
}
inline float32::operator float() const noexcept {
return this->convert();
}
template <typename Iterator>
class packed_bit_element {
public:
using value_type = bool;
constexpr operator value_type() const {
return ((*byte & mask) == UINT8_C(0)) ? value_type(0) : value_type(1);
}
packed_bit_element const& operator=(value_type const& v) const {
if (v == 0) {
*byte &= std::uint8_t(~mask);
} else {
*byte |= mask;
}
return *this;
}
packed_bit_element const& operator=(packed_bit_element const& v) const noexcept {
return *this = value_type{v};
}
packed_bit_element const& operator^=(value_type const& other) const noexcept {
return *this = *this ^ other;
}
packed_bit_element const& operator&=(value_type const& other) const noexcept {
return *this = *this & other;
}
packed_bit_element const& operator|=(value_type const& other) const noexcept {
return *this = *this | other;
}
constexpr packed_bit_element(packed_bit_element const& other) : byte(other.byte), mask(other.mask) {}
private:
friend class iterators::packed_bit_element<Iterator>;
constexpr packed_bit_element(Iterator byte_iter, uint8_t bit_index) noexcept
: byte{byte_iter}, mask{uint8_t(0x80u >> bit_index)} {}
Iterator byte;
std::uint8_t mask;
};
template <typename Iterator>
void swap(packed_bit_element<Iterator> a, packed_bit_element<Iterator> b) noexcept {
bool a_value = a;
bool b_value = b;
a = b_value;
b = a_value;
}
template <typename Iterator>
class packed_bit_byte {
public:
constexpr operator std::uint8_t() const noexcept {
return *byte;
}
packed_bit_byte const& operator=(std::uint8_t const& v) const noexcept {
*byte = v & mask;
return *this;
}
packed_bit_byte const& operator=(packed_bit_byte const& v) const noexcept {
return *this = std::uint8_t(v);
}
packed_bit_byte const& operator+=(std::uint8_t const& other) const noexcept {
return *this = std::uint8_t(*this + other);
}
packed_bit_byte const& operator-=(std::uint8_t const& other) const noexcept {
return *this = std::uint8_t(*this - other);
}
packed_bit_byte const& operator*=(std::uint8_t const& other) const noexcept {
return *this = std::uint8_t(*this * other);
}
packed_bit_byte const& operator/=(std::uint8_t const& other) const noexcept {
return *this = *this / other;
}
packed_bit_byte const& operator%=(std::uint8_t const& other) const noexcept {
return *this = *this % other;
}
packed_bit_byte const& operator^=(std::uint8_t const& other) const noexcept {
return *this = *this ^ other;
}
packed_bit_byte const& operator&=(std::uint8_t const& other) const noexcept {
return *this = *this & other;
}
packed_bit_byte const& operator|=(std::uint8_t const& other) const noexcept {
return *this = *this | other;
}
packed_bit_byte const& operator<<=(unsigned other) const noexcept {
return *this = *this << other;
}
packed_bit_byte const& operator>>=(unsigned other) const noexcept {
return *this = *this >> other;
}
constexpr packed_bit_byte(packed_bit_byte const& other) : byte{other.byte}, mask{other.mask} {}
private:
friend class iterators::packed_bit_byte<Iterator>;
constexpr packed_bit_byte(Iterator byte_iter, uint8_t byte_mask) noexcept : byte{byte_iter}, mask{byte_mask} {}
Iterator byte;
std::uint8_t mask;
};
template <typename Iterator>
void swap(packed_bit_byte<Iterator> a, packed_bit_byte<Iterator> b) noexcept {
std::uint8_t a_value = a;
std::uint8_t b_value = b;
a = b_value;
b = a_value;
}
} // namespace elements
} // namespace vector
} // namespace v_noabi
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
bit.hpp
For internal use only!
postlude.hpp
The bsoncxx v_noabi macro guard postlude header.
prelude.hpp
The bsoncxx v_noabi macro guard prelude header.
bsoncxx::v_noabi::vector::elements::float32::operator*=
float32 & operator*=(float const &other) noexcept
Operator *=, emulating normal float behavior.
Definition elements.hpp:62
bsoncxx::v_noabi::vector::elements::float32::operator-=
float32 & operator-=(float const &other) noexcept
Operator -=, emulating normal float behavior.
Definition elements.hpp:57
bsoncxx::v_noabi::vector::elements::float32::operator+=
float32 & operator+=(float const &other) noexcept
Operator +=, emulating normal float behavior.
Definition elements.hpp:52
bsoncxx::v_noabi::vector::elements::float32::operator/=
float32 & operator/=(float const &other) noexcept
Operator /=, emulating normal float behavior.
Definition elements.hpp:67
bsoncxx::v_noabi::vector::elements::float32::float32
float32(float value) noexcept
Construct a packed little-endian value from a float input in the local byte order.
Definition elements.hpp:107
bsoncxx::v_noabi::vector::elements::packed_bit_byte
Reference to a byte or partial byte within a vector of packed_bit elements. Allows access to each byt...
Definition elements.hpp:183
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator*=
packed_bit_byte const & operator*=(std::uint8_t const &other) const noexcept
Operator *=, emulating number reference behavior.
Definition elements.hpp:214
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator+=
packed_bit_byte const & operator+=(std::uint8_t const &other) const noexcept
Operator +=, emulating number reference behavior.
Definition elements.hpp:204
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator/=
packed_bit_byte const & operator/=(std::uint8_t const &other) const noexcept
Operator /=, emulating number reference behavior.
Definition elements.hpp:219
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator=
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
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator^=
packed_bit_byte const & operator^=(std::uint8_t const &other) const noexcept
Operator ^=, emulating number reference behavior.
Definition elements.hpp:229
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator&=
packed_bit_byte const & operator&=(std::uint8_t const &other) const noexcept
Operator &=, emulating number reference behavior.
Definition elements.hpp:234
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator-=
packed_bit_byte const & operator-=(std::uint8_t const &other) const noexcept
Operator -=, emulating number reference behavior.
Definition elements.hpp:209
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator|=
packed_bit_byte const & operator|=(std::uint8_t const &other) const noexcept
Operator |=, emulating number reference behavior.
Definition elements.hpp:239
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator%=
packed_bit_byte const & operator%=(std::uint8_t const &other) const noexcept
Operator %=, emulating number reference behavior.
Definition elements.hpp:224
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator<<=
packed_bit_byte const & operator<<=(unsigned other) const noexcept
Operator <<=, emulating number reference behavior.
Definition elements.hpp:244
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator>>=
packed_bit_byte const & operator>>=(unsigned other) const noexcept
Operator >>=, emulating number reference behavior.
Definition elements.hpp:249
bsoncxx::v_noabi::vector::elements::packed_bit_byte::operator=
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
bsoncxx::v_noabi::vector::elements::packed_bit_element
Reference to a single element in a packed_bit vector.
Definition elements.hpp:118
bsoncxx::v_noabi::vector::elements::packed_bit_element::operator^=
packed_bit_element const & operator^=(value_type const &other) const noexcept
Operator ^=, emulating bool reference behavior.
Definition elements.hpp:144
bsoncxx::v_noabi::vector::elements::packed_bit_element::operator=
packed_bit_element const & operator=(value_type const &v) const
Set the value of the element referenced.
Definition elements.hpp:129
bsoncxx::v_noabi::vector::elements::packed_bit_element::operator=
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
bsoncxx::v_noabi::vector::elements::packed_bit_element::operator&=
packed_bit_element const & operator&=(value_type const &other) const noexcept
Operator &=, emulating bool reference behavior.
Definition elements.hpp:149
bsoncxx::v_noabi::vector::elements::packed_bit_element::value_type
bool value_type
Value type chosen to represent a single-bit element.
Definition elements.hpp:121
bsoncxx::v_noabi::vector::elements::packed_bit_element::operator|=
packed_bit_element const & operator|=(value_type const &other) const noexcept
Operator |=, emulating bool reference behavior.
Definition elements.hpp:154
bsoncxx::v_noabi::vector::iterators::packed_bit_byte
Iterator for bytes within a packed_bit vector.
Definition iterators.hpp:169
bsoncxx::v_noabi::vector::iterators::packed_bit_element
Iterator for elements within a packed_bit vector.
Definition iterators.hpp:40
elements-fwd.hpp
Forward declarations for bsoncxx::v_noabi::vector::elements.
iterators-fwd.hpp
Forward declarations for bsoncxx::v_noabi::vector::iterators.
bsoncxx::v_noabi::vector::elements
Declares element accessor types for BSON Binary Vector.
bsoncxx::v_noabi::vector::elements::swap
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
bsoncxx::v_noabi::vector
Declarations related to the BSON Binary Vector subtype.
bsoncxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.