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/array/value-fwd.hpp> // IWYU pragma: export
//
#include <bsoncxx/v1/array/value.hpp> // IWYU pragma: export
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <utility>
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/document/element.hpp>
#include <bsoncxx/document/value.hpp>
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
namespace array {
class value {
public:
using deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
private:
unique_ptr_type _data;
std::size_t _length{0};
public:
using const_iterator = v_noabi::array::view::const_iterator;
using iterator = const_iterator;
~value() = default;
value(value&&) = default;
value& operator=(value&&) = default;
value(value const& other) : value{other.view()} {}
value& operator=(value const& other) {
*this = value{other.view()};
return *this;
}
value(std::uint8_t* data, std::size_t length, deleter_type deleter)
: _data{data, std::move(deleter)}, _length{length} {}
value(unique_ptr_type ptr, std::size_t length) : _data{std::move(ptr)}, _length{length} {}
explicit BSONCXX_ABI_EXPORT_CDECL_UNSTABLE() value(v_noabi::array::view view);
explicit operator v1::array::value() const& {
return v1::array::value{to_v1(this->view())};
}
explicit operator v1::array::value() && {
auto const length = _length;
_length = 0u;
return {_data.release(), length, std::move(_data.get_deleter())};
}
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(std::uint32_t i) const {
return const_iterator{*this->view().find(i)};
}
v_noabi::document::element operator[](std::uint32_t i) const {
return to_v1(this->view()).operator[](i);
}
std::uint8_t const* data() const {
return _data.get();
}
std::size_t size() const {
return _length; // Do NOT use _value.size().
}
std::size_t length() const {
return _length; // Do NOT use _value.length().
}
bool empty() const {
return _length == 5; // Do NOT use this->view().empty().
}
v_noabi::array::view view() const noexcept {
return {_data.get(), _length};
}
/* explicit(false) */ operator v_noabi::array::view() const noexcept {
return this->view();
}
unique_ptr_type release() {
return std::move(_data);
}
void reset(v_noabi::array::view view) {
*this = value{view};
}
};
} // namespace array
} // namespace v_noabi
} // namespace bsoncxx
namespace bsoncxx {
namespace v_noabi {
inline v_noabi::array::value from_v1(v1::array::value const& v) {
return v_noabi::array::value{from_v1(v.view())};
}
BSONCXX_ABI_EXPORT_CDECL_UNSTABLE(v_noabi::array::value) from_v1(v1::array::value&& v);
inline v1::array::value to_v1(v_noabi::array::value v) {
return v1::array::value{std::move(v)};
}
} // namespace v_noabi
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
BSONCXX_ABI_CDECL
#define BSONCXX_ABI_CDECL
Expands to __cdecl when built with MSVC on Windows.
Definition export.hpp:49
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::v1::array::value
A BSON array.
Definition value.hpp:39
bsoncxx::v1::array::value::view
v1::array::view view() const
Return a view of the BSON bytes as an array.
Definition value.hpp:135
bsoncxx::v_noabi::array::value
A read-only BSON array that owns its underlying buffer.
Definition value.hpp:44
bsoncxx::v_noabi::array::value::unique_ptr_type
std::unique_ptr< uint8_t[], deleter_type > unique_ptr_type
The type of the unique pointer used to manage the underlying BSON binary data.
Definition value.hpp:50
bsoncxx::v_noabi::array::value::const_iterator
v_noabi::array::view::const_iterator const_iterator
A const iterator over the contents of a document view.
Definition value.hpp:58
bsoncxx::v_noabi::array::value::deleter_type
void( *)(std::uint8_t *) deleter_type
The type of the deleter used to free the underlying BSON binary data.
Definition value.hpp:47
bsoncxx::v_noabi::array::value::cbegin
const_iterator cbegin() const
Definition value.hpp:155
bsoncxx::v_noabi::array::value::end
const_iterator end() const
Definition value.hpp:176
bsoncxx::v_noabi::array::value::release
unique_ptr_type release()
Transfer ownership of the underlying buffer to the caller.
Definition value.hpp:240
bsoncxx::v_noabi::array::value::find
const_iterator find(std::uint32_t i) const
Indexes into this BSON array. If the index is out-of-bounds, a past-the-end iterator will be returned...
Definition value.hpp:181
bsoncxx::v_noabi::array::value::view
v_noabi::array::view view() const noexcept
Get a view over the array owned by this value.
Definition value.hpp:218
bsoncxx::v_noabi::array::value::size
std::size_t size() const
Gets the length of the underlying buffer in bytes.
Definition value.hpp:196
bsoncxx::v_noabi::array::value::data
std::uint8_t const * data() const
Access the raw bytes of the underlying array.
Definition value.hpp:191
bsoncxx::v_noabi::array::value::cend
const_iterator cend() const
Definition value.hpp:162
bsoncxx::v_noabi::array::value::empty
bool empty() const
Return true when this->length() == 5.
Definition value.hpp:211
bsoncxx::v_noabi::array::value::length
std::size_t length() const
Gets the length of the underlying buffer in bytes.
Definition value.hpp:201
bsoncxx::v_noabi::array::value::iterator
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:61
bsoncxx::v_noabi::array::value::begin
const_iterator begin() const
Definition value.hpp:169
bsoncxx::v_noabi::array::value::reset
void reset(v_noabi::array::view view)
Replace the formerly-owned buffer with the new view. This will make a copy of the passed-in view.
Definition value.hpp:248
bsoncxx::v_noabi::array::value::value
value(std::uint8_t *data, std::size_t length, deleter_type deleter)
Constructs a value from a buffer.
Definition value.hpp:92
bsoncxx::v_noabi::array::value::operator[]
v_noabi::document::element operator[](std::uint32_t i) const
Indexes into this BSON array. If the index is out-of-bounds, the invalid array::element will be retur...
Definition value.hpp:186
bsoncxx::v_noabi::array::value::value
value(unique_ptr_type ptr, std::size_t length)
Constructs a value from a std::unique_ptr to a buffer. The ownership of the buffer is transferred to ...
Definition value.hpp:108
bsoncxx::v_noabi::array::view::const_iterator
A const iterator over the contents of an array view.
Definition view.hpp:174
bsoncxx::v_noabi::array::view
A read-only, non-owning view of a BSON document.
Definition view.hpp:41
bsoncxx::v_noabi::array::view::begin
const_iterator begin() const
Definition view.hpp:233
bsoncxx::v_noabi::array::view::find
const_iterator find(std::uint32_t i) const
Indexes into this BSON array. If the index is out-of-bounds, a past-the-end iterator will be returned...
bsoncxx::v_noabi::array::view::cend
const_iterator cend() const
Definition view.hpp:229
bsoncxx::v_noabi::array::view::cbegin
const_iterator cbegin() const
bsoncxx::v_noabi::array::view::end
const_iterator end() const
Definition view.hpp:237
bsoncxx::v_noabi::document::element
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:52
element.hpp
Provides bsoncxx::v_noabi::document::element.
bsoncxx::v1
Declares entities whose ABI stability is guaranteed for documented symbols.
bsoncxx::v_noabi::array
Declares entities representing a BSON array.
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.
value.hpp
Provides bsoncxx::v1::array::value.
value-fwd.hpp
Declares bsoncxx::v_noabi::array::value.
view.hpp
Provides bsoncxx::v_noabi::array::view.
value.hpp
Provides bsoncxx::v_noabi::document::value.