Thanks for getting back to me so quickly. It's great that the boilerplate is
able to make polymorphic serialization so straightforward and effortless.

Likewise, thanks for the information about those macros.  Having gone
through the Boost.Serialization documentation numerous times, I had assumed
that there would be something like that, but I was having trouble with
finding these details.

However, I'm still having some further difficulties in getting my own code
to successfully build.  As I noted in the example code at the bottom of my
original message, there are 2 further complicating factors in my own code as
compared with the sample code that you provided:
- Firstly, the derived class is a template class.
- Secondly, the derived template class does not have a default constructor
(it would be meaningless in the context).

I've spent a few hours now trying to make sense of the unit tests and code
associated with intrusive and non-intrusive pointers, and I'm still
extremely lost and confused.  I've tried understanding the different macros
that are applicable in each different situation, but I just can't figure out
the right combination of macros, plus the right way to write the
serialization functions to suit my needs.

Essentially, what I need to have happen is this:
- the base class (detail::Model_Base) is an abstract base class.  I presume
that the intrusive macro HPX_SERIALIZATION_POLYMORPHIC_ABSTRACT(Model_Base)
can be added within the class declaration.
- the derived class is a template class -- template <typename Model_Type>
detail::Model_Impl<Model_Type>
- the constructor for the derived class requires the Model_Type model_ to be
deserialized out of the archive.  model_  is the argument to the
constructor.  Furthermore, as far as I understand, I cannot use
type-deduction in the c-tor for the class template parameter.
- unlike the unit test code, I cannot use the factory function to create a
new pointer and then delegate back to the serialize(Archive, C<T>, unsigned)
function.  Does this mean I need separate save and load functions?   I'm
just really, really confused here...

>From what I can see, none of the examples / unit tests quite matches what
I'm trying to do, and I've been going in circles on my own.  Hopefully what
I'm trying to do isn't that difficult and you could please give me some
further direction on this.  

Thanks again,
Michael


> -----Original Message-----
> From: hpx-users-boun...@stellar.cct.lsu.edu [mailto:hpx-users-
> boun...@stellar.cct.lsu.edu] On Behalf Of Hartmut Kaiser
> Sent: September-20-16 10:01 AM
> To: hpx-users@stellar.cct.lsu.edu
> Subject: Re: [hpx-users] Serializing type-erased classes
> 
> 
> Sorry, I forgot to add macros to the classes, please see below:
> 
> > > Could someone please help me to better understand how to write
> > > serialization functions for a class using type erasure?  Looking at
> > > my code, and at the hpx/runtime/serialization/ code, I just can't
> > > figure out how to handle this.  Even assuming that the boilerplate
> > > code is able to correctly deal with pointers to a derived class, it
> > > appears to me that only the serialize function for the base class
> > > will be executed by the runtime system.
> >
> > Polymorphic serialization is handled correctly by the boilerplate code.
> > You serialize your object through a shared_ptr and write a normal
> > serialization function for your data:
> >
> >     struct A
> >     {
> >         virtual ~A() {}
> >         virtual void print_something() { cout << a << endl; }
> >
> >         A() : a(13) {}
> >
> >         int a;
> >
> >         template <typename Archive>
> >         void serialize(Archive& ar, unsigned)
> >         {
> >             ar & a;
> >         }
> 
>           HPX_SERIALIZATION_POLYMORPHIC_ABSTRACT(A);
> >     };
> >
> >     struct B : A
> >     {
> >         B() : b(0) {}
> >         B(int v) : b(v) {}
> >
> >         int b;
> >
> >         virtual void print_something() { cout << b << endl; }
> >
> >         template <typename Archive>
> >         void serialize(Archive& ar, unsigned)
> >         {
> >             ar & b;
> >             hpx::serialization::base_object<A>(*this);
> >         }
> 
>             HPX_SERIALIZATION_POLYMORPHIC(B)
> 
> >     }
> 
> Regards Hartmut
> ---------------
> http://boost-spirit.com
> http://stellar.cct.lsu.edu

