MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
view.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/array/view-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <bsoncxx/v1/array/view.hpp> // IWYU pragma: export
22
23#include <cstddef>
24#include <cstdint>
25#include <iterator>
26
27#include <bsoncxx/types/bson_value/view-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
28
31
33
34namespace bsoncxx {
35namespace v_noabi {
36namespace array {
37
41class view {
42 private:
44
45 public:
46 class const_iterator;
47
52
54 view() = default;
55
59 /* explicit(false) */ view(v1::array::view const& v) : _view{v} {}
60
62 view(std::uint8_t const* data, std::size_t length) : _view{data, length} {}
63
75 explicit operator v1::array::view() const {
76 return v1::array::view{_view.data()};
77 }
78
81
83 const_iterator cend() const;
84
86 const_iterator begin() const;
87
89 const_iterator end() const;
90
102
113 v_noabi::array::element operator[](std::uint32_t i) const;
114
120 std::uint8_t const* data() const {
121 return _view.data();
122 }
123
125 std::size_t size() const {
126 return _view.size();
127 }
128
130 std::size_t length() const {
131 return _view.length();
132 }
133
140 bool empty() const {
141 return _view.empty();
142 }
143
147 operator v_noabi::document::view() const {
148 return _view;
149 }
150
157 friend bool operator==(view lhs, view rhs) {
158 return lhs._view == rhs._view;
159 }
160
161 friend bool operator!=(view lhs, view rhs) {
162 return !(lhs == rhs);
163 }
164
166};
167
175 public:
180 using reference = element&;
181 using pointer = element*;
182 using iterator_category = std::forward_iterator_tag;
183 using difference_type = std::ptrdiff_t;
184
185 const_iterator() = default;
186
187 explicit const_iterator(element const& element) : _element{element} {}
188
189 reference operator*() {
190 return _element;
191 }
192
193 pointer operator->() {
194 return &_element;
195 }
196
197 BSONCXX_ABI_EXPORT_CDECL(const_iterator&) operator++();
198
199 const_iterator operator++(int) {
200 const_iterator before(*this);
201 operator++();
202 return before;
203 }
204
211 friend bool operator==(const_iterator const& lhs, const_iterator const& rhs) {
212 return lhs._element.raw() == rhs._element.raw() && lhs._element.offset() == rhs._element.offset();
213 }
214
215 friend bool operator!=(const_iterator const& lhs, const_iterator const& rhs) {
216 return !(lhs == rhs);
217 }
218
220
221 private:
222 element _element;
223};
224
225inline v_noabi::array::element view::operator[](std::uint32_t i) const {
226 return *(this->find(i));
227}
228
230 return {};
231}
232
234 return this->cbegin();
235}
236
238 return this->cend();
239}
240
241} // namespace array
242} // namespace v_noabi
243} // namespace bsoncxx
244
245namespace bsoncxx {
246namespace v_noabi {
247
252 return {v};
253}
254
259 return v1::array::view{v};
260}
261
262} // namespace v_noabi
263} // namespace bsoncxx
264
266
Provides 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.
A const iterator over the contents of an array view.
Definition view.hpp:174
view()=default
Default constructs a view.
A non-owning, read-only BSON array.
Definition view.hpp:49
A variant view type that accesses values in serialized BSON arrays.
Definition element.hpp:46
std::uint8_t const * raw() const
Getter for the raw bson bytes the element points to.
Definition element.hpp:90
std::uint32_t offset() const
Getter for the offset into the raw bson bytes the element points to.
Definition element.hpp:108
A const iterator over the contents of an array view.
Definition view.hpp:174
friend bool operator!=(const_iterator const &lhs, const_iterator const &rhs)
Compare two const_iterators for (in)-equality.
Definition view.hpp:215
friend bool operator==(const_iterator const &lhs, const_iterator const &rhs)
Compare two const_iterators for (in)-equality.
Definition view.hpp:211
element value_type
std::iterator_traits
Definition view.hpp:179
A read-only, non-owning view of a BSON document.
Definition view.hpp:41
bool empty() const
Return true when this->length() == 5.
Definition view.hpp:140
view()=default
Default constructs a view.
std::size_t length() const
Gets the length of the underlying buffer in bytes.
Definition view.hpp:130
const_iterator begin() const
Definition view.hpp:233
const_iterator find(std::uint32_t i) const
Indexes into this BSON array. If the index is out-of-bounds, a past-the-end iterator will be returned...
view(std::uint8_t const *data, std::size_t length)
Constructs a view from a buffer. The caller is responsible for ensuring that the lifetime of the resu...
Definition view.hpp:62
view(v1::array::view const &v)
Construct with the bsoncxx::v1 equivalent.
Definition view.hpp:59
const_iterator cend() const
Definition view.hpp:229
const_iterator iterator
Equivalent to const_iterator.
Definition view.hpp:51
friend bool operator!=(view lhs, view rhs)
Compare two views for (in)-equality.
Definition view.hpp:161
friend bool operator==(view lhs, view rhs)
Compare two views for (in)-equality.
Definition view.hpp:157
v_noabi::array::element operator[](std::uint32_t i) const
Indexes into this BSON array. If the index is out-of-bounds, the invalid array::element will be retur...
Definition view.hpp:225
const_iterator cbegin() const
std::size_t size() const
Gets the length of the underlying buffer in bytes.
Definition view.hpp:125
std::uint8_t const * data() const
Access the raw bytes of the underlying array.
Definition view.hpp:120
const_iterator end() const
Definition view.hpp:237
A read-only, non-owning view of a BSON document.
Definition view.hpp:40
Declares entities representing a BSON array.
Declares entities whose ABI stability is NOT guaranteed.
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
v_noabi::array::value from_v1(v1::array::value const &v)
Convert from the bsoncxx::v1 equivalent of v.
Definition value.hpp:267
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v1::array::view.
Declares bsoncxx::v_noabi::array::view.
Provides bsoncxx::v_noabi::document::view.
Declares bsoncxx::v_noabi::types::bson_value::view.