os/ossrv/stdcpp/tsrc/Boost_test/variant/inc/jobs.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
//-----------------------------------------------------------------------------
sl@0
     2
// boost-libs variant/libs/test/jobs.h header file
sl@0
     3
// See http://www.boost.org for updates, documentation, and revision history.
sl@0
     4
//-----------------------------------------------------------------------------
sl@0
     5
//
sl@0
     6
// Copyright (c) 2003
sl@0
     7
// Eric Friedman, Itay Maman
sl@0
     8
//
sl@0
     9
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
    10
// accompanying file LICENSE_1_0.txt or copy at
sl@0
    11
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
    12
sl@0
    13
#ifndef _JOBSH_INC_
sl@0
    14
#define _JOBSH_INC_
sl@0
    15
sl@0
    16
#include <algorithm>
sl@0
    17
#include <iostream>
sl@0
    18
#include <sstream>
sl@0
    19
#include <string>
sl@0
    20
#include <typeinfo>
sl@0
    21
#include <vector>
sl@0
    22
sl@0
    23
#include "boost/variant/variant_fwd.hpp"
sl@0
    24
#include "boost/variant/get.hpp"
sl@0
    25
#include "boost/variant/apply_visitor.hpp"
sl@0
    26
#include "boost/variant/static_visitor.hpp"
sl@0
    27
sl@0
    28
#include "boost/detail/workaround.hpp"
sl@0
    29
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
sl@0
    30
#    pragma warn -lvc
sl@0
    31
#endif
sl@0
    32
sl@0
    33
struct to_text : boost::static_visitor<std::string>
sl@0
    34
{
sl@0
    35
private: // NO_FUNCTION_TEMPLATE_ORDERING workaround
sl@0
    36
sl@0
    37
    template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
sl@0
    38
    std::string to_text_impl(
sl@0
    39
          const boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) >& operand, long
sl@0
    40
        ) const
sl@0
    41
    {
sl@0
    42
        std::ostringstream ost;
sl@0
    43
        ost << "[V] " << boost::apply_visitor(to_text(), operand);
sl@0
    44
sl@0
    45
        return ost.str();
sl@0
    46
    }
sl@0
    47
sl@0
    48
    template <typename Value>
sl@0
    49
    std::string to_text_impl(const Value& operand, int) const
sl@0
    50
    {
sl@0
    51
        std::ostringstream ost;
sl@0
    52
        ost << "[V] " << operand;
sl@0
    53
sl@0
    54
        return ost.str();
sl@0
    55
    }
sl@0
    56
sl@0
    57
public:
sl@0
    58
sl@0
    59
    template <typename T>
sl@0
    60
    std::string operator()(const T& operand) const
sl@0
    61
    {
sl@0
    62
        return to_text_impl(operand, 1L);
sl@0
    63
    }
sl@0
    64
sl@0
    65
};
sl@0
    66
sl@0
    67
struct total_sizeof : boost::static_visitor<int>
sl@0
    68
{
sl@0
    69
   total_sizeof() : total_(0) { }
sl@0
    70
sl@0
    71
   template<class Value>
sl@0
    72
   int operator()(const Value&) const
sl@0
    73
   {
sl@0
    74
      total_ += sizeof(Value);
sl@0
    75
      return total_;
sl@0
    76
   }
sl@0
    77
sl@0
    78
   int result() const
sl@0
    79
   {
sl@0
    80
      return total_;
sl@0
    81
   }
sl@0
    82
sl@0
    83
   mutable int total_;
sl@0
    84
sl@0
    85
}; // total_sizeof
sl@0
    86
sl@0
    87
sl@0
    88
sl@0
    89
//Function object: sum_int
sl@0
    90
//Description: Compute total sum of a series of numbers, (when called successively)
sl@0
    91
//Use sizeof(T) if applied with a non-integral type
sl@0
    92
struct sum_int : boost::static_visitor<int>
sl@0
    93
{
sl@0
    94
   
sl@0
    95
   sum_int() : total_(0) { }
sl@0
    96
sl@0
    97
sl@0
    98
   template<int n>
sl@0
    99
   struct int_to_type
sl@0
   100
   {
sl@0
   101
      BOOST_STATIC_CONSTANT(int, value = n);
sl@0
   102
   }; 
sl@0
   103
sl@0
   104
   //Integral type - add numerical value
sl@0
   105
   template<typename T>
sl@0
   106
   void add(T t, int_to_type<true> ) const
sl@0
   107
   {
sl@0
   108
      total_ += t;
sl@0
   109
   }
sl@0
   110
sl@0
   111
   //Other types - add sizeof<T>
sl@0
   112
   template<typename T>
sl@0
   113
   void add(T& , int_to_type<false> ) const
sl@0
   114
   {
sl@0
   115
      total_ += sizeof(T);
sl@0
   116
   }
sl@0
   117
sl@0
   118
   template<typename T>
sl@0
   119
   int operator()(const T& t) const
sl@0
   120
   {
sl@0
   121
      //Int_to_type is used to select the correct add() overload
sl@0
   122
      add(t, int_to_type<boost::is_integral<T>::value>());
sl@0
   123
      return total_;
sl@0
   124
   }
sl@0
   125
sl@0
   126
   int result() const
sl@0
   127
   {
sl@0
   128
      return total_;
sl@0
   129
   }
sl@0
   130
sl@0
   131
private:
sl@0
   132
   mutable int total_;
sl@0
   133
sl@0
   134
}; //sum_int
sl@0
   135
