我对我现在面临的一个问题有疑问。
在C ++中,我创建了一个能够检测给定模板方法的模板参数是否实现serialize
方法(增强串行化实现)的助手。
在我可以投掷给它的大多数类中,该助手都可以完美地工作,但是我想按如下方式扩展其功能。
让我们假设我有一个Foo
类,该类具有序列化方法。
鉴于vector<list<array<Foo,8>>>
,vector
和list
提供了array
方法,我希望我可以将它传递给任何嵌套的受支持容器serialize
。
我该如何迭代包含类型中的更深层次,直到不实现value_type
成员的类型?
提前谢谢
这是您当前的解决方案:
namespace check
{
///A set of classes and struct to be sure the serialised object is either trivial or implementing custom serialize
template <class Type, class Archive, typename = typename std::enable_if<std::is_class<Type>::value>::type>
class is_boost_serializable
{
private:
struct TypeOverloader {
void serialize(Archive& ar, const unsigned int version) {}
};
struct TypeExt : public Type, public TypeOverloader {
};
template <typename T, T t>
class DeductionHelper
{
};
class True
{
char m;
};
class False
{
True m[2];
};
template <typename TestType>
static False deduce(TestType*, DeductionHelper<void (TypeOverloader::*)(), &TestType::serialize>* = 0);
static True deduce(...);
public:
static const bool value = (sizeof(True) == sizeof(deduce((TypeExt*)(0))));
};
} // namespace check
答案 0 :(得分:2)
#include <utility>
#include <type_traits>
template <typename...>
using void_t = void;
template <typename Type, typename Archive, typename = void_t<>>
struct is_boost_serializable : std::false_type {};
template <typename Type, typename Archive>
struct is_boost_serializable<Type, Archive,
void_t<decltype(std::declval<Type&>().serialize(std::declval<Archive&>(), 0))>>
: std::true_type {};
template <typename Type, typename Archive, typename = void_t<>>
struct nested_is_boost_serializable
: is_boost_serializable<Type, Archive> {};
template <typename Type, typename Archive>
struct nested_is_boost_serializable<Type, Archive, void_t<typename Type::value_type>>
: nested_is_boost_serializable<typename Type::value_type, Archive> {};
答案 1 :(得分:1)
遵循Piotr的出色建议,我扩展了他的解决方案,以便也可以与boost普通可序列化类型(int,char,double ...)兼容。
这里是我的定制解决方案。
干杯,谢谢!
#include <type_traits>
#include <utility>
#include <vector>
#include <list>
#include <array>
#include <cassert>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/is_bitwise_serializable.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/set.hpp>
#include <boost/type_traits.hpp>
struct Arc
{
};
struct Foo
{
int pippo = 0;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar &pippo;
}
};
struct Bar
{
};
template <typename...>
using void_t = void;
template <typename Type, typename Archive = boost::archive::binary_oarchive, typename = void_t<>>
struct is_boost_serializable_base : std::false_type
{
};
template <class Type, typename Archive>
struct is_boost_serializable_base<Type, Archive,
void_t<decltype(std::declval<Type &>().serialize(std::declval<Archive &>(), 0))>>
: std::true_type
{
};
template <typename Type, typename Archive>
struct is_boost_serializable_base<Type, Archive,
typename std::enable_if<boost::serialization::is_bitwise_serializable<Type>::value>::type>
: std::true_type
{
};
template <typename Type, typename Archive = boost::archive::binary_oarchive, typename = void_t<>>
struct is_boost_serializable
: is_boost_serializable_base<Type, Archive>
{
};
template <typename Type, typename Archive>
struct is_boost_serializable<Type, Archive, void_t<typename Type::value_type>>
: is_boost_serializable<typename Type::value_type, Archive>
{
};
template <typename Type>
struct is_boost_serializable<Type, boost::archive::binary_oarchive, void_t<typename Type::value_type>>
: is_boost_serializable<typename Type::value_type, boost::archive::binary_oarchive>
{
};
int main()
{
static_assert(is_boost_serializable<Foo, Arc>::value, "!");
static_assert(is_boost_serializable<std::vector<std::list<std::array<Foo, 8>>>, Arc>::value, "!");
static_assert(is_boost_serializable<std::vector<std::list<std::array<int, 8>>>, Arc>::value, "!");
static_assert(not is_boost_serializable<std::vector<std::list<std::array<Bar, 8>>>, Arc>::value, "!");
}