Serialization of an instance of a Derived Template Class

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Serialization of an instance of a Derived Template Class

Boost - Users mailing list
Hi,

I am using Boost to serialize pointers of a base class which points to instances  of a derived template class. I am using boost 1.7.0 and running it on Ubuntu 16.04.

The source code of the example is available at https://github.com/PritamMG/Boost-Serialization-Example

My example has a base class Bar_A (definition available in include/Abstract.hpp) which has a virtual function toStr(). It has two derived classes Bar_B and Bar_C. Bar_B is a template class.
I have registered the derived classes in sc/Test.cpp. Class ObjectNew (include/Object.hpp) has a member pointer of type Bar_A which points to instances of classes Bar_B and Bar_C.

I get an error as "unregistered class".  I am not sure for which class do I get this error message.

There is also an issue of non-default. If Bar_B does not have a default constructor, save_construct_data and load_construct_data are over-ridden and there I run into an error "too many template-parameter-lists".

How do I define these functions? 

Thanks,
Pritam




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

Re: Serialization of an instance of a Derived Template Class

Boost - Users mailing list
Have you read the documentation?

Robert Ramey

On 10/31/19 2:47 AM, Pritam Gharat via Boost-users wrote:

> Hi,
>
> I am using Boost to serialize pointers of a base class which points to
> instances  of a derived template class. I am using boost 1.7.0 and
> running it on Ubuntu 16.04.
>
> The source code of the example is available at
> https://github.com/PritamMG/Boost-Serialization-Example
>
> My example has a base class Bar_A (definition available in
> include/Abstract.hpp) which has a virtual function toStr(). It has two
> derived classes Bar_B and Bar_C. Bar_B is a template class.
> I have registered the derived classes in sc/Test.cpp. Class ObjectNew
> (include/Object.hpp) has a member pointer of type Bar_A which points to
> instances of classes Bar_B and Bar_C.
>
> I get an error as "unregistered class".  I am not sure for which class
> do I get this error message.
>
> There is also an issue of non-default. If Bar_B does not have a default
> constructor, save_construct_data and load_construct_data are over-ridden
> and there I run into an error "too many template-parameter-lists".
>
> How do I define these functions?
>
> Thanks,
> Pritam
>
>
>
>
> _______________________________________________
> 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: Serialization of an instance of a Derived Template Class

Boost - Users mailing list
Hi Robert,

I have gone through the documentation. Below is the list of things I did for serialization and deserialization.
  1. Define serialize function in the class whose instances I wish to serialize. In my example it is ObjectNew which has pointers of type Bar_A and Foo_A. Both are base classes and the pointers of these pointers point to instances of theri derived classes. So I have serialize function defined for Bar_A, Foo_A and all their derived classes (which includes Foo_C, Foo_D, Bar_B, Bar_C).
  2. Call to boost::serialization::base_object in the serialize function of derived classes which in turn calls serialize function of the corresponding base classes.
  3. For classes with non-defualt constructor, I have over-ridden save_construct_data and load_construct_data functions.
  4. Registering derived classes whose instances are pointed by pointers with types of their base classes using BOOST_CLASS_EXPORT.
  5. And finally use boost::archive::text_oarchive and boost::archive::text_iarchive for serialization and deserialization.
Please let me know if I have missed something or I am doing something wrong.

Pritam Gharat

On Thu, Oct 31, 2019 at 5:13 PM Robert Ramey via Boost-users <[hidden email]> wrote:
Have you read the documentation?

Robert Ramey

On 10/31/19 2:47 AM, Pritam Gharat via Boost-users wrote:
> Hi,
>
> I am using Boost to serialize pointers of a base class which points to
> instances  of a derived template class. I am using boost 1.7.0 and
> running it on Ubuntu 16.04.
>
> The source code of the example is available at
> https://github.com/PritamMG/Boost-Serialization-Example
>
> My example has a base class Bar_A (definition available in
> include/Abstract.hpp) which has a virtual function toStr(). It has two
> derived classes Bar_B and Bar_C. Bar_B is a template class.
> I have registered the derived classes in sc/Test.cpp. Class ObjectNew
> (include/Object.hpp) has a member pointer of type Bar_A which points to
> instances of classes Bar_B and Bar_C.
>
> I get an error as "unregistered class".  I am not sure for which class
> do I get this error message.
>
> There is also an issue of non-default. If Bar_B does not have a default
> constructor, save_construct_data and load_construct_data are over-ridden
> and there I run into an error "too many template-parameter-lists".
>
> How do I define these functions?
>
> Thanks,
> Pritam
>
>
>
>
> _______________________________________________
> 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
Reply | Threaded
Open this post in threaded view
|

