os/ossrv/stdcpp/tsrc/Boost_test/variant/inc/jobs.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/tsrc/Boost_test/variant/inc/jobs.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,357 @@
     1.4 +//-----------------------------------------------------------------------------
     1.5 +// boost-libs variant/libs/test/jobs.h header file
     1.6 +// See http://www.boost.org for updates, documentation, and revision history.
     1.7 +//-----------------------------------------------------------------------------
     1.8 +//
     1.9 +// Copyright (c) 2003
    1.10 +// Eric Friedman, Itay Maman
    1.11 +//
    1.12 +// Distributed under the Boost Software License, Version 1.0. (See
    1.13 +// accompanying file LICENSE_1_0.txt or copy at
    1.14 +// http://www.boost.org/LICENSE_1_0.txt)
    1.15 +
    1.16 +#ifndef _JOBSH_INC_
    1.17 +#define _JOBSH_INC_
    1.18 +
    1.19 +#include <algorithm>
    1.20 +#include <iostream>
    1.21 +#include <sstream>
    1.22 +#include <string>
    1.23 +#include <typeinfo>
    1.24 +#include <vector>
    1.25 +
    1.26 +#include "boost/variant/variant_fwd.hpp"
    1.27 +#include "boost/variant/get.hpp"
    1.28 +#include "boost/variant/apply_visitor.hpp"
    1.29 +#include "boost/variant/static_visitor.hpp"
    1.30 +
    1.31 +#include "boost/detail/workaround.hpp"
    1.32 +#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
    1.33 +#    pragma warn -lvc
    1.34 +#endif
    1.35 +
    1.36 +struct to_text : boost::static_visitor<std::string>
    1.37 +{
    1.38 +private: // NO_FUNCTION_TEMPLATE_ORDERING workaround
    1.39 +
    1.40 +    template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
    1.41 +    std::string to_text_impl(
    1.42 +          const boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) >& operand, long
    1.43 +        ) const
    1.44 +    {
    1.45 +        std::ostringstream ost;
    1.46 +        ost << "[V] " << boost::apply_visitor(to_text(), operand);
    1.47 +
    1.48 +        return ost.str();
    1.49 +    }
    1.50 +
    1.51 +    template <typename Value>
    1.52 +    std::string to_text_impl(const Value& operand, int) const
    1.53 +    {
    1.54 +        std::ostringstream ost;
    1.55 +        ost << "[V] " << operand;
    1.56 +
    1.57 +        return ost.str();
    1.58 +    }
    1.59 +
    1.60 +public:
    1.61 +
    1.62 +    template <typename T>
    1.63 +    std::string operator()(const T& operand) const
    1.64 +    {
    1.65 +        return to_text_impl(operand, 1L);
    1.66 +    }
    1.67 +
    1.68 +};
    1.69 +
    1.70 +struct total_sizeof : boost::static_visitor<int>
    1.71 +{
    1.72 +   total_sizeof() : total_(0) { }
    1.73 +
    1.74 +   template<class Value>
    1.75 +   int operator()(const Value&) const
    1.76 +   {
    1.77 +      total_ += sizeof(Value);
    1.78 +      return total_;
    1.79 +   }
    1.80 +
    1.81 +   int result() const
    1.82 +   {
    1.83 +      return total_;
    1.84 +   }
    1.85 +
    1.86 +   mutable int total_;
    1.87 +
    1.88 +}; // total_sizeof
    1.89 +
    1.90 +
    1.91 +
    1.92 +//Function object: sum_int
    1.93 +//Description: Compute total sum of a series of numbers, (when called successively)
    1.94 +//Use sizeof(T) if applied with a non-integral type
    1.95 +struct sum_int : boost::static_visitor<int>
    1.96 +{
    1.97 +   
    1.98 +   sum_int() : total_(0) { }
    1.99 +
   1.100 +
   1.101 +   template<int n>
   1.102 +   struct int_to_type
   1.103 +   {
   1.104 +      BOOST_STATIC_CONSTANT(int, value = n);
   1.105 +   }; 
   1.106 +
   1.107 +   //Integral type - add numerical value
   1.108 +   template<typename T>
   1.109 +   void add(T t, int_to_type<true> ) const
   1.110 +   {
   1.111 +      total_ += t;
   1.112 +   }
   1.113 +
   1.114 +   //Other types - add sizeof<T>
   1.115 +   template<typename T>
   1.116 +   void add(T& , int_to_type<false> ) const
   1.117 +   {
   1.118 +      total_ += sizeof(T);
   1.119 +   }
   1.120 +
   1.121 +   template<typename T>
   1.122 +   int operator()(const T& t) const
   1.123 +   {
   1.124 +      //Int_to_type is used to select the correct add() overload
   1.125 +      add(t, int_to_type<boost::is_integral<T>::value>());
   1.126 +      return total_;
   1.127 +   }
   1.128 +
   1.129 +   int result() const
   1.130 +   {
   1.131 +      return total_;
   1.132 +   }
   1.133 +
   1.134 +private:
   1.135 +   mutable int total_;
   1.136 +
   1.137 +}; //sum_int
   1.138 +
   1.139 +
   1.140 +
   1.141 +
   1.142 +
   1.143 +
   1.144 +//Function object: sum_double
   1.145 +//Description: Compute total sum of a series of numbers, (when called successively)
   1.146 +//Accpetable input types: float, double (Other types are silently ignored)
   1.147 +struct sum_double : boost::static_visitor<double>
   1.148 +{
   1.149 +   
   1.150 +   sum_double() : total_(0) { }
   1.151 +
   1.152 +   void operator()(float value) const
   1.153 +   {
   1.154 +      total_ += value;
   1.155 +   }
   1.156 +
   1.157 +   void operator()(double value) const
   1.158 +   {
   1.159 +      total_ += value;
   1.160 +   }
   1.161 +
   1.162 +   template<typename T>
   1.163 +   void operator()(const T&) const
   1.164 +   {
   1.165 +      //Do nothing
   1.166 +   }
   1.167 +
   1.168 +   double result() const
   1.169 +   {
   1.170 +      return total_;
   1.171 +   }
   1.172 +
   1.173 +private:
   1.174 +   mutable double total_;
   1.175 +
   1.176 +}; //sum_double
   1.177 +
   1.178 +
   1.179 +
   1.180 +struct int_printer : boost::static_visitor<std::string>
   1.181 +{
   1.182 +   
   1.183 +   int_printer(std::string prefix_s = "") : prefix_s_(prefix_s) { }
   1.184 +   int_printer(const int_printer& other) : prefix_s_(other.prefix_s_)
   1.185 +   {
   1.186 +      ost_ << other.str();
   1.187 +   }
   1.188 +
   1.189 +   std::string operator()(int x) const
   1.190 +   {
   1.191 +      ost_ << prefix_s_ << x;
   1.192 +      return str();
   1.193 +   }
   1.194 +
   1.195 +   std::string operator()(const std::vector<int>& x) const
   1.196 +   {
   1.197 +      ost_ << prefix_s_;
   1.198 +
   1.199 +      //Use another Int_printer object for printing a list of all integers
   1.200 +      int_printer job(",");
   1.201 +      ost_ << std::for_each(x.begin(), x.end(), job).str();
   1.202 +      
   1.203 +      return str();
   1.204 +   }
   1.205 +
   1.206 +   std::string str() const
   1.207 +   {
   1.208 +      return ost_.str();
   1.209 +   }
   1.210 +
   1.211 +private:
   1.212 +   std::string prefix_s_;
   1.213 +   mutable std::ostringstream ost_;
   1.214 +};  //int_printer
   1.215 +
   1.216 +
   1.217 +struct int_adder : boost::static_visitor<>
   1.218 +{
   1.219 +   
   1.220 +   int_adder(int rhs) : rhs_(rhs) { }
   1.221 +
   1.222 +   result_type operator()(int& lhs) const
   1.223 +   {
   1.224 +      lhs += rhs_;
   1.225 +   }
   1.226 +
   1.227 +   template<typename T>
   1.228 +   result_type operator()(const T& ) const
   1.229 +   {
   1.230 +      //Do nothing
   1.231 +   }
   1.232 +
   1.233 +   int rhs_;
   1.234 +}; //int_adder
   1.235 +
   1.236 +
   1.237 +
   1.238 +
   1.239 +struct held_type_name : boost::static_visitor<std::string>
   1.240 +{
   1.241 +   
   1.242 +   template<typename T>
   1.243 +   std::string operator()(const T& ) const
   1.244 +   {
   1.245 +      ost_ << '[' << typeid(T).name() << ']';
   1.246 +      return result();
   1.247 +   }
   1.248 +
   1.249 +   std::string result() const
   1.250 +   {
   1.251 +      return ost_.str();
   1.252 +   }
   1.253 +
   1.254 +   mutable std::ostringstream ost_;
   1.255 +
   1.256 +}; //held_type_name
   1.257 +
   1.258 +
   1.259 +
   1.260 +
   1.261 +template<typename T>
   1.262 +struct spec 
   1.263 +{
   1.264 +   typedef T result;
   1.265 +};
   1.266 +
   1.267 +template<typename VariantType, typename S>
   1.268 +inline void verify(VariantType& var, spec<S>, std::string str = "")
   1.269 +{
   1.270 +   const VariantType& cvar = var;
   1.271 +
   1.272 +   BOOST_CHECK(boost::apply_visitor(total_sizeof(), cvar) == sizeof(S));
   1.273 +   BOOST_CHECK(cvar.type() == typeid(S));
   1.274 +
   1.275 +   //
   1.276 +   // Check get<>()
   1.277 +   //
   1.278 +   BOOST_CHECK(boost::get<S>(&var));
   1.279 +   BOOST_CHECK(boost::get<S>(&cvar));
   1.280 +
   1.281 +   const S* ptr1 = 0;
   1.282 +   const S* ptr2 = 0;
   1.283 +   try
   1.284 +   {
   1.285 +      S& r = boost::get<S>(var);
   1.286 +      ptr1 = &r;
   1.287 +   }
   1.288 +   catch(boost::bad_get& )
   1.289 +   {
   1.290 +      BOOST_ERROR( "get<S> failed unexpectedly" );
   1.291 +   }
   1.292 +
   1.293 +   try
   1.294 +   {
   1.295 +      const S& cr = boost::get<S>(cvar);
   1.296 +      ptr2 = &cr;
   1.297 +   }
   1.298 +   catch(boost::bad_get& )
   1.299 +   {
   1.300 +      BOOST_ERROR( "get<S> const failed unexpectedly" );
   1.301 +   }
   1.302 +
   1.303 +   BOOST_CHECK(ptr1 != 0 && ptr2 == ptr1);
   1.304 +
   1.305 +   //
   1.306 +   // Check string content
   1.307 +   //
   1.308 +   if(str.length() > 0)
   1.309 +   {
   1.310 +      std::string temp = boost::apply_visitor(to_text(), cvar);
   1.311 +      std::cout << "temp = " << temp << ", str = " << str << std::endl;
   1.312 +      BOOST_CHECK(temp == str);         
   1.313 +   }
   1.314 +}
   1.315 +
   1.316 +
   1.317 +template<typename VariantType, typename S>
   1.318 +inline void verify_not(VariantType& var, spec<S>)
   1.319 +{
   1.320 +   const VariantType& cvar = var;
   1.321 +
   1.322 +   BOOST_CHECK(cvar.type() != typeid(S));
   1.323 +
   1.324 +   //
   1.325 +   // Check get<>()
   1.326 +   //
   1.327 +   BOOST_CHECK(!boost::get<S>(&var));
   1.328 +   BOOST_CHECK(!boost::get<S>(&cvar));
   1.329 +
   1.330 +   const S* ptr1 = 0;
   1.331 +   const S* ptr2 = 0;
   1.332 +   try
   1.333 +   {
   1.334 +      S& r = boost::get<S>(var); // should throw
   1.335 +      BOOST_ERROR( "get<S> passed unexpectedly" );
   1.336 +
   1.337 +      ptr1 = &r;
   1.338 +   }
   1.339 +   catch(boost::bad_get& )
   1.340 +   {
   1.341 +      // do nothing except pass-through
   1.342 +   }
   1.343 +
   1.344 +   try
   1.345 +   {
   1.346 +      const S& cr = boost::get<S>(var); // should throw
   1.347 +      BOOST_ERROR( "get<S> const passed unexpectedly" );
   1.348 +
   1.349 +      ptr2 = &cr;
   1.350 +   }
   1.351 +   catch(boost::bad_get& )
   1.352 +   {
   1.353 +      // do nothing except pass-through
   1.354 +   }
   1.355 +
   1.356 +   BOOST_CHECK(ptr1 == 0 && ptr2 == 0);   
   1.357 +}
   1.358 +
   1.359 +
   1.360 +#endif //_JOBSH_INC_