MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <cstdlib>
18#include <memory>
19#include <type_traits>
20
22
26
28
29namespace bsoncxx {
30namespace v_noabi {
31namespace document {
32
39class value {
40 public:
41 using deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
42 using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
43
56 BSONCXX_ABI_EXPORT_CDECL() value(std::uint8_t* data, std::size_t length, deleter_type dtor);
57
67 BSONCXX_ABI_EXPORT_CDECL() value(unique_ptr_type ptr, std::size_t length);
68
78
79 ~value() = default;
80
82 BSONCXX_ABI_EXPORT_CDECL(value&) operator=(value const&);
83
84 value(value&&) = default;
85 value& operator=(value&&) = default;
86
95 template <typename T, detail::requires_not_t<int, std::is_same<T, array::view>> = 0>
96 explicit value(T const& t) : value({}) {
97 to_bson(t, *this);
98 }
99 template <typename T>
100 value& operator=(T const& t) {
101 *this = value{t};
102 return *this;
103 }
104
109
114
119
124
141
153
159 BSONCXX_ABI_EXPORT_CDECL(std::uint8_t const*) data() const;
160
169 BSONCXX_ABI_EXPORT_CDECL(std::size_t) length() const;
170
178
182 document::view view() const noexcept {
183 // Silence false positive with g++ 10.2.1 on Debian 11.
184 BSONCXX_PRIVATE_WARNINGS_PUSH();
185 BSONCXX_PRIVATE_WARNINGS_DISABLE(GCC("-Wmaybe-uninitialized"));
186 return document::view{static_cast<uint8_t*>(_data.get()), _length};
187 BSONCXX_PRIVATE_WARNINGS_POP();
188 }
189
195 operator document::view() const noexcept {
196 return view();
197 }
198
206 template <typename T>
207 T get() {
208 T temp{};
209 from_bson(temp, this->view());
210 return temp;
211 }
212
222 template <typename T>
223 void get(T& t) {
224 from_bson(t, this->view());
225 }
226
237
243
244 private:
245 unique_ptr_type _data;
246 std::size_t _length{0};
247};
248
253
255inline bool operator==(value const& lhs, value const& rhs) {
256 return (lhs.view() == rhs.view());
257}
258
260inline bool operator!=(value const& lhs, value const& rhs) {
261 return !(lhs == rhs);
262}
263
266
267} // namespace document
268} // namespace v_noabi
269} // namespace bsoncxx
270
271namespace bsoncxx {
272namespace document {
273
274using ::bsoncxx::v_noabi::document::operator==;
275using ::bsoncxx::v_noabi::document::operator!=;
276
277} // namespace document
278} // namespace bsoncxx
279
281
Provides bsoncxx::v_noabi::array::view.
#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.
value(std::uint8_t *data, std::size_t length, deleter_type dtor)
Constructs a value from a buffer. This constructor transfers ownership of the buffer to the resulting...
A polyfill for std::string_view.
Definition string_view.hpp:411
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:46
std::uint8_t const * data() const
Access the raw bytes of the underlying document.
value(std::uint8_t *data, std::size_t length, deleter_type dtor)
Constructs a value from a buffer. This constructor transfers ownership of the buffer to the resulting...
T get()
Constructs an object of type T from this document object. This method uses argument-dependent lookup ...
Definition value.hpp:207
element operator[](stdx::string_view key) const
Finds the first element of the document with the provided key. If there is no such element,...
std::size_t length() const
Gets the length of the underlying buffer.
document::view::const_iterator begin() const
bool operator==(value const &lhs, value const &rhs)
Compares two document values for (in)-equality.
Definition value.hpp:255
document::view::const_iterator find(stdx::string_view key) const
Finds the first element of the document with the provided key. If there is no such element,...
document::view::const_iterator cbegin() const
bool empty() const
Checks if the underlying document is empty, i.e. it is equivalent to the trivial document '{}...
unique_ptr_type release()
Transfer ownership of the underlying buffer to the caller.
void get(T &t)
Constructs an object of type T from this document object. This method uses argument-dependent lookup ...
Definition value.hpp:223
document::view view() const noexcept
Get a view over the document owned by this value.
Definition value.hpp:182
document::view::const_iterator end() const
document::view::const_iterator cend() const
bool operator!=(value const &lhs, value const &rhs)
Compares two document values for (in)-equality.
Definition value.hpp:260
void reset(document::view view)
Replace the formerly-owned buffer with the new view. This will make a copy of the passed-in view.
A const iterator over the contents of a document view.
Definition view.hpp:153
A read-only, non-owning view of a BSON document.
Definition view.hpp:35
Declares bsoncxx::v_noabi::document::value.
Provides bsoncxx::v_noabi::document::view.
Declares entities representing a BSON array.
Declares entities representing a BSON document.
Declares entities whose ABI stability is NOT guaranteed.
The top-level namespace within which all bsoncxx library entities are declared.
For internal use only!