diff --git a/cereal.hpp b/cereal.hpp index 8b46c9bd..36a662e8 100644 --- a/cereal.hpp +++ b/cereal.hpp @@ -6,6 +6,30 @@ namespace cereal { + namespace detail + { + struct NameValuePairCore {}; + } + + //! For holding name value pairs + template + struct NameValuePair : detail::NameValuePairCore + { + NameValuePair( std::string const & n, T const & v ) : name(n), value(v) {} + + std::string name; + T value; + }; + + //! Creates a name value pair + template inline + NameValuePair make_nvp( std::string const & name, T const & value ) + { + return {name, value}; + } + + //! Creates a name value pair for the variable T, using the same name + #define CEREAL_NVP(T) make_nvp("T", T); class BinaryOutputArchive { @@ -18,7 +42,7 @@ namespace cereal template typename std::enable_if() && traits::has_member_serialize(), BinaryOutputArchive &>::type - operator & (T & t) + operator & (T && t) { std::cout << "Member serialize" << std::endl; @@ -30,7 +54,7 @@ namespace cereal template typename std::enable_if() && traits::has_non_member_serialize(), BinaryOutputArchive &>::type - operator & (T & t) + operator & (T && t) { std::cout << "Non member serialize" << std::endl; @@ -95,9 +119,16 @@ namespace cereal typename std::enable_if::value, void>::type serialize(BinaryOutputArchive & ar, T & t) { - //ar.itsStream << t; ar.save_binary(std::addressof(t), sizeof(t)); std::cout << "Serializing POD size: " << sizeof(T) << " [" << t << "]" << std::endl; } + //! Serialization for NVP types to binary + template + typename std::enable_if::value, void>::type + serialize(BinaryOutputArchive & ar, T & t) + { + std::cout << "Serializing NVP: " << t.name << " " << t.value << std::endl; + ar & t.value; + } } diff --git a/test.cpp b/test.cpp index 096696e6..8e73db8d 100644 --- a/test.cpp +++ b/test.cpp @@ -82,5 +82,16 @@ int main() archive & t3; archive & t4; + int x = 5; + auto nvp = cereal::make_nvp("hello!", x); + //auto nvp2 = CEREAL_NVP(x); + + std::cout << std::is_base_of::value << std::endl; + std::cout << cereal::traits::has_non_member_serialize() << std::endl; + std::cout << cereal::traits::is_serializable() << std::endl; + + archive & nvp; + archive & cereal::make_nvp("another", x); + return 0; }