SequoiaDB
 All Classes Namespaces Files Functions Macros Pages
bsonobjiterator.h
Go to the documentation of this file.
1 
6 /* Copyright 2009 10gen Inc.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 //#include <boost/preprocessor/cat.hpp> // like the ## operator but works with __LINE__
27 namespace bson {
28 
38  public:
41  BSONObjIterator(const BSONObj& jso) {
42  int sz = jso.objsize();
43  if ( sz == 0 ) {
44  _pos = _theend = 0;
45  return;
46  }
47  _pos = jso.objdata() + 4;
48  _theend = jso.objdata() + sz - 1;
49  }
50 
51  BSONObjIterator() {
52  _pos = NULL ;
53  _theend = NULL ;
54  }
55 
56  BSONObjIterator( const char * start , const char * end ) {
57  _pos = start + 4;
58  _theend = end - 1;
59  }
60 
62  bool more() { return _pos < _theend; }
63 
66  bool moreWithEOO() { return _pos <= _theend; }
67 
70  BSONElement next( bool checkEnd ) {
71  assert( _pos <= _theend );
72  BSONElement e( _pos, checkEnd ? (int)(_theend + 1 - _pos) : -1 );
73  _pos += e.size( checkEnd ? (int)(_theend + 1 - _pos) : -1 );
74  return e;
75  }
76  BSONElement next() {
77  assert( _pos <= _theend );
78  BSONElement e(_pos);
79  _pos += e.size();
80  return e;
81  }
82  void operator++() { next(); }
83  void operator++(int) { next(); }
84 
85  BSONElement operator*() {
86  assert( _pos <= _theend );
87  return BSONElement(_pos);
88  }
89 
90  private:
91  const char* _pos;
92  const char* _theend;
93  };
94 
95 #define BSONOBJITERSORTED_DFTFIELDS 100
97  public:
98  BSONObjIteratorSorted( const BSONObj& o );
99 
101  assert( _fields );
102  if ( _fields != &_staticFields[0] )
103  delete[] _fields;
104  _fields = 0;
105  }
106 
107  bool more() {
108  return _cur < _nfields;
109  }
110 
111  BSONElement next() {
112  assert( _fields );
113  if ( _cur < _nfields )
114  return BSONElement( _fields[_cur++] );
115  return BSONElement();
116  }
117 
118  private:
119  const char ** _fields;
120  const char *_staticFields[BSONOBJITERSORTED_DFTFIELDS] ;
121  int _nfields;
122  int _cur;
123  };
124 
143 /*#define BSONForEach(e, obj) \
144  BSONObjIterator BOOST_PP_CAT(it_,__LINE__)(obj); \
145  for ( BSONElement e; \
146  (BOOST_PP_CAT(it_,__LINE__).more() ? \
147  (e = BOOST_PP_CAT(it_,__LINE__).next(), true) : \
148  false) ; ) */
149 }