MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
accessor.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 <type_traits>
22
24
29
31
32namespace bsoncxx {
33namespace v_noabi {
34namespace vector {
35
52template <typename Format>
53class accessor {
54 using format_traits = typename detail::format_traits<typename std::remove_cv<Format>::type>;
55
56 public:
58 using format = Format;
59
62 using const_value_type = typename format_traits::value_type const;
63
68 using value_type = typename std::conditional<
69 std::is_const<Format>::value,
70 typename format_traits::value_type const,
71 typename format_traits::value_type>::type;
72
75 using const_reference = typename format_traits::const_reference;
76
82 using reference = typename std::conditional<
83 std::is_const<Format>::value,
84 typename format_traits::const_reference,
85 typename format_traits::reference>::type;
86
89 using const_iterator = typename format_traits::const_iterator;
90
96 using iterator = typename std::conditional<
97 std::is_const<Format>::value,
98 typename format_traits::const_iterator,
99 typename format_traits::iterator>::type;
100
105 using byte_type = typename detail::accessor_data<format>::byte_type;
106
111 using byte_count_type = typename detail::accessor_data<format>::byte_count_type;
112
117 using element_count_type = typename format_traits::element_count_type;
118
123 using byte_difference_type = typename format_traits::byte_difference_type;
124
129 using element_difference_type = typename format_traits::element_difference_type;
130
133 using const_byte_reference = typename format_traits::const_byte_reference;
134
139 using byte_reference = typename std::conditional<
140 std::is_const<Format>::value,
141 typename format_traits::const_byte_reference,
142 typename format_traits::byte_reference>::type;
143
146 using const_byte_iterator = typename format_traits::const_byte_iterator;
147
152 using byte_iterator = typename std::conditional<
153 std::is_const<Format>::value,
154 typename format_traits::const_byte_iterator,
155 typename format_traits::byte_iterator>::type;
156
163 accessor(types::b_binary const& binary) : _data{(format::validate(binary), binary)} {}
164
166 constexpr accessor<format const> as_const() const noexcept {
167 // Erase the template parameter from accessor_data to allow conversion from possibly-not-const to const.
168 return {{_data.bytes, _data.size, _data.header_copy}};
169 }
170
172 constexpr byte_count_type byte_size() const noexcept {
173 return _data.size - byte_count_type(detail::header_size);
174 }
175
177 constexpr element_count_type size() const noexcept {
178 return format_traits::element_count(_data.size, _data.header_copy);
179 }
180
182 constexpr iterator begin() const noexcept {
183 return iterator(_data.bytes + detail::header_size);
184 }
185
187 constexpr iterator end() const noexcept {
188 return begin() + element_difference_type(size());
189 }
190
192 constexpr const_iterator cbegin() const noexcept {
193 return const_iterator(_data.bytes + detail::header_size);
194 }
195
197 constexpr const_iterator cend() const noexcept {
198 return cbegin() + element_difference_type(size());
199 }
200
203 reference front() noexcept {
204 return *begin();
205 }
206
209 constexpr const_reference front() const noexcept {
210 return *begin();
211 }
212
215 reference back() noexcept {
216 return *(begin() + element_difference_type(size() - 1u));
217 }
218
221 constexpr const_reference back() const noexcept {
222 return *(begin() + element_difference_type(size() - 1u));
223 }
224
226 constexpr byte_iterator byte_begin() const noexcept {
227 return format_traits::make_byte_iterator(begin(), end());
228 }
229
231 constexpr byte_iterator byte_end() const noexcept {
233 }
234
236 constexpr const_byte_iterator byte_cbegin() const noexcept {
237 return format_traits::make_byte_iterator(cbegin(), cend());
238 }
239
241 constexpr const_byte_iterator byte_cend() const noexcept {
243 }
244
248 return *byte_begin();
249 }
250
253 constexpr const_byte_reference byte_front() const noexcept {
254 return *byte_begin();
255 }
256
260 return *(byte_begin() + byte_difference_type(byte_size() - 1u));
261 }
262
265 constexpr const_byte_reference byte_back() const noexcept {
266 return *(byte_begin() + byte_difference_type(byte_size() - 1u));
267 }
268
279
290
296 if (index >= size()) {
298 }
299 return *(begin() + element_difference_type(index));
300 }
301
307 if (index >= size()) {
309 }
310 return *(begin() + element_difference_type(index));
311 }
312
317 return *(begin() + element_difference_type(index));
318 }
319
323 constexpr const_reference operator[](element_count_type index) const noexcept {
324 return *(begin() + element_difference_type(index));
325 }
326
328 constexpr bool empty() const noexcept {
329 return size() == 0u;
330 }
331
332 private:
334 friend class accessor<typename std::remove_const<format>::type>;
335
336 accessor(detail::accessor_data<format> data) noexcept : _data{data} {}
337
338 detail::accessor_data<format> _data;
339};
340
341} // namespace vector
342} // namespace v_noabi
343} // namespace bsoncxx
344
346
Declares bsoncxx::v_noabi::vector::accessor.
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
Provides bsoncxx::v_noabi::error_code.
Provides bsoncxx::v_noabi::exception.
Represents a BSON Binary element being constructed during an append operation.
Definition sub_binary.hpp:40
Base class for all exceptions thrown by the bsoncxx library unless otherwise specified.
Definition exception.hpp:33
Accessor for the contents of a valid BSON Binary Vector.
Definition accessor.hpp:53
const_reference at(element_count_type index) const
Obtain a const reference to a numbered element, with bounds checking.
Definition accessor.hpp:306
accessor(types::b_binary const &binary)
Construct a const vector accessor by validating a bsoncxx::v_noabi::types::b_binary reference.
Definition accessor.hpp:163
typename format_traits::const_iterator const_iterator
Iterator for const-qualified vector elements.
Definition accessor.hpp:89
reference at(element_count_type index)
Obtain a reference to a numbered element, with bounds checking.
Definition accessor.hpp:295
byte_reference byte_at(byte_count_type index)
Obtain a reference to a numbered byte, with bounds checking.
Definition accessor.hpp:273
constexpr iterator begin() const noexcept
Obtain a per-element iterator pointing to the beginning of the vector.
Definition accessor.hpp:182
constexpr const_byte_reference byte_front() const noexcept
Obtain a const reference to the first byte.
Definition accessor.hpp:253
Format format
The type from bsoncxx::v_noabi::vector::formats representing this vector's layout and element type.
Definition accessor.hpp:58
constexpr const_byte_reference byte_back() const noexcept
Obtain a const reference to the last byte.
Definition accessor.hpp:265
typename format_traits::const_byte_reference const_byte_reference
Type for referencing const-qualified vector bytes in-place.
Definition accessor.hpp:133
typename std::conditional< std::is_const< Format >::value, typename format_traits::value_type const, typename format_traits::value_type >::type value_type
A type suitable for holding element values.
Definition accessor.hpp:68
constexpr byte_count_type byte_size() const noexcept
Count the bytes of element data, not including any headers.
Definition accessor.hpp:172
byte_reference byte_back() noexcept
Obtain a reference to the last byte.
Definition accessor.hpp:259
constexpr byte_iterator byte_begin() const noexcept
Obtain a per-byte iterator pointing to the beginning of the vector.
Definition accessor.hpp:226
constexpr const_byte_iterator byte_cbegin() const noexcept
Obtain a const per-byte iterator pointing to the beginning of the vector.
Definition accessor.hpp:236
constexpr const_reference operator[](element_count_type index) const noexcept
Obtain a const reference to a numbered element, without bounds checking.
Definition accessor.hpp:323
const_byte_reference byte_at(byte_count_type index) const
Obtain a const reference to a numbered byte, with bounds checking.
Definition accessor.hpp:284
typename std::conditional< std::is_const< Format >::value, typename format_traits::const_byte_reference, typename format_traits::byte_reference >::type byte_reference
Type for referencing vector bytes in-place.
Definition accessor.hpp:139
typename std::conditional< std::is_const< Format >::value, typename format_traits::const_reference, typename format_traits::reference >::type reference
Type for referencing vector elements in-place.
Definition accessor.hpp:82
constexpr bool empty() const noexcept
Test whether the vector is empty.
Definition accessor.hpp:328
reference operator[](element_count_type index) noexcept
Obtain a reference to a numbered element, without bounds checking.
Definition accessor.hpp:316
constexpr const_reference back() const noexcept
Obtain a const reference to the last element.
Definition accessor.hpp:221
constexpr const_iterator cend() const noexcept
Obtain a const per-element end iterator.
Definition accessor.hpp:197
constexpr element_count_type size() const noexcept
Count the number of elements.
Definition accessor.hpp:177
typename format_traits::value_type const const_value_type
Const qualified version of value_type.
Definition accessor.hpp:62
typename format_traits::element_count_type element_count_type
Type for element counts.
Definition accessor.hpp:117
constexpr const_reference front() const noexcept
Obtain a const reference to the first element.
Definition accessor.hpp:209
typename detail::accessor_data< format >::byte_count_type byte_count_type
Type for byte counts.
Definition accessor.hpp:111
typename std::conditional< std::is_const< Format >::value, typename format_traits::const_iterator, typename format_traits::iterator >::type iterator
Element iterator type.
Definition accessor.hpp:96
typename format_traits::byte_difference_type byte_difference_type
Type for signed differences between byte iterators.
Definition accessor.hpp:123
typename format_traits::const_byte_iterator const_byte_iterator
Iterator for const-qualified vector bytes.
Definition accessor.hpp:146
byte_reference byte_front() noexcept
Obtain a reference to the first byte.
Definition accessor.hpp:247
constexpr const_iterator cbegin() const noexcept
Obtain a const per-element iterator pointing to the beginning of the vector.
Definition accessor.hpp:192
constexpr byte_iterator byte_end() const noexcept
Obtain a per-byte end iterator.
Definition accessor.hpp:231
reference back() noexcept
Obtain a reference to the last element.
Definition accessor.hpp:215
typename detail::accessor_data< format >::byte_type byte_type
Type for the underlying byte data.
Definition accessor.hpp:105
constexpr accessor< format const > as_const() const noexcept
Obtain a const version of this vector accessor, without re-validating the vector data.
Definition accessor.hpp:166
typename format_traits::element_difference_type element_difference_type
Type for signed differences between element iterators.
Definition accessor.hpp:129
typename std::conditional< std::is_const< Format >::value, typename format_traits::const_byte_iterator, typename format_traits::byte_iterator >::type byte_iterator
Byte iterator type.
Definition accessor.hpp:152
constexpr const_byte_iterator byte_cend() const noexcept
Obtain a const per-byte end iterator.
Definition accessor.hpp:241
typename format_traits::const_reference const_reference
Type for referencing const-qualified vector elements in-place.
Definition accessor.hpp:75
constexpr iterator end() const noexcept
Obtain a per-element end iterator.
Definition accessor.hpp:187
reference front() noexcept
Obtain a reference to the first element.
Definition accessor.hpp:203
For internal use only!
Declares entities in bsoncxx::v_noabi::vector::formats.
Declarations related to the BSON Binary Vector subtype.
Declares entities whose ABI stability is NOT guaranteed.
@ k_vector_out_of_range
Attempted out-of-range access to a BSON Binary Vector element.
Definition error_code.hpp:153
type
An enumeration of each BSON type.
Definition types.hpp:43
stdx::optional< document::view > validate(std::uint8_t const *data, std::size_t length)
Validates a BSON document. This is a simplified overload that will only do the bare minimum validatio...
The top-level namespace within which all bsoncxx library entities are declared.
A BSON binary data value.
Definition types.hpp:227
Declares bsoncxx::v_noabi::builder::basic::sub_binary.