MongoDB C++ Driver  mongocxx-3.10.2
value.hpp
1 // Copyright 2014 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 
21 #include <bsoncxx/document/value-fwd.hpp>
22 
23 #include <bsoncxx/array/view.hpp>
24 #include <bsoncxx/document/view.hpp>
25 #include <bsoncxx/stdx/type_traits.hpp>
26 
27 #include <bsoncxx/config/prelude.hpp>
28 
29 namespace bsoncxx {
30 namespace v_noabi {
31 namespace document {
32 
38 class value {
39  public:
40  using deleter_type = void (*)(std::uint8_t*);
41  using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
42 
55  value(std::uint8_t* data, std::size_t length, deleter_type dtor);
56 
66  value(unique_ptr_type ptr, std::size_t length);
67 
77 
78  value(const value&);
79  value& operator=(const value&);
80 
81  value(value&&) noexcept = default;
82  value& operator=(value&&) noexcept = default;
83 
92  template <typename T, detail::requires_not_t<int, std::is_same<T, array::view>> = 0>
93  explicit value(const T& t) : value({}) {
94  to_bson(t, *this);
95  }
96  template <typename T>
97  value& operator=(const T& t) {
98  *this = value{t};
99  return *this;
100  }
101 
106 
111 
116 
121 
137  document::view::const_iterator find(stdx::string_view key) const;
138 
149  element operator[](stdx::string_view key) const;
150 
156  const std::uint8_t* data() const;
157 
166  std::size_t length() const;
167 
174  bool empty() const;
175 
179  BSONCXX_INLINE document::view view() const noexcept;
180 
186  BSONCXX_INLINE operator document::view() const noexcept;
187 
195  template <typename T>
196  T get() {
197  T temp{};
198  from_bson(temp, this->view());
199  return temp;
200  }
201 
211  template <typename T>
212  void get(T& t) {
213  from_bson(t, this->view());
214  }
215 
225  unique_ptr_type release();
226 
232 
233  private:
234  unique_ptr_type _data;
235  std::size_t _length{0};
236 };
237 
238 BSONCXX_INLINE document::view value::view() const noexcept {
239  // Silence false positive with g++ 10.2.1 on Debian 11.
240  BSONCXX_PUSH_WARNINGS();
241  BSONCXX_DISABLE_WARNING(GCC("-Wmaybe-uninitialized"));
242  return document::view{static_cast<uint8_t*>(_data.get()), _length};
243  BSONCXX_POP_WARNINGS();
244 }
245 
246 BSONCXX_INLINE value::operator document::view() const noexcept {
247  return view();
248 }
249 
257 BSONCXX_INLINE bool operator==(const value& lhs, const value& rhs) {
258  return (lhs.view() == rhs.view());
259 }
260 
261 BSONCXX_INLINE bool operator!=(const value& lhs, const value& rhs) {
262  return !(lhs == rhs);
263 }
264 
268 
269 } // namespace document
270 } // namespace v_noabi
271 } // namespace bsoncxx
272 
273 namespace bsoncxx {
274 namespace document {
275 
276 using ::bsoncxx::v_noabi::document::operator==;
277 using ::bsoncxx::v_noabi::document::operator!=;
278 
279 } // namespace document
280 } // namespace bsoncxx
281 
282 #include <bsoncxx/config/postlude.hpp>
A variant view type that accesses values in serialized BSON documents.
Definition: element.hpp:45
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:38
value(std::uint8_t *data, std::size_t length, deleter_type dtor)
Constructs a value from a buffer.
T get()
Constructs an object of type T from this document object.
Definition: value.hpp:196
element operator[](stdx::string_view key) const
Finds the first element of the document with the provided key.
std::size_t length() const
Gets the length of the underlying buffer.
document::view::const_iterator begin() const
value(unique_ptr_type ptr, std::size_t length)
Constructs a value from a std::unique_ptr to a buffer.
document::view::const_iterator find(stdx::string_view key) const
Finds the first element of the document with the provided key.
document::view::const_iterator cbegin() const
bool empty() const
Checks if the underlying document is empty, i.e.
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.
Definition: value.hpp:212
bool operator==(const value &lhs, const value &rhs)
Compares two document values for (in)-equality.
Definition: value.hpp:257
document::view view() const noexcept
Get a view over the document owned by this value.
Definition: value.hpp:238
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.
const std::uint8_t * data() const
Access the raw bytes of the underlying document.
value(document::view view)
Constructs a value from a view of a document.
A const iterator over the contents of a document view.
Definition: view.hpp:155
A read-only, non-owning view of a BSON document.
Definition: view.hpp:35
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19