Loading...
Searching...
No Matches
value.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/value-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/detail/prelude.hpp>
#include <bsoncxx/v1/config/export.hpp>
#include <bsoncxx/v1/detail/type_traits.hpp>
#include <bsoncxx/v1/document/view.hpp> // IWYU pragma: export
#include <bsoncxx/v1/element/view.hpp> // IWYU pragma: export
#include <bsoncxx/v1/stdx/string_view.hpp>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
namespace bsoncxx {
namespace v1 {
namespace document {
class value {
public:
using deleter_type = std::function<void BSONCXX_ABI_CDECL(std::uint8_t*)>;
using default_deleter_type = std::default_delete<std::uint8_t[]>;
using noop_deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
static BSONCXX_ABI_EXPORT_CDECL(void) noop_deleter(std::uint8_t*);
using unique_ptr_type = std::unique_ptr<std::uint8_t[], deleter_type>;
private:
unique_ptr_type _data;
template <typename T>
using is_value = detail::is_alike<T, value>;
template <typename T>
using to_bson_expr = decltype(to_bson(std::declval<T const&>(), std::declval<value&>()));
template <typename T>
struct has_to_bson : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<to_bson_expr, T>> {};
template <typename T>
using from_bson_expr = decltype(from_bson(std::declval<T&>(), std::declval<v1::document::view>()));
template <typename T>
struct has_from_bson : detail::conjunction<detail::negation<is_value<T>>, detail::is_detected<from_bson_expr, T>> {
};
template <typename T>
struct is_valid_deleter
: detail::disjunction<
// std::function<T> may not be nothrow move constructible across all pre-C++20 implementations.
// Deliberately allow an exemption that assumes it is de facto nothrow move constructible.
std::is_same<T, deleter_type>,
// Same requirements for D in std::unique_ptr<T, D> but without (nothrow) move assignability which is not
// required for T in std::function<T>.
detail::conjunction<
std::is_convertible<T, deleter_type>,
std::is_nothrow_destructible<T>,
std::is_nothrow_move_constructible<T>>> {};
public:
using const_iterator = v1::document::view::const_iterator;
using iterator = const_iterator;
~value() = default;
value(value&& other) noexcept = default;
value& operator=(value&& other) noexcept = default;
value(value const& other) : value{other.view()} {}
value& operator=(value const& other) {
*this = value{other.view()};
return *this;
}
value() : _data{const_cast<std::uint8_t*>(v1::document::view{}.data()), &noop_deleter} {}
template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
value(std::uint8_t* data, Deleter deleter) : _data{data, std::move(deleter)} {}
template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
value(std::uint8_t* data, std::size_t length, Deleter deleter) : value{data, std::move(deleter)} {
(void)v1::document::view{data, length}; // May throw.
}
explicit value(std::uint8_t* data) : value{data, default_deleter_type{}} {}
explicit value(std::uint8_t* data, std::size_t length) : value{data} {
(void)v1::document::view{data, length}; // May throw.
}
explicit value(unique_ptr_type ptr) : _data{std::move(ptr)} {}
explicit value(unique_ptr_type ptr, std::size_t length) : value{std::move(ptr)} {
(void)v1::document::view{_data.get(), length}; // May throw.
}
explicit value(v1::document::view view) {
if (!view) {
return;
}
v1::document::view const empty;
if (view.data() == empty.data()) {
_data = unique_ptr_type{const_cast<std::uint8_t*>(empty.data()), &noop_deleter};
} else {
_data = unique_ptr_type{new std::uint8_t[view.size()], default_deleter_type{}};
std::memcpy(_data.get(), view.data(), view.size());
}
}
template <typename T, detail::enable_if_t<has_to_bson<T>::value>* = nullptr>
explicit value(T const& v) : value{} {
to_bson(v, *this); // ADL.
}
value& operator=(v1::document::view view) {
this->reset(view);
return *this;
}
template <typename T, detail::enable_if_t<has_to_bson<T>::value>* = nullptr>
value& operator=(T const& v) {
*this = value{v};
return *this;
}
template <typename T, detail::enable_if_t<has_from_bson<T>::value>* = nullptr>
void get(T& v) const {
from_bson(v, this->view()); // ADL.
}
template <
typename T,
detail::enable_if_t<detail::conjunction<std::is_default_constructible<T>, has_from_bson<T>>::value>* = nullptr>
T get() const {
T res;
this->get(res);
return res;
}
deleter_type const& get_deleter() const {
return _data.get_deleter();
}
unique_ptr_type release() {
return std::move(_data);
}
void reset(value v) {
*this = std::move(v);
}
void reset(v1::document::view v) {
*this = value{v};
}
v1::document::view view() const {
return v1::document::view{_data.get()};
}
/* explicit(false) */ operator v1::document::view() const {
return this->view();
}
const_iterator cbegin() const {
return this->view().cbegin();
}
const_iterator cend() const {
return this->view().cend();
}
const_iterator begin() const {
return this->view().begin();
}
const_iterator end() const {
return this->view().end();
}
const_iterator find(v1::stdx::string_view key) const {
return this->view().find(key);
}
v1::element::view operator[](v1::stdx::string_view key) const {
return this->view()[key];
}
std::uint8_t const* data() const {
return _data.get();
}
std::size_t size() const {
return this->view().size();
}
std::size_t length() const {
return this->view().length();
}
bool empty() const {
return this->view().empty();
}
explicit operator bool() const {
return this->view().operator bool();
}
friend bool operator==(value const& lhs, value const& rhs) {
return lhs.view() == rhs.view();
}
friend bool operator!=(value const& lhs, value const& rhs) {
return !(lhs == rhs);
}
};
} // namespace document
} // namespace v1
} // namespace bsoncxx
#include <bsoncxx/v1/detail/postlude.hpp>
export.hpp
Provides macros to control the set of symbols exported in the ABI.
BSONCXX_ABI_CDECL
#define BSONCXX_ABI_CDECL
Expands to __cdecl when built with MSVC on Windows.
Definition export.hpp:49
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::value
A BSON document.
Definition value.hpp:44
bsoncxx::v1::document::value::noop_deleter
static void noop_deleter(std::uint8_t *)
The deleter used to avoid freeing preallocated storage representing an empty BSON document.
bsoncxx::v1::document::value::view
v1::document::view view() const
Return a view of the BSON bytes as a document.
Definition value.hpp:375
bsoncxx::v1::document::value::reset
void reset(value v)
Replace the underlying BSON bytes with v.
Definition value.hpp:356
bsoncxx::v1::document::value::value
value(unique_ptr_type ptr)
Initialize as owning ptr.
Definition value.hpp:235
bsoncxx::v1::document::value::cend
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:392
bsoncxx::v1::document::value::value
value(unique_ptr_type ptr, std::size_t length)
Equivalent to value(unique_ptr_type ptr), but validates the embedded length against length.
Definition value.hpp:247
bsoncxx::v1::document::value::reset
void reset(v1::document::view v)
Replace the underlying BSON bytes with a copy of v.
Definition value.hpp:368
bsoncxx::v1::document::value::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 ...
Definition value.hpp:407
bsoncxx::v1::document::value::operator==
friend bool operator==(value const &lhs, value const &rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition value.hpp:442
bsoncxx::v1::document::value::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 value.hpp:412
bsoncxx::v1::document::value::operator=
value & operator=(T const &v)
Equivalent to *this = value{v}.
Definition value.hpp:305
bsoncxx::v1::document::value::value
value(v1::document::view view)
Initialize with a copy of the BSON bytes referenced by view.
Definition value.hpp:262
bsoncxx::v1::document::value::get
void get(T &v) const
Equivalent to from_bson(v, this->view()).
Definition value.hpp:318
bsoncxx::v1::document::value::get_deleter
deleter_type const & get_deleter() const
Return the current deleter.
Definition value.hpp:342
bsoncxx::v1::document::value::empty
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition value.hpp:432
bsoncxx::v1::document::value::unique_ptr_type
std::unique_ptr< std::uint8_t[], deleter_type > unique_ptr_type
The type of the unique pointer used to manage the underlying BSON bytes.
Definition value.hpp:82
bsoncxx::v1::document::value::deleter_type
std::function< void(std::uint8_t *)> deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:62
bsoncxx::v1::document::value::begin
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:397
bsoncxx::v1::document::value::noop_deleter_type
void( *)(std::uint8_t *) noop_deleter_type
The type of a pointer to noop_deleter.
Definition value.hpp:72
bsoncxx::v1::document::value::value
value(value &&other) noexcept=default
Move construction.
bsoncxx::v1::document::value::operator=
value & operator=(value const &other)
Copy assignment.
Definition value.hpp:160
bsoncxx::v1::document::value::value
value(T const &v)
Equivalent to to_bson(v, *this); after default-initialization.
Definition value.hpp:285
bsoncxx::v1::document::value::length
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition value.hpp:427
bsoncxx::v1::document::value::default_deleter_type
std::default_delete< std::uint8_t[]> default_deleter_type
The deleter used to free copied BSON bytes when a user-provided deleter is not specified.
Definition value.hpp:67
bsoncxx::v1::document::value::operator=
value & operator=(value &&other) noexcept=default
Move assignment.
bsoncxx::v1::document::value::value
value(std::uint8_t *data, std::size_t length)
Equivalent to value(std::uint8_t* data), but validates the embedded length against length.
Definition value.hpp:224
bsoncxx::v1::document::value::cbegin
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:387
bsoncxx::v1::document::value::value
value(std::uint8_t *data, Deleter deleter)
Initialize as owning data which will be freed with deleter.
Definition value.hpp:187
bsoncxx::v1::document::value::~value
~value()=default
Destroy this object.
bsoncxx::v1::document::value::iterator
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:121
bsoncxx::v1::document::value::data
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition value.hpp:417
bsoncxx::v1::document::value::operator=
value & operator=(v1::document::view view)
Equivalent to this->reset(view).
Definition value.hpp:292
bsoncxx::v1::document::value::release
unique_ptr_type release()
Release ownership of the underlying BSON bytes.
Definition value.hpp:349
bsoncxx::v1::document::value::value
value(value const &other)
Copy construction.
Definition value.hpp:152
bsoncxx::v1::document::value::get
T get() const
Equivalent to T value; this->get(value); return value;.
Definition value.hpp:333
bsoncxx::v1::document::value::operator!=
friend bool operator!=(value const &lhs, value const &rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition value.hpp:447
bsoncxx::v1::document::value::size
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition value.hpp:422
bsoncxx::v1::document::value::value
value()
Initialize as an empty document (or array).
Definition value.hpp:173
bsoncxx::v1::document::value::const_iterator
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition value.hpp:118
bsoncxx::v1::document::value::value
value(std::uint8_t *data, std::size_t length, Deleter deleter)
Equivalent to value(std::uint8_t* data, Deleter deleter), but validates the embedded length against l...
Definition value.hpp:201
bsoncxx::v1::document::value::end
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:402
bsoncxx::v1::document::value::value
value(std::uint8_t *data)
Initialize as owning data which will be freed with default_deleter_type.
Definition value.hpp:212
bsoncxx::v1::document::view::const_iterator
A const iterator over the elements of a view.
Definition view.hpp:264
bsoncxx::v1::document::view
A non-owning, read-only BSON document.
Definition view.hpp:52
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::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::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::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::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
Declares entities whose ABI stability is guaranteed for documented symbols.
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
type_traits.hpp
For internal use only!
value-fwd.hpp
Declares bsoncxx::v1::document::value.
view.hpp
Provides bsoncxx::v1::document::view.
view.hpp
Provides bsoncxx::v1::element::view.
string_view.hpp
Provides std::string_view-related polyfills for library API usage.