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/document/view-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/document/view.hpp> // IWYU pragma: export
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <bsoncxx/document/element.hpp>
#include <bsoncxx/stdx/string_view.hpp>
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
namespace document {
class view {
private:
v1::document::view _view;
std::size_t _length;
public:
class const_iterator;
using iterator = const_iterator;
view() : _view{}, _length{_view.length()} {}
/* explicit(false) */ view(v1::document::view const& v) : _view{v}, _length{v.length()} {}
view(std::uint8_t const* data, std::size_t length) : _view{data}, _length{length} {}
explicit operator v1::document::view() const {
return _view;
}
BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(const_iterator) cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;
BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(const_iterator) find(v1::stdx::string_view key) const;
v_noabi::document::element operator[](v1::stdx::string_view key) const;
std::uint8_t const* data() const {
return _view.data();
}
std::size_t size() const {
return _length; // Do NOT use _view.size().
}
std::size_t length() const {
return _length; // Do NOT use _view.length().
}
bool empty() const {
return _length == 5u; // Do NOT use _view.empty().
}
friend bool operator==(view lhs, view rhs) {
return (lhs._length == rhs._length) && (std::memcmp(lhs.data(), rhs.data(), lhs._length) == 0);
}
friend bool operator!=(view lhs, view rhs) {
return !(lhs == rhs);
}
};
class view::const_iterator {
private:
v_noabi::document::element _element;
public:
using value_type = element;
using reference = element&;
using pointer = element*;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
const_iterator() = default;
explicit const_iterator(v_noabi::document::element const& element) : _element(element) {}
reference operator*() {
return _element;
}
pointer operator->() {
return &_element;
}
BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(const_iterator&) operator++();
const_iterator operator++(int) {
const_iterator before(*this);
operator++();
return before;
}
friend bool operator==(const_iterator const& lhs, const_iterator const& rhs) {
return 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);
}
};
inline v_noabi::document::element view::operator[](v1::stdx::string_view key) const {
return *(this->find(key));
}
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();
}
} // namespace document
} // namespace v_noabi
} // namespace bsoncxx
namespace bsoncxx {
namespace v_noabi {
inline v_noabi::document::view from_v1(v1::document::view const& v) {
return {v};
}
inline v1::document::view to_v1(v_noabi::document::view const& v) {
return v1::document::view{v};
}
} // namespace v_noabi
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
BSONCXX_ABI_EXPORT_CDECL_UNSTABLE
#define BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(...)
Equivalent to BSONCXX_ABI_EXPORT_UNSTABLE with BSONCXX_ABI_CDECL.
Definition config.hpp:28
postlude.hpp
The bsoncxx v_noabi macro guard postlude header.
prelude.hpp
The bsoncxx v_noabi macro guard prelude header.
bsoncxx::document::view::const_iterator
A const iterator over the contents of a document view.
Definition view.hpp:205
bsoncxx::document::view::view
view()
Default constructs a view.
Definition view.hpp:61
bsoncxx::v1::document::view
A non-owning, read-only BSON document.
Definition view.hpp:52
bsoncxx::v1::stdx::string_view
A polyfill for std::string_view.
Definition string_view.hpp:412
bsoncxx::v_noabi::document::element
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:52
bsoncxx::v_noabi::document::element::raw
std::uint8_t const * raw() const
Getter for the raw bson bytes the element points to.
Definition element.hpp:90
bsoncxx::v_noabi::document::element::offset
std::uint32_t offset() const
Getter for the offset into the raw bson bytes the element points to.
Definition element.hpp:108
bsoncxx::v_noabi::document::view::const_iterator
A const iterator over the contents of a document view.
Definition view.hpp:205
bsoncxx::v_noabi::document::view::const_iterator::value_type
element value_type
std::iterator_traits
Definition view.hpp:213
bsoncxx::v_noabi::document::view::const_iterator::operator!=
friend bool operator!=(const_iterator const &lhs, const_iterator const &rhs)
Compares two const_iterators for (in)-equality.
Definition view.hpp:249
bsoncxx::v_noabi::document::view::const_iterator::operator==
friend bool operator==(const_iterator const &lhs, const_iterator const &rhs)
Compares two const_iterators for (in)-equality.
Definition view.hpp:245
bsoncxx::v_noabi::document::view
A read-only, non-owning view of a BSON document.
Definition view.hpp:40
bsoncxx::v_noabi::document::view::find
const_iterator find(v1::stdx::string_view key) const
Finds the first element of the document with the provided key. If there is no such element,...
bsoncxx::v_noabi::document::view::cend
const_iterator cend() const
Definition view.hpp:260
bsoncxx::v_noabi::document::view::empty
bool empty() const
Return true when this->length() == 5.
Definition view.hpp:178
bsoncxx::v_noabi::document::view::data
std::uint8_t const * data() const
Access the raw bytes of the underlying document.
Definition view.hpp:152
bsoncxx::v_noabi::document::view::begin
const_iterator begin() const
Definition view.hpp:264
bsoncxx::v_noabi::document::view::view
view(v1::document::view const &v)
Construct with the bsoncxx::v1 equivalent.
Definition view.hpp:66
bsoncxx::v_noabi::document::view::operator[]
v_noabi::document::element operator[](v1::stdx::string_view key) const
Finds the first element of the document with the provided key. If there is no such element,...
Definition view.hpp:256
bsoncxx::v_noabi::document::view::view
view(std::uint8_t const *data, std::size_t length)
Constructs a view from a buffer. The caller is responsible for ensuring that the lifetime of the resu...
Definition view.hpp:81
bsoncxx::v_noabi::document::view::operator!=
friend bool operator!=(view lhs, view rhs)
Compare two document views for (in)-equality.
Definition view.hpp:192
bsoncxx::v_noabi::document::view::operator==
friend bool operator==(view lhs, view rhs)
Compare two document views for (in)-equality.
Definition view.hpp:188
bsoncxx::v_noabi::document::view::size
std::size_t size() const
Gets the length of the underlying buffer in bytes.
Definition view.hpp:163
bsoncxx::v_noabi::document::view::length
std::size_t length() const
Gets the length of the underlying buffer in bytes.
Definition view.hpp:168
bsoncxx::v_noabi::document::view::view
view()
Default constructs a view.
Definition view.hpp:61
bsoncxx::v_noabi::document::view::end
const_iterator end() const
Definition view.hpp:268
bsoncxx::v_noabi::document::view::cbegin
const_iterator cbegin() const
bsoncxx::v_noabi::document::view::iterator
const_iterator iterator
Equivalent to const_iterator.
Definition view.hpp:51
element.hpp
Provides bsoncxx::v_noabi::document::element.
bsoncxx::v_noabi::document
Declares entities representing a BSON document.
bsoncxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
bsoncxx::v_noabi::to_v1
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
bsoncxx::v_noabi::from_v1
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:263
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
view.hpp
Provides bsoncxx::v1::document::view.
view-fwd.hpp
Declares bsoncxx::v_noabi::document::view.
string_view.hpp
Provides std::string_view-related polyfills for library API usage.