MongoDB C++ Driver  mongocxx-3.9.0
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
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/array/view.hpp>
22 #include <bsoncxx/document/view.hpp>
23 #include <bsoncxx/stdx/type_traits.hpp>
24 
25 #include <bsoncxx/config/prelude.hpp>
26 
27 namespace bsoncxx {
28 inline namespace v_noabi {
29 namespace document {
30 
36 class BSONCXX_API value {
37  public:
38  using deleter_type = void (*)(std::uint8_t*);
39  using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
40 
53  value(std::uint8_t* data, std::size_t length, deleter_type dtor);
54 
64  value(unique_ptr_type ptr, std::size_t length);
65 
75 
76  value(const value&);
77  value& operator=(const value&);
78 
79  value(value&&) noexcept = default;
80  value& operator=(value&&) noexcept = default;
81 
89  template <typename T, detail::requires_not_t<int, std::is_same<T, array::view>> = 0>
90  explicit value(const T& t) : value({}) {
91  to_bson(t, *this);
92  }
93  template <typename T>
94  value& operator=(const T& t) {
95  *this = value{t};
96  return *this;
97  }
98 
103 
108 
113 
118 
134  document::view::const_iterator find(stdx::string_view key) const;
135 
146  element operator[](stdx::string_view key) const;
147 
153  const std::uint8_t* data() const;
154 
163  std::size_t length() const;
164 
171  bool empty() const;
172 
176  BSONCXX_INLINE document::view view() const noexcept;
177 
183  BSONCXX_INLINE operator document::view() const noexcept;
184 
192  template <typename T>
193  T get() {
194  T temp{};
195  from_bson(temp, this->view());
196  return temp;
197  }
198 
208  template <typename T>
209  void get(T& t) {
210  from_bson(t, this->view());
211  }
212 
222  unique_ptr_type release();
223 
229 
230  private:
231  unique_ptr_type _data;
232  std::size_t _length{0};
233 };
234 
235 #if !defined(__clang__) && defined(__GNUC__) && (__cplusplus >= 201709L)
236 // Silence false positive with g++ 10.2.1 on Debian 11.
237 #pragma GCC diagnostic push
238 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
239 #endif
240 BSONCXX_INLINE document::view value::view() const noexcept {
241  return document::view{static_cast<uint8_t*>(_data.get()), _length};
242 }
243 #if !defined(__clang__) && defined(__GNUC__) && (__cplusplus >= 201709L)
244 #pragma GCC diagnostic pop
245 #endif
246 
247 BSONCXX_INLINE value::operator document::view() const noexcept {
248  return view();
249 }
250 
258 BSONCXX_INLINE bool operator==(const value& lhs, const value& rhs) {
259  return (lhs.view() == rhs.view());
260 }
261 
262 BSONCXX_INLINE bool operator!=(const value& lhs, const value& rhs) {
263  return !(lhs == rhs);
264 }
265 
269 
270 } // namespace document
271 } // namespace v_noabi
272 } // namespace bsoncxx
273 
274 #include <bsoncxx/config/postlude.hpp>
A variant view type that accesses values in serialized BSON documents.
Definition: element.hpp:76
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:36
value(std::uint8_t *data, std::size_t length, deleter_type dtor)
Constructs a value from a buffer.
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:209
bool operator==(const value &lhs, const value &rhs)
Compares two document values for (in)-equality.
Definition: value.hpp:258
document::view view() const noexcept
Get a view over the document owned by this value.
Definition: value.hpp:240
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:153
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
The top-level namespace for bsoncxx library entities.
Definition: element.hpp:24