Re: Serialization of an instance of a Derived Template Class

Boost - Users mailing list
On 11/1/19 4:46 AM, Pritam Gharat via Boost-users wrote:
> Hi Robert,
>
> I have gone through the documentation. Below is the list of things I did
> for serialization and deserialization.
>
>  1. Define serialize function in the class whose instances I wish to
>     serialize. In my example it is ObjectNew which has pointers of type
>     Bar_A and Foo_A. Both are base classes and the pointers of these
>     pointers point to instances of theri derived classes.
Not sure I'm getting this.  So we have
        struct Bar_A_base ...
        struct Foo_A_base ...

        struct Bar_A_derived : public Bar_A_base ...
        struct Foo_A_base : public Bar_A_base ...

        struct ObjectNew {
                Bar_A_base *m_bar_a;
                Foo_A_base *m_foo_a;
                ...
                template<class Archive>
                void serialize(Archive & ar, const unsigned in version){
                        ar & m_bar_a;
                        ar & m_foo_a;
                }
        };

Right?

>     So I have
>     serialize function defined for Bar_A, Foo_A and all their derived
>     classes (which includes Foo_C, Foo_D, Bar_B, Bar_C).
sounds good
>  2. Call to boost::serialization::base_object in the serialize function
>     of derived classes which in turn calls serialize function of the
>     corresponding base classes.
sounds good
>  3. For classes with non-defualt constructor, I have over-ridden
>     save_construct_data and load_construct_data functions.
sounds good
>  4. Registering derived classes whose instances are pointed by pointers
>     with types of their base classes using BOOST_CLASS_EXPORT.
sounds good
>  5. And finally use boost::archive::text_oarchive and
>     boost::archive::text_iarchive for serialization and deserialization.

sounds good
> Please let me know if I have missed something or I am doing something wrong.

That sounds about right.

