MongoDB C++ Driver legacy-1.0.1
Loading...
Searching...
No Matches
embedded_builder.h
1// embedded_builder.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
20namespace mongo {
21
22 // utility class for assembling hierarchical objects
24 public:
26 _builders.push_back( std::make_pair( "", b ) );
27 }
28 // It is assumed that the calls to prepareContext will be made with the 'name'
29 // parameter in lex ascending order.
30 void prepareContext( std::string &name ) {
31 int i = 1, n = _builders.size();
32 while( i < n &&
33 name.substr( 0, _builders[ i ].first.length() ) == _builders[ i ].first &&
34 ( name[ _builders[i].first.length() ] == '.' || name[ _builders[i].first.length() ] == 0 )
35 ) {
36 name = name.substr( _builders[ i ].first.length() + 1 );
37 ++i;
38 }
39 for( int j = n - 1; j >= i; --j ) {
40 popBuilder();
41 }
42 for( std::string next = splitDot( name ); !next.empty(); next = splitDot( name ) ) {
43 addBuilder( next );
44 }
45 }
46 void appendAs( const BSONElement &e, std::string name ) {
47 if ( e.type() == Object && e.valuesize() == 5 ) { // empty object -- this way we can add to it later
48 std::string dummyName = name + ".foo";
49 prepareContext( dummyName );
50 return;
51 }
52 prepareContext( name );
53 back()->appendAs( e, name );
54 }
55 BufBuilder &subarrayStartAs( std::string name ) {
56 prepareContext( name );
57 return back()->subarrayStart( name );
58 }
59 void done() {
60 while( ! _builderStorage.empty() )
61 popBuilder();
62 }
63
64 static std::string splitDot( std::string & str ) {
65 size_t pos = str.find( '.' );
66 if ( pos == std::string::npos )
67 return "";
68 std::string ret = str.substr( 0, pos );
69 str = str.substr( pos + 1 );
70 return ret;
71 }
72
73 private:
74 void addBuilder( const std::string &name ) {
75 boost::shared_ptr< BSONObjBuilder > newBuilder( new BSONObjBuilder( back()->subobjStart( name ) ) );
76 _builders.push_back( std::make_pair( name, newBuilder.get() ) );
77 _builderStorage.push_back( newBuilder );
78 }
79 void popBuilder() {
80 back()->done();
81 _builders.pop_back();
82 _builderStorage.pop_back();
83 }
84
85 BSONObjBuilder *back() { return _builders.back().second; }
86
87 std::vector< std::pair< std::string, BSONObjBuilder * > > _builders;
88 std::vector< boost::shared_ptr< BSONObjBuilder > > _builderStorage;
89
90 };
91
92} //namespace mongo
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
int valuesize() const
size in bytes of the element's value (when applicable).
Definition bsonelement.h:166
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:109
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:53
BufBuilder & subarrayStart(const StringData &fieldName)
add header for a new subarray and return bufbuilder for writing to the subarray's body
Definition bsonobjbuilder.h:199
BSONObjBuilder & appendAs(const BSONElement &e, const StringData &fieldName)
append an element but with a new name
Definition bsonobjbuilder.h:131
BSONObj done()
Fetch the object we have built.
Definition bsonobjbuilder.h:623
Definition embedded_builder.h:23
the main MongoDB namespace
Definition bulk_operation_builder.h:24
@ Object
an embedded object
Definition bsontypes.h:48