С++ how to manage bsonobjbuilder

59 views
Skip to first unread message

kotovki...@gmail.com

unread,
May 16, 2017, 5:57:51 PM5/16/17
to BSON
Hello world! I have few questions about bsonobjbuilder.
1) How can i reset whole bsonobjbuilder? Is that proper way to do that bsonBuilder.bb().reset(); ?
2) I have void* const pBytes and size_t size. received over the internet from another bsonobjbuilder. How can i initialize bsonobjbuilder with this raw data?
3) I need binary data form bsonobjbuilder to send it over the internet, is that proper way to retrieve bin. data and actual size of it? bsonBuilder.bb().buf(); bsonBuilder.len(); ?
Thanks!

Dwight Merriman

unread,
May 17, 2017, 9:56:11 AM5/17/17
to BSON


On Tuesday, May 16, 2017 at 5:57:51 PM UTC-4, kotovki...@gmail.com wrote:
Hello world! I have few questions about bsonobjbuilder.
1) How can i reset whole bsonobjbuilder? Is that proper way to do that bsonBuilder.bb().reset(); ?
 
probably not a good idea.  if it were indended a reset() at the upper class would exist.  
the best thing to do is just allocated a new bsonobjbuilder each time it is not too expensive.

Dwight Merriman

unread,
May 17, 2017, 1:57:52 PM5/17/17
to BSON

2) I have void* const pBytes and size_t size. received over the internet from another bsonobjbuilder. How can i initialize bsonobjbuilder with this raw data?

in that case it is already "built".  so instead perhaps:

    BSONObj myObj(pBytes);
    // do something with it...
 
3) I need binary data form bsonobjbuilder to send it over the internet, is that proper way to retrieve bin. data and actual size of it? bsonBuilder.bb().buf(); bsonBuilder.len(); ?
Thanks!

get a reference to a BSONObj the builder built, and then use the BSONObj methods to get the buf pointer and length for the BSONObj.

something like:

bsonobj o = myBuilder.done();
mySend( o.objdata(), o.objsize() );


 

kotovki...@gmail.com

unread,
May 17, 2017, 4:52:40 PM5/17/17
to BSON
Dwight Merriman thanks you a lot! For second case i need to continue object building that's why i need to construct bsonobjbuilder. Can you suggest me how to manage that properly?

kotovki...@gmail.com

unread,
May 17, 2017, 4:56:34 PM5/17/17
to BSON
Ok, and if i'll send data like you suggest, how can i create new bsonobjbuilder with this data? Thanks!

kotovki...@gmail.com

unread,
May 17, 2017, 5:04:46 PM5/17/17
to BSON
I resolve second issue like that. Is this correct? It works for me.

Packet::Packet( const void* const _pData, int _size )

{

    clear();

    

    auto serializedBson = _bson::bsonobj( static_cast< const char* >( _pData ) );

    mpBsonBuilder->appendElements( serializedBson );

}



Reply all
Reply to author
Forward
0 new messages