1 | //----------------------------------------------------------------------------- |
---|
2 | // boost-libs variant/test/test7.cpp header file |
---|
3 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
4 | //----------------------------------------------------------------------------- |
---|
5 | // |
---|
6 | // Copyright (c) 2003 |
---|
7 | // Eric Friedman, Itay Maman |
---|
8 | // |
---|
9 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
10 | // accompanying file LICENSE_1_0.txt or copy at |
---|
11 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
12 | |
---|
13 | #include "boost/test/minimal.hpp" |
---|
14 | #include "boost/variant.hpp" |
---|
15 | |
---|
16 | #include "jobs.h" |
---|
17 | |
---|
18 | #include <iostream> |
---|
19 | #include <algorithm> |
---|
20 | #include <string> |
---|
21 | #include <map> |
---|
22 | |
---|
23 | #include "boost/detail/workaround.hpp" |
---|
24 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) |
---|
25 | # include "boost/mpl/bool.hpp" |
---|
26 | # include "boost/type_traits/is_same.hpp" |
---|
27 | #endif |
---|
28 | |
---|
29 | |
---|
30 | using namespace boost; |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | struct jas |
---|
35 | { |
---|
36 | jas(int n = 364); |
---|
37 | jas(const jas& other); |
---|
38 | |
---|
39 | ~jas(); |
---|
40 | jas& operator=(const jas& other); |
---|
41 | |
---|
42 | void swap(jas& other); |
---|
43 | |
---|
44 | int n_; |
---|
45 | |
---|
46 | int sn_; |
---|
47 | static int s_inst_id_; |
---|
48 | }; |
---|
49 | |
---|
50 | struct Tracker |
---|
51 | { |
---|
52 | typedef map<const jas*,int> table_type; |
---|
53 | typedef table_type::iterator iterator_type; |
---|
54 | |
---|
55 | static table_type s_this_to_sn_; |
---|
56 | |
---|
57 | static void insert(const jas& j) |
---|
58 | { |
---|
59 | s_this_to_sn_[&j] = j.sn_; |
---|
60 | cout << "jas( " << j.sn_ << ") Registered" << endl; |
---|
61 | } |
---|
62 | |
---|
63 | static void remove(const jas& j) |
---|
64 | { |
---|
65 | iterator_type iter = s_this_to_sn_.find(&j); |
---|
66 | BOOST_CHECK(iter != s_this_to_sn_.end()); |
---|
67 | BOOST_CHECK( ((*iter).second) == j.sn_); |
---|
68 | |
---|
69 | int sn = (*iter).second; |
---|
70 | if(sn != j.sn_) |
---|
71 | { |
---|
72 | cout << "Mismatch: this = " << (*iter).first << ", sn_ = " << sn |
---|
73 | << ", other: this = " << &j << ", j.sn_ = " << j.sn_ << endl; |
---|
74 | } |
---|
75 | |
---|
76 | BOOST_CHECK(sn == j.sn_); |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | s_this_to_sn_.erase(&j); |
---|
83 | cout << "jas( " << j.sn_ << ") Removed" << endl; |
---|
84 | } |
---|
85 | |
---|
86 | static void check() |
---|
87 | { |
---|
88 | BOOST_CHECK(s_this_to_sn_.empty()); |
---|
89 | } |
---|
90 | }; |
---|
91 | |
---|
92 | Tracker::table_type Tracker::s_this_to_sn_; |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | jas::jas(int n) : n_(n) |
---|
97 | { |
---|
98 | sn_ = s_inst_id_; |
---|
99 | s_inst_id_ += 1; |
---|
100 | |
---|
101 | Tracker::insert(*this); |
---|
102 | } |
---|
103 | |
---|
104 | jas::jas(const jas& other) : n_(other.n_) |
---|
105 | { |
---|
106 | sn_ = s_inst_id_; |
---|
107 | s_inst_id_ += 1; |
---|
108 | |
---|
109 | Tracker::insert(*this); |
---|
110 | } |
---|
111 | |
---|
112 | jas::~jas() |
---|
113 | { |
---|
114 | Tracker::remove(*this); |
---|
115 | } |
---|
116 | |
---|
117 | jas& jas::operator=(const jas& other) |
---|
118 | { |
---|
119 | jas temp(other); |
---|
120 | swap(temp); |
---|
121 | |
---|
122 | return *this; |
---|
123 | } |
---|
124 | |
---|
125 | void jas::swap(jas& other) |
---|
126 | { |
---|
127 | Tracker::remove(*this); |
---|
128 | Tracker::remove(other); |
---|
129 | |
---|
130 | std::swap(n_, other.n_); |
---|
131 | std::swap(sn_, other.sn_); |
---|
132 | |
---|
133 | Tracker::insert(*this); |
---|
134 | Tracker::insert(other); |
---|
135 | } |
---|
136 | |
---|
137 | int jas::s_inst_id_ = 0; |
---|
138 | |
---|
139 | |
---|
140 | bool operator==(const jas& a, const jas& b) |
---|
141 | { |
---|
142 | return a.n_ == b.n_; |
---|
143 | } |
---|
144 | |
---|
145 | ostream& operator<<(ostream& out, const jas& a) |
---|
146 | { |
---|
147 | cout << "jas::n_ = " << a.n_; |
---|
148 | return out; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | template<typename ValueType> |
---|
153 | struct compare_helper : boost::static_visitor<bool> |
---|
154 | { |
---|
155 | compare_helper(ValueType& expected) : expected_(expected) { } |
---|
156 | |
---|
157 | #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200) |
---|
158 | |
---|
159 | bool operator()(const ValueType& value) |
---|
160 | { |
---|
161 | return value == expected_; |
---|
162 | } |
---|
163 | |
---|
164 | template <typename T> |
---|
165 | bool operator()(const T& ) |
---|
166 | { |
---|
167 | return false; |
---|
168 | } |
---|
169 | |
---|
170 | #else // MSVC6 |
---|
171 | |
---|
172 | private: |
---|
173 | |
---|
174 | bool compare_impl(const ValueType& value, boost::mpl::true_) |
---|
175 | { |
---|
176 | return value == expected_; |
---|
177 | } |
---|
178 | |
---|
179 | template <typename T> |
---|
180 | bool compare_impl(const T&, boost::mpl::false_) |
---|
181 | { |
---|
182 | return false; |
---|
183 | } |
---|
184 | |
---|
185 | public: |
---|
186 | |
---|
187 | template <typename T> |
---|
188 | bool operator()(const T& value) |
---|
189 | { |
---|
190 | typedef typename boost::is_same<T, ValueType>::type |
---|
191 | T_is_ValueType; |
---|
192 | |
---|
193 | return compare_impl(value, T_is_ValueType()); |
---|
194 | } |
---|
195 | |
---|
196 | #endif // MSVC6 workaround |
---|
197 | |
---|
198 | ValueType& expected_; |
---|
199 | |
---|
200 | }; |
---|
201 | |
---|
202 | template<typename VariantType, typename ExpectedType> |
---|
203 | void var_compare(const VariantType& v, ExpectedType expected) |
---|
204 | { |
---|
205 | compare_helper<ExpectedType> ch(expected); |
---|
206 | |
---|
207 | bool checks = boost::apply_visitor(ch, v); |
---|
208 | BOOST_CHECK(checks); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | void run() |
---|
213 | { |
---|
214 | variant<string, short> v0; |
---|
215 | |
---|
216 | var_compare(v0, string("")); |
---|
217 | |
---|
218 | v0 = 8; |
---|
219 | var_compare(v0, static_cast<short>(8)); |
---|
220 | |
---|
221 | v0 = "penny lane"; |
---|
222 | var_compare(v0, string("penny lane")); |
---|
223 | |
---|
224 | variant<jas, string, int> v1, v2 = jas(195); |
---|
225 | var_compare(v1, jas(364)); |
---|
226 | |
---|
227 | v1 = jas(500); |
---|
228 | v1.swap(v2); |
---|
229 | |
---|
230 | var_compare(v1, jas(195)); |
---|
231 | var_compare(v2, jas(500)); |
---|
232 | |
---|
233 | |
---|
234 | variant<string, int> v3; |
---|
235 | var_compare(v3, string("")); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | int test_main(int , char* []) |
---|
240 | { |
---|
241 | run(); |
---|
242 | Tracker::check(); |
---|
243 | |
---|
244 | return 0; |
---|
245 | } |
---|
246 | |
---|