MongoDB C++ Driver mongocxx-3.0.0
Loading...
Searching...
No Matches
view.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 <cstddef>
18#include <cstdint>
19#include <iterator>
20
21#include <bsoncxx/document/element.hpp>
22#include <bsoncxx/stdx/string_view.hpp>
23
24#include <bsoncxx/config/prelude.hpp>
25
26namespace bsoncxx {
27BSONCXX_INLINE_NAMESPACE_BEGIN
28namespace document {
29
33class BSONCXX_API view {
34 public:
35 class BSONCXX_API iterator;
36 class BSONCXX_API const_iterator;
37
43
53 view(const std::uint8_t* data, std::size_t length);
54
59
64
68 iterator begin() const;
69
73 iterator end() const;
74
90 iterator find(stdx::string_view key) const;
91
102 element operator[](stdx::string_view key) const;
103
109 const std::uint8_t* data() const;
110
119 std::size_t length() const;
120
127 bool empty() const;
128
136 friend BSONCXX_API bool BSONCXX_CALL operator==(view, view);
137 friend BSONCXX_API bool BSONCXX_CALL operator!=(view, view);
141
142 private:
143 const std::uint8_t* _data;
144 std::size_t _length;
145};
146
153class BSONCXX_API view::iterator : public std::iterator<std::forward_iterator_tag, element> {
154 public:
155 iterator();
156 explicit iterator(const element& element);
157
158 reference operator*();
159 pointer operator->();
160
161 iterator& operator++();
162 iterator operator++(int);
163
171 friend BSONCXX_API bool BSONCXX_CALL operator==(const iterator&, const iterator&);
172 friend BSONCXX_API bool BSONCXX_CALL operator!=(const iterator&, const iterator&);
176
177 private:
178 element _element;
179};
180
187class BSONCXX_API view::const_iterator
188 : public std::iterator<std::forward_iterator_tag, element, std::ptrdiff_t, const element*,
189 const element&> {
190 public:
192 explicit const_iterator(const element& element);
193
194 reference operator*();
195 pointer operator->();
196
197 const_iterator& operator++();
198 const_iterator operator++(int);
199
207 friend BSONCXX_API bool BSONCXX_CALL operator==(const const_iterator&, const const_iterator&);
208 friend BSONCXX_API bool BSONCXX_CALL operator!=(const const_iterator&, const const_iterator&);
212
213 private:
214 element _element;
215};
216
217} // namespace document
218BSONCXX_INLINE_NAMESPACE_END
219} // namespace bsoncxx
220
221#include <bsoncxx/config/postlude.hpp>
A variant view type that accesses values in serialized BSON documents.
Definition element.hpp:70
A const iterator over the contents of a document view.
Definition view.hpp:189
friend bool operator!=(const const_iterator &, const const_iterator &)
Compares two const_iterators for (in)-equality.
friend bool operator==(const const_iterator &, const const_iterator &)
Compares two const_iterators for (in)-equality.
A mutable iterator over the contents of a document view.
Definition view.hpp:153
friend bool operator!=(const iterator &, const iterator &)
Compares two iterators for in-equality.
friend bool operator==(const iterator &, const iterator &)
Compares two iterators for in-equality.
A read-only, non-owning view of a BSON document.
Definition view.hpp:33
iterator find(stdx::string_view key) const
Finds the first element of the document with the provided key.
const std::uint8_t * data() const
Access the raw bytes of the underlying document.
const_iterator cbegin() const
friend bool operator==(view, view)
Compare two document views for (in)-equality.
view()
Default constructs a view.
view(const std::uint8_t *data, std::size_t length)
Constructs a view from a buffer.
std::size_t length() const
Gets the length of the underlying buffer.
bool empty() const
Checks if the underlying document is empty, i.e.
iterator end() const
friend bool operator!=(view, view)
Compare two document views for (in)-equality.
iterator begin() const
element operator[](stdx::string_view key) const
Finds the first element of the document with the provided key.
const_iterator cend() const