Loading...
Searching...
No Matches
accessor.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/accessor-fwd.hpp> // IWYU pragma: export
//
#include <type_traits>
#include <bsoncxx/builder/basic/sub_binary-fwd.hpp>
#include <bsoncxx/exception/error_code.hpp>
#include <bsoncxx/exception/exception.hpp>
#include <bsoncxx/vector/detail.hpp>
#include <bsoncxx/vector/formats.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
namespace vector {
template <typename Format>
class accessor {
using format_traits = typename detail::format_traits<typename std::remove_cv<Format>::type>;
public:
using format = Format;
using const_value_type = typename format_traits::value_type const;
using value_type = typename std::conditional<
std::is_const<Format>::value,
typename format_traits::value_type const,
typename format_traits::value_type>::type;
using const_reference = typename format_traits::const_reference;
using reference = typename std::conditional<
std::is_const<Format>::value,
typename format_traits::const_reference,
typename format_traits::reference>::type;
using const_iterator = typename format_traits::const_iterator;
using iterator = typename std::conditional<
std::is_const<Format>::value,
typename format_traits::const_iterator,
typename format_traits::iterator>::type;
using byte_type = typename detail::accessor_data<format>::byte_type;
using byte_count_type = typename detail::accessor_data<format>::byte_count_type;
using element_count_type = typename format_traits::element_count_type;
using byte_difference_type = typename format_traits::byte_difference_type;
using element_difference_type = typename format_traits::element_difference_type;
using const_byte_reference = typename format_traits::const_byte_reference;
using byte_reference = typename std::conditional<
std::is_const<Format>::value,
typename format_traits::const_byte_reference,
typename format_traits::byte_reference>::type;
using const_byte_iterator = typename format_traits::const_byte_iterator;
using byte_iterator = typename std::conditional<
std::is_const<Format>::value,
typename format_traits::const_byte_iterator,
typename format_traits::byte_iterator>::type;
accessor(types::b_binary const& binary) : _data{(format::validate(binary), binary)} {}
constexpr accessor<format const> as_const() const noexcept {
// Erase the template parameter from accessor_data to allow conversion from possibly-not-const to const.
return {{_data.bytes, _data.size, _data.header_copy}};
}
constexpr byte_count_type byte_size() const noexcept {
return _data.size - byte_count_type(detail::header_size);
}
constexpr element_count_type size() const noexcept {
return format_traits::element_count(_data.size, _data.header_copy);
}
constexpr iterator begin() const noexcept {
return iterator(_data.bytes + detail::header_size);
}
constexpr iterator end() const noexcept {
return begin() + element_difference_type(size());
}
constexpr const_iterator cbegin() const noexcept {
return const_iterator(_data.bytes + detail::header_size);
}
constexpr const_iterator cend() const noexcept {
return cbegin() + element_difference_type(size());
}
reference front() noexcept {
return *begin();
}
constexpr const_reference front() const noexcept {
return *begin();
}
reference back() noexcept {
return *(begin() + element_difference_type(size() - 1u));
}
constexpr const_reference back() const noexcept {
return *(begin() + element_difference_type(size() - 1u));
}
constexpr byte_iterator byte_begin() const noexcept {
return format_traits::make_byte_iterator(begin(), end());
}
constexpr byte_iterator byte_end() const noexcept {
return byte_begin() + byte_difference_type(byte_size());
}
constexpr const_byte_iterator byte_cbegin() const noexcept {
return format_traits::make_byte_iterator(cbegin(), cend());
}
constexpr const_byte_iterator byte_cend() const noexcept {
return byte_cbegin() + byte_difference_type(byte_size());
}
byte_reference byte_front() noexcept {
return *byte_begin();
}
constexpr const_byte_reference byte_front() const noexcept {
return *byte_begin();
}
byte_reference byte_back() noexcept {
return *(byte_begin() + byte_difference_type(byte_size() - 1u));
}
constexpr const_byte_reference byte_back() const noexcept {
return *(byte_begin() + byte_difference_type(byte_size() - 1u));
}
byte_reference byte_at(byte_count_type index) {
if (index >= byte_size()) {
throw v_noabi::exception{v_noabi::error_code::k_vector_out_of_range};
}
return *(byte_begin() + byte_difference_type(index));
}
const_byte_reference byte_at(byte_count_type index) const {
if (index >= byte_size()) {
throw v_noabi::exception{v_noabi::error_code::k_vector_out_of_range};
}
return *(byte_begin() + byte_difference_type(index));
}
reference at(element_count_type index) {
if (index >= size()) {
throw v_noabi::exception{v_noabi::error_code::k_vector_out_of_range};
}
return *(begin() + element_difference_type(index));
}
const_reference at(element_count_type index) const {
if (index >= size()) {
throw v_noabi::exception{v_noabi::error_code::k_vector_out_of_range};
}
return *(begin() + element_difference_type(index));
}
reference operator[](element_count_type index) noexcept {
return *(begin() + element_difference_type(index));
}
constexpr const_reference operator[](element_count_type index) const noexcept {
return *(begin() + element_difference_type(index));
}
constexpr bool empty() const noexcept {
return size() == 0u;
}
private:
friend class v_noabi::builder::basic::sub_binary;
friend class accessor<typename std::remove_const<format>::type>;
accessor(detail::accessor_data<format> data) noexcept : _data{data} {}
detail::accessor_data<format> _data;
};
} // namespace vector
} // namespace v_noabi
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
accessor-fwd.hpp
Declares bsoncxx::v_noabi::vector::accessor.
postlude.hpp
The bsoncxx v_noabi macro guard postlude header.
prelude.hpp
The bsoncxx v_noabi macro guard prelude header.
error_code.hpp
Provides bsoncxx::v_noabi::error_code.
exception.hpp
Provides bsoncxx::v_noabi::exception.
bsoncxx::v_noabi::builder::basic::sub_binary
Represents a BSON Binary element being constructed during an append operation.
Definition sub_binary.hpp:40
bsoncxx::v_noabi::exception
Base class for all exceptions thrown by the bsoncxx library unless otherwise specified.
Definition exception.hpp:37
bsoncxx::v_noabi::vector::accessor
Accessor for the contents of a valid BSON Binary Vector.
Definition accessor.hpp:53
bsoncxx::v_noabi::vector::accessor::at
const_reference at(element_count_type index) const
Obtain a const reference to a numbered element, with bounds checking.
Definition accessor.hpp:306
bsoncxx::v_noabi::vector::accessor::accessor
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
bsoncxx::v_noabi::vector::accessor::const_iterator
typename format_traits::const_iterator const_iterator
Iterator for const-qualified vector elements.
Definition accessor.hpp:89
bsoncxx::v_noabi::vector::accessor::at
reference at(element_count_type index)
Obtain a reference to a numbered element, with bounds checking.
Definition accessor.hpp:295
bsoncxx::v_noabi::vector::accessor::byte_at
byte_reference byte_at(byte_count_type index)
Obtain a reference to a numbered byte, with bounds checking.
Definition accessor.hpp:273
bsoncxx::v_noabi::vector::accessor::begin
constexpr iterator begin() const noexcept
Obtain a per-element iterator pointing to the beginning of the vector.
Definition accessor.hpp:182
bsoncxx::v_noabi::vector::accessor::byte_front
constexpr const_byte_reference byte_front() const noexcept
Obtain a const reference to the first byte.
Definition accessor.hpp:253
bsoncxx::v_noabi::vector::accessor::format
Format format
The type from bsoncxx::v_noabi::vector::formats representing this vector's layout and element type.
Definition accessor.hpp:58
bsoncxx::v_noabi::vector::accessor::byte_back
constexpr const_byte_reference byte_back() const noexcept
Obtain a const reference to the last byte.
Definition accessor.hpp:265
bsoncxx::v_noabi::vector::accessor::const_byte_reference
typename format_traits::const_byte_reference const_byte_reference
Type for referencing const-qualified vector bytes in-place.
Definition accessor.hpp:133
bsoncxx::v_noabi::vector::accessor::value_type
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
bsoncxx::v_noabi::vector::accessor::byte_size
constexpr byte_count_type byte_size() const noexcept
Count the bytes of element data, not including any headers.
Definition accessor.hpp:172
bsoncxx::v_noabi::vector::accessor::byte_back
byte_reference byte_back() noexcept
Obtain a reference to the last byte.
Definition accessor.hpp:259
bsoncxx::v_noabi::vector::accessor::byte_begin
constexpr byte_iterator byte_begin() const noexcept
Obtain a per-byte iterator pointing to the beginning of the vector.
Definition accessor.hpp:226
bsoncxx::v_noabi::vector::accessor::byte_cbegin
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
bsoncxx::v_noabi::vector::accessor::operator[]
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
bsoncxx::v_noabi::vector::accessor::byte_at
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
bsoncxx::v_noabi::vector::accessor::byte_reference
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
bsoncxx::v_noabi::vector::accessor::reference
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
bsoncxx::v_noabi::vector::accessor::empty
constexpr bool empty() const noexcept
Test whether the vector is empty.
Definition accessor.hpp:328
bsoncxx::v_noabi::vector::accessor::operator[]
reference operator[](element_count_type index) noexcept
Obtain a reference to a numbered element, without bounds checking.
Definition accessor.hpp:316
bsoncxx::v_noabi::vector::accessor::back
constexpr const_reference back() const noexcept
Obtain a const reference to the last element.
Definition accessor.hpp:221
bsoncxx::v_noabi::vector::accessor::cend
constexpr const_iterator cend() const noexcept
Obtain a const per-element end iterator.
Definition accessor.hpp:197
bsoncxx::v_noabi::vector::accessor::size
constexpr element_count_type size() const noexcept
Count the number of elements.
Definition accessor.hpp:177
bsoncxx::v_noabi::vector::accessor::const_value_type
typename format_traits::value_type const const_value_type
Const qualified version of value_type.
Definition accessor.hpp:62
bsoncxx::v_noabi::vector::accessor::element_count_type
typename format_traits::element_count_type element_count_type
Type for element counts.
Definition accessor.hpp:117
bsoncxx::v_noabi::vector::accessor::front
constexpr const_reference front() const noexcept
Obtain a const reference to the first element.
Definition accessor.hpp:209
bsoncxx::v_noabi::vector::accessor::byte_count_type
typename detail::accessor_data< format >::byte_count_type byte_count_type
Type for byte counts.
Definition accessor.hpp:111
bsoncxx::v_noabi::vector::accessor::iterator
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
bsoncxx::v_noabi::vector::accessor::byte_difference_type
typename format_traits::byte_difference_type byte_difference_type
Type for signed differences between byte iterators.
Definition accessor.hpp:123
bsoncxx::v_noabi::vector::accessor::const_byte_iterator
typename format_traits::const_byte_iterator const_byte_iterator
Iterator for const-qualified vector bytes.
Definition accessor.hpp:146
bsoncxx::v_noabi::vector::accessor::byte_front
byte_reference byte_front() noexcept
Obtain a reference to the first byte.
Definition accessor.hpp:247
bsoncxx::v_noabi::vector::accessor::cbegin
constexpr const_iterator cbegin() const noexcept
Obtain a const per-element iterator pointing to the beginning of the vector.
Definition accessor.hpp:192
bsoncxx::v_noabi::vector::accessor::byte_end
constexpr byte_iterator byte_end() const noexcept
Obtain a per-byte end iterator.
Definition accessor.hpp:231
bsoncxx::v_noabi::vector::accessor::back
reference back() noexcept
Obtain a reference to the last element.
Definition accessor.hpp:215
bsoncxx::v_noabi::vector::accessor::byte_type
typename detail::accessor_data< format >::byte_type byte_type
Type for the underlying byte data.
Definition accessor.hpp:105
bsoncxx::v_noabi::vector::accessor::as_const
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
bsoncxx::v_noabi::vector::accessor::element_difference_type
typename format_traits::element_difference_type element_difference_type
Type for signed differences between element iterators.
Definition accessor.hpp:129
bsoncxx::v_noabi::vector::accessor::byte_iterator
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
bsoncxx::v_noabi::vector::accessor::byte_cend
constexpr const_byte_iterator byte_cend() const noexcept
Obtain a const per-byte end iterator.
Definition accessor.hpp:241
bsoncxx::v_noabi::vector::accessor::const_reference
typename format_traits::const_reference const_reference
Type for referencing const-qualified vector elements in-place.
Definition accessor.hpp:75
bsoncxx::v_noabi::vector::accessor::end
constexpr iterator end() const noexcept
Obtain a per-element end iterator.
Definition accessor.hpp:187
bsoncxx::v_noabi::vector::accessor::front
reference front() noexcept
Obtain a reference to the first element.
Definition accessor.hpp:203
detail.hpp
For internal use only!
formats.hpp
Declares entities in bsoncxx::v_noabi::vector::formats.
bsoncxx::v_noabi::vector
Declarations related to the BSON Binary Vector subtype.
bsoncxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
bsoncxx::v_noabi::error_code::k_vector_out_of_range
@ k_vector_out_of_range
Attempted out-of-range access to a BSON Binary Vector element.
Definition error_code.hpp:157
bsoncxx::v_noabi::type
type
An enumeration of each BSON type.
Definition types.hpp:47
bsoncxx::v_noabi::validate
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...
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
bsoncxx::v_noabi::types::b_binary
A BSON binary data value.
Definition types.hpp:268
sub_binary-fwd.hpp
Declares bsoncxx::v_noabi::builder::basic::sub_binary.