> > > The classes in question look something like the following.  I think
> > > that the code is fairly straightforward - the Model class is
> > > essentially a wrapper for a variety of concrete model types: first
> > > an object of concrete model type is created and then passed to
> > > Model::Construct(Model_Type) which returns a Model wrapper object.
> > > This Model type is required as a parameter for some of my Components
> > > -- as such, these classes all need to support serialization.
> > >
> > > class Model{
> > >
> > > private:
> > >
> > >      std::unique_ptr<detail::Model_Base> impl_;
> > >
> > > public:
> > >
> > >     Model(std::unique_ptr<detail::Model_Base>&& model_ptr ) :
> > > implex_(std::move(model_ptr>){}
> > >     Model() = default;
> > >     Model(Model&& other) = default;
> > >     Model(Model const& other) : impl(other.impl_->Clone()){}
> > >
> > >     template <typename Model_Type>
> > >     static Model Construct(Model_Type const& model){
> > >
> > >          std::unique_ptr<detail::Model_Base> model_impl_ =
> > > std::make_unique<detail::Model_Impl<Model_Type>>(model);
> > >          Model model_{std::move(model_impl_)};
> > >          return model_;
> > >
> > > /* ... */
> > >
> > >      private:
> > >
> > >      friend class hpx::serialization::access;
> > >
> > >      template <typename Archive>
> > >      void serialize(Archive& ar, const unsigned int version){ ar &
> > impl_;
> > > }
> > >
> > > };
> > >
> > > namespace detail{
> > >
> > > struct Model_Base {
> > >
> > >   virtual Matrix Model(Matrix const &inputdata, vector<Matrix>
> > > parameters);
> > >
> > > /* ... */
> > >
> > > virtual fx::modeling::detail::Model_Base *Clone();
> > >
> > > private:
> > >    friend class hpx::serialization::access;
> > >
> > > protected:
> > >    template <typename Archive>
> > >    void serialize(Archive& ar, const unsigned int version){}
> > >
> > > }
> > >
> > >
> > > template <typename Model_Type> struct Model_Impl : public
> Model_Base
> > > {
> > > public:
> > >    using model_type = Model_Type;
> > >    Model_Impl(Model_Type model) : model_(model) {}
> > >    Model_Impl(Model_Impl const &other) : model_(other.model_) {}
> > >    Model_Impl(Model_Impl &&other) = default;
> > >
> > >    Matrix Evaluate_Model(Matrix const &inputdata, vector<Matrix>
> > > parameters) override;
> > >
> > >    /* ... */
> > >    virtual fx::modeling::detail::Model_Base *Clone() override;
> > >
> > >
> > > private:
> > >    Model_Type model_;
> > >
> > >    friend class hpx::serialization::access;
> > >
> > >    template <typename Archive>
> > >    void serialize(Archive& ar, const unsigned int version){ ar &
> > > model_;
> > }
> > >
> > > };
> > >
> > > As usual, I greatly appreciate any help that you can offer.
> > >
> > > Just a final note -- as this is for a personal project, I have
> > > flexibility in changing things fairly broadly.  If the above
> > > approach is wrong or problematic, I would also appreciate any
> > > suggestions on how to better handle this.
> > >
> > > Thanks and regards,
> > >
> > > Michael
> > >
> > > _______________________________________________
> > > hpx-users mailing list
> > > hpx-users@stellar.cct.lsu.edu
> > > https://mail.cct.lsu.edu/mailman/listinfo/hpx-users
> 
> _______________________________________________
> hpx-users mailing list
> hpx-users@stellar.cct.lsu.edu
> https://mail.cct.lsu.edu/mailman/listinfo/hpx-users

_______________________________________________
hpx-users mailing list
hpx-users@stellar.cct.lsu.edu
https://mail.cct.lsu.edu/mailman/listinfo/hpx-users

Reply via email to