Re: [Boost-users] [Serialization] is_serializable trait?
Re: [Serialization] is_serializable trait? (Chris Fairles)
I also took a stab at making a boostified version:
#include <iostream> #include
#include #include #include #include namespace boost { namespace detail {
template <class T> struct has_serialize_mem_fun { typedef void (T::*SerializationFun)(int &, unsigned);
template <SerializationFun> struct A {};
template <class Q> static ::boost::type_traits::yes_type has_serialize_mem_fun_tester(A<&Q::serialize>*);
template <class Q> static ::boost::type_traits::no_type has_serialize_mem_fun_tester(...);
BOOST_STATIC_CONSTANT(bool, value = (sizeof(has_serialize_mem_fun_tester<T>(0)) == sizeof(::boost::type_traits::yes_type)));
}; template
struct is_serializable_impl { BOOST_STATIC_CONSTANT(bool, value = has_serialize_mem_fun<T>::value); }; template <typename T> struct is_serializable_impl
::type> { BOOST_STATIC_CONSTANT(bool, value = true); }; }
BOOST_TT_AUX_BOOL_TRAIT_DEF1(is_serializable,T,::boost::detail::is_serializable_impl<T>::value)
}
struct A { template <class Stream> void serialize(Stream & strm, unsigned version) {} };
struct B {};
struct C { void serialize() {} };
int main() { using namespace std; cout << boolalpha << boost::is_serializable<A>::value << '\n' // true << boost::is_serializable<int>::value << '\n' // true << boost::is_serializable<float>::value << '\n' // true << boost::is_serializable<double>::value << '\n' // true << boost::is_serializable<char>::value << '\n' // true << boost::is_serializable<B>::value << '\n' // false << boost::is_serializable<C>::value << '\n' // false << boost::is_serializable<string>::value << endl; //false but should be true }
Needs specializations for standard containers as well but this suits my needs currently. Thoughts?
I tried to implement is_non_intrusively_serializable metafunction, but
found no general solution. Although I've impelented it for all types
which are instances of some class template. For example, it works
correctly
for vector<int>, some_class
participants (1)
-
Roman Perepelitsa