MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <bsoncxx/array/value-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <bsoncxx/v1/array/value.hpp> // IWYU pragma: export
22
23#include <cstdint>
24#include <cstdlib>
25#include <memory>
26#include <utility>
27
31
33
34namespace bsoncxx {
35namespace v_noabi {
36namespace array {
37
44class value {
45 private:
46 v1::array::value _value;
47 std::size_t _length;
48
49 public:
51 using deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
52
54 using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
55
58
61
62 ~value() = default;
63
64 value(value&&) = default;
65 value& operator=(value&&) = default;
66
67 value(value const& other) : value{other.view()} {}
68
69 value& operator=(value const& other) {
70 *this = value{other.view()};
71 return *this;
72 }
73
91 value(std::uint8_t* data, std::size_t length, deleter_type deleter)
92 : _value{data, std::move(deleter)}, _length{length} {}
93
107 value(unique_ptr_type ptr, std::size_t length) : _value{std::move(ptr)}, _length{length} {}
108
118
130 explicit operator v1::array::value() const& {
131 return _value;
132 }
133
145 explicit operator v1::array::value() && {
146 _length = 0u;
147 return std::move(_value);
148 }
149
154 return this->view().cbegin();
155 }
156
161 return this->view().cend();
162 }
163
168 return this->view().begin();
169 }
170
175 return this->view().end();
176 }
177
179 const_iterator find(std::uint32_t i) const {
180 return const_iterator{*_value.view().find(i)};
181 }
182
184 v_noabi::document::element operator[](std::uint32_t i) const {
185 return _value.operator[](i);
186 }
187
189 std::uint8_t const* data() const {
190 return _value.data();
191 }
192
194 std::size_t size() const {
195 return _length; // Do NOT use _value.size().
196 }
197
199 std::size_t length() const {
200 return _length; // Do NOT use _value.length().
201 }
202
209 bool empty() const {
210 return _length == 5; // Do NOT use _value.empty().
211 }
212
216 v_noabi::array::view view() const noexcept {
217 return {_value.data(), _length};
218 }
219
225 /* explicit(false) */ operator v_noabi::array::view() const noexcept {
226 return this->view();
227 }
228
239 auto ptr = _value.release();
240
241 // Invariant: the underlying deleter type MUST be `deleter_type`.
242 auto const deleter_ptr = ptr.get_deleter().target<deleter_type>();
243
244 // Invariant: `ptr` implies `deleter_ptr`, but not the reverse.
245 return {ptr.release(), deleter_ptr ? *deleter_ptr : nullptr};
246 }
247
253 _value.reset(to_v1(view));
254 }
255};
256
257} // namespace array
258} // namespace v_noabi
259} // namespace bsoncxx
260
261namespace bsoncxx {
262namespace v_noabi {
263
270
278
283 return v1::array::value{std::move(v)};
284}
285
286} // namespace v_noabi
287} // namespace bsoncxx
288
290
#define BSONCXX_ABI_CDECL
Expands to __cdecl when built with MSVC on Windows.
Definition export.hpp:49
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
A BSON array.
Definition value.hpp:41
v1::array::view view() const
Return a view of the BSON bytes as an array.
Definition value.hpp:137
A read-only BSON array that owns its underlying buffer.
Definition value.hpp:44
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:54
v_noabi::array::view::const_iterator const_iterator
A const iterator over the contents of a document view.
Definition value.hpp:57
void( *)(std::uint8_t *) deleter_type
The type of the deleter used to free the underlying BSON binary data.
Definition value.hpp:51
const_iterator cbegin() const
Definition value.hpp:153
const_iterator end() const
Definition value.hpp:174
unique_ptr_type release()
Transfer ownership of the underlying buffer to the caller.
Definition value.hpp:238
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:179
v_noabi::array::view view() const noexcept
Get a view over the array owned by this value.
Definition value.hpp:216
std::size_t size() const
Gets the length of the underlying buffer in bytes.
Definition value.hpp:194
std::uint8_t const * data() const
Access the raw bytes of the underlying array.
Definition value.hpp:189
const_iterator cend() const
Definition value.hpp:160
bool empty() const
Return true when this->length() == 5.
Definition value.hpp:209
std::size_t length() const
Gets the length of the underlying buffer in bytes.
Definition value.hpp:199
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:60
const_iterator begin() const
Definition value.hpp:167
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:252
value(std::uint8_t *data, std::size_t length, deleter_type deleter)
Constructs a value from a buffer.
Definition value.hpp:91
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:184
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:107
A const iterator over the contents of an array view.
Definition view.hpp:174
A read-only, non-owning view of a BSON document.
Definition view.hpp:41
const_iterator begin() const
Definition view.hpp:233
const_iterator cend() const
Definition view.hpp:229
const_iterator cbegin() const
const_iterator end() const
Definition view.hpp:237
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:52
Provides bsoncxx::v_noabi::document::element.
Declares entities whose ABI stability is guaranteed for documented symbols.
Declares entities representing a BSON array.
Declares entities whose ABI stability is NOT guaranteed.
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:267
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v1::array::value.
Declares bsoncxx::v_noabi::array::value.
Provides bsoncxx::v_noabi::array::view.
Provides bsoncxx::v_noabi::document::value.