]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant/test/jobs.h
1 //-----------------------------------------------------------------------------
2 // boost-libs variant/libs/test/jobs.h 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)
23 #include "boost/variant/variant_fwd.hpp"
24 #include "boost/variant/get.hpp"
25 #include "boost/variant/apply_visitor.hpp"
26 #include "boost/variant/static_visitor.hpp"
28 #include "boost/type_index.hpp"
29 #include "boost/detail/workaround.hpp"
30 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
34 struct to_text
: boost::static_visitor
<std::string
>
36 private: // NO_FUNCTION_TEMPLATE_ORDERING workaround
38 template < BOOST_VARIANT_ENUM_PARAMS(typename U
) >
39 std::string
to_text_impl(
40 const boost::variant
< BOOST_VARIANT_ENUM_PARAMS(U
) >& operand
, long
43 std::ostringstream ost
;
44 ost
<< "[V] " << boost::apply_visitor(to_text(), operand
);
49 template <typename Value
>
50 std::string
to_text_impl(const Value
& operand
, int) const
52 std::ostringstream ost
;
53 ost
<< "[V] " << operand
;
61 std::string
operator()(const T
& operand
) const
63 return to_text_impl(operand
, 1L);
68 struct total_sizeof
: boost::static_visitor
<int>
70 total_sizeof() : total_(0) { }
73 int operator()(const Value
&) const
75 total_
+= sizeof(Value
);
90 //Function object: sum_int
91 //Description: Compute total sum of a series of numbers, (when called successively)
92 //Use sizeof(T) if applied with a non-integral type
93 struct sum_int
: boost::static_visitor
<int>
96 sum_int() : total_(0) { }
102 BOOST_STATIC_CONSTANT(int, value
= n
);
105 //Integral type - add numerical value
107 void add(T t
, int_to_type
<true> ) const
112 //Other types - add sizeof<T>
114 void add(T
& , int_to_type
<false> ) const
120 int operator()(const T
& t
) const
122 //Int_to_type is used to select the correct add() overload
123 add(t
, int_to_type
<boost::is_integral
<T
>::value
>());
142 //Function object: sum_double
143 //Description: Compute total sum of a series of numbers, (when called successively)
144 //Accpetable input types: float, double (Other types are silently ignored)
145 struct sum_double
: boost::static_visitor
<double>
148 sum_double() : total_(0) { }
150 void operator()(float value
) const
155 void operator()(double value
) const
161 void operator()(const T
&) const
166 double result() const
172 mutable double total_
;
178 struct int_printer
: boost::static_visitor
<std::string
>
181 int_printer(std::string prefix_s
= "") : prefix_s_(prefix_s
) { }
182 int_printer(const int_printer
& other
) : prefix_s_(other
.prefix_s_
)
187 std::string
operator()(int x
) const
189 ost_
<< prefix_s_
<< x
;
193 std::string
operator()(const std::vector
<int>& x
) const
197 //Use another Int_printer object for printing a list of all integers
198 int_printer
job(",");
199 ost_
<< std::for_each(x
.begin(), x
.end(), job
).str();
204 std::string
str() const
210 std::string prefix_s_
;
211 mutable std::ostringstream ost_
;
215 struct int_adder
: boost::static_visitor
<>
218 int_adder(int rhs
) : rhs_(rhs
) { }
220 result_type
operator()(int& lhs
) const
226 result_type
operator()(const T
& ) const
242 template<typename VariantType
, typename S
>
243 inline void verify(VariantType
& var
, spec
<S
>, std::string str
= "")
245 const VariantType
& cvar
= var
;
247 BOOST_CHECK(boost::apply_visitor(total_sizeof(), cvar
) == sizeof(S
));
248 BOOST_CHECK(cvar
.type() == boost::typeindex::type_id
<S
>());
253 BOOST_CHECK(boost::get
<S
>(&var
));
254 BOOST_CHECK(boost::get
<S
>(&cvar
));
260 S
& r
= boost::get
<S
>(var
);
263 catch(const boost::bad_get
& )
265 BOOST_ERROR( "get<S> failed unexpectedly" );
270 const S
& cr
= boost::get
<S
>(cvar
);
273 catch(const boost::bad_get
& )
275 BOOST_ERROR( "get<S> const failed unexpectedly" );
278 BOOST_CHECK(ptr1
!= 0 && ptr2
== ptr1
);
281 // Check string content
285 std::string temp
= boost::apply_visitor(to_text(), cvar
);
286 std::cout
<< "temp = " << temp
<< ", str = " << str
<< std::endl
;
287 BOOST_CHECK(temp
== str
);
292 template<typename VariantType
, typename S
>
293 inline void verify_not(VariantType
& var
, spec
<S
>)
295 const VariantType
& cvar
= var
;
297 BOOST_CHECK(cvar
.type() != boost::typeindex::type_id
<S
>());
302 BOOST_CHECK(!boost::get
<S
>(&var
));
303 BOOST_CHECK(!boost::get
<S
>(&cvar
));
309 S
& r
= boost::get
<S
>(var
); // should throw
310 BOOST_ERROR( "get<S> passed unexpectedly" );
314 catch(const boost::bad_get
& )
316 // do nothing except pass-through
321 const S
& cr
= boost::get
<S
>(var
); // should throw
322 BOOST_ERROR( "get<S> const passed unexpectedly" );
326 catch(const boost::bad_get
& )
328 // do nothing except pass-through
331 BOOST_CHECK(ptr1
== 0 && ptr2
== 0);