From 2dedb62363e4f2211a5e14b2d8813bb664e8e26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Wed, 29 Sep 2021 11:45:45 +0200 Subject: [PATCH] Serializer: Detect Existence of 'serializeOp' Member Function More direct test than looking for a 'serializeObject' member. --- ebos/eclmpiserializer.hh | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/ebos/eclmpiserializer.hh b/ebos/eclmpiserializer.hh index 9d0e372e5..842b0ea6c 100644 --- a/ebos/eclmpiserializer.hh +++ b/ebos/eclmpiserializer.hh @@ -22,7 +22,10 @@ #define ECL_MPI_SERIALIZER_HH #include + #include +#include +#include #include namespace Opm { @@ -68,7 +71,7 @@ public: //! \tparam T Type for vector elements //! \tparam complexType Whether or not T is a complex type //! \param data The vector to (de-)serialize - template + template void vector(std::vector& data) { auto handle = [&](auto& d) @@ -355,7 +358,9 @@ public: data.serializeOp(*this); } - //! \brief Serialize and broadcast on root process, de-serialize on others. + //! \brief Serialize and broadcast on root process, de-serialize on + //! others. + //! //! \tparam T Type of class to broadcast //! \param data Class to broadcast template @@ -466,6 +471,21 @@ protected: constexpr static bool value = true; }; + //! Detect existence of \c serializeOp member function + //! + //! Base case (no \c serializeOp member function) + template + struct has_serializeOp : public std::false_type {}; + + //! Detect existence of \c serializeOp member function + //! + //! Non-default, albeit common, case (type has \c serializeOp member + //! function) + template + struct has_serializeOp< + T, std::void_t().serializeOp(std::declval()))> + > : public std::true_type {}; + //! \brief Handler for pairs. //! \details If data is POD or a string, we pass it to the underlying serializer, //! if not we assume a complex type. @@ -499,21 +519,6 @@ protected: data->serializeOp(*this); } - //! \brief Checks if a type has a serializeOp member. - //! \detail Ideally we would check for the serializeOp member, - //! but this is a member template. For simplicity, - //! we use serializeObject as our check for now. - template - class has_serializeOp - { - using yes_type = char; - using no_type = long; - template static yes_type test(decltype(&U::serializeObject)); - template static no_type test(...); - public: - static constexpr bool value = sizeof(test(0)) == sizeof(yes_type); - }; - Dune::CollectiveCommunication m_comm; //!< Communicator to broadcast using Operation m_op = Operation::PACKSIZE; //!< Current operation