> 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;
        }
    };

    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);
        }
    }

    void foo(std::shared_ptr<A> a)
    {
        a->print_something();   // prints 42
    }

    HPX_PLAIN_ACTION(foo, foo_action);

    void bar()
    {
        std::shared_ptr<A> b = new B(42);
        async(foo_action, find_here(), b);
    }
    
HTH
Regards Hartmut
---------------
http://boost-spirit.com
http://stellar.cct.lsu.edu

> 
> Perhaps I don't understand how the underlying serialization subsystem
> works in HPX, but it would seem to me that there should be *something*
> particular that needs to be done in my code to be able to properly
> handle this situation.  I suspect that this is the latest cause of
> segmentation faults that I'm having on my project...
> 
> 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

Reply via email to