MongoDB C++ Driver 4.4.0
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1// Copyright 2009-present 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 <bsoncxx/v1/array/value-fwd.hpp> // IWYU pragma: export
18
19//
20
22
23#include <bsoncxx/v1/array/view.hpp> // IWYU pragma: export
25#include <bsoncxx/v1/document/value.hpp> // IWYU pragma: export
26
27#include <cstddef>
28#include <cstdint>
29#include <type_traits>
30#include <utility>
31
32namespace bsoncxx {
33namespace v1 {
34namespace array {
35
39class value {
40 private:
42
43 template <typename T>
44 struct is_valid_deleter : std::is_constructible<v1::document::value, std::uint8_t*, T> {};
45
46 public:
49
52
55
58
61
63 ~value() = default;
64
66 value(value&& other) noexcept = default;
67
69 value& operator=(value&& other) noexcept = default;
70
72 value(value const& other) : _value(other._value) {}
73
76 this->reset(view);
77 return *this;
78 }
79
81 value& operator=(value const& other) {
82 _value = other._value;
83 return *this;
84 }
85
87 value() = default;
88
90 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
91 value(std::uint8_t* data, Deleter deleter) : _value{data, std::move(deleter)} {}
92
94 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
95 value(std::uint8_t* data, std::size_t length, Deleter deleter) : _value{data, length, std::move(deleter)} {}
96
98 explicit value(std::uint8_t* data) : _value{data} {}
99
101 value(std::uint8_t* data, std::size_t length) : _value{data, length} {}
102
104 explicit value(unique_ptr_type ptr) : _value{std::move(ptr)} {}
105
107 value(unique_ptr_type ptr, std::size_t length) : _value{std::move(ptr), length} {}
108
110 explicit value(v1::array::view view) : _value{view} {}
111
113 deleter_type const& get_deleter() const {
114 return _value.get_deleter();
115 }
116
119 return _value.release();
120 }
121
123 void reset(value v) {
124 _value = std::move(v._value);
125 }
126
129 *this = value{v};
130 }
131
136 return v1::array::view{_value.data()};
137 }
138
142 /* explicit(false) */ operator v1::array::view() const {
143 return this->view();
144 }
145
148 return this->view().cbegin();
149 }
150
153 return this->view().cend();
154 }
155
158 return this->view().begin();
159 }
160
163 return this->view().end();
164 }
165
167 v1::array::view::const_iterator find(std::uint32_t i) const {
168 return this->view().find(i);
169 }
170
172 v1::element::view operator[](std::uint32_t i) const {
173 return this->view()[i];
174 }
175
177 std::uint8_t const* data() const {
178 return this->view().data();
179 }
180
182 std::size_t size() const {
183 return this->view().size();
184 }
185
187 std::size_t length() const {
188 return this->view().length();
189 }
190
192 bool empty() const {
193 return this->view().empty();
194 }
195
197 explicit operator bool() const {
198 return this->view().operator bool();
199 }
200
202 friend bool operator==(value const& lhs, value const& rhs) {
203 return lhs.view() == rhs.view();
204 }
205
207 friend bool operator!=(value const& lhs, value const& rhs) {
208 return !(lhs == rhs);
209 }
210};
211
212} // namespace array
213} // namespace v1
214} // namespace bsoncxx
215
217
The bsoncxx v1 macro guard postlude header.
The bsoncxx v1 macro guard prelude header.
v1::array::view::const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:152
value(value const &other)
Copy construction.
Definition value.hpp:72
value(std::uint8_t *data, std::size_t length, Deleter deleter)
Equivalent to value(std::uint8_t* data, Deleter deleter), but validates the embedded length against l...
Definition value.hpp:95
value(unique_ptr_type ptr)
Initialize as owning ptr.
Definition value.hpp:104
value & operator=(value const &other)
Copy assignment.
Definition value.hpp:81
deleter_type const & get_deleter() const
Return the current deleter.
Definition value.hpp:113
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition value.hpp:182
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition value.hpp:57
friend bool operator==(value const &lhs, value const &rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition value.hpp:202
v1::array::view::const_iterator find(std::uint32_t i) const
Return a const iterator to the element within the represented BSON array at index i via key string co...
Definition value.hpp:167
v1::array::view::const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:157
v1::array::view view() const
Return a view of the BSON bytes as an array.
Definition value.hpp:135
v1::array::view::const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition value.hpp:147
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition value.hpp:187
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition value.hpp:177
~value()=default
Destroy this object.
value(std::uint8_t *data)
Initialize as owning data which will be freed with default_deleter_type.
Definition value.hpp:98
value & operator=(v1::array::view view)
Equivalent to this->reset(view).
Definition value.hpp:75
v1::document::value::deleter_type deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:48
void reset(value v)
Replace the underlying BSON bytes with v.
Definition value.hpp:123
value(std::uint8_t *data, std::size_t length)
Equivalent to value(std::uint8_t* data), but validates the embedded length against length.
Definition value.hpp:101
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition value.hpp:192
unique_ptr_type release()
Release ownership of the underlying BSON bytes.
Definition value.hpp:118
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:60
value(std::uint8_t *data, Deleter deleter)
Initialize as owning data which will be freed with deleter.
Definition value.hpp:91
v1::array::view::const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition value.hpp:162
v1::element::view operator[](std::uint32_t i) const
Return the first element within the represented BSON array whose key compares equal to i.
Definition value.hpp:172
value(v1::array::view view)
Initialize with a copy of the BSON bytes referenced by view.
Definition value.hpp:110
value & operator=(value &&other) noexcept=default
Move assignment. noexcept
friend bool operator!=(value const &lhs, value const &rhs)
Compare equal when the BSON bytes represented by lhs and rhs compare equal.
Definition value.hpp:207
void reset(v1::array::view v)
Replace the underlying BSON bytes with a copy of v.
Definition value.hpp:128
value()=default
Initialize as an empty document (or array).
value(value &&other) noexcept=default
Move construction. noexcept
v1::document::value::default_deleter_type default_deleter_type
The deleter used to free copied BSON bytes when a user-provided deleter is not specified.
Definition value.hpp:51
v1::document::value::unique_ptr_type unique_ptr_type
The type of the unique pointer used to manage the underlying BSON bytes.
Definition value.hpp:54
value(unique_ptr_type ptr, std::size_t length)
Equivalent to value(unique_ptr_type ptr), but validates the embedded length against length.
Definition value.hpp:107
A non-owning, read-only BSON array.
Definition view.hpp:47
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition view.hpp:69
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:104
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition view.hpp:79
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:94
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition view.hpp:84
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition view.hpp:50
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition view.hpp:74
const_iterator find(std::uint32_t i) const
Return a const iterator to the element within the represented BSON array at index i via key string co...
const_iterator cend() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:99
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:109
A BSON document.
Definition value.hpp:44
std::unique_ptr< std::uint8_t[], deleter_type > unique_ptr_type
The type of the unique pointer used to manage the underlying BSON bytes.
Definition value.hpp:82
std::function< void(std::uint8_t *)> deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:62
std::default_delete< std::uint8_t[]> default_deleter_type
The deleter used to free copied BSON bytes when a user-provided deleter is not specified.
Definition value.hpp:67
A const iterator over the elements of a view.
Definition view.hpp:264
A non-owning, read-only BSON element.
Definition view.hpp:82
Declares entities representing a BSON array.
Declares entities whose ABI stability is guaranteed for documented symbols.
The top-level namespace within which all bsoncxx library entities are declared.
Declares bsoncxx::v1::array::value.
Provides bsoncxx::v1::array::view.
For internal use only!
Provides bsoncxx::v1::document::value.