Update contrib.
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/detail/workaround.hpp"
29 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
33 struct to_text : boost::static_visitor<std::string>
35 private: // NO_FUNCTION_TEMPLATE_ORDERING workaround
37 template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
38 std::string to_text_impl(
39 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) >& operand, long
42 std::ostringstream ost;
43 ost << "[V] " << boost::apply_visitor(to_text(), operand);
48 template <typename Value>
49 std::string to_text_impl(const Value& operand, int) const
51 std::ostringstream ost;
52 ost << "[V] " << operand;
60 std::string operator()(const T& operand) const
62 return to_text_impl(operand, 1L);
67 struct total_sizeof : boost::static_visitor<int>
69 total_sizeof() : total_(0) { }
72 int operator()(const Value&) const
74 total_ += sizeof(Value);
89 //Function object: sum_int
90 //Description: Compute total sum of a series of numbers, (when called successively)
91 //Use sizeof(T) if applied with a non-integral type
92 struct sum_int : boost::static_visitor<int>
95 sum_int() : total_(0) { }
101 BOOST_STATIC_CONSTANT(int, value = n);
104 //Integral type - add numerical value
106 void add(T t, int_to_type<true> ) const
111 //Other types - add sizeof<T>
113 void add(T& , int_to_type<false> ) const
119 int operator()(const T& t) const
121 //Int_to_type is used to select the correct add() overload
122 add(t, int_to_type<boost::is_integral<T>::value>());
141 //Function object: sum_double
142 //Description: Compute total sum of a series of numbers, (when called successively)
143 //Accpetable input types: float, double (Other types are silently ignored)
144 struct sum_double : boost::static_visitor<double>
147 sum_double() : total_(0) { }
149 void operator()(float value) const
154 void operator()(double value) const
160 void operator()(const T&) const
165 double result() const
171 mutable double total_;
177 struct int_printer : boost::static_visitor<std::string>
180 int_printer(std::string prefix_s = "") : prefix_s_(prefix_s) { }
181 int_printer(const int_printer& other) : prefix_s_(other.prefix_s_)
186 std::string operator()(int x) const
188 ost_ << prefix_s_ << x;
192 std::string operator()(const std::vector<int>& x) const
196 //Use another Int_printer object for printing a list of all integers
197 int_printer job(",");
198 ost_ << std::for_each(x.begin(), x.end(), job).str();
203 std::string str() const
209 std::string prefix_s_;
210 mutable std::ostringstream ost_;
214 struct int_adder : boost::static_visitor<>
217 int_adder(int rhs) : rhs_(rhs) { }
219 result_type operator()(int& lhs) const
225 result_type operator()(const T& ) const
236 struct held_type_name : boost::static_visitor<std::string>
240 std::string operator()(const T& ) const
242 ost_ << '[' << typeid(T).name() << ']';
246 std::string result() const
251 mutable std::ostringstream ost_;
264 template<typename VariantType, typename S>
265 inline void verify(VariantType& var, spec<S>, std::string str = "")
267 const VariantType& cvar = var;
269 BOOST_CHECK(boost::apply_visitor(total_sizeof(), cvar) == sizeof(S));
270 BOOST_CHECK(cvar.type() == typeid(S));
275 BOOST_CHECK(boost::get<S>(&var));
276 BOOST_CHECK(boost::get<S>(&cvar));
282 S& r = boost::get<S>(var);
285 catch(boost::bad_get& )
287 BOOST_ERROR( "get<S> failed unexpectedly" );
292 const S& cr = boost::get<S>(cvar);
295 catch(boost::bad_get& )
297 BOOST_ERROR( "get<S> const failed unexpectedly" );
300 BOOST_CHECK(ptr1 != 0 && ptr2 == ptr1);
303 // Check string content
307 std::string temp = boost::apply_visitor(to_text(), cvar);
308 std::cout << "temp = " << temp << ", str = " << str << std::endl;
309 BOOST_CHECK(temp == str);
314 template<typename VariantType, typename S>
315 inline void verify_not(VariantType& var, spec<S>)
317 const VariantType& cvar = var;
319 BOOST_CHECK(cvar.type() != typeid(S));
324 BOOST_CHECK(!boost::get<S>(&var));
325 BOOST_CHECK(!boost::get<S>(&cvar));
331 S& r = boost::get<S>(var); // should throw
332 BOOST_ERROR( "get<S> passed unexpectedly" );
336 catch(boost::bad_get& )
338 // do nothing except pass-through
343 const S& cr = boost::get<S>(var); // should throw
344 BOOST_ERROR( "get<S> const passed unexpectedly" );
348 catch(boost::bad_get& )
350 // do nothing except pass-through
353 BOOST_CHECK(ptr1 == 0 && ptr2 == 0);