Skip to content

Commit

Permalink
nicer way to create bson
Browse files Browse the repository at this point in the history
     BSONField<int> x("x");
     BSONObj o = BSON( x(5) );
     ASSERT_EQUALS( BSON( "x" << 5 ) , o );

     o = BSON( x.gt(5) );
     ASSERT_EQUALS( BSON( "x" << BSON( "$gt" << 5 ) ) , o );
  • Loading branch information
erh committed Jun 21, 2010
1 parent 66bdde3 commit a7f063d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bson/bsoninlines.h
Expand Up @@ -572,4 +572,11 @@ namespace mongo {
}

inline void BSONElement::Val(BSONObj& v) const { v = Obj(); }

template<typename T>
inline BSONFieldValue<BSONObj> BSONField<T>::query( const char * q , const T& t ) const {
BSONObjBuilder b;
b.append( q , t );
return BSONFieldValue<BSONObj>( _name , b.obj() );
}
}
55 changes: 55 additions & 0 deletions bson/bsonobjbuilder.h
Expand Up @@ -33,6 +33,48 @@ namespace mongo {
#pragma warning( disable : 4355 )
#endif

template<typename T>
class BSONFieldValue {
public:
BSONFieldValue( const string& name , const T& t ){
_name = name;
_t = t;
}

const T& value() const { return _t; }
const string& name() const { return _name; }

private:
string _name;
T _t;
};

template<typename T>
class BSONField {
public:
BSONField( const string& name , const string& longName="" )
: _name(name), _longName(longName){}
const string& name() const { return _name; }
operator string() const { return _name; }

BSONFieldValue<T> make( const T& t ) const {
return BSONFieldValue<T>( _name , t );
}

BSONFieldValue<BSONObj> gt( const T& t ) const { return query( "$gt" , t ); }
BSONFieldValue<BSONObj> lt( const T& t ) const { return query( "$lt" , t ); }

BSONFieldValue<BSONObj> query( const char * q , const T& t ) const;

BSONFieldValue<T> operator()( const T& t ) const {
return BSONFieldValue<T>( _name , t );
}

private:
string _name;
string _longName;
};

/** Utility for creating a BSONObj.
See also the BSON() and BSON_ARRAY() macros.
*/
Expand Down Expand Up @@ -527,6 +569,19 @@ namespace mongo {
return _s << l;
}

template<typename T>
BSONObjBuilderValueStream& operator<<( const BSONField<T>& f ) {
_s.endField( f.name().c_str() );
return _s;
}

template<typename T>
BSONObjBuilder& operator<<( const BSONFieldValue<T>& v ) {
append( v.name().c_str() , v.value() );
return *this;
}


/** @return true if we are using our own bufbuilder, and not an alternate that was given to us in our constructor */
bool owned() const { return &_b == &_buf; }

Expand Down
28 changes: 28 additions & 0 deletions dbtests/jsobjtests.cpp
Expand Up @@ -1512,6 +1512,33 @@ namespace JsobjTests {
}
};

class BSONFieldTests {
public:
void run(){
{
BSONField<int> x("x");
BSONObj o = BSON( x << 5 );
ASSERT_EQUALS( BSON( "x" << 5 ) , o );
}

{
BSONField<int> x("x");
BSONObj o = BSON( x.make(5) );
ASSERT_EQUALS( BSON( "x" << 5 ) , o );
}

{
BSONField<int> x("x");
BSONObj o = BSON( x(5) );
ASSERT_EQUALS( BSON( "x" << 5 ) , o );

o = BSON( x.gt(5) );
ASSERT_EQUALS( BSON( "x" << BSON( "$gt" << 5 ) ) , o );
}

}
};

class All : public Suite {
public:
All() : Suite( "jsobj" ){
Expand Down Expand Up @@ -1607,6 +1634,7 @@ namespace JsobjTests {
add< ElementSetTest >();
add< EmbeddedNumbers >();
add< BuilderPartialItearte >();
add< BSONFieldTests >();
}
} myall;

Expand Down

0 comments on commit a7f063d

Please sign in to comment.