Link to home
Start Free TrialLog in
Avatar of shootah
shootah

asked on

Boost Archive intialization as a class member

I am currently serializing data in a class in which a member function is called and passed the data and I declare a static boost::archive in that function and serialize the data to a binary file.  This works fine when I deserialize (in a while loop with EOF condition) but now I would like to serialize to the same file/archive from other functions in the class.  The Boost tutorials do not show any other way to intialize an archive.  

Basically I want to declare a boost::archive::binary_oarchive as a member variable.  Problem is, how do I initialize it, say, in the constructor after I have opened a file stream?  My instinct tells me this is not allowed, but I was hoping there was a way to do this.

 (FYI, I've tried to serialize by declaring different instances of the archive file in each function with a common file stream, but on deserialization, i get an "invalid signature" exception).
SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shootah
shootah

ASKER

I have tried this in the past.  This is a bit embarassing because this question is probably trivial, but how do I use the insertion operator for an archive pointer (I guess for any pointer that overloads the insertion operator)? "ar<<data" doesn't compile:

"no match for ‘operator<<’ in ‘((CDmsoHlaFederateAmbassador*)this)->CDmsoHlaFederateAmbassador::m_ar << data’"

If I'm not getting what you are asking me to do, maybe you can post an example?  Here is what I've implemented more or less.





 
typedef boost::archive::binary_oarchive oarchive_t;

class SomeClass{

public:
std::ofstream m_ofStream;   
oarchive_t* m_ar;

SomeClass(std::string sRecordFileName){

m_ofStream.open(sRecordFileName.c_str(), std::ios::binary);
oarchive_t* oa = new oarchive_t(m_ofStream);
m_ar = oa;
}

Foo(SerializeableClass data){
  
  m_ar<<data;

}

}

Open in new window

>>"ar<<data" doesn't compile

Well, that should simply be '(*m_ar)<<data;' - since you are using a pointer, you need to dereference it.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shootah

ASKER

I love experts exchange.  It magnifies my inept programming knowledge every time.  :)

jkr,
  I implemented evilrix's suggestion with your help and it works.  I tried your second implementation as well but there seems to be an exception thrown.  I'm not sure what the exception is because it falls into the catch all.  It does not get caught by (boost::archive::archive_exception) so maybe it's a stream exception or something.  Creating the archive off the stack is preferable (seems cleaner I guess).  Is there a way to discover the exception thrown?  Maybe via gdb?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> It does not get caught by (boost::archive::archive_exception)
All boost exceptions derive from std::exception so just catch that by reference and use the what() member to display the reason.

http://www.cplusplus.com/reference/std/exception/exception/

catch(std::exception const & e)
{
    std::cerr << e.what();
}

Open in new window

Avatar of shootah

ASKER

Yep that worked.  Thanks.
Avatar of shootah

ASKER

Last comment was in reference to jkr's suggestion btw.
Avatar of shootah

ASKER

evilrix, thanks, i'll keep that in mind in the future.
BTW: AFAIK you can't create an archive object with a stream what is not open (at least, not with my somewhat limited experience of this part of the boost library).
Avatar of shootah

ASKER

Yeah, that's what it looks like.  Once the stream was initialized per jkr's suggestion it seemed to work.
I was guessing that the binary_oarchive might check the stream state, thus the 2nd suggestion - yet I have to admit it was just a shot in the dark ;o)
Hahaha... I've been working with archive just recently so I should have realised earlier what the problem was. It only dawned on me after I posted the comment about how to catch the exception. Heh -- it's been a long day at the office :)
>>it's been a long day at the office :)

Still there and trying to ignore the urge to punch the developers of MS' 'MIDL' in the stomach ;o)
>>  trying to ignore the urge to punch the developers of MS' 'MIDL'
Don't fight it... seriously -- they deserve it :)
Indeed - especially when a line like

int BeapInit([in, string] const char* pszInit);

results in generated code that reads

/* interface Beap */
/* [auto_handle][version][uuid] */

int BeapInit(
    /* [string][in] */ const unsigned char *pszInit);

and you are looking for the reason of the 'unresolved external' errors for over an hour pulling your hair out  :-(
That'll teach you to use COM :)

So glad I am not a Windows programmer!!!
COM? Que? RPC! ;o)