Loading...
Searching...
No Matches
view.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/v1/document/view-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/detail/prelude.hpp>
#include <bsoncxx/v1/config/export.hpp>
#include <bsoncxx/v1/detail/bit.hpp>
#include <bsoncxx/v1/element/view.hpp> // IWYU pragma: export
#include <bsoncxx/v1/stdx/string_view.hpp>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <system_error>
#include <type_traits>
namespace bsoncxx {
namespace v1 {
namespace document {
class view {
public:
class const_iterator;
using iterator = const_iterator;
private:
enum : std::size_t { _empty_length = 5u };
std::uint8_t const* _data;
template <detail::endian e = detail::endian::native>
std::int32_t raw_size() const;
public:
BSONCXX_ABI_EXPORT_CDECL() view();
explicit view(std::uint8_t const* data) : _data{data} {}
BSONCXX_ABI_EXPORT view(std::uint8_t const* data, std::size_t length);
std::uint8_t const* data() const {
return _data;
}
std::size_t size() const;
std::size_t length() const {
return this->size();
}
bool empty() const {
return this->size() == _empty_length && _data[4] == 0u;
}
explicit operator bool() const {
return this->size() >= _empty_length;
}
BSONCXX_ABI_EXPORT_CDECL(const_iterator) cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;
BSONCXX_ABI_EXPORT_CDECL(const_iterator) find(v1::stdx::string_view key) const;
v1::element::view operator[](v1::stdx::string_view key) const;
friend bool operator==(view lhs, view rhs) {
if (!lhs != !rhs) {
return false;
}
return !lhs || (lhs.length() == rhs.length() && std::memcmp(lhs.data(), rhs.data(), lhs.length()) == 0);
}
friend bool operator!=(view lhs, view rhs) {
return !(lhs == rhs);
}
enum class errc {
zero,
invalid_length,
invalid_data,
};
static BSONCXX_ABI_EXPORT_CDECL(std::error_category const&) error_category();
friend std::error_code make_error_code(errc v) {
return {static_cast<int>(v), error_category()};
}
};
template <>
inline std::int32_t view::raw_size<detail::endian::little>() const {
std::int32_t res;
std::memcpy(&res, _data, sizeof(res));
return res;
}
template <>
inline std::int32_t view::raw_size<detail::endian::big>() const {
std::int32_t res;
auto const bytes = reinterpret_cast<unsigned char*>(&res);
bytes[0] = _data[3];
bytes[1] = _data[2];
bytes[2] = _data[1];
bytes[3] = _data[0];
return res;
}
inline std::size_t view::size() const {
return _data ? static_cast<std::uint32_t>(this->raw_size()) : 0u;
}
class view::const_iterator {
private:
v1::element::view _element;
public:
using iterator_category = std::input_iterator_tag;
using value_type = v1::element::view;
using difference_type = std::ptrdiff_t;
using pointer = value_type const*;
using reference = value_type;
const_iterator() = default;
reference operator*() const {
return _element;
}
pointer operator->() const {
return &_element;
}
BSONCXX_ABI_EXPORT_CDECL(const_iterator&) operator++();
const_iterator operator++(int) {
const_iterator tmp = *this;
this->operator++();
return tmp;
}
friend bool operator==(const_iterator const& lhs, const_iterator const& rhs) {
if (!lhs._element != !rhs._element) {
return false;
}
return !lhs._element ||
(lhs._element.raw() == rhs._element.raw() && lhs._element.offset() == rhs._element.offset());
}
friend bool operator!=(const_iterator const& lhs, const_iterator const& rhs) {
return !(lhs == rhs);
}
class internal;
private:
explicit BSONCXX_ABI_EXPORT_CDECL() const_iterator(v1::element::view element);
};
inline view::const_iterator view::cend() const {
return {};
}
inline view::const_iterator view::begin() const {
return this->cbegin();
}
inline view::const_iterator view::end() const {
return this->cend();
}
inline v1::element::view view::operator[](v1::stdx::string_view key) const {
return *(this->find(key));
}
} // namespace document
} // namespace v1
} // namespace bsoncxx
template <>
struct std::is_error_code_enum<bsoncxx::v1::document::view::errc> : true_type {};
#include <bsoncxx/v1/detail/postlude.hpp>
bit.hpp
For internal use only!
export.hpp
Provides macros to control the set of symbols exported in the ABI.
BSONCXX_ABI_EXPORT
#define BSONCXX_ABI_EXPORT
Exports the associated entity as part of the ABI.
Definition export.hpp:15
BSONCXX_ABI_EXPORT_CDECL
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
postlude.hpp
The bsoncxx v1 macro guard postlude header.
prelude.hpp
The bsoncxx v1 macro guard prelude header.
bsoncxx::v1::document::view::const_iterator
A const iterator over the elements of a view.
Definition view.hpp:264
bsoncxx::v1::document::view::const_iterator::operator++
const_iterator operator++(int)
Post-increment this iterator.
Definition view.hpp:329
bsoncxx::v1::document::view::const_iterator::operator!=
friend bool operator!=(const_iterator const &lhs, const_iterator const &rhs)
Equivalent to !(lhs == rhs).
Definition view.hpp:353
bsoncxx::v1::document::view::const_iterator::const_iterator
const_iterator()=default
Initialize as an end iterator.
bsoncxx::v1::document::view::const_iterator::operator==
friend bool operator==(const_iterator const &lhs, const_iterator const &rhs)
Compare equal when lhs and rhs point to the same element within the same range of BSON bytes.
Definition view.hpp:341
bsoncxx::v1::document::view::const_iterator::operator++
const_iterator & operator++()
Pre-increment this iterator.
bsoncxx::v1::document::view::const_iterator::value_type
v1::element::view value_type
Provide std::iterator_traits<T>::value_type.
Definition view.hpp:277
bsoncxx::v1::document::view::const_iterator::operator->
pointer operator->() const
Access the current element.
Definition view.hpp:313
bsoncxx::v1::document::view::const_iterator::reference
value_type reference
Provide std::iterator_traits<T>::reference.
Definition view.hpp:292
bsoncxx::v1::document::view::const_iterator::difference_type
std::ptrdiff_t difference_type
Provide std::iterator_traits<T>::difference_type.
Definition view.hpp:282
bsoncxx::v1::document::view::const_iterator::pointer
value_type const * pointer
Provide std::iterator_traits<T>::pointer.
Definition view.hpp:287
bsoncxx::v1::document::view::const_iterator::operator*
reference operator*() const
Return the current element.
Definition view.hpp:304
bsoncxx::v1::document::view::const_iterator::iterator_category
std::input_iterator_tag iterator_category
Provide std::iterator_traits<T>::iterator_category.
Definition view.hpp:272
bsoncxx::v1::document::view::view
view()
Initialize as an empty view.
bsoncxx::v1::document::view::iterator
const_iterator iterator
Equivalent to const_iterator.
Definition view.hpp:59
bsoncxx::v1::document::view::operator!=
friend bool operator!=(view lhs, view rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition view.hpp:207
bsoncxx::v1::document::view::operator==
friend bool operator==(view lhs, view rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition view.hpp:199
bsoncxx::v1::document::view::errc
errc
Errors codes which may be returned by bsoncxx::v1::document::view.
Definition view.hpp:216
bsoncxx::v1::document::view::errc::invalid_length
@ invalid_length
Length is invalid.
Definition view.hpp:218
bsoncxx::v1::document::view::errc::zero
@ zero
Zero.
Definition view.hpp:217
bsoncxx::v1::document::view::errc::invalid_data
@ invalid_data
Data is invalid.
Definition view.hpp:219
bsoncxx::v1::document::view::operator[]
v1::element::view operator[](v1::stdx::string_view key) const
Return the first element within the represented BSON document whose key compares equal to key.
Definition view.hpp:375
bsoncxx::v1::document::view::empty
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition view.hpp:132
bsoncxx::v1::document::view::length
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition view.hpp:119
bsoncxx::v1::document::view::make_error_code
friend std::error_code make_error_code(errc v)
Support implicit conversion to std::error_code.
Definition view.hpp:230
bsoncxx::v1::document::view::view
view(std::uint8_t const *data, std::size_t length)
Equivalent to view(std::uint8_t const* data), but validates the embedded length against length.
bsoncxx::v1::document::view::begin
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:367
bsoncxx::v1::document::view::data
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition view.hpp:104
bsoncxx::v1::document::view::cend
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:363
bsoncxx::v1::document::view::end
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:371
bsoncxx::v1::document::view::size
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition view.hpp:253
bsoncxx::v1::document::view::find
const_iterator find(v1::stdx::string_view key) const
Return a const iterator to the element within the represented BSON document whose key compares equal ...
bsoncxx::v1::document::view::error_category
static std::error_category const & error_category()
The error category for bsoncxx::v1::document::view::errc.
bsoncxx::v1::document::view::cbegin
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
bsoncxx::v1::element::view
A non-owning, read-only BSON element.
Definition view.hpp:82
bsoncxx::v1::element::view::raw
std::uint8_t const * raw() const
Return the "raw" component of the underlying BSON bytes.
bsoncxx::v1::element::view::offset
std::uint32_t offset() const
Return the "offset" component of the underlying BSON bytes.
bsoncxx::v1::stdx::string_view
A polyfill for std::string_view.
Definition string_view.hpp:412
bsoncxx::v1::document
Declares entities representing a BSON document.
bsoncxx::v1::element
Declares entities representing a BSON element.
bsoncxx::v1
Declares entities whose ABI stability is guaranteed for documented symbols.
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
view-fwd.hpp
Declares bsoncxx::v1::document::view.
view.hpp
Provides bsoncxx::v1::element::view.
string_view.hpp
Provides std::string_view-related polyfills for library API usage.