1616#ifndef ALICEO2_BOOSTSERIALIZER_H
1717#define ALICEO2_BOOSTSERIALIZER_H
1818
19+ #include < utility>
20+ #include < type_traits>
21+ #include < array>
22+ #include < vector>
23+ #include < list>
24+ #include < map>
25+ #include < set>
26+
1927#include < boost/archive/binary_iarchive.hpp>
2028#include < boost/archive/binary_oarchive.hpp>
2129#include < boost/serialization/is_bitwise_serializable.hpp>
@@ -33,61 +41,46 @@ namespace utils
3341{
3442namespace check
3543{
36- // /A set of classes and struct to be sure the serialised object is either trivial or implementing custom serialize
37- template <class Type , class Archive , typename = typename std::enable_if<std::is_class<Type>::value>::type>
38- class is_boost_serializable
39- {
40- private:
41- struct TypeOverloader {
42- void serialize (Archive& ar, const unsigned int version) {}
43- };
44- struct TypeExt : public Type , public TypeOverloader {
45- };
46- template <typename T, T t>
47- class DeductionHelper
48- {
49- };
44+ // template <class Type, class Archive, typename = typename std::enable_if<std::is_class<Type>::value>::type>
45+ template <typename ...>
46+ using void_t = void ;
5047
51- class True
52- {
53- char m;
54- };
55- class False
56- {
57- True m[2 ];
58- };
48+ template <typename Type, typename Archive = boost::archive::binary_oarchive, typename = void_t <>>
49+ struct is_boost_serializable_base : std::false_type {
50+ };
5951
60- template <typename TestType>
61- static False deduce (TestType*, DeductionHelper<void (TypeOverloader::*)(), &TestType::serialize>* = 0);
62- static True deduce (...);
52+ template <class Type , typename Archive>
53+ struct is_boost_serializable_base <Type, Archive,
54+ void_t <decltype (std::declval<Type &>().serialize(std::declval<Archive &>(), 0 ))>>
55+ : std::true_type {
56+ };
6357
64- public:
65- static const bool value = (sizeof (True) == sizeof (deduce((TypeExt*)(0 ))));
58+ template <typename Type, typename Archive>
59+ struct is_boost_serializable_base <Type, Archive,
60+ typename std::enable_if<boost::serialization::is_bitwise_serializable<Type>::value>::type>
61+ : std::true_type {
62+ };
63+
64+ template <typename Type, typename Archive = boost::archive::binary_oarchive, typename = void_t <>>
65+ struct is_boost_serializable
66+ : is_boost_serializable_base<Type, Archive> {
67+ };
68+
69+ template <typename Type, typename Archive>
70+ struct is_boost_serializable <Type, Archive, void_t <typename Type::value_type>>
71+ : is_boost_serializable<typename Type::value_type, Archive> {
72+ };
73+
74+ template <typename Type>
75+ struct is_boost_serializable <Type, boost::archive::binary_oarchive, void_t <typename Type::value_type>>
76+ : is_boost_serializable<typename Type::value_type, boost::archive::binary_oarchive> {
6677};
6778} // namespace check
6879
6980template <typename ContT>
70- typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::binary_oarchive>::value
71- && std::is_class<typename ContT::value_type>::value, std::ostringstream>::type
72- BoostSerialize (const ContT &dataSet)
73- {
74- static_assert (check::is_boost_serializable<typename ContT::value_type, boost::archive::binary_oarchive>::value,
75- " This class doesn't provide a boost serializer." );
76- // / Serialises a container (vector, array or list) using boost serialisation routines.
77- // / Requires the contained type to be either trivial or provided with an overried of boost::serialise method.
78- std::ostringstream buffer;
79- boost::archive::binary_oarchive outputArchive (buffer);
80- outputArchive << dataSet;
81- return buffer;
82- }
83-
84- template <typename ContT, typename ContentT = typename ContT::value_type>
85- typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::binary_oarchive>::value
86- && !(std::is_class<ContentT>::value), std::ostringstream>::type
87- BoostSerialize (const ContT &dataSet)
81+ typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::binary_oarchive>::value, std::ostringstream>::type
82+ BoostSerialize (const ContT& dataSet)
8883{
89- static_assert (boost::serialization::is_bitwise_serializable<typename ContT::value_type>::value,
90- " This type doesn't provide a boost serializer." );
9184 // / Serialises a container (vector, array or list) using boost serialisation routines.
9285 // / Requires the contained type to be either trivial or provided with an overried of boost::serialise method.
9386 std::ostringstream buffer;
@@ -97,49 +90,16 @@ typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::bina
9790}
9891
9992template <typename ContT>
100- typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::binary_iarchive>::value
101- && std::is_class<typename ContT::value_type>::value, ContT>::type
102- BoostDeserialize (std::string &msgStr)
93+ typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::binary_iarchive>::value, ContT>::type
94+ BoostDeserialize (std::string& msgStr)
10395{
104- static_assert (check::is_boost_serializable<typename ContT::value_type, boost::archive::binary_oarchive>::value,
105- " This class doesn't provide a boost deserializer." );
10696 // / Deserialises a msg contained in a string in a container type (vector, array or list) of the provided type.
10797 ContT output;
10898 std::istringstream buffer (msgStr);
10999 boost::archive::binary_iarchive inputArchive (buffer);
110100 inputArchive >> output;
111101 return std::move (output);
112102}
113-
114- template <typename ContT, typename ContentT = typename ContT::value_type>
115- typename std::enable_if<check::is_boost_serializable<ContT, boost::archive::binary_iarchive>::value
116- && !(std::is_class<ContentT>::value), ContT>::type
117- BoostDeserialize (std::string &msgStr)
118- {
119- static_assert (boost::serialization::is_bitwise_serializable<typename ContT::value_type>::value,
120- " This type doesn't provide a boost serializer." );
121- // / Deserialises a msg contained in a string in a container type (vector, array or list) of the provided type.
122- ContT output;
123- std::istringstream buffer (msgStr);
124- boost::archive::binary_iarchive inputArchive (buffer);
125- inputArchive >> output;
126- return std::move (output);
127- }
128-
129- template <typename T>
130- struct has_serializer
131- {
132- template <class , class > class checker ;
133-
134- template <typename C>
135- static std::true_type test (checker<C, decltype (&o2::utils::BoostSerialize<C>)> *);
136-
137- template <typename C>
138- static std::false_type test (...);
139-
140- typedef decltype (test<T>(nullptr )) type;
141- static const bool value = std::is_same<std::true_type, decltype (test<T>(nullptr ))>::value;
142- };
143103} // namespace utils
144104} // namespace o2
145105
0 commit comments