MongoDB C++ Driver mongocxx-4.0.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 <cstdlib>
18#include <memory>
19#include <type_traits>
20
22
26
28
29namespace bsoncxx {
30namespace v_noabi {
31namespace document {
32
38class value {
39 public:
40 using deleter_type = void(BSONCXX_ABI_CDECL*)(std::uint8_t*);
41 using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
42
55 BSONCXX_ABI_EXPORT_CDECL() value(std::uint8_t* data, std::size_t length, deleter_type dtor);
56
66 BSONCXX_ABI_EXPORT_CDECL() value(unique_ptr_type ptr, std::size_t length);
67
76 explicit BSONCXX_ABI_EXPORT_CDECL() value(document::view view);
77
78 ~value() = default;
79
81 BSONCXX_ABI_EXPORT_CDECL(value&) operator=(const value&);
82
83 value(value&&) = default;
84 value& operator=(value&&) = default;
85
94 template <typename T, detail::requires_not_t<int, std::is_same<T, array::view>> = 0>
95 explicit value(const T& t) : value({}) {
96 to_bson(t, *this);
97 }
98 template <typename T>
99 value& operator=(const T& t) {
100 *this = value{t};
101 return *this;
102 }
103
108
113
118
123
140
152
158 BSONCXX_ABI_EXPORT_CDECL(const std::uint8_t*) data() const;
159
168 BSONCXX_ABI_EXPORT_CDECL(std::size_t) length() const;
169
177
181 document::view view() const noexcept {
182 // Silence false positive with g++ 10.2.1 on Debian 11.
183 BSONCXX_PUSH_WARNINGS();
184 BSONCXX_DISABLE_WARNING(GCC("-Wmaybe-uninitialized"));
185 return document::view{static_cast<uint8_t*>(_data.get()), _length};
186 BSONCXX_POP_WARNINGS();
187 }
188
194 operator document::view() const noexcept {
195 return view();
196 }
197
205 template <typename T>
206 T get() {
207 T temp{};
208 from_bson(temp, this->view());
209 return temp;
210 }
211
221 template <typename T>
222 void get(T& t) {
223 from_bson(t, this->view());
224 }
225
236
242
243 private:
244 unique_ptr_type _data;
245 std::size_t _length{0};
246};
247
252
254inline bool operator==(const value& lhs, const value& rhs) {
255 return (lhs.view() == rhs.view());
256}
257
259inline bool operator!=(const value& lhs, const value& rhs) {
260 return !(lhs == rhs);
261}
262
265
266} // namespace document
267} // namespace v_noabi
268} // namespace bsoncxx
269
270namespace bsoncxx {
271namespace document {
272
273using ::bsoncxx::v_noabi::document::operator==;
274using ::bsoncxx::v_noabi::document::operator!=;
275
276} // namespace document
277} // namespace bsoncxx
278
280
285
286#if defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
287
288namespace bsoncxx {
289namespace document {
290
293
296
297} // namespace document
298} // namespace bsoncxx
299
300#endif // defined(BSONCXX_PRIVATE_DOXYGEN_PREPROCESSOR)
Provides bsoncxx::v_noabi::array::view.
The bsoncxx macro guard postlude header.
The bsoncxx macro guard prelude header.
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:46
A read-only BSON document that owns its underlying buffer. When a document::value goes out of scope,...
Definition value.hpp:38
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:206
bool operator!=(const value &lhs, const value &rhs)
Compares two document values for (in)-equality.
Definition value.hpp:259
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
const std::uint8_t * data() const
Access the raw bytes of the underlying document.
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:222
bool operator==(const value &lhs, const value &rhs)
Compares two document values for (in)-equality.
Definition value.hpp:254
document::view view() const noexcept
Get a view over the document owned by this value.
Definition value.hpp:181
document::view::const_iterator end() const
document::view::const_iterator cend() const
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
A polyfill for std::string_view.
Definition string_view.hpp:436
Declares bsoncxx::v_noabi::document::value.
Provides bsoncxx::v_noabi::document::view.
#define BSONCXX_ABI_CDECL
Expands to __cdecl when built with MSVC on Windows.
Definition fwd.hpp:169
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition fwd.hpp:177
bool operator!=(const v_noabi::document::element &elem, const v_noabi::types::bson_value::view &v)
bsoncxx::v_noabi::document::operator!=(const v_noabi::document::element& elem, const v_noabi::types::...
bool operator==(const v_noabi::document::element &elem, const v_noabi::types::bson_value::view &v)
bsoncxx::v_noabi::document::operator==(const v_noabi::document::element& elem, const v_noabi::types::...
The top-level namespace within which all bsoncxx library entities are declared.
The top-level namespace reserved for the C++ standard library.
Provides <type_traits>-related polyfills for internal use.