[serialization] Serialize function pointers
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
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sohail Somani Sent: Monday, April 02, 2007 2:22 PM To: boost-users@lists.boost.org Subject: [Boost-users] [serialization] Serialize function pointers
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_; };
This was what I did to get my test to compile and run (same process, so it will work), which is totally not portable :) ar & boost::serialization::make_nvp("the_ptr_",boost::serialization::make_bin ary_object(&the_ptr_,sizeof(the_ptr_))); Any suggestions would be much appreciated. Thanks, Sohail
-----Original Message----- From: Sohail Somani
Is it possible to serialize function pointers? I'm trying
to serialize
this guy:
struct f { typedef void(*function_t)(); function_t the_ptr_; };
This was what I did to get my test to compile and run (same process, so it will work), which is totally not portable :)
ar & boost::serialization::make_nvp("the_ptr_",boost::serialization ::make_binary_object(&the_ptr_,sizeof(the_ptr_)));
Oh duh, nevermind this will never be portable.
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
Robert Ramey wrote: ... // wrap in function objects struct function_object_1 : function_object{ ... Whoops - forgot ": function_object" - a key incredient. Robert Ramey
Well the thing I wanted to avoid was having to write a new function object everytime for each permutation of argument/return types (i.e. a template). I think its possible if one uses function lookup. If I ever get un-lazy enough to try it, I might just post it here! -----Original Message----- From: boost-users-bounces@lists.boost.org on behalf of Robert Ramey Sent: Mon 4/2/2007 7:07 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [serialization] Serialize function pointers 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; };
participants (2)
-
Robert Ramey
-
Sohail Somani