MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
bsonobjiterator.h
1// bsonobjiterator.h
2
3/* Copyright 2009 10gen Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include <boost/preprocessor/cat.hpp> // like the ## operator but works with __LINE__
21
22#include "mongo/bson/bsonobj.h"
23#include "mongo/base/disallow_copying.h"
24
25namespace mongo {
26
38public:
42 int sz = jso.objsize();
43 if (MONGO_unlikely(sz == 0)) {
44 _pos = _theend = 0;
45 return;
46 }
47 _pos = jso.objdata() + 4;
48 _theend = jso.objdata() + sz - 1;
49 }
50
51 BSONObjIterator(const char* start, const char* end) {
52 _pos = start + 4;
53 _theend = end - 1;
54 }
55
57 bool more() {
58 return _pos < _theend;
59 }
60
63 bool moreWithEOO() {
64 return _pos <= _theend;
65 }
66
69 BSONElement next(bool checkEnd) {
70 verify(_pos <= _theend);
71
72 int maxLen = -1;
73 if (checkEnd) {
74 maxLen = _theend + 1 - _pos;
75 verify(maxLen > 0);
76 }
77
78 BSONElement e(_pos, maxLen);
79 int esize = e.size(maxLen);
80 massert(16446, "BSONElement has bad size", esize > 0);
81 _pos += esize;
82
83 return e;
84 }
85 BSONElement next() {
86 verify(_pos <= _theend);
87 BSONElement e(_pos);
88 _pos += e.size();
89 return e;
90 }
91 void operator++() {
92 next();
93 }
94 void operator++(int) {
95 next();
96 }
97
98 BSONElement operator*() {
99 verify(_pos <= _theend);
100 return BSONElement(_pos);
101 }
102
103private:
104 const char* _pos;
105 const char* _theend;
106};
107
110 MONGO_DISALLOW_COPYING(BSONIteratorSorted);
111
112public:
114 verify(_fields);
115 delete[] _fields;
116 _fields = 0;
117 }
118
119 bool more() {
120 return _cur < _nfields;
121 }
122
123 BSONElement next() {
124 verify(_fields);
125 if (_cur < _nfields)
126 return BSONElement(_fields[_cur++]);
127 return BSONElement();
128 }
129
130protected:
131 class ElementFieldCmp;
132 BSONIteratorSorted(const BSONObj& o, const ElementFieldCmp& cmp);
133
134private:
135 const char** _fields;
136 int _nfields;
137 int _cur;
138};
139
142public:
143 BSONObjIteratorSorted(const BSONObj& object);
144};
145
152public:
154};
155
174#define BSONForEach(e, obj) \
175 ::mongo::BSONObjIterator BOOST_PP_CAT(it_, __LINE__)(obj); \
176 for (::mongo::BSONElement e; \
177 (BOOST_PP_CAT(it_, __LINE__).more() ? (e = BOOST_PP_CAT(it_, __LINE__).next(), true) \
178 : false); \
179 /*nothing*/)
180}
Provides iteration of a BSONArray's BSONElements in numeric field order.
Definition bsonobjiterator.h:151
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
int size(int maxLen) const
Size of the element.
Base class implementing ordered iteration through BSONElements.
Definition bsonobjiterator.h:109
Provides iteration of a BSONObj's BSONElements in lexical field order.
Definition bsonobjiterator.h:141
iterator for a BSONObj
Definition bsonobjiterator.h:37
bool moreWithEOO()
Definition bsonobjiterator.h:63
BSONObjIterator(const BSONObj &jso)
Create an iterator for a BSON object.
Definition bsonobjiterator.h:41
BSONElement next(bool checkEnd)
Definition bsonobjiterator.h:69
bool more()
Definition bsonobjiterator.h:57
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
int objsize() const
Definition bsonobj.h:308
const char * objdata() const
Definition bsonobj.h:303
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
Definition bsonobj.h:581