>
> Pritam Gharat
>
> On Thu, Oct 31, 2019 at 5:13 PM Robert Ramey via Boost-users
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Have you read the documentation?
>
>     Robert Ramey
>
>     On 10/31/19 2:47 AM, Pritam Gharat via Boost-users wrote:
>      > Hi,
>      >
>      > I am using Boost to serialize pointers of a base class which
>     points to
>      > instances  of a derived template class. I am using boost 1.7.0 and
>      > running it on Ubuntu 16.04.
>      >
>      > The source code of the example is available at
>      > https://github.com/PritamMG/Boost-Serialization-Example
>      >
>      > My example has a base class Bar_A (definition available in
>      > include/Abstract.hpp) which has a virtual function toStr(). It
>     has two
>      > derived classes Bar_B and Bar_C. Bar_B is a template class.
>      > I have registered the derived classes in sc/Test.cpp. Class
>     ObjectNew
>      > (include/Object.hpp) has a member pointer of type Bar_A which
>     points to
>      > instances of classes Bar_B and Bar_C.
>      >
>      > I get an error as "unregistered class".  I am not sure for which
>     class
>      > do I get this error message.
>      >
>      > There is also an issue of non-default. If Bar_B does not have a
>     default
>      > constructor, save_construct_data and load_construct_data are
>     over-ridden
>      > and there I run into an error "too many template-parameter-lists".
>      >
>      > How do I define these functions?
>      >
>      > Thanks,
>      > Pritam
>      >
>      >
>      >
>      >
>      > _______________________________________________
>      > Boost-users mailing list
>      > [hidden email] <mailto:[hidden email]>
>      > https://lists.boost.org/mailman/listinfo.cgi/boost-users
>      >
>
>
>     _______________________________________________
>     Boost-users mailing list
>     [hidden email] <mailto:[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
Reply | Threaded
Open this post in threaded view
|

Re: Serialization of an instance of a Derived Template Class

Boost - Users mailing list


On Fri, Nov 1, 2019 at 3:51 PM Robert Ramey via Boost-users <[hidden email]> wrote:
On 11/1/19 4:46 AM, Pritam Gharat via Boost-users wrote:
> Hi Robert,
>
> I have gone through the documentation. Below is the list of things I did
> for serialization and deserialization.
>
>  1. Define serialize function in the class whose instances I wish to
>     serialize. In my example it is ObjectNew which has pointers of type
>     Bar_A and Foo_A. Both are base classes and the pointers of these
>     pointers point to instances of theri derived classes.
Not sure I'm getting this.  So we have
        struct Bar_A_base ...
        struct Foo_A_base ...

        struct Bar_A_derived : public Bar_A_base ...
        struct Foo_A_base : public Bar_A_base ...

        struct ObjectNew {
                Bar_A_base *m_bar_a;
                Foo_A_base *m_foo_a;
                ...
                template<class Archive>
                void serialize(Archive & ar, const unsigned in version){
                        ar & m_bar_a;
                        ar & m_foo_a;
                }
        };

Right?

Yes. Something similar to that. I am attaching the header files for the class definitions. 


>     So I have
>     serialize function defined for Bar_A, Foo_A and all their derived
>     classes (which includes Foo_C, Foo_D, Bar_B, Bar_C).
sounds good
>  2. Call to boost::serialization::base_object in the serialize function
>     of derived classes which in turn calls serialize function of the
>     corresponding base classes.
sounds good
>  3. For classes with non-defualt constructor, I have over-ridden
>     save_construct_data and load_construct_data functions.
sounds good
>  4. Registering derived classes whose instances are pointed by pointers
>     with types of their base classes using BOOST_CLASS_EXPORT.
sounds good
>  5. And finally use boost::archive::text_oarchive and
>     boost::archive::text_iarchive for serialization and deserialization.

sounds good
> Please let me know if I have missed something or I am doing something wrong.

That sounds about right.
>
> Pritam Gharat
>
> On Thu, Oct 31, 2019 at 5:13 PM Robert Ramey via Boost-users
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Have you read the documentation?
>
>     Robert Ramey
>
>     On 10/31/19 2:47 AM, Pritam Gharat via Boost-users wrote:
>      > Hi,
>      >
>      > I am using Boost to serialize pointers of a base class which
>     points to
>      > instances  of a derived template class. I am using boost 1.7.0 and
>      > running it on Ubuntu 16.04.
>      >
>      > The source code of the example is available at
>      > https://github.com/PritamMG/Boost-Serialization-Example
>      >
>      > My example has a base class Bar_A (definition available in
>      > include/Abstract.hpp) which has a virtual function toStr(). It
>     has two
>      > derived classes Bar_B and Bar_C. Bar_B is a template class.
>      > I have registered the derived classes in sc/Test.cpp. Class
>     ObjectNew
>      > (include/Object.hpp) has a member pointer of type Bar_A which
>     points to
>      > instances of classes Bar_B and Bar_C.
>      >
>      > I get an error as "unregistered class".  I am not sure for which
>     class
>      > do I get this error message.
>      >
>      > There is also an issue of non-default. If Bar_B does not have a
>     default
>      > constructor, save_construct_data and load_construct_data are
>     over-ridden
>      > and there I run into an error "too many template-parameter-lists".
>      >
>      > How do I define these functions?
>      >
>      > Thanks,
>      > Pritam
>      >
>      >
>      >
>      >
>      > _______________________________________________
>      > Boost-users mailing list
>      > [hidden email] <mailto:[hidden email]>
>      > https://lists.boost.org/mailman/listinfo.cgi/boost-users
>      >
>
>
>     _______________________________________________
>     Boost-users mailing list
>     [hidden email] <mailto:[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

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

Abstract.hpp (9K) Download Attachment
Object.hpp (4K) Download Attachment