boost serialization of polymorph classes from DLLs

Discussion in 'C++' started by MindWrapper, Dec 11, 2007.

  1. MindWrapper

    MindWrapper Guest

    boost serialization of polymorph classes from DLLs

    Folks,

    Let's consider following code
    --------------------------------
    // base.h
    // abstract base class
    class IBase
    {
    }

    BOOST_IS_ABSTRACT(IBase)
    --------------------------------



    ---------------------------
    class derived : public IBase
    {
    friend class boost::serialization::access;

    template <class Archive>
    void serialize(Archive & ar, const unsigned int version )
    {
    //...
    }
    }


    BOOST_CLASS_EXPORT_GUID(derived, "derived ");

    ---------------------------



    Main application knows only about IBase and it dynamically loads dll
    where class derived is defined. Dll exports some method to create
    instance of derived :

    ---------------------------
    IBase* extern "C" __declspec(dllexport)
    create(IShapesFactory* pfactory)
    {
    return new Derived;
    };
    ---------------------------

    What I want to figure out is that possible to serialize polymorphic
    pointer received in such way


    Something like that:

    std::eek:fstream ofs("file");
    boost::archive::text_oarchive oa(ofs);


    IBase* p;
    // initialize p using above exported method. So, p points to derived
    instance.
    oa & p; // ???


    As I understood this is in principle impossible, because for each
    combination of type and archive following template declared in boost
    library must be instantiated

    template<class Archive, class T>
    inline void serialize(
    Archive & ar,
    T & t,
    const unsigned int file_version
    ){
    // invoke member function for class T
    t.serialize(ar, file_version);
    }

    So for main application template instantiation is not available?


    Does anyone have another opinion?


    Thank you!
     
    MindWrapper, Dec 11, 2007
    #1
    1. Advertising

  2. On Dec 11, 2:51 pm, MindWrapper <> wrote:
    > boost serialization of polymorph classes from DLLs
    >
    > Folks,
    >
    > Let's consider following code
    > --------------------------------
    > // base.h
    > // abstract base class
    > class IBase
    > {
    >
    > }
    >
    > BOOST_IS_ABSTRACT(IBase)
    > --------------------------------
    >
    > ---------------------------
    > class derived : public IBase
    > {
    > friend class boost::serialization::access;
    >
    > template <class Archive>
    > void serialize(Archive & ar, const unsigned int version )
    > {
    > //...
    > }
    >
    > }
    >
    > BOOST_CLASS_EXPORT_GUID(derived, "derived ");
    >
    > ---------------------------
    >
    > Main application knows only about IBase and it dynamically loads dll
    > where class derived is defined. Dll exports some method to create
    > instance of derived :
    >
    > ---------------------------
    > IBase* extern "C" __declspec(dllexport)
    > create(IShapesFactory* pfactory)
    > {
    > return new Derived;};
    >
    > ---------------------------
    >
    > What I want to figure out is that possible to serialize polymorphic
    > pointer received in such way
    >
    > Something like that:
    >
    > std::eek:fstream ofs("file");
    > boost::archive::text_oarchive oa(ofs);
    >
    > IBase* p;
    > // initialize p using above exported method. So, p points to derived
    > instance.
    > oa & p; // ???
    >
    > As I understood this is in principle impossible, because for each
    > combination of type and archive following template declared in boost
    > library must be instantiated
    >
    > template<class Archive, class T>
    > inline void serialize(
    > Archive & ar,
    > T & t,
    > const unsigned int file_version
    > ){
    > // invoke member function for class T
    > t.serialize(ar, file_version);
    >
    > }
    >
    > So for main application template instantiation is not available?
    >
    > Does anyone have another opinion?


    You would be better off posting this to the boost mailing lists.

    But, you are right. For your main application, the template function
    serialize will not be available because the class and its own
    serialization infrastructure lies in the dll loaded dynamically. And
    that DLL does not know what archive is to be used because the
    serialization would be invoked from the main app.

    Since you say that the main application has no knowledge of the
    various derived classes, the only option that I see is the DLL
    providing the serialization functionality on its own. Can you have
    serialize/deserialize functions exported from the DLL that use one (or
    as many you wish to support) of the archive types (text/xml/binary)
    and the dll has the complete responsibility thereon for the
    serialization/deserialization? I am not very sure if that would be
    possible but if yes, it still would suffer from the limitation on the
    set of supported archive types.
     
    Abhishek Padmanabh, Dec 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,313
    deadsea
    Jan 2, 2005
  2. Mika Vainio
    Replies:
    9
    Views:
    1,054
    Uwe Schnitker
    Apr 27, 2004
  3. Replies:
    3
    Views:
    1,085
  4. marcus hall

    Polymorph in Place

    marcus hall, May 10, 2007, in forum: C++
    Replies:
    4
    Views:
    466
    Massimo
    May 15, 2007
  5. Fred
    Replies:
    2
    Views:
    366
    Ron Natalie
    Jun 3, 2007
Loading...

Share This Page