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 
20 #include <bsoncxx/array/value-fwd.hpp>
21 
22 #include <bsoncxx/array/view.hpp>
23 #include <bsoncxx/document/value.hpp>
24 
25 #include <bsoncxx/config/prelude.hpp>
26 
27 namespace bsoncxx {
28 namespace v_noabi {
29 namespace array {
30 
36 class 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 
74  explicit value(array::view view);
75 
76  value(const value&);
77  value& operator=(const value&);
78 
79  value(value&&) noexcept = default;
80  value& operator=(value&&) noexcept = default;
81 
85  BSONCXX_INLINE array::view view() const noexcept;
86 
92  BSONCXX_INLINE operator array::view() const noexcept;
93 
103  unique_ptr_type release();
104 
105  private:
106  unique_ptr_type _data;
107  std::size_t _length{0};
108 };
109 
110 BSONCXX_INLINE array::view value::view() const noexcept {
111  return array::view{static_cast<uint8_t*>(_data.get()), _length};
112 }
113 
114 BSONCXX_INLINE value::operator array::view() const noexcept {
115  return view();
116 }
117 
118 } // namespace array
119 } // namespace v_noabi
120 } // namespace bsoncxx
121 
122 #include <bsoncxx/config/postlude.hpp>
A read-only BSON array that owns its underlying buffer.
Definition: value.hpp:36
unique_ptr_type release()
Transfer ownership of the underlying buffer to the caller.
value(array::view view)
Constructs a value from a view of an array.
array::view view() const noexcept
Get a view over the document owned by this value.
Definition: value.hpp:110
value(unique_ptr_type ptr, std::size_t length)
Constructs a value from a std::unique_ptr to a buffer.
value(std::uint8_t *data, std::size_t length, deleter_type dtor)
Constructs a value from a buffer.
A read-only, non-owning view of a BSON document.
Definition: view.hpp:36
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19