diff --git a/binary_archive/array.hpp b/binary_archive/array.hpp new file mode 100644 index 00000000..3974a5fe --- /dev/null +++ b/binary_archive/array.hpp @@ -0,0 +1,26 @@ +#ifndef CEREAL_BINARY_ARCHIVE_ARRAY_HPP_ +#define CEREAL_BINARY_ARCHIVE_ARRAY_HPP_ + +#include +#include + +namespace cereal +{ + //! Serialization for std::array types to binary + template + void save( BinaryOutputArchive & ar, std::array const & array ) + { + std::cout << "Saving array" << std::endl; + ar.save_binary( array.data(), N * sizeof(T) ); + } + + //! Serialization for std::array to binary + template + void load( BinaryInputArchive & ar, std::array & array ) + { + std::cout << "Loading array" << std::endl; + ar.load_binary( array.data(), N * sizeof(T) ); + } +} // namespace cereal + +#endif // CEREAL_BINARY_ARCHIVE_ARRAY_HPP_ diff --git a/binary_archive.hpp b/binary_archive/binary_archive.hpp similarity index 100% rename from binary_archive.hpp rename to binary_archive/binary_archive.hpp diff --git a/binary_archive/string.hpp b/binary_archive/string.hpp new file mode 100644 index 00000000..98379d7e --- /dev/null +++ b/binary_archive/string.hpp @@ -0,0 +1,34 @@ +#ifndef CEREAL_BINARY_ARCHIVE_STRING_HPP_ +#define CEREAL_BINARY_ARCHIVE_STRING_HPP_ + +#include +#include + +namespace cereal +{ + //! Serialization for basic_string types to binary + template + void save(BinaryOutputArchive & ar, std::basic_string const & str) + { + std::cout << "Saving string" << std::endl; + + // Save number of chars + the data + ar & str.size(); + ar.save_binary(str.data(), str.size() * sizeof(CharT)); + } + + //! Serialization for basic_string types from binary + template + void load(BinaryInputArchive & ar, std::basic_string & str) + { + std::cout << "Loading string" << std::endl; + + size_t size; + ar & size; + str.resize(size); + ar.load_binary(const_cast(str.data()), size * sizeof(CharT)); + } +} // namespace cereal + +#endif // CEREAL_BINARY_ARCHIVE_STRING_HPP_ + diff --git a/binary_archive/vector.hpp b/binary_archive/vector.hpp new file mode 100644 index 00000000..84fa9c0f --- /dev/null +++ b/binary_archive/vector.hpp @@ -0,0 +1,39 @@ +#ifndef CEREAL_BINARY_ARCHIVE_VECTOR_HPP_ +#define CEREAL_BINARY_ARCHIVE_VECTOR_HPP_ + +#include +#include + +namespace cereal +{ + //! Serialization for std::vector types to binary + template + void save( BinaryOutputArchive & ar, std::vector const & vector ) + { + std::cout << "Saving vector" << std::endl; + + const size_t dataSize = std::addressof(vector.back()) - std::addressof(vector.front()); + + ar & vector.size(); // number of elements + ar & dataSize; // size of data (may be larger due to allocator strategy) + ar.save_binary( array.data(), size ); // actual data + } + + //! Serialization for std::vector to binary + template + void load( BinaryInputArchive & ar, std::vector & vector ) + { + std::cout << "Loading array" << std::endl; + + size_t dataSize; + size_t vectorSize; + ar & vectorSize; + ar & dataSize; + + vector.resize( vectorSize ); + + ar.load_binary( vector.data(), dataSize ); + } +} // namespace cereal + +#endif // CEREAL_BINARY_ARCHIVE_VECTOR_HPP_ diff --git a/traits.hpp b/detail/traits.hpp similarity index 100% rename from traits.hpp rename to detail/traits.hpp diff --git a/details/traits.hpp b/details/traits.hpp new file mode 100644 index 00000000..149b6365 --- /dev/null +++ b/details/traits.hpp @@ -0,0 +1,103 @@ +#ifndef CEREAL_DETAILS_TRAITS_HPP_ +#define CEREAL_DETAILS_TRAITS_HPP_ + +#include + +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(); + } + } +} + +#endif // CEREAL_DETAILS_TRAITS_HPP_ diff --git a/json_archive.hpp b/json_archive/json_archive.hpp similarity index 100% rename from json_archive.hpp rename to json_archive/json_archive.hpp