Serialization of a complex data structure

Hi all. I've to serialize a structure that has as attributes two structures. An example is: struct Example{ Container* results_container; MyMap this_is_a_map; Example(){} ... ... }; where Container* is a pointer to another struct, defined by me, and MyMap is a c++ map. I've tried to serialize it using the shared_ptr example of boost serialization: struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ } Container* results_container; MyMap this_is_a_map; Example(){} ... ... }; and in the main i call: boost::shared_ptr<Example> myExample(new Example); ... ... // serialize it { std::ofstream ofs(filename.c_str()); boost::archive::text_oarchive oa(ofs); oa << myExample; } boost::shared_ptr<Example> myNewExample(new Example); { // open the archive std::ifstream ifs(filename.c_str()); boost::archive::text_iarchive ia(ifs); // restore the schedule from the archive ia >> myNewExample; } Now myNewExample is a well format instance of Example, but the attributes are empty. Can anyone help me? I would not have to change all files with nested structures, as are many, not just two as in the example Thanks all. Marco. -- Marco Meoni - Sbaush www.marcomeoni.net

Container* result_container is an attribute of the struct Example.
Thanks for attention.
On 4/28/10, Robert Ramey
Marco Meoni - Sbaush wrote:
Now myNewExample is a well format instance of Example, but the attributes are empty.
What do you mean by "attributes" ?
Robert Ramey
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS } Container* results_container; MyMap this_is_a_map; Example(){} ... ... };
Try specifying which members you want to serialize in the serialize function. See above. Robert Ramey

I obtain this error: c:\boost\boost_1_42\boost\serialization\access.hpp(118)
: error C2039: 'serialize' : is not a member of 'Container'
I can overcome the problem by defining the serialize method inside
container. But container is an external library, so i can't modify it. I
would serialize the entire contents of Example without touching the others
structures. I thought that smart pointers could be the right solution for
me. What i shoud do instead in your opinion?
thanks.
Marco.
On Wed, Apr 28, 2010 at 11:19 PM, Robert Ramey
Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS
} Container* results_container; MyMap this_is_a_map; Example(){} ... ... }; Try specifying which members you want to serialize in the serialize function. See above. Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

I've tried to serialize all the structure with "ar & *this;" inside
serialize function.
I've obtained a stack overflow.
Unhandled exception at 0x004660b9 in example.exe: 0xC00000FD: Stack
overflow.
Have you idea how can i serialize all my object?
On Thu, Apr 29, 2010 at 11:08 AM, Marco Meoni - Sbaush
I obtain this error: c:\boost\boost_1_42\boost\serialization\access.hpp(118) : error C2039: 'serialize' : is not a member of 'Container'
I can overcome the problem by defining the serialize method inside container. But container is an external library, so i can't modify it. I would serialize the entire contents of Example without touching the others structures. I thought that smart pointers could be the right solution for me. What i shoud do instead in your opinion?
thanks.
Marco.
On Wed, Apr 28, 2010 at 11:19 PM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS
} Container* results_container; MyMap this_is_a_map; Example(){} ... ... }; Try specifying which members you want to serialize in the serialize function. See above. Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
I've tried to serialize all the structure with "ar & *this;" inside serialize function.
Don't do this. Robert Ramey
I've obtained a stack overflow. Unhandled exception at 0x004660b9 in example.exe: 0xC00000FD: Stack overflow.
Have you idea how can i serialize all my object?
On Thu, Apr 29, 2010 at 11:08 AM, Marco Meoni - Sbaush
wrote: I obtain this error: c:\boost\boost_1_42\boost\serialization\access.hpp(118) : error C2039: 'serialize' : is not a member of 'Container'
I can overcome the problem by defining the serialize method inside container. But container is an external library, so i can't modify it. I would serialize the entire contents of Example without touching the others structures. I thought that smart pointers could be the right solution for me. What i shoud do instead in your opinion?
thanks.
Marco.
On Wed, Apr 28, 2010 at 11:19 PM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS
} Container* results_container; MyMap this_is_a_map; Example(){} ... ... };
Try specifying which members you want to serialize in the serialize function. See above.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
I obtain this error: c:\boost\boost_1_42\boost\serialization\access.hpp(118) : error C2039: 'serialize' : is not a member of 'Container'
I can overcome the problem by defining the serialize method inside container. But container is an external library, so i can't modify it. I would serialize the entire contents of Example without touching the others structures. I thought that smart pointers could be the right solution for me. What i shoud do instead in your opinion?
thanks.
Marco.
On Wed, Apr 28, 2010 at 11:19 PM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS
} Container* results_container; MyMap this_is_a_map; Example(){} ... ... };
Try specifying which members you want to serialize in the serialize function. See above.
Make sure your serialize function is in the proper namespace - see manual. Robert Ramey

