MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
element.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/document/element-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <bsoncxx/v1/element/view.hpp> // IWYU pragma: export
22
23#include <cstddef>
24#include <cstdint>
25
28#include <bsoncxx/document/view-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
29#include <bsoncxx/types-fwd.hpp>
32
33#include <bsoncxx/stdx/optional.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
35
37
38namespace bsoncxx {
39namespace v_noabi {
40namespace document {
41
52class element {
53 private:
55
56 public:
63 element() = default;
64
68 /* explicit(false) */ element(v1::element::view const& v) : _view{v} {}
69
73 explicit operator v1::element::view() const {
74 return _view;
75 }
76
81 explicit operator bool() const {
82 return _view.operator bool();
83 }
84
90 std::uint8_t const* raw() const {
91 return _view.raw();
92 }
93
99 std::uint32_t length() const {
100 return _view.length();
101 }
102
108 std::uint32_t offset() const {
109 return _view.offset();
110 }
111
117 std::uint32_t keylen() const {
118 return _view.keylen();
119 }
120
129
138
139#pragma push_macro("X")
140#undef X
141#define X(_name, _value) BSONCXX_ABI_EXPORT_CDECL(v_noabi::types::b_##_name) get_##_name() const;
142
152#pragma pop_macro("X")
153
163
173
180
187
203
219};
220
227
230
232inline bool operator==(v_noabi::types::bson_value::view const& lhs, element const& rhs) {
233 return rhs == lhs;
234}
235
237inline bool operator!=(element const& lhs, v_noabi::types::bson_value::view const& rhs) {
238 return !(lhs == rhs);
239}
240
242inline bool operator!=(v_noabi::types::bson_value::view const& lhs, element const& rhs) {
243 return !(lhs == rhs);
244}
245
248
249} // namespace document
250} // namespace v_noabi
251} // namespace bsoncxx
252
253namespace bsoncxx {
254namespace v_noabi {
255
256// Ambiguous whether `v1::element::view` should be converted to `v_noabi::array::element` or
257// `v_noabi::document::element.` Require users to explicitly cast to the expected type instead.
258//
259// v_noabi::document::element from_v1(v1::element::view const& v);
260
267
268} // namespace v_noabi
269} // namespace bsoncxx
270
271namespace bsoncxx {
272namespace document {
273
274using v_noabi::document::operator==;
275using v_noabi::document::operator!=;
276
277} // namespace document
278} // namespace bsoncxx
279
281
Declares bsoncxx::v_noabi::array::element.
#define BSONCXX_ABI_EXPORT_CDECL(...)
Equivalent to BSONCXX_ABI_EXPORT with BSONCXX_ABI_CDECL.
Definition export.hpp:52
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
element()=default
Construct an invalid element.
A non-owning, read-only BSON element.
Definition view.hpp:84
A polyfill for std::string_view.
Definition string_view.hpp:412
A variant view type that accesses values in serialized BSON arrays.
Definition element.hpp:46
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:52
v_noabi::types::bson_value::view get_value() const
Getter for a types::bson_value::view variant wrapper of the value portion of the element.
element operator[](v1::stdx::string_view key) const
If this element is a document, finds the first element of the document with the provided key....
std::uint32_t keylen() const
Getter for the element's key length.
Definition element.hpp:117
v_noabi::array::element operator[](std::uint32_t i) const
If this element is an array, indexes into this BSON array. If the index is out-of-bounds,...
v1::stdx::string_view key() const
Getter for the element's key.
v_noabi::types::bson_value::value type_value() const
Equivalent to get_owning_value() const.
std::uint8_t const * raw() const
Getter for the raw bson bytes the element points to.
Definition element.hpp:90
bool operator==(element const &lhs, v_noabi::types::bson_value::view const &rhs)
Convenience methods to compare for equality against a bson_value.
std::uint32_t length() const
Getter for length of the raw bson bytes the element points to.
Definition element.hpp:99
std::uint32_t offset() const
Getter for the offset into the raw bson bytes the element points to.
Definition element.hpp:108
element()=default
Construct an invalid element.
bool operator==(v_noabi::types::bson_value::view const &lhs, element const &rhs)
Convenience methods to compare for equality against a bson_value.
Definition element.hpp:232
element(v1::element::view const &v)
Construct with the bsoncxx::v1 equivalent.
Definition element.hpp:68
v_noabi::types::bson_value::view type_view() const
Equivalent to get_value() const.
bool operator!=(v_noabi::types::bson_value::view const &lhs, element const &rhs)
Convenience methods to compare for equality against a bson_value.
Definition element.hpp:242
v_noabi::types::bson_value::value get_owning_value() const
Getter for a types::bson_value::value variant wrapper of the value portion of the element....
v_noabi::type type() const
Getter for the type of the element.
bool operator!=(element const &lhs, v_noabi::types::bson_value::view const &rhs)
Convenience methods to compare for equality against a bson_value.
Definition element.hpp:237
An owning variant type that represents any BSON type.
Definition value.hpp:57
A non-owning variant that can contain any BSON type.
Definition view.hpp:62
Declares bsoncxx::v_noabi::document::element.
Declares entities representing a BSON document.
Declares entities representing a BSON document.
Declares entities whose ABI stability is NOT guaranteed.
type
An enumeration of each BSON type.
Definition types.hpp:47
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
The top-level namespace within which all bsoncxx library entities are declared.
Declares entities used to represent BSON types.
Provides bsoncxx::v1::element::view.
#define BSONCXX_V1_TYPES_XMACRO(X)
X-macro over the name and value of BSON types.
Definition id-fwd.hpp:44
Declares bsoncxx::v_noabi::document::view.
Provides std::optional-related polyfills for library API usage.
Provides std::string_view-related polyfills for library API usage.
Declares bsoncxx::v_noabi::types::bson_value::value.
Declares bsoncxx::v_noabi::types::bson_value::view.