1 /*
2   Copyright (c) 2014, Randolph Voorhies, Shane Grant
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7       * Redistributions of source code must retain the above copyright
8         notice, this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright
10         notice, this list of conditions and the following disclaimer in the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of cereal nor the
13         names of its contributors may be used to endorse or promote products
14         derived from this software without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
20   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef CEREAL_TEST_USER_DATA_ADAPTERS_H_
28 #define CEREAL_TEST_USER_DATA_ADAPTERS_H_
29 
30 #include "common.hpp"
31 #define CEREAL_FUTURE_EXPERIMENTAL
32 #include <cereal/archives/adapters.hpp>
33 
34 struct SomeStruct {};
35 
36 struct UserData
37 {
UserDataUserData38   UserData( SomeStruct * pp, SomeStruct & r ) :
39     p(pp), ref(r) {}
40 
41   SomeStruct * p;
42   std::reference_wrapper<SomeStruct> ref;
43 };
44 
45 struct UserStruct
46 {
UserStructUserStruct47   UserStruct( std::int32_t i,
48               SomeStruct * pointer,
49               SomeStruct & reference ) :
50     i32( i ),
51     p( pointer ),
52     ref( reference )
53   { }
54 
55   UserStruct & operator=( UserStruct const & ) = delete;
56 
57   std::int32_t i32;
58   SomeStruct const * p;
59   SomeStruct & ref;
60 
61   template <class Archive>
serializeUserStruct62   void serialize( Archive & ar )
63   {
64     ar( i32 );
65   }
66 
67   template <class Archive>
load_and_constructUserStruct68   static void load_and_construct( Archive & ar, cereal::construct<UserStruct> & construct )
69   {
70     std::int32_t ii;
71     ar( ii );
72     auto & data = cereal::get_user_data<UserData>( ar );
73     construct( ii, data.p, data.ref.get() );
74   }
75 };
76 
77 template <class IArchive, class OArchive> inline
test_user_data_adapters()78 void test_user_data_adapters()
79 {
80   std::random_device rd;
81   std::mt19937 gen(rd());
82 
83   auto rng = [&](){ return random_value<int>(gen); };
84 
85   for(int ii=0; ii<100; ++ii)
86   {
87     SomeStruct ss;
88     std::unique_ptr<UserStruct> o_ptr( new UserStruct( rng(), &ss, ss ) );
89 
90     std::ostringstream os;
91     {
92       OArchive oar(os);
93 
94       oar(o_ptr);
95     }
96 
97     decltype( o_ptr  ) i_ptr;
98 
99     std::istringstream is(os.str());
100     {
101       UserData ud(&ss, ss);
102       cereal::UserDataAdapter<UserData, IArchive> iar(ud, is);
103 
104       iar(i_ptr);
105     }
106 
107     CHECK_EQ( i_ptr->p, o_ptr->p );
108     CHECK_EQ( std::addressof(i_ptr->ref), std::addressof(o_ptr->ref) );
109     CHECK_EQ( i_ptr->i32, o_ptr->i32 );
110 
111     std::istringstream bad_is(os.str());
112     {
113       IArchive iar(bad_is);
114 
115       CHECK_THROWS_AS( iar(i_ptr), ::cereal::Exception );
116     }
117   }
118 }
119 
120 #endif // CEREAL_TEST_USER_DATA_ADAPTERS_H_
121