[Serialization] Is this example legal usage of the serialization library

Hi, Is it ok to serialize/deserialize in the object I get when dereference a pointer ? void f() { B* bObj=new B(); /// save { std::ofstream ofs("C:\\hh.txt"); boost::archive::xml_oarchive oa(ofs); oa & make_nvp("bObj",*bObj); } //// Load { std::ifstream ifs("C:\\hh.txt"); boost::archive::xml_iarchive ia(ifs); ia & make_nvp("bObj",*bObj); } } My second question : Is it ok to serialize/deserialize in *this ? Thanks

Elizabeta wrote:
Hi,
Is it ok to serialize/deserialize in the object I get when dereference a pointer ?
void f() {
B* bObj=new B();
/// save { std::ofstream ofs("C:\\hh.txt"); boost::archive::xml_oarchive oa(ofs); oa & make_nvp("bObj",*bObj); } //// Load { std::ifstream ifs("C:\\hh.txt");
boost::archive::xml_iarchive ia(ifs); ia & make_nvp("bObj",*bObj); } }
My second question : Is it ok to serialize/deserialize in *this ?
Thanks
I'm going to rephrase the question for my own convenience: "How is the serialization library intended to be used to do the equivalent of: stream << *x ?" Here is my answer to my own question. Serializing a data object in a way that most people intend requires that the object recreated be "equivalent or substitutable" for the original object. When an object includes a pointer, it becomes clear that this is not so easy. There are a number of issues the crop up - including a) the fact that pointer to class X might really be refering to some derived class (virtual inheritance) b) there might be multiple pointers refering to the same object. c) same problems for references d) an the fact that all this might be going on simultaneously. The usage of ar << *x or ar << *this is an indication that the user is likely to "re-implement" some of the serialization library. This might occur because the simple interface of the library suggests that it doesn't do much and this is not at all true. Sooooo if you find yourself wanting to do this, step back and think about why you want to do this. Look into the library documentation and examples to see how you might get what you want without doing this. If it's still not clear, it's likely that your overlooking something that the library does that is not obvious. If you still feel you have to do this, I've failed in my task. So the short answer to my phrasing of your question is: "How is the serialization library intended to be used to do the equivalent of: stream << *x ?" ar << x This DOES do ar << *x plus everything else that has to be done to make such an operation make any sense at all. Robert Ramey

Hi ,
Thanks for the explanation, and for the great library.
I never thought about that, that the pointer may be referenced by other
objects, or maybe refering to some derived object.
Here is why I want to do that :
I have project saving/loading feature implemented with boost serialization
library. As part of my project saving , I need to save state for the dockbar
controls too ( things like are buttons enabled or disabled).
But because the saving and loading is on different thread I just can't say :
ar<< dockBarPtr;
because than the serialization library will create my dockbar widget in
thread other than the main thread which is error.
So far I am doing this and everything works fine, but I was afraid that am
doing something prone to error, which is true, as I suspect . I guess it is
time to rethink this
On Thu, Aug 26, 2010 at 8:52 PM, Robert Ramey
Elizabeta wrote:
Hi,
Is it ok to serialize/deserialize in the object I get when dereference a pointer ?
void f() {
B* bObj=new B();
/// save { std::ofstream ofs("C:\\hh.txt"); boost::archive::xml_oarchive oa(ofs); oa & make_nvp("bObj",*bObj); } //// Load { std::ifstream ifs("C:\\hh.txt");
boost::archive::xml_iarchive ia(ifs); ia & make_nvp("bObj",*bObj); } }
My second question : Is it ok to serialize/deserialize in *this ?
Thanks
I'm going to rephrase the question for my own convenience:
"How is the serialization library intended to be used to do the equivalent of: stream << *x ?"
Here is my answer to my own question.
Serializing a data object in a way that most people intend requires that the object recreated be "equivalent or substitutable" for the original object. When an object includes a pointer, it becomes clear that this is not so easy. There are a number of issues the crop up - including
a) the fact that pointer to class X might really be refering to some derived class (virtual inheritance) b) there might be multiple pointers refering to the same object. c) same problems for references d) an the fact that all this might be going on simultaneously.
The usage of ar << *x or ar << *this is an indication that the user is likely to "re-implement" some of the serialization library. This might occur because the simple interface of the library suggests that it doesn't do much and this is not at all true. Sooooo if you find yourself wanting to do this, step back and think about why you want to do this. Look into the library documentation and examples to see how you might get what you want without doing this. If it's still not clear, it's likely that your overlooking something that the library does that is not obvious. If you still feel you have to do this, I've failed in my task.
So the short answer to my phrasing of your question is:
"How is the serialization library intended to be used to do the equivalent of: stream << *x ?"
ar << x
This DOES do ar << *x plus everything else that has to be done to make such an operation make any sense at all.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

elizabeta petreska wrote:
Hi ,
Thanks for the explanation, and for the great library.
I never thought about that, that the pointer may be referenced by other objects, or maybe refering to some derived object.
Here is why I want to do that : I have project saving/loading feature implemented with boost serialization library. As part of my project saving , I need to save state for the dockbar controls too ( things like are buttons enabled or disabled). But because the saving and loading is on different thread I just can't say :
ar<< dockBarPtr;
because than the serialization library will create my dockbar widget in thread other than the main thread which is error.
I don't see how doing ar << *dockBarPtr would address this. I'm guessing that this is a windows program as I've had similar problems. I ended up creating a custom windows message. I post the message to the main window. When the UI thread gets the message, it does serialization or whatever you want on the UI thread. Of course this has nothing to do with serialization.
So far I am doing this and everything works fine, but I was afraid that am doing something prone to error, which is true, as I suspect . I guess it is time to rethink this
yep - that's why we get paid the big bucks. Robert Ramey
participants (3)
-
Elizabeta
-
elizabeta petreska
-
Robert Ramey