MongoDB C++ Driver  mongocxx-3.0.3
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/document/view.hpp>
21 
22 #include <bsoncxx/config/prelude.hpp>
23 
24 namespace bsoncxx {
25 BSONCXX_INLINE_NAMESPACE_BEGIN
26 namespace document {
27 
33 class BSONCXX_API value {
34  public:
35  using deleter_type = void (*)(std::uint8_t*);
36  using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
37 
50  value(std::uint8_t* data, std::size_t length, deleter_type dtor);
51 
61  value(unique_ptr_type ptr, std::size_t length);
62 
71  explicit value(document::view view);
72 
73  value(const value&);
74  value& operator=(const value&);
75 
76  value(value&&) noexcept = default;
77  value& operator=(value&&) noexcept = default;
78 
82  BSONCXX_INLINE document::view view() const noexcept;
83 
89  BSONCXX_INLINE operator document::view() const noexcept;
90 
100  unique_ptr_type release();
101 
102  private:
103  unique_ptr_type _data;
104  std::size_t _length;
105 };
106 
107 BSONCXX_INLINE document::view value::view() const noexcept {
108  return document::view{static_cast<uint8_t*>(_data.get()), _length};
109 }
110 
111 BSONCXX_INLINE value::operator document::view() const noexcept {
112  return view();
113 }
114 
115 } // namespace document
116 BSONCXX_INLINE_NAMESPACE_END
117 } // namespace bsoncxx
118 
119 #include <bsoncxx/config/postlude.hpp>
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:33
document::view view() const noexcept
Get a view over the document owned by this value.
Definition: value.hpp:107
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
Definition: element.hpp:24