[serialization][any]Do we have serialization support for boost::any?
Hi, Do we have serialization support for boost::any? If not, how is it possible to serialize boost::any? Regards, Lirong
Li Lirong wrote:
Hi, Do we have serialization support for boost::any?
Not that I know of
If not, how is it possible to serialize boost::any?
The same way everythign else has been serialized. a) make a module to do it b) make a test c) add a boost license d) upload it to the vault. Robert Ramey
Li Lirong wrote:
Hi, Do we have serialization support for boost::any? If not, how is it possible to serialize boost::any?
Good question. Robert, is it possible to place BOOST_CLASS_EXPORT inside class definition, so that I can write something like that: class any_base {}; template<class T> class any_value_holder : public any_base { ....... BOOST_CLASS_EXPORT(any_value_holder); }; and have BOOST_CLASS_EXPORT run for each instantiation of any_value_holder? - Volodya
Vladimir Prus wrote:
Li Lirong wrote:
Hi, Do we have serialization support for boost::any? If not, how is it possible to serialize boost::any?
Good question. Robert, is it possible to place BOOST_CLASS_EXPORT inside class definition, so that I can write something like that:
class any_base {}; template<class T> class any_value_holder : public any_base { ....... BOOST_CLASS_EXPORT(any_value_holder); };
and have BOOST_CLASS_EXPORT run for each instantiation of any_value_holder?
- Volodya
This would tie the GUID "any_value_holder" to multiple types any_value_holder<T> so it wouldn't be unique any more. This is another manifestation of the general problem of retreiving an external string from a typename. type_info does this but in a non-portable way. Of course you know this as you rasied this issue in the first place. Much fame and fortune will accrue to he who creates the function template<typename T> char * guid() which returns a unique char string in a platform independent way. Good Luck Robert Ramey
"Robert Ramey"
Much fame and fortune will accrue to he who creates the function
template<typename T> char * guid()
which returns a unique char string in a platform independent way.
Might be done pretty easily on top of typeof emulation. Of course, custom type/template registration would be required. Regards, Arkadiy
Arkadiy Vertleyb wrote:
"Robert Ramey"
wrote Much fame and fortune will accrue to he who creates the function
template<typename T> char * guid()
which returns a unique char string in a platform independent way.
Might be done pretty easily on top of typeof emulation. Of course, custom type/template registration would be required.
I don't see how typeof helps? One, not exactly platform independent, way is to have a per platform translation of typeid(T).name() to a portable description. And by "unique char string" I hope Robert meant the string contents are unique, not that it would return the same pointer in all contexts. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org
"Rene Rivera"
Arkadiy Vertleyb wrote:
"Robert Ramey"
wrote Much fame and fortune will accrue to he who creates the function
template<typename T> char * guid()
which returns a unique char string in a platform independent way.
Might be done pretty easily on top of typeof emulation. Of course, custom type/template registration would be required.
I don't see how typeof helps?
Perhaps "on top" was incorrect and confusing. The typeof emulation works by decomposing the type into nodes, such as types and templates, by the means of template specializations created when types/templates are registered. It should not be difficult to augment the registration macros to add one more specialization dealing with the problem discussed. Regards, Arkadiy
Arkadiy Vertleyb wrote:
"Robert Ramey"
wrote Much fame and fortune will accrue to he who creates the function
template<typename T> char * guid()
which returns a unique char string in a platform independent way.
Might be done pretty easily on top of typeof emulation. Of course, custom type/template registration would be required.
Regards, Arkadiy
"Robert Ramey"
Arkadiy Vertleyb wrote:
"Robert Ramey"
wrote Much fame and fortune will accrue to he who creates the function
template<typename T> char * guid()
which returns a unique char string in a platform independent way.
Might be done pretty easily on top of typeof emulation. Of course, custom type/template registration would be required.
Regards, Arkadiy
Hi Robert, was any message intended here :-) Regards, Arkadiy
Robert Ramey wrote:
Do we have serialization support for boost::any? If not, how is it possible to serialize boost::any?
Good question. Robert, is it possible to place BOOST_CLASS_EXPORT inside class definition, so that I can write something like that:
class any_base {}; template<class T> class any_value_holder : public any_base { ....... BOOST_CLASS_EXPORT(any_value_holder); };
and have BOOST_CLASS_EXPORT run for each instantiation of any_value_holder?
This would tie the GUID "any_value_holder" to multiple types any_value_holder<T> so it wouldn't be unique any more.
So, basically we can't make "any" serializable?
This is another manifestation of the general problem of retreiving an external string from a typename. type_info does this but in a non-portable way. Of course you know this as you rasied this issue in the first place.
Much fame and fortune will accrue to he who creates the function
template<typename T> char * guid()
which returns a unique char string in a platform independent way.
But failing that, what about adding the following macros: I_WANT_TO_USE_TYPEID_WHEN_SERIALIZING_DERIVED_CLASSES_OF(any_base) or USE_THE_FOLLOWING_FUNCTOR_TO_GET_GUI_FOR_CLASSES_DERIVED_FROM(any_base, serialization::typeid_guid_maker) ? At least that would make boost::any serializable. - Volodya
Vladimir Prus wrote:
But failing that, what about adding the following macros:
I_WANT_TO_USE_TYPEID_WHEN_SERIALIZING_DERIVED_CLASSES_OF(any_base)
or
USE_THE_FOLLOWING_FUNCTOR_TO_GET_GUI_FOR_CLASSES_DERIVED_FROM(any_base, serialization::typeid_guid_maker)
? At least that would make boost::any serializable.
I don't think it will make it DEserializable, though. You still need to map the string to a type when loading, which requires that you have previously registered the type. A boost::any is equivalent to a pointer to an abstract class from serialization point of view.
Peter Dimov wrote:
Vladimir Prus wrote:
But failing that, what about adding the following macros:
I_WANT_TO_USE_TYPEID_WHEN_SERIALIZING_DERIVED_CLASSES_OF(any_base)
or
USE_THE_FOLLOWING_FUNCTOR_TO_GET_GUI_FOR_CLASSES_DERIVED_FROM(any_base, serialization::typeid_guid_maker)
? At least that would make boost::any serializable.
I don't think it will make it DEserializable, though. You still need to map the string to a type when loading, which requires that you have previously registered the type. A boost::any is equivalent to a pointer to an abstract class from serialization point of view.
Ah, yes, you're right. Guess we still need a way to register every new instantiation with the serialization library. Maybe, something like this will work: template<class T> class any_value_holder { static boost::proably_not_yet_written::serialization_register< any_value_holder<T> > register_it; }; I don't have any idea how to make this non-intrusively, though. - Volodya
All that is needed is a "function" which returns a pointer to a char string.
template<class T>
const char * guid();
So that one could write the following program - which compiles. I haven't
checked to see that it really works.
#include
Vladimir Prus wrote:
Robert Ramey wrote:
Do we have serialization support for boost::any? If not, how is it possible to serialize boost::any?
Good question. Robert, is it possible to place BOOST_CLASS_EXPORT inside class definition, so that I can write something like that:
class any_base {}; template<class T> class any_value_holder : public any_base { ....... BOOST_CLASS_EXPORT(any_value_holder); };
and have BOOST_CLASS_EXPORT run for each instantiation of any_value_holder?
This would tie the GUID "any_value_holder" to multiple types any_value_holder<T> so it wouldn't be unique any more.
So, basically we can't make "any" serializable?
I doubt its hard to make "any" serializable. Its the automatic export of any<T> (or whatever it is) that's the difficulty. If one is willing to add BOOST_CLASS_EXPORT_GUID(any<X>, "any_X") for each any<X> used in one's application there would be no problem. The issue isn't the serialization of boost::any per se, but rather how to generate a character name from a type. As I write thsi, I was going to mention that shared_ptr<T> has the same problem --- But, it doesn't now I have to think about if there is any problem at all or what. Robert Ramey
Robert Ramey wrote:
As I write thsi, I was going to mention that shared_ptr<T> has the same problem --- But, it doesn't now I have to think about if there is any problem at all or what.
Robert Ramey
Now I remember - shared_ptr is marked with no_tracking. That is we never serialize a pointer to a shared_ptr (makes sense!). Thus we never have to invoke BOOST_CLASS_EXPORT(shared_ptr<X>) . So, if boost::any can be no_tracking then there is not problem at all. Just implement serialization for boost::any<T> and be sure that BOOST_CLASS_EXPORT is applied to each T as usual. Robert Ramey
Robert Ramey wrote:
Robert Ramey wrote:
As I write thsi, I was going to mention that shared_ptr<T> has the same problem --- But, it doesn't now I have to think about if there is any problem at all or what.
Robert Ramey
Now I remember - shared_ptr is marked with no_tracking. That is we never serialize a pointer to a shared_ptr (makes sense!). Thus we never have to invoke BOOST_CLASS_EXPORT(shared_ptr<X>) .
So, if boost::any can be no_tracking then there is not problem at all. Just implement serialization for boost::any<T> and be sure that BOOST_CLASS_EXPORT is applied to each T as usual.
The problem is: there's no boost::any<T>, there's boost::any, which is non-template class. Any part of program can assign any type to boost::any. It is very unreasonable to expect that user invokes BOOST_CLASS_EXPORT_GUID(any_value_holder<X>, "any_value_holder_X") for all instantiations of any_value_holder<X> that are *implicitly* created in the program. In fact, ordinary user does not even now that any_value_holder exists. So, my original question stands -- it is even possible to make boost::any serializable? - Volodya
Vladimir Prus wrote:
So, my original question stands -- it is even possible to make boost::any serializable?
I've spent a few minutes looking at boost::any and don't see any obvious way
to do it in a general way. If it is possible, it would only be so by
depending on or expanding upon implementation of serialization.
So, what can I suggest? Here are some very preliminary thoughts.
The difficulty in serializing boost::any is that we only know which type is
being serialized at runtime. If we know the possible types that our
instance of boost::any might contain we might be able to use something like:
struct my_class {
boost::any m_any;
...
};
template<class Archive>
void my_class::save(Archive & ar, const unsigned int version){
// define which types we think we will be using
// (be prepared for bad_anycast exception);
boost::variant
Robert Ramey wrote:
Vladimir Prus wrote:
So, my original question stands -- it is even possible to make boost::any serializable?
I've spent a few minutes looking at boost::any and don't see any obvious way to do it in a general way. If it is possible, it would only be so by depending on or expanding upon implementation of serialization.
So, what can I suggest? Here are some very preliminary thoughts. .... template<class Archive> void my_class::load(Archive & ar, const unsigned int version){ // define which types we think we will be = m_any; boost::variant
v; ar >> v; m_any = v; }
I don't think this is good in general. If it's good, then we don't need boost::any at all, we can just boost::variant. What about idea I've expressed in another email: Guess we still need a way to register every new instantiation with the serialization library. Maybe, something like this will work: template<class T> class any_value_holder { static boost::proably_not_yet_written::serialization_register< any_value_holder<T> > register_it; }; Is it possible to make this work? - Volodya
Vladimir Prus wrote:
Robert Ramey wrote:
What about idea I've expressed in another email:
Guess we still need a way to register every new instantiation with the serialization library. Maybe, something like this will work:
template<class T> class any_value_holder { static boost::proably_not_yet_written::serialization_register< any_value_holder<T> > register_it; };
Is it possible to make this work?
I think you would also need to explicitly instantiate it for each type. Which puts us back to square one of having to specify the types ahead of time. Its possible that progress could be made here, but I think it would be pretty tricky. Here are some off hand ideas. It seems to me that boost::any is a basically a wrapper around a pointer to a type ValueType. If ValueType is exported it would possible to make things work. However, boost::any public interface doesn't expose enough of its public interface to make it serializable. So if one wanted to do this he would have to do one of the following: a) make some private variables public b) add some functions to expose required implementation features c) add an intrusive serialize function I believe that c would be the most straight forward - but probably the most objectionable to the powers that be. So if you just need it for your own purposes use c) and upload it to the vault. If you're a gluton for punishment do b), upload it to the vault, and try to make a case for getting the change accepted. a) would be just a hack - (though that's what I did with the first implementation of shared_ptr - and I caught hell for it!) So if you do this, you might want to keep it to yourself. Robert Ramey
Robert Ramey wrote:
template<class T> class any_value_holder { static boost::proably_not_yet_written::serialization_register< any_value_holder<T> > register_it; };
Is it possible to make this work?
I think you would also need to explicitly instantiate it for each type. Which puts us back to square one of having to specify the types ahead of time.
With some cooperation from boost::any, this is not necessary: template<class T> class any_holder { public: any_holder() { ®istrator_; } private: static registrator<T> registrator_; }; will run constructor of 'registrator_' while generating no code in any_holder's constructor. Complete example attached.
Its possible that progress could be made here, but I think it would be pretty tricky. Here are some off hand ideas.
It seems to me that boost::any is a basically a wrapper around a pointer to a type ValueType. If ValueType is exported it would possible to make things work. However, boost::any public interface doesn't expose enough of its public interface to make it serializable. So if one wanted to do this he would have to do one of the following:
a) make some private variables public b) add some functions to expose required implementation features
Namely? boost::any already has const std::type_info & type() const; method and if it can be used to implement non-intrusive serialization, this will be great. - Volodya
I happy to defer to you guys on this as I only gave it a very cursory look. I would like to see boost::any serializable. My short look made me think it couldn't be done without modfication of boost::any but I would be pleased to be proven wrong. Those who need this and want to make it happen have my full encouragement. Personally I don't see why a small modification to boost::any should be a problem it that would help make it serializable - but as I said, I'm happy to let those who actually need it deal with it. Good Luck, Robert Ramey Vladimir Prus wrote:
Robert Ramey wrote:
template<class T> class any_value_holder { static boost::proably_not_yet_written::serialization_register< any_value_holder<T> > register_it; };
Is it possible to make this work?
I think you would also need to explicitly instantiate it for each type. Which puts us back to square one of having to specify the types ahead of time.
With some cooperation from boost::any, this is not necessary:
template<class T> class any_holder { public: any_holder() { ®istrator_; }
private: static registrator<T> registrator_;
};
will run constructor of 'registrator_' while generating no code in any_holder's constructor. Complete example attached.
Its possible that progress could be made here, but I think it would be pretty tricky. Here are some off hand ideas.
It seems to me that boost::any is a basically a wrapper around a pointer to a type ValueType. If ValueType is exported it would possible to make things work. However, boost::any public interface doesn't expose enough of its public interface to make it serializable. So if one wanted to do this he would have to do one of the following:
a) make some private variables public b) add some functions to expose required implementation features
Namely? boost::any already has
const std::type_info & type() const;
method and if it can be used to implement non-intrusive serialization, this will be great.
- Volodya
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (6)
-
Arkadiy Vertleyb
-
Li Lirong
-
Peter Dimov
-
Rene Rivera
-
Robert Ramey
-
Vladimir Prus