/* Copyright (c) 2014, Randolph Voorhies, Shane Grant All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CEREAL_TEST_STRUCTS_MINIMAL_H_ #define CEREAL_TEST_STRUCTS_MINIMAL_H_ #include "common.hpp" struct BogusBase { template void serialize( Archive & ) {} template void save( Archive & ) const {} template void load( Archive & ) {} template int save_minimal( Archive const & ) const { return 0; } template void load_minimal( Archive const &, int const & ) {} }; struct BogusBaseVersioned { template void serialize( Archive &, const std::uint32_t ) {} template void save( Archive &, const std::uint32_t ) const {} template void load( Archive &, const std::uint32_t ) {} template int save_minimal( Archive const &, const std::uint32_t ) const { return 0; } template void load_minimal( Archive const &, int const &, const std::uint32_t ) {} }; struct BogusBasePolymorphic { template void serialize( Archive & ) {} virtual void doesNothing() {} virtual ~BogusBasePolymorphic() {} }; class SpecializedMSerialize : public BogusBase { public: SpecializedMSerialize() = default; SpecializedMSerialize( int xx ) : x(xx) {} int x; private: friend class cereal::access; template void serialize( Archive & ar ) { ar( x ); } }; class SpecializedMSerializeVersioned : public BogusBaseVersioned { public: SpecializedMSerializeVersioned() = default; SpecializedMSerializeVersioned( int xx ) : x(xx) {} int x; private: friend class cereal::access; template void serialize( Archive & ar, const std::uint32_t ) { ar( x ); } }; class SpecializedMSplit : public BogusBase { public: SpecializedMSplit() = default; SpecializedMSplit( int xx ) : x(xx) {} int x; private: friend class cereal::access; template void save( Archive & ar ) const { ar( x ); } template void load( Archive & ar ) { ar( x ); } }; class SpecializedMSplitVersioned : public BogusBaseVersioned { public: SpecializedMSplitVersioned() = default; SpecializedMSplitVersioned( int xx ) : x(xx) {} int x; private: friend class cereal::access; template void save( Archive & ar, const std::uint32_t ) const { ar( x ); } template void load( Archive & ar, const std::uint32_t ) { ar( x ); } }; class SpecializedMSplitPolymorphic : public BogusBasePolymorphic { public: SpecializedMSplitPolymorphic() = default; SpecializedMSplitPolymorphic( int xx ) : x(xx) {} virtual ~SpecializedMSplitPolymorphic() {} int x; private: friend class cereal::access; template void save( Archive & ar ) const { ar( x ); } template void load( Archive & ar ) { ar( x ); } }; class SpecializedMSplitMinimal : public BogusBase { public: SpecializedMSplitMinimal() = default; SpecializedMSplitMinimal( int xx ) : x(xx) {} int x; private: friend class cereal::access; template int save_minimal( Archive const & ) const { return x; } template void load_minimal( Archive const &, int const & value ) { x = value; } }; class SpecializedMSplitVersionedMinimal : public BogusBaseVersioned { public: SpecializedMSplitVersionedMinimal() = default; SpecializedMSplitVersionedMinimal( int xx ) : x(xx) {} int x; private: friend class cereal::access; template int save_minimal( Archive const &, const std::uint32_t ) const { return x; } template void load_minimal( Archive const &, int const & value, const std::uint32_t ) { x = value; } }; class SpecializedNMSerialize : public BogusBase { public: SpecializedNMSerialize() = default; SpecializedNMSerialize( int xx ) : x(xx) {} int x; }; template void serialize( Archive & ar, SpecializedNMSerialize & s ) { ar( s.x ); } class SpecializedNMSerializeVersioned : public BogusBaseVersioned { public: SpecializedNMSerializeVersioned() = default; SpecializedNMSerializeVersioned( int xx ) : x(xx) {} int x; }; template void serialize( Archive & ar, SpecializedNMSerializeVersioned & s ) { ar( s.x ); } class SpecializedNMSplit : public BogusBase { public: SpecializedNMSplit() = default; SpecializedNMSplit( int xx ) : x(xx) {} int x; }; template void load( Archive & ar, SpecializedNMSplit & s ) { ar( s.x ); } template void save( Archive & ar, SpecializedNMSplit const & s ) { ar( s.x ); } class SpecializedNMSplitVersioned : public BogusBaseVersioned { public: SpecializedNMSplitVersioned() = default; SpecializedNMSplitVersioned( int xx ) : x(xx) {} int x; }; template void load( Archive & ar, SpecializedNMSplitVersioned & s, const std::uint32_t ) { ar( s.x ); } template void save( Archive & ar, SpecializedNMSplitVersioned const & s, const std::uint32_t ) { ar( s.x ); } class SpecializedNMSplitMinimal : public BogusBase { public: SpecializedNMSplitMinimal() = default; SpecializedNMSplitMinimal( int xx ) : x(xx) {} int x; }; template void load_minimal( Archive const &, SpecializedNMSplitMinimal & s, int const & value ) { s.x = value; } template int save_minimal( Archive const &, SpecializedNMSplitMinimal const & s ) { return s.x; } class SpecializedNMSplitVersionedMinimal : public BogusBaseVersioned { public: SpecializedNMSplitVersionedMinimal() = default; SpecializedNMSplitVersionedMinimal( int xx ) : x(xx) {} int x; }; template void load_minimal( Archive const &, SpecializedNMSplitVersionedMinimal & s, int const & value, const std::uint32_t ) { s.x = value; } template int save_minimal( Archive const &, SpecializedNMSplitVersionedMinimal const & s, const std::uint32_t ) { return s.x; } namespace cereal { template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; template struct specialize {}; } CEREAL_REGISTER_TYPE(SpecializedMSplitPolymorphic) CEREAL_REGISTER_POLYMORPHIC_RELATION(BogusBasePolymorphic, SpecializedMSplitPolymorphic) template inline void test_structs_specialized() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { SpecializedMSerialize o_iser = { random_value(gen) }; SpecializedMSerializeVersioned o_iserv = { random_value(gen) }; SpecializedMSplit o_ispl = { random_value(gen) }; SpecializedMSplitVersioned o_isplv = { random_value(gen) }; // added re: issue #180 std::shared_ptr o_shared_ispl = std::make_shared( random_value(gen) ); SpecializedMSplitMinimal o_isplm = { random_value(gen) }; SpecializedMSplitVersionedMinimal o_isplvm = { random_value(gen) }; SpecializedNMSerialize o_eser = { random_value(gen) }; SpecializedNMSerializeVersioned o_eserv = { random_value(gen) }; SpecializedNMSplit o_espl = { random_value(gen) }; SpecializedNMSplitVersioned o_esplv = { random_value(gen) }; SpecializedNMSplitMinimal o_esplm = { random_value(gen) }; SpecializedNMSplitVersionedMinimal o_esplvm = { random_value(gen) }; std::ostringstream os; { OArchive oar(os); oar( o_iser, o_iserv, o_ispl, o_isplv, o_shared_ispl, o_isplm, o_isplvm, o_eser, o_eserv, o_espl, o_esplv, o_esplm, o_esplvm ); } decltype(o_iser) i_iser; decltype(o_iserv) i_iserv; decltype(o_ispl) i_ispl; decltype(o_isplv) i_isplv; decltype(o_shared_ispl) i_shared_ispl; decltype(o_isplm) i_isplm; decltype(o_isplvm) i_isplvm; decltype(o_eser) i_eser; decltype(o_eserv) i_eserv; decltype(o_espl) i_espl; decltype(o_esplv) i_esplv; decltype(o_esplm) i_esplm; decltype(o_esplvm) i_esplvm; std::istringstream is(os.str()); { IArchive iar(is); iar( i_iser, i_iserv, i_ispl, i_isplv, i_shared_ispl, i_isplm, i_isplvm, i_eser, i_eserv, i_espl, i_esplv, i_esplm, i_esplvm ); } CHECK_EQ(i_iser.x, o_iser.x); CHECK_EQ(i_iserv.x, o_iserv.x); CHECK_EQ(i_ispl.x, o_ispl.x); CHECK_EQ(i_isplv.x, o_isplv.x); CHECK_EQ(dynamic_cast(i_shared_ispl.get())->x, dynamic_cast(o_shared_ispl.get())->x); CHECK_EQ(i_isplm.x, o_isplm.x); CHECK_EQ(i_isplvm.x, o_isplvm.x); CHECK_EQ(i_eser.x, o_eser.x); CHECK_EQ(i_eserv.x, o_eserv.x); CHECK_EQ(i_espl.x, o_espl.x); CHECK_EQ(i_esplv.x, o_esplv.x); CHECK_EQ(i_esplm.x, o_esplm.x); CHECK_EQ(i_esplvm.x, o_esplvm.x); } } #endif // CEREAL_TEST_STRUCTS_SPECIALIZED_H_