Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2007-04-02 22:07:58


Sohail Somani wrote:

This is an interesting question which I think might be doable in an elegant
way.

// Suppose we have a few functions with the following signature void f(int);

struct function_object {
    virtual void operator()(int) = 0;
};

// wrap in function objects
struct function_object_1 {
    // overload function call operator
    void operator()(int){
        // do something
    }
    template<class Archive>
    serialize(Archive &ar, const unsigned int version){
        // serialize base class - just register relationship from derived to
base
        ar & boost::serialization::base_object<function_object>(*this);
        // simple case - nothing to do
    }
};

BOOST_CLASS_EXPORT(function_object_1)

// wrap in function objects
struct function_object_2 {
    int m_state; // some extra data
    // overload function call operator
    void operator()(int){
        // do something else
    }
    serialize(Archive &ar, const unsigned int version){
        // serialize base class - just register relationship from derived to
base
        ar & boost::serialization::base_object<function_object>(*this);
        // complex case - save some variables
        ar & m_state;
    }
    function_object_2(int i) :
        m_m_state(i)
    {}
};

BOOST_CLASS_EXPORT(function_object_2)

void main(){
    function_object_1 x1 = function_object_1();
    ...
    function_object const *pfo = x1;
    ...
    ar << pfo;
    ...
    function_object *pfo1;
    ar >> pf01;
    ...
    // call correct function
    (*pfo)();

}

That's just a sketch - no guarentees - perhaps it serves a food for thought.

Robert Ramey

> Hi,
>
> Is it possible to serialize function pointers? I'm trying to serialize
> this guy:
>
> struct f
> {
> typedef void(*function_t)();
> function_t the_ptr_;
> };
>
> Thanks,
>
> Sohail


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net