MongoDB C++ Driver 4.2.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
41class value {
42 private:
44
45 template <typename T>
46 struct is_valid_deleter : std::is_constructible<v1::document::value, std::uint8_t*, T> {};
47
48 public:
51
54
57
60
63
65 ~value() = default;
66
68 value(value&& other) noexcept = default;
69
71 value& operator=(value&& other) noexcept = default;
72
74 value(value const& other) : _value(other._value) {}
75
78 this->reset(view);
79 return *this;
80 }
81
83 value& operator=(value const& other) {
84 _value = other._value;
85 return *this;
86 }
87
89 value() = default;
90
92 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
93 value(std::uint8_t* data, Deleter deleter) : _value{data, std::move(deleter)} {}
94
96 template <typename Deleter, detail::enable_if_t<is_valid_deleter<Deleter>::value>* = nullptr>
97 value(std::uint8_t* data, std::size_t length, Deleter deleter) : _value{data, length, std::move(deleter)} {}
98
100 explicit value(std::uint8_t* data) : _value{data} {}
101
103 value(std::uint8_t* data, std::size_t length) : _value{data, length} {}
104
106 explicit value(unique_ptr_type ptr) : _value{std::move(ptr)} {}
107
109 value(unique_ptr_type ptr, std::size_t length) : _value{std::move(ptr), length} {}
110
112 explicit value(v1::array::view view) : _value{view} {}
113
115 deleter_type const& get_deleter() const {
116 return _value.get_deleter();
117 }
118
121 return _value.release();
122 }
123
125 void reset(value v) {
126 _value = std::move(v._value);
127 }
128
131 *this = value{v};
132 }
133
138 return v1::array::view{_value.data()};
139 }
140
144 /* explicit(false) */ operator v1::array::view() const {
145 return this->view();
146 }
147
150 return this->view().cbegin();
151 }
152
155 return this->view().cend();
156 }
157
160 return this->view().begin();
161 }
162
165 return this->view().end();
166 }
167
169 v1::array::view::const_iterator find(std::uint32_t i) const {
170 return this->view().find(i);
171 }
172
174 v1::element::view operator[](std::uint32_t i) const {
175 return this->view()[i];
176 }
177
179 std::uint8_t const* data() const {
180 return this->view().data();
181 }
182
184 std::size_t size() const {
185 return this->view().size();
186 }
187
189 std::size_t length() const {
190 return this->view().length();
191 }
192
194 bool empty() const {
195 return this->view().empty();
196 }
197
199 explicit operator bool() const {
200 return this->view().operator bool();
201 }
202
204 friend bool operator==(value const& lhs, value const& rhs) {
205 return lhs.view() == rhs.view();
206 }
207
209 friend bool operator!=(value const& lhs, value const& rhs) {
210 return !(lhs == rhs);
211 }
212};
213
214} // namespace array
215} // namespace v1
216} // namespace bsoncxx
217
219
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:154
value(value const &other)
Copy construction.
Definition value.hpp:74
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:97
value(unique_ptr_type ptr)
Initialize as owning ptr.
Definition value.hpp:106
value & operator=(value const &other)
Copy assignment.
Definition value.hpp:83
deleter_type const & get_deleter() const
Return the current deleter.
Definition value.hpp:115
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition value.hpp:184
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition value.hpp:59
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:204
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:169
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:159
v1::array::view view() const
Return a view of the BSON bytes as an array.
Definition value.hpp:137
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:149
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition value.hpp:189
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition value.hpp:179
~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:100
value & operator=(v1::array::view view)
Equivalent to this->reset(view).
Definition value.hpp:77
v1::document::value::deleter_type deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:50
void reset(value v)
Replace the underlying BSON bytes with v.
Definition value.hpp:125
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:103
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition value.hpp:194
unique_ptr_type release()
Release ownership of the underlying BSON bytes.
Definition value.hpp:120
const_iterator iterator
Equivalent to const_iterator.
Definition value.hpp:62
value(std::uint8_t *data, Deleter deleter)
Initialize as owning data which will be freed with deleter.
Definition value.hpp:93
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:164
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:174
value(v1::array::view view)
Initialize with a copy of the BSON bytes referenced by view.
Definition value.hpp:112
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:209
void reset(v1::array::view v)
Replace the underlying BSON bytes with a copy of v.
Definition value.hpp:130
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:53
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:56
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:109
A non-owning, read-only BSON array.
Definition view.hpp:49
std::uint8_t const * data() const
Return a pointer to the BSON bytes being represented.
Definition view.hpp:71
const_iterator begin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:106
std::size_t length() const
Return the length of the BSON bytes being represented.
Definition view.hpp:81
const_iterator cbegin() const
Return a const iterator to the beginning of the range of BSON elements within this view.
Definition view.hpp:96
bool empty() const
Return true when the BSON bytes represents an empty view:
Definition view.hpp:86
v1::document::view::const_iterator const_iterator
A const iterator over the elements of a view.
Definition view.hpp:52
std::size_t size() const
Return the length of the BSON bytes being represented.
Definition view.hpp:76
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:101
const_iterator end() const
Return a const iterator to the end of the range of BSON elements within this view.
Definition view.hpp:111
A BSON document.
Definition value.hpp:46
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:84
std::function< void(std::uint8_t *)> deleter_type
The type of the deleter used to free the underlying BSON bytes.
Definition value.hpp:64
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:69
A const iterator over the elements of a view.
Definition view.hpp:274
A non-owning, read-only BSON element.
Definition view.hpp:84
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.