1 | // (C) Copyright 2009 Robert Ramey |
2 | // Use, modification and distribution are subject to the |
3 | // Boost Software License, Version 1.0. (See accompanying file |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | // See http://www.boost.org for most recent version including documentation. |
7 | |
8 | // note: this is a compile only test. |
9 | #include <sstream> |
10 | #include <boost/config.hpp> // BOOST_STATIC_CONST |
11 | |
12 | #include <boost/serialization/static_warning.hpp> |
13 | #include <boost/serialization/tracking.hpp> |
14 | #include <boost/serialization/level.hpp> |
15 | #include <boost/serialization/version.hpp> |
16 | #include <boost/serialization/nvp.hpp> |
17 | |
18 | #include <boost/archive/text_iarchive.hpp> |
19 | #include <boost/archive/text_oarchive.hpp> |
20 | |
21 | // track_selectivly with class information in the archive |
22 | // is unsafe when used with a pointer and should trigger a warning |
23 | struct check1 { |
24 | template<class Archive> |
25 | void serialize(Archive & ar, const unsigned int version); |
26 | check1(){} |
27 | }; |
28 | |
29 | BOOST_CLASS_IMPLEMENTATION(check1, boost::serialization::object_serializable) |
30 | BOOST_CLASS_TRACKING(check1, boost::serialization::track_selectively) |
31 | |
32 | // the combination of versioning + no class information |
33 | // should trigger a warning |
34 | struct check2 { |
35 | template<class Archive> |
36 | void serialize(Archive & ar, const unsigned int version); |
37 | check2(){} |
38 | }; |
39 | |
40 | BOOST_CLASS_IMPLEMENTATION(check2, boost::serialization::object_serializable) |
41 | BOOST_CLASS_VERSION(check2, 1) |
42 | // use track always to turn off warning tested above |
43 | BOOST_CLASS_TRACKING(check2, boost::serialization::track_always) |
44 | |
45 | // serializing a type marked as "track_never" through a pointer |
46 | // is likely an error |
47 | struct check3 { |
48 | template<class Archive> |
49 | void serialize(Archive & ar, const unsigned int version); |
50 | check3(){} |
51 | }; |
52 | |
53 | BOOST_CLASS_TRACKING(check3, boost::serialization::track_never) |
54 | |
55 | template<class T> |
56 | int f(){ |
57 | BOOST_STATIC_WARNING(T::value); |
58 | BOOST_STATIC_ASSERT(T::value); |
59 | return 0; |
60 | } |
61 | |
62 | ///////////////////////////////////////////////////////////////////////// |
63 | // compilation of this program should show a total of 10 warning messages |
64 | int main(int /* argc */, char * /* argv */[]){ |
65 | std::stringstream s; |
66 | { |
67 | boost::archive::text_oarchive oa(s); |
68 | |
69 | check1 const c1_out; |
70 | oa << c1_out; |
71 | |
72 | check1 c1_non_const_out; |
73 | oa << c1_non_const_out; // warn check_object_tracking |
74 | |
75 | check1 * const c1_ptr_out = 0; |
76 | oa << c1_ptr_out; // warn check_pointer_level |
77 | |
78 | check2 const * c2_ptr_out; |
79 | oa << c2_ptr_out; // error check_object_versioning |
80 | |
81 | check3 * const c3_ptr_out = 0; |
82 | oa << c3_ptr_out; // warning check_pointer_tracking |
83 | |
84 | check2 const c2_out; |
85 | oa << c2_out; // error check_object_versioning |
86 | } |
87 | { |
88 | boost::archive::text_iarchive ia(s); |
89 | |
90 | check1 const c1_in; |
91 | ia >> c1_in; // check_const_loading |
92 | |
93 | check1 * c1_ptr_in = 0; |
94 | ia >> c1_ptr_in; // warn check_pointer_level |
95 | |
96 | check2 * c2_ptr_in; |
97 | ia >> c2_ptr_in; // error check_object_versioning |
98 | |
99 | check3 * c3_ptr_in = 0; |
100 | ia >> c3_ptr_in; // warning check_pointer_tracking |
101 | |
102 | check2 c2_in; |
103 | ia >> c2_in; // error check_object_versioning |
104 | } |
105 | { |
106 | boost::archive::text_oarchive oa(s); |
107 | |
108 | check1 const c1_out; |
109 | oa << BOOST_SERIALIZATION_NVP(c1_out); |
110 | |
111 | check1 c1_non_const_out; |
112 | oa << BOOST_SERIALIZATION_NVP(c1_non_const_out); // warn check_object_tracking |
113 | |
114 | check1 * const c1_ptr_out = 0; |
115 | oa << BOOST_SERIALIZATION_NVP(c1_ptr_out); // warn check_pointer_level |
116 | |
117 | check2 const * c2_ptr_out; |
118 | oa << BOOST_SERIALIZATION_NVP(c2_ptr_out); // error check_object_versioning |
119 | |
120 | check3 * const c3_ptr_out = 0; |
121 | oa << BOOST_SERIALIZATION_NVP(c3_ptr_out); // warning check_pointer_tracking |
122 | |
123 | check2 const c2_out; |
124 | oa << BOOST_SERIALIZATION_NVP(c2_out); // error check_object_versioning |
125 | } |
126 | { |
127 | boost::archive::text_iarchive ia(s); |
128 | |
129 | check1 const c1_in; |
130 | ia >> BOOST_SERIALIZATION_NVP(c1_in); // check_const_loading |
131 | |
132 | check1 * c1_ptr_in = 0; |
133 | ia >> BOOST_SERIALIZATION_NVP(c1_ptr_in); // warn check_pointer_level |
134 | |
135 | check2 * c2_ptr_in; |
136 | ia >> BOOST_SERIALIZATION_NVP(c2_ptr_in); // error check_object_versioning |
137 | |
138 | check3 * c3_ptr_in = 0; |
139 | ia >> BOOST_SERIALIZATION_NVP(c3_ptr_in); // warning check_pointer_tracking |
140 | |
141 | check2 c2_in; |
142 | ia >> BOOST_SERIALIZATION_NVP(c2_in); // error check_object_versioning |
143 | } |
144 | return 0; |
145 | } |
146 | |