boost::serialization - Non intrusive serialization of user-defined members?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
Hi,
I have a class structure like

class MemberClass;

class ContainerClass {
  MemberClass special_member;
  int other_members;
};

i want to write a non-intrusive serialization:

template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
{
  ar & c.other_members; // easy - built-in type
  serialize(ar, c.special_member, version);  //use same version???
}

Assuming there's is also a non-intrusive
void serialize(Archive & ar, MemberClass & m, const unsigned int version);

this compiles and works but i feel this is not the way to do it because it reuses the version number of the container class for the member class? So how to handle that? Must the Container class manage version of its members individually?
I suggest to add a section "Non-intrusively Serializable Members" to documentation with an example.

Cheers,
Lars R.


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
On 3/5/18 5:12 AM, Lars Ruoff via Boost-users wrote:

> Hi,
> I have a class structure like
>
> class MemberClass;
>
> class ContainerClass {
>    MemberClass special_member;
>    int other_members;
> };
>
> Following the example in
> http://www.boost.org/doc/libs/1_66_0/libs/serialization/doc/
> i want to write a non-intrusive serialization:
>
> template<class Archive>
> void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
> {
>    ar & c.other_members; // easy - built-in type
>    serialize(ar, c.special_member, version);  //use same version???
> }
>
> Assuming there's is also a non-intrusive
> void serialize(Archive & ar, MemberClass & m, const unsigned int version);
>
> this compiles and works but i feel this is not the way to do it because
> it reuses the version number of the container class for the member
> class? So how to handle that? Must the Container class manage version of
> its members individually?
> I suggest to add a section "Non-intrusively Serializable Members" to
> documentation with an example.
>
> Cheers,
> Lars R.

I'm not sure what the "special member" of the container class is.
Presumably it's your own class as STL classes have no "special members".
  Assuming this to be the case there should be something like:

struct ContainerClass {
        ...
        void serialize(Achive &ar, const unsigned int v){
                ar & other_members;
                ar & special_member
        }
        ...
};

or

template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
{
        ar & c.other_members; // easy - built-in type
        ar & c.special_member;
}


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
Maybe i didn't express it clearly enough.
I want non-intrusive serialization.
I want to modify neither ContqinerClass, nor MemberClass.
(Both are just custom classes, no link with STL containers. I just wanted to express that Container contains an instance of Member)

So how can i write
ar & c.special_member;
since there is no serialize member function for this class?

And when i'll be using an non-intrusive (free function) serialize, how do i handle the version number?


On Mon, Mar 5, 2018 at 4:51 PM, Robert Ramey via Boost-users <[hidden email]> wrote:
On 3/5/18 5:12 AM, Lars Ruoff via Boost-users wrote:
Hi,
I have a class structure like

class MemberClass;

class ContainerClass {
   MemberClass special_member;
   int other_members;
};

Following the example in http://www.boost.org/doc/libs/1_66_0/libs/serialization/doc/
i want to write a non-intrusive serialization:

template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
{
   ar & c.other_members; // easy - built-in type
   serialize(ar, c.special_member, version);  //use same version???
}

Assuming there's is also a non-intrusive
void serialize(Archive & ar, MemberClass & m, const unsigned int version);

this compiles and works but i feel this is not the way to do it because it reuses the version number of the container class for the member class? So how to handle that? Must the Container class manage version of its members individually?
I suggest to add a section "Non-intrusively Serializable Members" to documentation with an example.

Cheers,
Lars R.

I'm not sure what the "special member" of the container class is. Presumably it's your own class as STL classes have no "special members".  Assuming this to be the case there should be something like:

struct ContainerClass {
        ...
        void serialize(Achive &ar, const unsigned int v){
                ar & other_members;
                ar & special_member
        }
        ...
};

or

template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
{
        ar & c.other_members; // easy - built-in type
        ar & c.special_member;
}


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
non-intrusive serialisation is covered here:

http://www.boost.org/doc/libs/1_66_0/libs/serialization/doc/serializati
on.html#splittingfreefunctions

given classes:

MyNamespace::MyClass
MyNamespace::MyContainer

you could write:

namespace boost { namespace serialization {

  template<class Archive>
  void serialize(Archive & ar, MyNamespace::MyContainer& c, const
unsigned int version)
  {
     ar & c.other_members; // easy - built-in type
     ar & c.special_member;
  }

  template<class Archive>
  void serialize(Archive & ar, MyNamespace::MyClass& c, const unsigned
int version)
  {
     ar & c.foo; // easy - built-in type
     if (version > 1)
     {
       ar & c.bar; // easy - built-in type
     }
  }

}}

Or you could put the serialize template functions in the MyNamespace
namespace and reply on ADL.



On Mon, 2018-03-05 at 17:36 +0100, Lars Ruoff via Boost-users wrote:

> Maybe i didn't express it clearly enough.
> I want non-intrusive serialization.
> I want to modify neither ContqinerClass, nor MemberClass.
> (Both are just custom classes, no link with STL containers. I just
> wanted to express that Container contains an instance of Member)
>
> So how can i write
> ar & c.special_member;
> since there is no serialize member function for this class?
>
> And when i'll be using an non-intrusive (free function) serialize,
> how do i handle the version number?
>
>
>
> On Mon, Mar 5, 2018 at 4:51 PM, Robert Ramey via Boost-users <boost-u
> [hidden email]> wrote:
> > On 3/5/18 5:12 AM, Lars Ruoff via Boost-users wrote:
> > > Hi,
> > > I have a class structure like
> > >
> > > class MemberClass;
> > >
> > > class ContainerClass {
> > >    MemberClass special_member;
> > >    int other_members;
> > > };
> > >
> > > Following the example in http://www.boost.org/doc/libs/1_66_0/lib
> > > s/serialization/doc/
> > > i want to write a non-intrusive serialization:
> > >
> > > template<class Archive>
> > > void serialize(Archive & ar, ContainerClass & c, const unsigned
> > > int version)
> > > {
> > >    ar & c.other_members; // easy - built-in type
> > >    serialize(ar, c.special_member, version);  //use same
> > > version???
> > > }
> > >
> > > Assuming there's is also a non-intrusive
> > > void serialize(Archive & ar, MemberClass & m, const unsigned int
> > > version);
> > >
> > > this compiles and works but i feel this is not the way to do it
> > > because it reuses the version number of the container class for
> > > the member class? So how to handle that? Must the Container class
> > > manage version of its members individually?
> > > I suggest to add a section "Non-intrusively Serializable Members"
> > > to documentation with an example.
> > >
> > > Cheers,
> > > Lars R.
> > >
> >  
> > I'm not sure what the "special member" of the container class is.
> > Presumably it's your own class as STL classes have no "special
> > members".  Assuming this to be the case there should be something
> > like:
> >
> > struct ContainerClass {
> >         ...
> >         void serialize(Achive &ar, const unsigned int v){
> >                 ar & other_members;
> >                 ar & special_member
> >         }
> >         ...
> > };
> >
> > or
> >
> > template<class Archive>
> > void serialize(Archive & ar, ContainerClass & c, const unsigned int
> > version)
> > {
> >         ar & c.other_members; // easy - built-in type
> >         ar & c.special_member;
> > }
> >
> >
> > _______________________________________________
> > Boost-users mailing list
> > [hidden email]
> > https://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
On 3/5/18 8:36 AM, Lars Ruoff via Boost-users wrote:
> Maybe i didn't express it clearly enough.
> I want non-intrusive serialization.
> I want to modify neither ContqinerClass, nor MemberClass.
> (Both are just custom classes, no link with STL containers. I just
> wanted to express that Container contains an instance of Member)
>
> So how can i write
> ar & c.special_member;
> since there is no serialize member function for this class?

That's the problem.  the type of c.special_member must be serializable.
If it's not already, you have to make it serializable by writing your
own serialize function for it.  It can be either intrusive or
non-intrusive - doesn't matter.  But it has to be there.  The versioning
of type type of special_member is handled in the serialization of that type.

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
Sorry, still don't understand.
As said, i have defined
void serialize(Archive & ar, MemberClass & m, const unsigned int version);

so
template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
{
  ar & c.other_members; // easy - built-in type
  serialize(ar, c.special_member, version);  //use same version???
}
compiles.

My question is what *version* number do i need to provide in the call to serialize for c.special_member?
- The same as with what serialize(Archive & ar, ContainerClass & c, const unsigned int version) got called? (Like i did in example) This doesn't make sense to me.
- Or I need to handle it myself, say i need to call  serialize(ar, c.special_member, 3); because i *know* the current version of MemberClass is version 3.
- Or there is some archive magic i don't see.



On Mon, Mar 5, 2018 at 5:52 PM, Robert Ramey via Boost-users <[hidden email]> wrote:
On 3/5/18 8:36 AM, Lars Ruoff via Boost-users wrote:
Maybe i didn't express it clearly enough.
I want non-intrusive serialization.
I want to modify neither ContqinerClass, nor MemberClass.
(Both are just custom classes, no link with STL containers. I just wanted to express that Container contains an instance of Member)

So how can i write
ar & c.special_member;
since there is no serialize member function for this class?

That's the problem.  the type of c.special_member must be serializable. If it's not already, you have to make it serializable by writing your own serialize function for it.  It can be either intrusive or non-intrusive - doesn't matter.  But it has to be there.  The versioning of type type of special_member is handled in the serialization of that type.


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
On 3/5/18 9:06 AM, Lars Ruoff via Boost-users wrote:

> Sorry, still don't understand.
> As said, i have defined
> void serialize(Archive & ar, MemberClass & m, const unsigned int version);
>
> so
> template<class Archive>
> void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
> {
>    ar & c.other_members; // easy - built-in type
>    serialize(ar, c.special_member, version);  //use same version???
> }
> compiles.

where is

template<class Archive>
void serialize(Archive & ar, special_member_type & t, const unsigned int
version){
        ar & ??;
};

?

If you have this you could write:

template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int
version){
        ar & c.other_members; // easy - built-in type
        ar & c.special_member;  //uses it's own version
}

Robert Ramey

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost::serialization - Non intrusive serialization of user-defined members?

Boost - Users mailing list
Ah, ok.
I assumed the syntax
  ar & c.special_member;  //uses it's own version
only worked with the intrusive version (i.e. serialize as a member of MemberClass).
Seems it still works with non-intrusive. Pure magic.
Thanks!


On Mon, Mar 5, 2018 at 6:31 PM, Robert Ramey via Boost-users <[hidden email]> wrote:
On 3/5/18 9:06 AM, Lars Ruoff via Boost-users wrote:
Sorry, still don't understand.
As said, i have defined
void serialize(Archive & ar, MemberClass & m, const unsigned int version);

so
template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version)
{
   ar & c.other_members; // easy - built-in type
   serialize(ar, c.special_member, version);  //use same version???
}
compiles.

where is

template<class Archive>
void serialize(Archive & ar, special_member_type & t, const unsigned int version){
        ar & ??;
};

?

If you have this you could write:

template<class Archive>
void serialize(Archive & ar, ContainerClass & c, const unsigned int version){
        ar & c.other_members; // easy - built-in type
        ar & c.special_member;  //uses it's own version
}

Robert Ramey


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users