sl@0
   136
sl@0
   137
sl@0
   138
sl@0
   139
sl@0
   140
sl@0
   141
//Function object: sum_double
sl@0
   142
//Description: Compute total sum of a series of numbers, (when called successively)
sl@0
   143
//Accpetable input types: float, double (Other types are silently ignored)
sl@0
   144
struct sum_double : boost::static_visitor<double>
sl@0
   145
{
sl@0
   146
   
sl@0
   147
   sum_double() : total_(0) { }
sl@0
   148
sl@0
   149
   void operator()(float value) const
sl@0
   150
   {
sl@0
   151
      total_ += value;
sl@0
   152
   }
sl@0
   153
sl@0
   154
   void operator()(double value) const
sl@0
   155
   {
sl@0
   156
      total_ += value;
sl@0
   157
   }
sl@0
   158
sl@0
   159
   template<typename T>
sl@0
   160
   void operator()(const T&) const
sl@0
   161
   {
sl@0
   162
      //Do nothing
sl@0
   163
   }
sl@0
   164
sl@0
   165
   double result() const
sl@0
   166
   {
sl@0
   167
      return total_;
sl@0
   168
   }
sl@0
   169
sl@0
   170
private:
sl@0
   171
   mutable double total_;
sl@0
   172
sl@0
   173
}; //sum_double
sl@0
   174
sl@0
   175
sl@0
   176
sl@0
   177
struct int_printer : boost::static_visitor<std::string>
sl@0
   178
{
sl@0
   179
   
sl@0
   180
   int_printer(std::string prefix_s = "") : prefix_s_(prefix_s) { }
sl@0
   181
   int_printer(const int_printer& other) : prefix_s_(other.prefix_s_)
sl@0
   182
   {
sl@0
   183
      ost_ << other.str();
sl@0
   184
   }
sl@0
   185
sl@0
   186
   std::string operator()(int x) const
sl@0
   187
   {
sl@0
   188
      ost_ << prefix_s_ << x;
sl@0
   189
      return str();
sl@0
   190
   }
sl@0
   191
sl@0
   192
   std::string operator()(const std::vector<int>& x) const
sl@0
   193
   {
sl@0
   194
      ost_ << prefix_s_;
sl@0
   195
sl@0
   196
      //Use another Int_printer object for printing a list of all integers
sl@0
   197
      int_printer job(",");
sl@0
   198
      ost_ << std::for_each(x.begin(), x.end(), job).str();
sl@0
   199
      
sl@0
   200
      return str();
sl@0
   201
   }
sl@0
   202
sl@0
   203
   std::string str() const
sl@0
   204
   {
sl@0
   205
      return ost_.str();
sl@0
   206
   }
sl@0
   207
sl@0
   208
private:
sl@0
   209
   std::string prefix_s_;
sl@0
   210
   mutable std::ostringstream ost_;
sl@0
   211
};  //int_printer
sl@0
   212
sl@0
   213
sl@0
   214
struct int_adder : boost::static_visitor<>
sl@0
   215
{
sl@0
   216
   
sl@0
   217
   int_adder(int rhs) : rhs_(rhs) { }
sl@0
   218
sl@0
   219
   result_type operator()(int& lhs) const
sl@0
   220
   {
sl@0
   221
      lhs += rhs_;
sl@0
   222
   }
sl@0
   223
sl@0
   224
   template<typename T>
sl@0
   225
   result_type operator()(const T& ) const
sl@0
   226
   {
sl@0
   227
      //Do nothing
sl@0
   228
   }
sl@0
   229
sl@0
   230
   int rhs_;
sl@0
   231
}; //int_adder
sl@0
   232
sl@0
   233
sl@0
   234
sl@0
   235
sl@0
   236
struct held_type_name : boost::static_visitor<std::string>
sl@0
   237
{
sl@0
   238
   
sl@0
   239
   template<typename T>
sl@0
   240
   std::string operator()(const T& ) const
sl@0
   241
   {
sl@0
   242
      ost_ << '[' << typeid(T).name() << ']';
sl@0
   243
      return result();
sl@0
   244
   }
sl@0
   245
sl@0
   246
   std::string result() const
sl@0
   247
   {
sl@0
   248
      return ost_.str();
sl@0
   249
   }
sl@0
   250
sl@0
   251
   mutable std::ostringstream ost_;
sl@0
   252
sl@0
   253
}; //held_type_name
sl@0
   254
