MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
detail.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#include <array>
22#include <cstdint>
23#include <cstring>
24
25#include <bsoncxx/types.hpp>
29
31
32namespace bsoncxx {
33namespace v_noabi {
34namespace vector {
35namespace detail {
36
37// Implementation detail. Size of the BSON Binary Vector header, in bytes.
38constexpr std::size_t header_size = 2;
39
40// Implementation detail. Type for local copies of the vector header.
41typedef std::array<std::uint8_t, header_size> header;
42
43// @brief Implementation detail. Common data for each accessor type.
44// @tparam Format One of the @ref bsoncxx::v_noabi::vector::formats types, optionally const.
45template <typename Format>
46struct accessor_data {
47 using byte_type = typename std::conditional<std::is_const<Format>::value, std::uint8_t const, std::uint8_t>::type;
48 using byte_count_type = std::uint32_t;
49
50 byte_type* bytes;
51 byte_count_type size;
52 header header_copy;
53
54 // Construct accessor_data around a b_binary that has already had its subtype and size validated.
55 accessor_data(types::b_binary const& binary) : accessor_data{binary.bytes, binary.size} {}
56
57 // Construct accessor_data with an existing header copy
58 accessor_data(byte_type* bytes, byte_count_type size, header header_copy)
59 : bytes{bytes}, size{size}, header_copy{header_copy} {}
60
61 // Construct accessor_data around binary data that has already been validated, and capture a new header copy.
62 accessor_data(byte_type* bytes, byte_count_type size) : bytes{bytes}, size{size} {
63 std::memcpy(header_copy.data(), bytes, header_size);
64 }
65};
66
67// @brief Implementation detail. Default format traits.
68struct format_traits_base {
69 using element_count_type = std::size_t;
70
71 using byte_difference_type = std::ptrdiff_t;
72 using element_difference_type = std::ptrdiff_t;
73
74 using byte_reference = std::uint8_t&;
75 using const_byte_reference = std::uint8_t const&;
76
77 using byte_iterator = std::uint8_t*;
78 using const_byte_iterator = std::uint8_t const*;
79};
80
81// @brief Implementation detail. Format traits, specialized by format.
82// @tparam Format One of the @ref bsoncxx::v_noabi::vector::formats types, without qualifiers.
83template <typename Format>
84struct format_traits;
85
86// @brief Implementation detail. Format traits for bsoncxx::v_noabi::vector::formats::f_int8.
87template <>
88struct format_traits<formats::f_int8> : format_traits_base {
89 using value_type = std::int8_t;
90
91 using reference = std::int8_t&;
92 using const_reference = std::int8_t const&;
93 using iterator = std::int8_t*;
94 using const_iterator = std::int8_t const*;
95
96 static constexpr std::size_t element_count(std::uint32_t binary_data_length, header) noexcept {
97 return binary_data_length - header_size;
98 }
99
100 static byte_iterator make_byte_iterator(iterator element, iterator) noexcept {
101 return byte_iterator(static_cast<void*>(element));
102 }
103
104 static const_byte_iterator make_byte_iterator(const_iterator element, const_iterator) noexcept {
105 return const_byte_iterator(static_cast<void const*>(element));
106 }
107};
108
109// @brief Implementation detail. Format traits for bsoncxx::v_noabi::vector::formats::f_float32.
110template <>
111struct format_traits<formats::f_float32> : format_traits_base {
112 using value_type = float;
113
114 using reference = elements::float32&;
115 using const_reference = elements::float32 const&;
116 using iterator = elements::float32*;
117 using const_iterator = elements::float32 const*;
118
119 static constexpr std::size_t element_count(std::uint32_t binary_data_length, header) noexcept {
120 return (binary_data_length - header_size) / sizeof(float);
121 }
122
123 static byte_iterator make_byte_iterator(iterator element, iterator) noexcept {
124 return byte_iterator(static_cast<void*>(element));
125 }
126
127 static const_byte_iterator make_byte_iterator(const_iterator element, const_iterator) noexcept {
128 return const_byte_iterator(static_cast<void const*>(element));
129 }
130};
131
132// @brief Implementation detail. Format traits for bsoncxx::v_noabi::vector::formats::f_packed_bit.
133template <>
134struct format_traits<formats::f_packed_bit> : format_traits_base {
135 using value_type = bool;
136
137 using iterator = iterators::packed_bit_element<std::uint8_t*>;
138 using const_iterator = iterators::packed_bit_element<std::uint8_t const*>;
139 using reference = iterator::reference;
140 using const_reference = const_iterator::reference;
141
142 using byte_iterator = iterators::packed_bit_byte<std::uint8_t*>;
143 using const_byte_iterator = iterators::packed_bit_byte<std::uint8_t const*>;
144 using byte_reference = byte_iterator::reference;
145 using const_byte_reference = const_byte_iterator::reference;
146
147 using byte_difference_type = byte_iterator::difference_type;
148 using element_difference_type = iterator::difference_type;
149
150 static std::size_t element_count(std::uint32_t binary_data_length, header hdr) noexcept {
151 return std::size_t{binary_data_length - header_size} * std::size_t{8u} - std::size_t{hdr[1] & 7u};
152 }
153
154 static byte_iterator make_byte_iterator(iterator element, iterator element_end) noexcept {
155 return {element, element_end};
156 }
157
158 static constexpr const_byte_iterator make_byte_iterator(
159 const_iterator element,
160 const_iterator element_end) noexcept {
161 return {element, element_end};
162 }
163};
164
165} // namespace detail
166} // namespace vector
167} // namespace v_noabi
168} // namespace bsoncxx
169
171
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
For internal use only!
Declares entities in bsoncxx::v_noabi::vector::elements.
Declares entities in bsoncxx::v_noabi::vector::formats.
Declares entities in bsoncxx::v_noabi::vector::iterators.
Declarations related to the BSON Binary Vector subtype.
Declares entities whose ABI stability is NOT guaranteed.
type
An enumeration of each BSON type.
Definition types.hpp:43
The top-level namespace within which all bsoncxx library entities are declared.
Provides entities used to represent BSON types.