MongoDB  2.7.0
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 
25 namespace mongo {
26 
38  public:
41  BSONObjIterator(const BSONObj& jso) {
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() { return _pos < _theend; }
58 
60  bool moreWithEOO() { return _pos <= _theend; }
61 
63  BSONElement next( bool checkEnd ) {
64  verify( _pos <= _theend );
65 
66  int maxLen = -1;
67  if ( checkEnd ) {
68  maxLen = _theend + 1 - _pos;
69  verify( maxLen > 0 );
70  }
71 
72  BSONElement e( _pos, maxLen );
73  int esize = e.size( maxLen );
74  massert( 16446, "BSONElement has bad size", esize > 0 );
75  _pos += esize;
76 
77  return e;
78  }
79  BSONElement next() {
80  verify( _pos <= _theend );
81  BSONElement e(_pos);
82  _pos += e.size();
83  return e;
84  }
85  void operator++() { next(); }
86  void operator++(int) { next(); }
87 
88  BSONElement operator*() {
89  verify( _pos <= _theend );
90  return BSONElement(_pos);
91  }
92 
93  private:
94  const char* _pos;
95  const char* _theend;
96  };
97 
100  MONGO_DISALLOW_COPYING(BSONIteratorSorted);
101  public:
102  ~BSONIteratorSorted() {
103  verify( _fields );
104  delete[] _fields;
105  _fields = 0;
106  }
107 
108  bool more() {
109  return _cur < _nfields;
110  }
111 
112  BSONElement next() {
113  verify( _fields );
114  if ( _cur < _nfields )
115  return BSONElement( _fields[_cur++] );
116  return BSONElement();
117  }
118 
119  protected:
120  class ElementFieldCmp;
121  BSONIteratorSorted( const BSONObj &o, const ElementFieldCmp &cmp );
122 
123  private:
124  const char ** _fields;
125  int _nfields;
126  int _cur;
127  };
128 
131  public:
132  BSONObjIteratorSorted( const BSONObj &object );
133  };
134 
141  public:
142  BSONArrayIteratorSorted( const BSONArray &array );
143  };
144 
163 #define BSONForEach(e, obj) \
164  BSONObjIterator BOOST_PP_CAT(it_,__LINE__)(obj); \
165  for ( BSONElement e; \
166  (BOOST_PP_CAT(it_,__LINE__).more() ? \
167  (e = BOOST_PP_CAT(it_,__LINE__).next(), true) : \
168  false) ; \
169  /*nothing*/ )
170 
171 }
int size(int maxLen) const
Size of the element.
Definition: bson-inl.h:491
const char * objdata() const
Definition: bsonobj.h:266
Base class implementing ordered iteration through BSONElements.
Definition: bsonobjiterator.h:99
int objsize() const
Definition: bsonobj.h:270
bool moreWithEOO()
Definition: bsonobjiterator.h:60
BSONObjIterator(const BSONObj &jso)
Create an iterator for a BSON object.
Definition: bsonobjiterator.h:41
Provides iteration of a BSONObj's BSONElements in lexical field order.
Definition: bsonobjiterator.h:130
iterator for a BSONObj
Definition: bsonobjiterator.h:37
bool more()
Definition: bsonobjiterator.h:57
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:62
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
Provides iteration of a BSONArray's BSONElements in numeric field order.
Definition: bsonobjiterator.h:140
BSONElement next(bool checkEnd)
Definition: bsonobjiterator.h:63
Definition: bsonobj.h:585