diff --git a/Makefile b/Makefile index efe48d3a..12c1ff40 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,3 @@ -all: test.cpp cereal.hpp - g++ -std=c++0x test.cpp -o test -ljsoncpp +all: test.cpp + g++ -std=c++0x test.cpp -o test -ljsoncpp -I./.. + diff --git a/binary_archive/binary_archive.hpp b/binary_archive/binary_archive.hpp index e20ca18d..e8575a15 100644 --- a/binary_archive/binary_archive.hpp +++ b/binary_archive/binary_archive.hpp @@ -1,8 +1,10 @@ -#pragma once +#ifndef CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_ +#define CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_ + +#include namespace cereal { - // ###################################################################### class BinaryOutputArchive : public OutputArchive { @@ -47,60 +49,39 @@ namespace cereal std::istream & itsStream; }; - //! Saving for POD types to binary template - typename std::enable_if::value, void>::type - save(BinaryOutputArchive & ar, T const & t) - { - ar.save_binary(std::addressof(t), sizeof(t)); - //std::cout << "Saving POD size: " << sizeof(T) << " [" << t << "]" << std::endl; - } + typename std::enable_if::value, void>::type + save(BinaryOutputArchive & ar, T const & t) + { + ar.save_binary(std::addressof(t), sizeof(t)); + //std::cout << "Saving POD size: " << sizeof(T) << " [" << t << "]" << std::endl; + } //! Loading for POD types from binary template - typename std::enable_if::value, void>::type - load(BinaryInputArchive & ar, T & t) - { - ar.load_binary(std::addressof(t), sizeof(t)); - //std::cout << "Loading POD size: " << sizeof(T) << " [" << t << "]" << std::endl; - } + typename std::enable_if::value, void>::type + load(BinaryInputArchive & ar, T & t) + { + ar.load_binary(std::addressof(t), sizeof(t)); + //std::cout << "Loading POD size: " << sizeof(T) << " [" << t << "]" << std::endl; + } //! Saving for NVP types to binary template - void save(BinaryOutputArchive & ar, NameValuePair const & t) - { - //std::cout << "Saving NVP: " << t.name << std::endl; - ar & t.value; - } + void save(BinaryOutputArchive & ar, NameValuePair const & t) + { + //std::cout << "Saving NVP: " << t.name << std::endl; + ar & t.value; + } //! Loading for NVP types from binary template - void load(BinaryInputArchive & ar, NameValuePair t) - { - //std::cout << "Loading NVP... " << std::endl; - ar & t.value; - } - - //! Serialization for basic_string types to binary - template - void save(BinaryOutputArchive & ar, std::basic_string const & str) - { - // Save number of chars + the data - ar & str.size(); - ar.save_binary(str.data(), str.size() * sizeof(CharT)); - - //std::cout << "Saving string: " << str << std::endl; - } - - //! Serialization for basic_string types from binary - template - void load(BinaryInputArchive & ar, std::basic_string & str) - { - size_t size; - ar & size; - str.resize(size); - ar.load_binary(const_cast(str.data()), size * sizeof(CharT)); - //std::cout << "Loading string: " << str << std::endl; - } + void load(BinaryInputArchive & ar, NameValuePair t) + { + //std::cout << "Loading NVP... " << std::endl; + ar & t.value; + } } + +#endif // CEREAL_BINARY_ARCHIVE_BINARY_ARCHIVE_HPP_ diff --git a/binary_archive/vector.hpp b/binary_archive/vector.hpp index 84fa9c0f..64fbca46 100644 --- a/binary_archive/vector.hpp +++ b/binary_archive/vector.hpp @@ -20,10 +20,10 @@ namespace cereal } //! Serialization for std::vector to binary - template + template void load( BinaryInputArchive & ar, std::vector & vector ) { - std::cout << "Loading array" << std::endl; + std::cout << "Loading vector" << std::endl; size_t dataSize; size_t vectorSize; @@ -34,6 +34,29 @@ namespace cereal ar.load_binary( vector.data(), dataSize ); } + + //! Serialization for std::vector types to binary + template + void save( BinaryOutputArchive & ar, std::vector const & vector ) + { + std::cout << "Saving vector of bool" << std::endl; + + ar & vector.size(); // number of elements + for( auto it = vector.begin(), end = vector.end(); it != end; ++it ) + ar & (*it); + } + + //! Serialization for std::vector to binary + template + void load( BinaryInputArchive & ar, std::vector & vector ) + { + size_t size; + ar & size; + + vector.resize( size ); + for( auto it = vector.begin(), end = vector.end(); it != end; ++it ) + ar & (*it); + } } // namespace cereal #endif // CEREAL_BINARY_ARCHIVE_VECTOR_HPP_ diff --git a/cereal.hpp b/cereal.hpp index 3ee24893..eab4ed56 100644 --- a/cereal.hpp +++ b/cereal.hpp @@ -1,8 +1,10 @@ -#pragma once +#ifndef CEREAL_CEREAL_HPP_ +#define CEREAL_CEREAL_HPP_ #include #include -#include "traits.hpp" + +#include namespace cereal { @@ -163,3 +165,5 @@ namespace cereal ArchiveType * const self; }; // class InputArchive } + +#endif // CEREAL_CEREAL_HPP_ diff --git a/detail/traits.hpp b/detail/traits.hpp deleted file mode 100644 index 7704453a..00000000 --- a/detail/traits.hpp +++ /dev/null @@ -1,96 +0,0 @@ -namespace cereal -{ - namespace traits - { - template struct Void { typedef void type; }; - - // ###################################################################### - // Member Serialize - template - struct has_member_serialize: std::false_type {}; - - template - struct has_member_serialize< T, A, - typename Void< - decltype( std::declval().serialize( std::declval() ) ) - >::type - >: std::true_type {}; - - // ###################################################################### - // Non Member Serialize - template char & serialize(A&, T&); - template - bool constexpr has_non_member_serialize() - { return std::is_void(), std::declval()))>::value; }; - - // ###################################################################### - // Member Load - template - struct has_member_load: std::false_type {}; - - template - struct has_member_load< T, A, - typename Void< - decltype( std::declval().load( std::declval() ) ) - >::type - >: std::true_type {}; - - // ###################################################################### - // Non Member Load - template char & load(A&, T&); - template - bool constexpr has_non_member_load() - { return std::is_void(), std::declval()))>::value; }; - - // ###################################################################### - // Member Save - template - struct has_member_save: std::false_type {}; - - template - struct has_member_save< T, A, - typename Void< - decltype( std::declval().save( std::declval() ) ) - >::type - >: std::true_type {}; - - // ###################################################################### - // Non Member Save - template char & save(A&, T const &); - template - bool constexpr has_non_member_save() - { return std::is_void(), std::declval()))>::value; }; - - // ###################################################################### - template - constexpr bool has_member_split() - { return has_member_load() && has_member_save(); } - - // ###################################################################### - template - constexpr bool has_non_member_split() - { return has_non_member_load() && has_non_member_save(); } - - // ###################################################################### - template - constexpr bool is_output_serializable() - { - return - has_member_save() ^ - has_non_member_save() ^ - has_member_serialize() ^ - has_non_member_serialize(); - } - - // ###################################################################### - template - constexpr bool is_input_serializable() - { - return - has_member_load() ^ - has_non_member_load() ^ - has_member_serialize() ^ - has_non_member_serialize(); - } - } -} diff --git a/details/traits.hpp b/details/traits.hpp index 149b6365..f2e58e79 100644 --- a/details/traits.hpp +++ b/details/traits.hpp @@ -2,6 +2,7 @@ #define CEREAL_DETAILS_TRAITS_HPP_ #include +#include namespace cereal { @@ -97,6 +98,30 @@ namespace cereal has_member_serialize() ^ has_non_member_serialize(); } + + constexpr std::false_type is_smart_ptr(...) + { + return {}; + } + + template + constexpr std::true_type is_smart_ptr( std::unique_ptr const & p ) + { + return {}; + } + + template + constexpr std::true_type is_smart_ptr( std::shared_ptr const & p ) + { + return {}; + } + + //! Returns true if the type T is a pointer or smart pointer (in std library) + template + constexpr bool is_any_pointer() + { + return std::is_pointer() || std::is_same() ))>::value; + } } } diff --git a/json_archive/json_archive.hpp b/json_archive/json_archive.hpp index 659f9d1b..d296d213 100644 --- a/json_archive/json_archive.hpp +++ b/json_archive/json_archive.hpp @@ -1,6 +1,7 @@ -#pragma once +#ifndef CEREAL_JSON_ARCHIVE_JSON_ARCHIVE_HPP_ +#define CEREAL_JSON_ARCHIVE_JSON_ARCHIVE_HPP_ -#include "cereal.hpp" +#include #include #include @@ -96,3 +97,5 @@ namespace cereal //std::cout << "Loading string: " << str << std::endl; } } + +#endif // CEREAL_JSON_ARCHIVE_JSON_ARCHIVE_HPP_ diff --git a/test.cpp b/test.cpp index 37189cd8..39ac979b 100644 --- a/test.cpp +++ b/test.cpp @@ -1,6 +1,9 @@ -#include "cereal.hpp" -#include "binary_archive.hpp" -#include "json_archive.hpp" + +#include +#include +#include +#include + #include #include #include @@ -102,7 +105,6 @@ struct Everything t4.a == o.t4.a && s == o.s; } - }; // ###################################################################### @@ -138,6 +140,7 @@ int main() std::string hello = "Hello, World!"; json & CEREAL_NVP(hello); //json & CEREAL_NVP(e_out); <<< Need to figure out how to recurse! + // return 0; }