Hello all, I'm a newbie to boost serialization and i need some help. I'm using boost serialization, to serialize a class, which contains 3 std::string members. After i serialize i add it to a database. The database I'm using is berkeley-db. The database API needs key/data pair, both in void* type. Now for my problem, the key is std::string, so I'm passing in key as (void *)key.c_str(), I know its crude, but i'm just starting. Now for the data part, I've to serialize the complete class. As, boost::archive::binary_oarchive, takes ostream& as argument for constructor, I've done like this. std::ostringstream os ; boost::archive::binary_oarchive oa (os) ; oa << class_obj ; so the class_obj has been written to os. so i'm sending data to db api as (void *)os.str().c_str(). To retrieve data, I'm using something like this data=db.get_data() ; istringstream is((char *)data) ; boost::archive::binary_iarchive (is) ; The code compiled fine and at run time i'm getting exception basic::string::resize during construction of binary_iarchive. Obviously i was doing wrong. My question is how to serialize data and write it to memory than to a file? and how do i access it again? Thanks in advance, Surya
Surya Kiran Gullapalli wrote:
std::ostringstream os ; boost::archive::binary_oarchive oa (os) ; oa << class_obj ;
This could well be a problem. binary archives require that the associated stream i/o be opened with the ios_base_binary flag. My documentation for ostrstream shows the following prototype. ostrstream( ); ostrstream( char *_Ptr, streamsize _Count, ios_base::openmode _Mode = ios_base::out );As test, you might try text_?archive and see if the problem goes away.Robert Ramey
I've modified my code and did something like this. std::stringstream ss ; boost::archive::binary_oarchive oa (ss) ; os << class_obj ; so i'm sending data to db api as (void *)ss. To retrieve data, I'm using something like this data=db.get_data() ; istringstream is (ios_base::binary) ; is << (char *)data.get_data() ; boost::archive::binary_iarchive (is) ; This time I'm getting stream_error exception. what I'm not able to figure out was how can i create a stringstream object from void * pointer. Thanks, Surya
Raw pointers to prmitives can't be serialized without some extra effort. In your case, the easiet would be to create a string from your char data and serialize it. When you load it, deserialize to a string then take the pointer. Robert Ramey Surya Kiran Gullapalli wrote:
I've modified my code and did something like this.
std::stringstream ss ; boost::archive::binary_oarchive oa (ss) ; os << class_obj ;
so i'm sending data to db api as (void *)ss.
To retrieve data, I'm using something like this
data=db.get_data() ; istringstream is (ios_base::binary) ; is << (char *)data.get_data() ; boost::archive::binary_iarchive (is) ;
This time I'm getting stream_error exception.
what I'm not able to figure out was how can i create a stringstream object from void * pointer.
Thanks, Surya
participants (2)
-
Robert Ramey
-
Surya Kiran Gullapalli