MongoDB C++ Driver  mongocxx-3.7.0
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 
20 #include <bsoncxx/array/view.hpp>
21 #include <bsoncxx/document/view.hpp>
22 
23 #include <bsoncxx/config/prelude.hpp>
24 
25 namespace bsoncxx {
26 BSONCXX_INLINE_NAMESPACE_BEGIN
27 namespace document {
28 
34 class BSONCXX_API value {
35  public:
36  using deleter_type = void (*)(std::uint8_t*);
37  using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
38 
51  value(std::uint8_t* data, std::size_t length, deleter_type dtor);
52 
62  value(unique_ptr_type ptr, std::size_t length);
63 
72  explicit value(document::view view);
73 
74  value(const value&);
75  value& operator=(const value&);
76 
77  value(value&&) noexcept = default;
78  value& operator=(value&&) noexcept = default;
79 
87  template <typename T,
88  typename std::enable_if<!std::is_same<T, typename array::view>::value, int>::type = 0>
89  explicit value(const T& t) : value({}) {
90  to_bson(t, *this);
91  }
92  template <typename T>
93  value& operator=(const T& t) {
94  *this = value{t};
95  return *this;
96  }
97 
101  document::view::const_iterator cbegin() const;
102 
106  document::view::const_iterator cend() const;
107 
111  document::view::const_iterator begin() const;
112 
116  document::view::const_iterator end() const;
117 
133  document::view::const_iterator find(stdx::string_view key) const;
134 
145  element operator[](stdx::string_view key) const;
146 
152  const std::uint8_t* data() const;
153 
162  std::size_t length() const;
163 
170  bool empty() const;
171 
175  BSONCXX_INLINE document::view view() const noexcept;
176 
182  BSONCXX_INLINE operator document::view() const noexcept;
183 
191  template <typename T>
192  T get() {
193  T temp{};
194  from_bson(temp, this->view());
195  return temp;
196  }
197 
207  template <typename T>
208  void get(T& t) {
209  from_bson(t, this->view());
210  }
211 
221  unique_ptr_type release();
222 
227  void reset(document::view view);
228 
229  private:
230  unique_ptr_type _data;
231  std::size_t _length{0};
232 };
233 
234 BSONCXX_INLINE document::view value::view() const noexcept {
235  return document::view{static_cast<uint8_t*>(_data.get()), _length};
236 }
237 
238 BSONCXX_INLINE value::operator document::view() const noexcept {
239  return view();
240 }
241 
249 BSONCXX_INLINE bool operator==(const value& lhs, const value& rhs) {
250  return (lhs.view() == rhs.view());
251 }
252 
253 BSONCXX_INLINE bool operator!=(const value& lhs, const value& rhs) {
254  return !(lhs == rhs);
255 }
256 
260 
261 } // namespace document
262 BSONCXX_INLINE_NAMESPACE_END
263 } // namespace bsoncxx
264 
265 #include <bsoncxx/config/postlude.hpp>
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::document::value::get
void get(T &t)
Constructs an object of type T from this document object.
Definition: value.hpp:208
bsoncxx::document::value
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:34
bsoncxx::document::value::view
document::view view() const noexcept
Get a view over the document owned by this value.
Definition: value.hpp:234
bsoncxx::document::value::operator==
bool operator==(const value &lhs, const value &rhs)
Compares two document values for (in)-equality.
Definition: value.hpp:249
bsoncxx::document::value::value
value(const T &t)
Constructor used for serialization of user objects.
Definition: value.hpp:89
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:46
bsoncxx::document::view
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33