i read all the boost manual, but i don't have found how serialize nested
structures editing only the first struct.
i can't put the serialize method in the Container because Container contains
a lot of other structures, and these structures contain other structures and
so on.
I wish I could serialize all serializing only the parent class
Can you help me, pointing the manual section with this, or show me a basic
example?
Thank you so much.
On Thu, Apr 29, 2010 at 6:29 PM, Robert Ramey
Marco Meoni - Sbaush wrote:
I obtain this error: c:\boost\boost_1_42\boost\serialization\access.hpp(118) : error C2039: 'serialize' : is not a member of 'Container'
I can overcome the problem by defining the serialize method inside container. But container is an external library, so i can't modify it. I would serialize the entire contents of Example without touching the others structures. I thought that smart pointers could be the right solution for me. What i shoud do instead in your opinion?
thanks.
Marco.
On Wed, Apr 28, 2010 at 11:19 PM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS
} Container* results_container; MyMap this_is_a_map; Example(){} ... ... };
Try specifying which members you want to serialize in the serialize function. See above.
Make sure your serialize function is in the proper namespace - see manual.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

Can someone help me?
Robert, where can I find what you're talking about?
I did all the tests reported in
http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/index.html but I
could not find a good solution for me. Could you help me?
On Thu, Apr 29, 2010 at 5:53 PM, Marco Meoni - Sbaush
i read all the boost manual, but i don't have found how serialize nested structures editing only the first struct.
i can't put the serialize method in the Container because Container contains a lot of other structures, and these structures contain other structures and so on.
I wish I could serialize all serializing only the parent class
Can you help me, pointing the manual section with this, or show me a basic example? Thank you so much.
On Thu, Apr 29, 2010 at 6:29 PM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
I obtain this error: c:\boost\boost_1_42\boost\serialization\access.hpp(118) : error C2039: 'serialize' : is not a member of 'Container'
I can overcome the problem by defining the serialize method inside container. But container is an external library, so i can't modify it. I would serialize the entire contents of Example without touching the others structures. I thought that smart pointers could be the right solution for me. What i shoud do instead in your opinion?
thanks.
Marco.
On Wed, Apr 28, 2010 at 11:19 PM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
struct Example{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & results_container; // INCLUDE THIS
} Container* results_container; MyMap this_is_a_map; Example(){} ... ... };
Try specifying which members you want to serialize in the serialize function. See above.
Make sure your serialize function is in the proper namespace - see manual.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
Can someone help me? Robert, where can I find what you're talking about?
I don't know what you're trying to do so I'll take a guess. Does this look like what you want to do? struct Container { int m_container_member; ... // more members template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container_member; ... // more members } }; struct Example { friend class boost::serialization::access; // not really necessary as all "struct" members are public template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container; } }; int main(int argc, char * argv[]){ const Container c = ... std::fostream os("filename"); boost::serialization::archive ar(os); ar & c; } Does that help any? Robert Ramey

