diff --git a/binary_archive/array.hpp b/binary_archive/array.hpp index 11d18847..06befa56 100644 --- a/binary_archive/array.hpp +++ b/binary_archive/array.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::array primitive types to binary - template + template inline typename std::enable_if::value, void>::type save( BinaryOutputArchive & ar, std::array const & array ) { @@ -15,7 +15,7 @@ namespace cereal } //! Loading for std::array primitive types to binary - template + template inline typename std::enable_if::value, void>::type load( BinaryInputArchive & ar, std::array & array ) { @@ -23,7 +23,7 @@ namespace cereal } //! Saving for std::array all other types to binary - template + template inline typename std::enable_if::value, void>::type save( BinaryOutputArchive & ar, std::array const & array ) { @@ -32,7 +32,7 @@ namespace cereal } //! Loading for std::array all other types to binary - template + template inline typename std::enable_if::value, void>::type load( BinaryInputArchive & ar, std::array & array ) { diff --git a/binary_archive/bitset.hpp b/binary_archive/bitset.hpp index 972ce9b1..42a68e25 100644 --- a/binary_archive/bitset.hpp +++ b/binary_archive/bitset.hpp @@ -11,11 +11,17 @@ namespace cereal enum class type : uint8_t { ulong, - uulong, + ullong, string }; - }; + template inline + CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive) + serialize( Archive & ar, type & t ) + { + ar & reinterpret_cast( t ); + } + } //! Serializing (save) for std::bitset to binary template inline @@ -31,8 +37,8 @@ namespace cereal { try { - auto const b = bits.to_uulong(); - ar & bitset_detail::type::uulong; + auto const b = bits.to_ullong(); + ar & bitset_detail::type::ullong; ar & b; } catch( std::overflow_error const & e ) @@ -59,7 +65,7 @@ namespace cereal bits = std::bitset( b ); break; } - case bitset_detail::type::uulong: + case bitset_detail::type::ullong: { unsigned long long b; ar & b; diff --git a/binary_archive/deque.hpp b/binary_archive/deque.hpp index 10d1216e..f1c35cb5 100644 --- a/binary_archive/deque.hpp +++ b/binary_archive/deque.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::deque to binary - template + template inline void save( BinaryOutputArchive & ar, std::deque const & deque ) { ar & deque.size(); @@ -17,7 +17,7 @@ namespace cereal } //! Loading for std::deque to binary - template + template inline void load( BinaryInputArchive & ar, std::deque & deque ) { size_t size; diff --git a/binary_archive/forward_list.hpp b/binary_archive/forward_list.hpp index b71786ad..efa15afa 100644 --- a/binary_archive/forward_list.hpp +++ b/binary_archive/forward_list.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::forward_list all other types to binary - template + template inline void save( BinaryOutputArchive & ar, std::forward_list const & forward_list ) { // save position for size of list @@ -28,7 +28,7 @@ namespace cereal } //! Saving for std::forward_list all other types to binary (non-const version) - template + template inline void save( BinaryOutputArchive & ar, std::forward_list & forward_list ) { // save position for size of list diff --git a/binary_archive/list.hpp b/binary_archive/list.hpp index 571db70f..7295a602 100644 --- a/binary_archive/list.hpp +++ b/binary_archive/list.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::list to binary - template + template inline void save( BinaryOutputArchive & ar, std::list const & list ) { ar & list.size(); @@ -17,7 +17,7 @@ namespace cereal } //! Loading for std::list to binary - template + template inline void load( BinaryInputArchive & ar, std::list & list ) { size_t size; diff --git a/binary_archive/map.hpp b/binary_archive/map.hpp index 5032013b..60adc5ce 100644 --- a/binary_archive/map.hpp +++ b/binary_archive/map.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::map to binary - template + template inline void save( BinaryOutputArchive & ar, std::map const & map ) { ar & map.size(); @@ -20,7 +20,7 @@ namespace cereal } //! Loading for std::map to binary - template + template inline void load( BinaryInputArchive & ar, std::map & map ) { size_t size; @@ -38,7 +38,7 @@ namespace cereal } //! Saving for std::multimap to binary - template + template inline void save( BinaryOutputArchive & ar, std::multimap const & multimap ) { ar & multimap.size(); @@ -51,7 +51,7 @@ namespace cereal } //! Loading for std::multimap to binary - template + template inline void load( BinaryInputArchive & ar, std::multimap & multimap ) { size_t size; diff --git a/binary_archive/memory.hpp b/binary_archive/memory.hpp index c261d55e..a726d9ce 100644 --- a/binary_archive/memory.hpp +++ b/binary_archive/memory.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving std::shared_ptr to binary - template + template inline void save( BinaryOutputArchive & ar, std::shared_ptr const & ptr ) { uint32_t id = ar.registerSharedPointer( ptr.get() ); @@ -20,7 +20,7 @@ namespace cereal } //! Loading std::shared_ptr to binary - template + template inline void load( BinaryInputArchive & ar, std::shared_ptr & ptr ) { uint32_t id; @@ -40,7 +40,7 @@ namespace cereal } //! Saving std::weak_ptr to binary - template + template inline void save( BinaryOutputArchive & ar, std::weak_ptr const & ptr ) { auto sptr = ptr.lock(); @@ -48,7 +48,7 @@ namespace cereal } //! Loading std::weak_ptr from binary - template + template inline void load( BinaryInputArchive & ar, std::weak_ptr & ptr ) { std::shared_ptr sptr; @@ -57,14 +57,14 @@ namespace cereal } //! Saving std::unique_ptr to binary - template + template inline void save( BinaryOutputArchive & ar, std::unique_ptr const & ptr ) { ar & *ptr; } //! Loading std::unique_ptr from binary - template + template inline void load( BinaryInputArchive & ar, std::unique_ptr & ptr ) { ptr.reset(new T); diff --git a/binary_archive/queue.hpp b/binary_archive/queue.hpp index d6f2b739..823572e3 100644 --- a/binary_archive/queue.hpp +++ b/binary_archive/queue.hpp @@ -9,7 +9,7 @@ namespace cereal namespace queue_detail { //! Allows access to the protected container in queue - template + template inline C const & container( std::queue const & queue ) { struct H : public std::queue @@ -24,7 +24,7 @@ namespace cereal } //! Allows access to the protected container in priority queue - template + template inline C const & container( std::priority_queue const & priority_queue ) { struct H : public std::priority_queue @@ -37,9 +37,9 @@ namespace cereal return H::get( priority_queue ); } - + //! Allows access to the protected comparator in priority queue - template + template inline Comp const & comparator( std::priority_queue const & priority_queue ) { struct H : public std::priority_queue @@ -55,14 +55,14 @@ namespace cereal } //! Saving for std::queue to binary - template + template inline void save( BinaryOutputArchive & ar, std::queue const & queue ) { ar & queue_detail::container( queue ); } //! Loading for std::queue to binary - template + template inline void load( BinaryInputArchive & ar, std::queue & queue ) { C container; @@ -71,7 +71,7 @@ namespace cereal } //! Saving for std::priority_queue to binary - template + template inline void save( BinaryOutputArchive & ar, std::priority_queue const & priority_queue ) { ar & queue_detail::comparator( priority_queue ); @@ -79,12 +79,12 @@ namespace cereal } //! Loading for std::priority_queue to binary - template + template inline void load( BinaryInputArchive & ar, std::priority_queue & priority_queue ) { Comp comparator; ar & comparator; - + C container; ar & container; diff --git a/binary_archive/set.hpp b/binary_archive/set.hpp index 10c3c357..cdcbf491 100644 --- a/binary_archive/set.hpp +++ b/binary_archive/set.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::set to binary - template + template inline void save( BinaryOutputArchive & ar, std::set const & set ) { ar & set.size(); @@ -17,7 +17,7 @@ namespace cereal } //! Loading for std::set to binary - template + template inline void load( BinaryInputArchive & ar, std::set & set ) { size_t size; @@ -33,7 +33,7 @@ namespace cereal } //! Saving for std::multiset to binary - template + template inline void save( BinaryOutputArchive & ar, std::multiset const & multiset ) { ar & multiset.size(); @@ -43,7 +43,7 @@ namespace cereal } //! Loading for std::multiset to binary - template + template inline void load( BinaryInputArchive & ar, std::multiset & multiset ) { size_t size; diff --git a/binary_archive/stack.hpp b/binary_archive/stack.hpp index d831bfde..91231353 100644 --- a/binary_archive/stack.hpp +++ b/binary_archive/stack.hpp @@ -9,7 +9,7 @@ namespace cereal namespace stack_detail { //! Allows access to the protected container in stack - template + template inline C const & container( std::stack const & stack ) { struct H : public std::stack @@ -25,14 +25,14 @@ namespace cereal } //! Saving for std::stack to binary - template + template inline void save( BinaryOutputArchive & ar, std::stack const & stack ) { ar & stack_detail::container( stack ); } //! Loading for std::stack to binary - template + template inline void load( BinaryInputArchive & ar, std::stack & stack ) { C container; diff --git a/binary_archive/string.hpp b/binary_archive/string.hpp index 2548f212..22fc66d8 100644 --- a/binary_archive/string.hpp +++ b/binary_archive/string.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Serialization for basic_string types to binary - template + template inline void save(BinaryOutputArchive & ar, std::basic_string const & str) { // Save number of chars + the data @@ -16,7 +16,7 @@ namespace cereal } //! Serialization for basic_string types from binary - template + template inline void load(BinaryInputArchive & ar, std::basic_string & str) { size_t size; diff --git a/binary_archive/unordered_map.hpp b/binary_archive/unordered_map.hpp index ccdba4a0..1cb0b7d0 100644 --- a/binary_archive/unordered_map.hpp +++ b/binary_archive/unordered_map.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::unordered_map to binary - template + template inline void save( BinaryOutputArchive & ar, std::unordered_map const & unordered_map ) { ar & unordered_map.size(); @@ -20,7 +20,7 @@ namespace cereal } //! Loading for std::unordered_map to binary - template + template inline void load( BinaryInputArchive & ar, std::unordered_map & unordered_map ) { size_t size; @@ -40,7 +40,7 @@ namespace cereal } //! Saving for std::unordered_multimap to binary - template + template inline void save( BinaryOutputArchive & ar, std::unordered_multimap const & unordered_multimap ) { ar & unordered_multimap.size(); @@ -53,7 +53,7 @@ namespace cereal } //! Loading for std::unordered_multimap to binary - template + template inline void load( BinaryInputArchive & ar, std::unordered_multimap & unordered_multimap ) { size_t size; diff --git a/binary_archive/unordered_set.hpp b/binary_archive/unordered_set.hpp index ef1cadb9..3c413ca5 100644 --- a/binary_archive/unordered_set.hpp +++ b/binary_archive/unordered_set.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Saving for std::unordered_set to binary - template + template inline void save( BinaryOutputArchive & ar, std::unordered_set const & unordered_set ) { ar & unordered_set.size(); @@ -17,7 +17,7 @@ namespace cereal } //! Loading for std::unordered_set to binary - template + template inline void load( BinaryInputArchive & ar, std::unordered_set & unordered_set ) { size_t size; @@ -35,7 +35,7 @@ namespace cereal } //! Saving for std::unordered_multiset to binary - template + template inline void save( BinaryOutputArchive & ar, std::unordered_multiset const & unordered_multiset ) { ar & unordered_multiset.size(); @@ -45,7 +45,7 @@ namespace cereal } //! Loading for std::unordered_multiset to binary - template + template inline void load( BinaryInputArchive & ar, std::unordered_multiset & unordered_multiset ) { size_t size; diff --git a/binary_archive/vector.hpp b/binary_archive/vector.hpp index 9ca3d22e..762f5106 100644 --- a/binary_archive/vector.hpp +++ b/binary_archive/vector.hpp @@ -7,7 +7,7 @@ namespace cereal { //! Serialization for std::vectors of arithmetic (but not bool) types to binary - template + template inline typename std::enable_if::value && !std::is_same::value, void>::type save( BinaryOutputArchive & ar, std::vector const & vector ) { @@ -19,7 +19,7 @@ namespace cereal } //! Serialization for std::vectors of arithmetic (but not bool) types to binary - template + template inline typename std::enable_if::value && !std::is_same::value, void>::type load( BinaryInputArchive & ar, std::vector & vector ) { @@ -34,7 +34,7 @@ namespace cereal } //! Serialization for non-arithmetic (and bool) vector types to binary - template + template inline typename std::enable_if::value || std::is_same::value, void>::type save( BinaryOutputArchive & ar, std::vector const & vector ) { @@ -44,7 +44,7 @@ namespace cereal } //! Serialization for non-arithmetic (and bool) vector types from binary - template + template inline typename std::enable_if::value || std::is_same::value, void>::type load( BinaryInputArchive & ar, std::vector & vector ) { diff --git a/unittests.cpp b/unittests.cpp index b0e43437..8eee6391 100644 --- a/unittests.cpp +++ b/unittests.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -154,6 +155,15 @@ std::basic_string random_basic_string(std::mt19937 & gen) return s; } +template +std::string random_binary_string(std::mt19937 & gen) +{ + std::string s(N, ' '); + for(auto & c : s ) + c = std::uniform_int_distribution('0', '1')(gen); + return s; +} + // ###################################################################### BOOST_AUTO_TEST_CASE( binary_pod ) { @@ -1668,3 +1678,84 @@ BOOST_AUTO_TEST_CASE( binary_tuple ) BOOST_CHECK_EQUAL( i_espltuple == o_espltuple, true ); } } + +// ###################################################################### +BOOST_AUTO_TEST_CASE( binary_complex ) +{ + std::random_device rd; + std::mt19937 gen(rd()); + + auto rngF = [&](){ return random_value(gen); }; + auto rngD = [&](){ return random_value(gen); }; + auto rngLD = [&](){ return random_value(gen); }; + + for(int i=0; i<100; ++i) + { + std::ostringstream os; + cereal::BinaryOutputArchive oar(os); + + std::complex o_float( rngF(), rngF() ); + std::complex o_double( rngD(), rngD() ); + std::complex o_ldouble( rngLD(), rngLD() ); + + oar & o_float; + oar & o_double; + oar & o_ldouble; + + std::istringstream is(os.str()); + cereal::BinaryInputArchive iar(is); + + std::complex i_float; + std::complex i_double; + std::complex i_ldouble; + + iar & i_float; + iar & i_double; + iar & i_ldouble; + + BOOST_CHECK_EQUAL( o_float, i_float ); + BOOST_CHECK_EQUAL( o_double, i_double ); + BOOST_CHECK_EQUAL( o_ldouble, i_ldouble ); + } +} + +// ###################################################################### +BOOST_AUTO_TEST_CASE( binary_bitset ) +{ + std::random_device rd; + std::mt19937 gen(rd()); + + auto rng32 = [&](){ return random_binary_string<32>( gen ); }; + auto rng65 = [&](){ return random_binary_string<65>( gen ); }; + auto rng256 = [&](){ return random_binary_string<256>( gen ); }; + + for(int i=0; i<100; ++i) + { + std::ostringstream os; + cereal::BinaryOutputArchive oar(os); + + std::bitset<32> o_bit32( rng32() ); + std::bitset<65> o_bit65( rng65() ); + std::bitset<256> o_bit256( rng256() ); + + oar & o_bit32; + oar & o_bit65; + oar & o_bit256; + + std::istringstream is(os.str()); + cereal::BinaryInputArchive iar(is); + + std::bitset<32> i_bit32; + std::bitset<65> i_bit65; + std::bitset<256> i_bit256; + + iar & i_bit32; + iar & i_bit65; + iar & i_bit256; + + BOOST_CHECK_EQUAL( o_bit32, i_bit32 ); + BOOST_CHECK_EQUAL( o_bit65, i_bit65 ); + BOOST_CHECK_EQUAL( o_bit256, i_bit256 ); + } +} +