diff --git a/include/cereal/archives/binary.hpp b/include/cereal/archives/binary.hpp index f9a67d1a..9b8a2639 100644 --- a/include/cereal/archives/binary.hpp +++ b/include/cereal/archives/binary.hpp @@ -119,22 +119,6 @@ namespace cereal ar( t.size ); } - //! Saving arrays - template inline - typename std::enable_if::value, void>::type - save(BinaryOutputArchive & ar, T const & array) - { - ar.saveBinary(array, traits::sizeof_array() * sizeof(typename std::remove_all_extents::type)); - } - - //! Loading arrays - template inline - typename std::enable_if::value, void>::type - load(BinaryInputArchive & ar, T & array) - { - ar.loadBinary(array, traits::sizeof_array() * sizeof(typename std::remove_all_extents::type)); - } - //! Saving binary data template inline void save(BinaryOutputArchive & ar, BinaryData const & bd) diff --git a/include/cereal/cereal.hpp b/include/cereal/cereal.hpp index 433e08d0..35140ec8 100644 --- a/include/cereal/cereal.hpp +++ b/include/cereal/cereal.hpp @@ -36,7 +36,6 @@ #include #include #include -#include namespace cereal { @@ -556,4 +555,7 @@ namespace cereal }; // class InputArchive } // namespace cereal +// This include needs to come after things such as binary_data, make_nvp, etc +#include + #endif // CEREAL_CEREAL_HPP_ diff --git a/include/cereal/types/common.hpp b/include/cereal/types/common.hpp index 43b9b23c..6ad0260b 100644 --- a/include/cereal/types/common.hpp +++ b/include/cereal/types/common.hpp @@ -27,7 +27,7 @@ #ifndef CEREAL_TYPES_COMMON_HPP_ #define CEREAL_TYPES_COMMON_HPP_ -#include +#include namespace cereal { @@ -46,7 +46,34 @@ namespace cereal static_assert(!sizeof(T), "Cereal does not support serializing raw pointers - please use a smart pointer"); } - // TODO: move C style arrays into here + namespace common_detail + { + //! Serialization for arrays if BinaryData is supported + /*! @internal */ + template inline + void serializeArray( Archive & ar, T & array, std::true_type /* binary_supported */ ) + { + ar( binary_data( array, traits::sizeof_array() * sizeof(typename std::remove_all_extents::type) ) ); + } + + //! Serialization for arrays if BinaryData is not supported + /*! @internal */ + template inline + void serializeArray( Archive & ar, T & array, std::false_type /* binary_supported */ ) + { + for( auto & i : array ) + ar( i ); + } + } + + //! Serialization for arrays + template inline + typename std::enable_if::value, void>::type + serialize(Archive & ar, T & array) + { + common_detail::serializeArray( ar, array, + std::integral_constant, Archive>()>() ); + } } // namespace cereal #endif // CEREAL_TYPES_COMMON_HPP_ diff --git a/sandbox.cpp b/sandbox.cpp index 501cd958..2248aa77 100644 --- a/sandbox.cpp +++ b/sandbox.cpp @@ -315,6 +315,15 @@ int main() assert(e_in == e_out); + { + cereal::BinaryOutputArchive archive(std::cout); + int xxx[] = {-1, 95, 3}; + archive( xxx ); + + cereal::XMLOutputArchive archive2(std::cout); + archive2( xxx ); + } + { std::ofstream os("out.xml"); cereal::XMLOutputArchive oar( os );