sl@0
   255
sl@0
   256
sl@0
   257
sl@0
   258
template<typename T>
sl@0
   259
struct spec 
sl@0
   260
{
sl@0
   261
   typedef T result;
sl@0
   262
};
sl@0
   263
sl@0
   264
template<typename VariantType, typename S>
sl@0
   265
inline void verify(VariantType& var, spec<S>, std::string str = "")
sl@0
   266
{
sl@0
   267
   const VariantType& cvar = var;
sl@0
   268
sl@0
   269
   BOOST_CHECK(boost::apply_visitor(total_sizeof(), cvar) == sizeof(S));
sl@0
   270
   BOOST_CHECK(cvar.type() == typeid(S));
sl@0
   271
sl@0
   272
   //
sl@0
   273
   // Check get<>()
sl@0
   274
   //
sl@0
   275
   BOOST_CHECK(boost::get<S>(&var));
sl@0
   276
   BOOST_CHECK(boost::get<S>(&cvar));
sl@0
   277
sl@0
   278
   const S* ptr1 = 0;
sl@0
   279
   const S* ptr2 = 0;
sl@0
   280
   try
sl@0
   281
   {
sl@0
   282
      S& r = boost::get<S>(var);
sl@0
   283
      ptr1 = &r;
sl@0
   284
   }
sl@0
   285
   catch(boost::bad_get& )
sl@0
   286
   {
sl@0
   287
      BOOST_ERROR( "get<S> failed unexpectedly" );
sl@0
   288
   }
sl@0
   289
sl@0
   290
   try
sl@0
   291
   {
sl@0
   292
      const S& cr = boost::get<S>(cvar);
sl@0
   293
      ptr2 = &cr;
sl@0
   294
   }
sl@0
   295
   catch(boost::bad_get& )
sl@0
   296
   {
sl@0
   297
      BOOST_ERROR( "get<S> const failed unexpectedly" );
sl@0
   298
   }
sl@0
   299
sl@0
   300
   BOOST_CHECK(ptr1 != 0 && ptr2 == ptr1);
sl@0
   301
sl@0
   302
   //
sl@0
   303
   // Check string content
sl@0
   304
   //
sl@0
   305
   if(str.length() > 0)
sl@0
   306
   {
sl@0
   307
      std::string temp = boost::apply_visitor(to_text(), cvar);
sl@0
   308
      std::cout << "temp = " << temp << ", str = " << str << std::endl;
sl@0
   309
      BOOST_CHECK(temp == str);         
sl@0
   310
   }
sl@0
   311
}
sl@0
   312
sl@0
   313
sl@0
   314
template<typename VariantType, typename S>
sl@0
   315
inline void verify_not(VariantType& var, spec<S>)
sl@0
   316
{
sl@0
   317
   const VariantType& cvar = var;
sl@0
   318
sl@0
   319
   BOOST_CHECK(cvar.type() != typeid(S));
sl@0
   320
sl@0
   321
   //
sl@0
   322
   // Check get<>()
sl@0
   323
   //
sl@0
   324
   BOOST_CHECK(!boost::get<S>(&var));
sl@0
   325
   BOOST_CHECK(!boost::get<S>(&cvar));
sl@0
   326
sl@0
   327
   const S* ptr1 = 0;
sl@0
   328
   const S* ptr2 = 0;
sl@0
   329
   try
sl@0
   330
   {
sl@0
   331
      S& r = boost::get<S>(var); // should throw
sl@0
   332
      BOOST_ERROR( "get<S> passed unexpectedly" );
sl@0
   333
sl@0
   334
      ptr1 = &r;
sl@0
   335
   }
sl@0
   336
   catch(boost::bad_get& )
sl@0
   337
   {
sl@0
   338
      // do nothing except pass-through
sl@0
   339
   }
sl@0
   340
sl@0
   341
   try
sl@0
   342
   {
sl@0
   343
      const S& cr = boost::get<S>(var); // should throw
sl@0
   344
      BOOST_ERROR( "get<S> const passed unexpectedly" );
sl@0
   345
sl@0
   346
      ptr2 = &cr;
sl@0
   347
   }
sl@0
   348
   catch(boost::bad_get& )
sl@0
   349
   {
sl@0
   350
      // do nothing except pass-through
sl@0
   351
   }
sl@0
   352
sl@0
   353
   BOOST_CHECK(ptr1 == 0 && ptr2 == 0);   
sl@0
   354
}
sl@0
   355
sl@0
   356
sl@0
   357
#endif //_JOBSH_INC_