]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant/test/test7.cpp
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/test/test7.cpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
7 // Eric Friedman, Itay Maman
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)
13 #include "boost/config.hpp"
16 #pragma warning(disable:4244) // conversion from 'const int' to 'const short'
19 #include "boost/test/minimal.hpp"
20 #include "boost/variant.hpp"
29 #include "boost/detail/workaround.hpp"
30 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
31 # include "boost/mpl/bool.hpp"
32 # include "boost/type_traits/is_same.hpp"
36 using namespace boost
;
43 jas(const jas
& other
);
46 jas
& operator=(const jas
& other
);
48 void swap(jas
& other
);
53 static int s_inst_id_
;
58 typedef map
<const jas
*,int> table_type
;
59 typedef table_type::iterator iterator_type
;
61 static table_type s_this_to_sn_
;
63 static void insert(const jas
& j
)
65 s_this_to_sn_
[&j
] = j
.sn_
;
66 cout
<< "jas( " << j
.sn_
<< ") Registered" << endl
;
69 static void remove(const jas
& j
)
71 iterator_type iter
= s_this_to_sn_
.find(&j
);
72 BOOST_CHECK(iter
!= s_this_to_sn_
.end());
73 BOOST_CHECK( ((*iter
).second
) == j
.sn_
);
75 int sn
= (*iter
).second
;
78 cout
<< "Mismatch: this = " << (*iter
).first
<< ", sn_ = " << sn
79 << ", other: this = " << &j
<< ", j.sn_ = " << j
.sn_
<< endl
;
82 BOOST_CHECK(sn
== j
.sn_
);
88 s_this_to_sn_
.erase(&j
);
89 cout
<< "jas( " << j
.sn_
<< ") Removed" << endl
;
94 BOOST_CHECK(s_this_to_sn_
.empty());
98 Tracker::table_type
Tracker::s_this_to_sn_
;
102 jas::jas(int n
) : n_(n
)
107 Tracker::insert(*this);
110 jas::jas(const jas
& other
) : n_(other
.n_
)
115 Tracker::insert(*this);
120 Tracker::remove(*this);
123 jas
& jas::operator=(const jas
& other
)
131 void jas::swap(jas
& other
)
133 Tracker::remove(*this);
134 Tracker::remove(other
);
136 std::swap(n_
, other
.n_
);
137 std::swap(sn_
, other
.sn_
);
139 Tracker::insert(*this);
140 Tracker::insert(other
);
143 int jas::s_inst_id_
= 0;
146 bool operator==(const jas
& a
, const jas
& b
)
151 ostream
& operator<<(ostream
& out
, const jas
& a
)
153 cout
<< "jas::n_ = " << a
.n_
;
158 template<typename ValueType
>
159 struct compare_helper
: boost::static_visitor
<bool>
161 compare_helper(ValueType
& expected
) : expected_(expected
) { }
163 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
165 bool operator()(const ValueType
& value
)
167 return value
== expected_
;
170 template <typename T
>
171 bool operator()(const T
& )
180 bool compare_impl(const ValueType
& value
, boost::mpl::true_
)
182 return value
== expected_
;
185 template <typename T
>
186 bool compare_impl(const T
&, boost::mpl::false_
)
193 template <typename T
>
194 bool operator()(const T
& value
)
196 typedef typename
boost::is_same
<T
, ValueType
>::type
199 return compare_impl(value
, T_is_ValueType());
202 #endif // MSVC6 workaround
204 ValueType
& expected_
;
207 compare_helper
& operator=(const compare_helper
&);
211 template<typename VariantType
, typename ExpectedType
>
212 void var_compare(const VariantType
& v
, ExpectedType expected
)
214 compare_helper
<ExpectedType
> ch(expected
);
216 bool checks
= boost::apply_visitor(ch
, v
);
223 variant
<string
, short> v0
;
225 var_compare(v0
, string(""));
228 var_compare(v0
, static_cast<short>(8));
231 var_compare(v0
, string("penny lane"));
233 variant
<jas
, string
, int> v1
, v2
= jas(195);
234 var_compare(v1
, jas(364));
239 var_compare(v1
, jas(195));
240 var_compare(v2
, jas(500));
243 variant
<string
, int> v3
;
244 var_compare(v3
, string(""));
248 int test_main(int , char* [])