Yes.
I did an example like this to learn how to serialize in the "classical"
way.
My problem is that i can't define a serialize method inside Container, but i
would serialize it anyway. How can i serialize the Example strut entirely?
Do you understand my problem?
On Fri, Apr 30, 2010 at 3:49 AM, Robert Ramey
Marco Meoni - Sbaush wrote:
Can someone help me? Robert, where can I find what you're talking about?
I don't know what you're trying to do so I'll take a guess.
Does this look like what you want to do?
struct Container { int m_container_member; ... // more members template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container_member; ... // more members } };
struct Example { friend class boost::serialization::access; // not really necessary as all "struct" members are public template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container; } };
int main(int argc, char * argv[]){ const Container c = ... std::fostream os("filename"); boost::serialization::archive ar(os); ar & c; }
Does that help any?
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
Yes. I did an example like this to learn how to serialize in the "classical" way.
My problem is that i can't define a serialize method inside Container, but i would serialize it anyway. How can i serialize the Example strut entirely?
Do you understand my problem?
Please don't top post. How does: http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/tutorial.html#no... differ from what you want to do? Jeff

I read about the non-intrusive serialization but my problem is:
in non-intrusive example the code for serialization is
void serialize(Archive & ar, gps_position & g, const unsigned int version) {
ar & g.degrees;
ar & g.minutes;
ar & g.seconds;
}
In my example i should do this:
ar & results_container;
result_container is not a STL type, but is a Container*. So i should declare
in Container body the serialization method (as suggested by Robert). Is this
right?
The big problem is that i can't write in Container body, because is not a
mine class, but is a lib class.
How can i serialize all my members in the same way of STL type, without
worring about nested serialize methods?
Is the asked question clear?
thank you so much.
On Fri, Apr 30, 2010 at 2:55 PM, Jeff Flinn
Marco Meoni - Sbaush wrote:
Yes. I did an example like this to learn how to serialize in the "classical" way. My problem is that i can't define a serialize method inside Container, but i would serialize it anyway. How can i serialize the Example strut entirely?
Do you understand my problem?
Please don't top post. How does:
http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/tutorial.html#no...
differ from what you want to do?
Jeff
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

Try looking inside boost/serialization/vector.hpp to see how it was done for the STL containers. Robert Ramey Marco Meoni - Sbaush wrote:
Yes. I did an example like this to learn how to serialize in the "classical" way.
My problem is that i can't define a serialize method inside Container, but i would serialize it anyway. How can i serialize the Example strut entirely?
Do you understand my problem?
On Fri, Apr 30, 2010 at 3:49 AM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
Can someone help me? Robert, where can I find what you're talking about?
I don't know what you're trying to do so I'll take a guess.
Does this look like what you want to do?
struct Container { int m_container_member; ... // more members template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container_member; ... // more members } };
struct Example { friend class boost::serialization::access; // not really necessary as all "struct" members are public template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container; } };
int main(int argc, char * argv[]){ const Container c = ... std::fostream os("filename"); boost::serialization::archive ar(os); ar & c; }
Does that help any?
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I don't know if I understood correctly but I understood that the key
function of vector serialization is split_free. I did not understand how to
use this technique in the case of my example. Can you help me understand the
workings of the vector serialization so as to know how to extend it to
classes? What can i read to understand how to solve?
Thanks...
Marco.
On Fri, Apr 30, 2010 at 6:08 PM, Robert Ramey
Try looking inside boost/serialization/vector.hpp to see how it was done for the STL containers.
Robert Ramey
Marco Meoni - Sbaush wrote:
Yes. I did an example like this to learn how to serialize in the "classical" way.
My problem is that i can't define a serialize method inside Container, but i would serialize it anyway. How can i serialize the Example strut entirely?
Do you understand my problem?
On Fri, Apr 30, 2010 at 3:49 AM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
Can someone help me? Robert, where can I find what you're talking about?
I don't know what you're trying to do so I'll take a guess.
Does this look like what you want to do?
struct Container { int m_container_member; ... // more members template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container_member; ... // more members } };
struct Example { friend class boost::serialization::access; // not really necessary as all "struct" members are public template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container; } };
int main(int argc, char * argv[]){ const Container c = ... std::fostream os("filename"); boost::serialization::archive ar(os); ar & c; }
Does that help any?
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

Hi all.
I understood how to serialize correctly nested structures, defining
serialize method inside all.
I've a problem with an attribute of a struct. The attribute is border of
OpenCV CvSeq type.
Here is my code... what can i do to solve this?
struct Element{
unsigned int id;
double area;
double perimeter;
CvRect bounding_box;
CvSeq* border;
...
...
};
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, Element & g, const unsigned int version)
{
ar & g.id;
ar & g.area ;
ar & g.perimeter ;
ar & g.bounding_box.width ;
ar & g.bounding_box.height ;
ar & g.bounding_box.x ;
ar & g.bounding_box.y ;
// ar & g.border; I CAN'T DO THIS...
}
}
}
The error that I obtain
is: c:\boost\boost_1_42\boost\serialization\access.hpp(118) : error C2039:
'serialize' : is not a member of 'CvSeq'
c:\opencv2.0\include\opencv\cxtypes.h(1240) : see declaration of
'CvSeq'
For CvRect bouding:box i overcame the problem serializing the sub-elements
of CvRect.
thanks for help.
Marco.
On Mon, May 3, 2010 at 3:39 PM, Marco Meoni - Sbaush
I don't know if I understood correctly but I understood that the key function of vector serialization is split_free. I did not understand how to use this technique in the case of my example. Can you help me understand the workings of the vector serialization so as to know how to extend it to classes? What can i read to understand how to solve?
Thanks...
Marco.
On Fri, Apr 30, 2010 at 6:08 PM, Robert Ramey
wrote: Try looking inside boost/serialization/vector.hpp to see how it was done for the STL containers.
Robert Ramey
Marco Meoni - Sbaush wrote:
Yes. I did an example like this to learn how to serialize in the "classical" way.
My problem is that i can't define a serialize method inside Container, but i would serialize it anyway. How can i serialize the Example strut entirely?
Do you understand my problem?
On Fri, Apr 30, 2010 at 3:49 AM, Robert Ramey
wrote: Marco Meoni - Sbaush wrote:
Can someone help me? Robert, where can I find what you're talking about?
I don't know what you're trying to do so I'll take a guess.
Does this look like what you want to do?
struct Container { int m_container_member; ... // more members template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container_member; ... // more members } };
struct Example { friend class boost::serialization::access; // not really necessary as all "struct" members are public template<class Archive> void serialize(Archive & ar, const unsigned int version){ ar & m_container; } };
int main(int argc, char * argv[]){ const Container c = ... std::fostream os("filename"); boost::serialization::archive ar(os); ar & c; }
Does that help any?
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
Hi all. I understood how to serialize correctly nested structures, defining serialize method inside all.
I've a problem with an attribute of a struct. The attribute is border of OpenCV CvSeq type.
Here is my code... what can i do to solve this?
struct Element{ unsigned int id; double area; double perimeter; CvRect bounding_box; CvSeq* border; ... ... };
namespace boost { namespace serialization {
template<class Archive> void serialize(Archive & ar, Element & g, const unsigned int version) { ar & g.id http://g.id; ar & g.area ; ar & g.perimeter ; ar & g.bounding_box.width ; ar & g.bounding_box.height ; ar & g.bounding_box.x ; ar & g.bounding_box.y ; // ar & g.border; I CAN'T DO THIS...
You need to do the same for CvSeq that you did for Element: template<class Archive> void serialize(Archive& ar, CvSeq& aCvSeq , const unsigned int) { ... Jeff

Yes, ok, but i can't access or write in OpenCV source code.
How can i serialize however?
On Mon, May 10, 2010 at 4:02 PM, Jeff Flinn
Marco Meoni - Sbaush wrote:
Hi all. I understood how to serialize correctly nested structures, defining serialize method inside all. I've a problem with an attribute of a struct. The attribute is border of OpenCV CvSeq type.
Here is my code... what can i do to solve this?
struct Element{ unsigned int id; double area; double perimeter; CvRect bounding_box; CvSeq* border; ... ... };
namespace boost { namespace serialization {
template<class Archive> void serialize(Archive & ar, Element & g, const unsigned int version) { ar & g.id http://g.id;
ar & g.area ; ar & g.perimeter ; ar & g.bounding_box.width ; ar & g.bounding_box.height ; ar & g.bounding_box.x ; ar & g.bounding_box.y ; // ar & g.border; I CAN'T DO THIS...
You need to do the same for CvSeq that you did for Element:
template<class Archive> void serialize(Archive& ar, CvSeq& aCvSeq , const unsigned int) { ...
Jeff
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Marco Meoni - Sbaush www.marcomeoni.net

Marco Meoni - Sbaush wrote:
Yes, ok, but i can't access or write in OpenCV source code. How can i serialize however?
Top posting makes it difficult to keep this thread readable. I've no idea what OpenCV is, but I would assume the class you want to serialize has accessors to retrieve data relevant to constructing an equivalent instance. Serialize those data items, then you deserialize those items and use them to construct the equivalent instance. Jeff
participants (3)
-
Jeff Flinn
-
Marco Meoni - Sbaush
-
Robert Ramey