From a5dcb04c97e8fa449df7542a1b711e4b44ae85f0 Mon Sep 17 00:00:00 2001 From: Randolph Voorhies Date: Tue, 11 Jun 2013 18:18:21 -0700 Subject: [PATCH] First commit - basic split/serialize checking works --- Makefile | 2 ++ cereal.hpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 Makefile create mode 100644 cereal.hpp create mode 100644 test.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..10d7b2c3 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: test.cpp cereal.hpp + g++ -std=c++0x test.cpp -o test diff --git a/cereal.hpp b/cereal.hpp new file mode 100644 index 00000000..1fbfe512 --- /dev/null +++ b/cereal.hpp @@ -0,0 +1,103 @@ +#pragma once + +#include +#include + +namespace cereal +{ + namespace detail + { + template struct Void { typedef void type; }; + + template + struct has_serialize: std::false_type {}; + + template + struct has_serialize< T, A, + typename Void< + decltype( std::declval().serialize( std::declval(), 0 ) ) + >::type + >: std::true_type {}; + + template + struct has_load: std::false_type {}; + + template + struct has_load< T, A, + typename Void< + decltype( std::declval().load( std::declval(), 0 ) ) + >::type + >: std::true_type {}; + + template + struct has_save: std::false_type {}; + + template + struct has_save< T, A, + typename Void< + decltype( std::declval().save( std::declval(), 0 ) ) + >::type + >: std::true_type {}; + + template + constexpr bool has_split() + { + return has_load() && has_save(); + } + + template + constexpr bool is_serializable() + { + return has_split() ^ has_serialize(); + } + } + + class BinaryOutputArchive + { + public: + + BinaryOutputArchive() {} + + BinaryOutputArchive(std::ostream & stream) + { + } + + template + typename std::enable_if() && detail::has_serialize(), + BinaryOutputArchive &>::type + operator & (T const & t) + { + std::cout << "Saving non-split member" << std::endl; + //t.serialize(*this, detail::version::value) + return *this; + } + + template + typename std::enable_if() && detail::has_split(), + BinaryOutputArchive &>::type + operator & (T const & t) + { + std::cout << "Saving split member" << std::endl; + //t.save(*this, detail::version::value); + return *this; + } + + template + typename std::enable_if(), BinaryOutputArchive &>::type + operator & (T const & t) + { + static_assert(detail::is_serializable(), "Trying to serialize an unserializable type.\n\n" + "Types must either have a serialize function, or separate save/load functions (but not both).\n" + "Serialize functions generally have the following signature:\n\n" + "template\n" + " void serialize(int & ar, unsigned int version)\n" + " {\n" + " ar & member1 & member2 & member3;\n" + " }\n\n" ); + return *this; + } + + + }; + +} diff --git a/test.cpp b/test.cpp new file mode 100644 index 00000000..8a9afaa9 --- /dev/null +++ b/test.cpp @@ -0,0 +1,92 @@ +#include "cereal.hpp" + +// ################################### +struct Test1 +{ + int a; + std::string b; + + template + void serialize(Archive & ar, unsigned int version) + { + ar & a & b; + } +}; + +// ################################### +struct Test2 +{ + int a; + std::string b; + + template + void save(Archive & ar, unsigned int version) + { + ar & a & b; + } + + template + void load(Archive & ar, unsigned int version) + { + ar & a & b; + } +}; + +// ################################### +struct Test3 +{ + int a; + std::string b; + + template + void serialize(int & ar, unsigned int version) + { + ar & a; + } +}; + +// ################################### +struct Test4 +{ + int a; + std::string b; + + template + void serialize(Archive & ar, unsigned int version) + { + ar & a & b; + } + + template + void save(Archive & ar, unsigned int version) + { + ar & a & b; + } + + template + void load(Archive & ar, unsigned int version) + { + ar & a & b; + } +}; + +// ###################################################################### +int main() +{ + cereal::BinaryOutputArchive archive; + + Test1 t1; + Test2 t2; + Test3 t3; + Test4 t4; + + archive & t1; + archive & t2; + //archive & t3; + //archive & t4; + + + + + return 0; +}