summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/variant/test/jobs.h
blob: 9b965554a230290d8deb21fa630b59b39d3f4271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//-----------------------------------------------------------------------------
// boost-libs variant/libs/test/jobs.h header file
// See http://www.boost.org for updates, documentation, and revision history.
//-----------------------------------------------------------------------------
//
// Copyright (c) 2003
// Eric Friedman, Itay Maman
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef _JOBSH_INC_
#define _JOBSH_INC_

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>

#include "boost/variant/variant_fwd.hpp"
#include "boost/variant/get.hpp"
#include "boost/variant/apply_visitor.hpp"
#include "boost/variant/static_visitor.hpp"

#include "boost/type_index.hpp"
#include "boost/detail/workaround.hpp"
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
#    pragma warn -lvc
#endif

struct to_text : boost::static_visitor<std::string>
{
private: // NO_FUNCTION_TEMPLATE_ORDERING workaround

    template < BOOST_VARIANT_ENUM_PARAMS(typename U) >
    std::string to_text_impl(
          const boost::variant< BOOST_VARIANT_ENUM_PARAMS(U) >& operand, long
        ) const
    {
        std::ostringstream ost;
        ost << "[V] " << boost::apply_visitor(to_text(), operand);

        return ost.str();
    }

    template <typename Value>
    std::string to_text_impl(const Value& operand, int) const
    {
        std::ostringstream ost;
        ost << "[V] " << operand;

        return ost.str();
    }

public:

    template <typename T>
    std::string operator()(const T& operand) const
    {
        return to_text_impl(operand, 1L);
    }

};

struct total_sizeof : boost::static_visitor<int>
{
   total_sizeof() : total_(0) { }

   template<class Value>
   int operator()(const Value&) const
   {
      total_ += sizeof(Value);
      return total_;
   }

   int result() const
   {
      return total_;
   }

   mutable int total_;

}; // total_sizeof



//Function object: sum_int
//Description: Compute total sum of a series of numbers, (when called successively)
//Use sizeof(T) if applied with a non-integral type
struct sum_int : boost::static_visitor<int>
{
   
   sum_int() : total_(0) { }


   template<int n>
   struct int_to_type
   {
      BOOST_STATIC_CONSTANT(int, value = n);
   }; 

   //Integral type - add numerical value
   template<typename T>
   void add(T t, int_to_type<true> ) const
   {
      total_ += t;
   }

   //Other types - add sizeof<T>
   template<typename T>
   void add(T& , int_to_type<false> ) const
   {
      total_ += sizeof(T);
   }

   template<typename T>
   int operator()(const T& t) const
   {
      //Int_to_type is used to select the correct add() overload
      add(t, int_to_type<boost::is_integral<T>::value>());
      return total_;
   }

   int result() const
   {
      return total_;
   }

private:
   mutable int total_;

}; //sum_int






//Function object: sum_double
//Description: Compute total sum of a series of numbers, (when called successively)
//Accpetable input types: float, double (Other types are silently ignored)
struct sum_double : boost::static_visitor<double>
{
   
   sum_double() : total_(0) { }

   void operator()(float value) const
   {
      total_ += value;
   }

   void operator()(double value) const
   {
      total_ += value;
   }

   template<typename T>
   void operator()(const T&) const
   {
      //Do nothing
   }

   double result() const
   {
      return total_;
   }

private:
   mutable double total_;

}; //sum_double



struct int_printer : boost::static_visitor<std::string>
{
   
   int_printer(std::string prefix_s = "") : prefix_s_(prefix_s) { }
   int_printer(const int_printer& other) : prefix_s_(other.prefix_s_)
   {
      ost_ << other.str();
   }

   std::string operator()(int x) const
   {
      ost_ << prefix_s_ << x;
      return str();
   }

   std::string operator()(const std::vector<int>& x) const
   {
      ost_ << prefix_s_;

      //Use another Int_printer object for printing a list of all integers
      int_printer job(",");
      ost_ << std::for_each(x.begin(), x.end(), job).str();
      
      return str();
   }

   std::string str() const
   {
      return ost_.str();
   }

private:
   std::string prefix_s_;
   mutable std::ostringstream ost_;
};  //int_printer


struct int_adder : boost::static_visitor<>
{
   
   int_adder(int rhs) : rhs_(rhs) { }

   result_type operator()(int& lhs) const
   {
      lhs += rhs_;
   }

   template<typename T>
   result_type operator()(const T& ) const
   {
      //Do nothing
   }

   int rhs_;
}; //int_adder



template<typename T>
struct spec 
{
   typedef T result;
};

template<typename VariantType, typename S>
inline void verify(VariantType& var, spec<S>, std::string str = "")
{
   const VariantType& cvar = var;

   BOOST_TEST(boost::apply_visitor(total_sizeof(), cvar) == sizeof(S));
   BOOST_TEST(cvar.type() == boost::typeindex::type_id<S>());

   //
   // Check get<>()
   //
   BOOST_TEST(boost::get<S>(&var));
   BOOST_TEST(boost::get<S>(&cvar));

   const S* ptr1 = 0;
   const S* ptr2 = 0;
   try
   {
      S& r = boost::get<S>(var);
      ptr1 = &r;
   }
   catch(const boost::bad_get& )
   {
      BOOST_ERROR( "get<S> failed unexpectedly" );
   }

   try
   {
      const S& cr = boost::get<S>(cvar);
      ptr2 = &cr;
   }
   catch(const boost::bad_get& )
   {
      BOOST_ERROR( "get<S> const failed unexpectedly" );
   }

   BOOST_TEST(ptr1 != 0 && ptr2 == ptr1);

   //
   // Check string content
   //
   if(str.length() > 0)
   {
      std::string temp = boost::apply_visitor(to_text(), cvar);
      std::cout << "temp = " << temp << ", str = " << str << std::endl;
      BOOST_TEST(temp == str);
   }
}


template<typename VariantType, typename S>
inline void verify_not(VariantType& var, spec<S>)
{
   const VariantType& cvar = var;

   BOOST_TEST(cvar.type() != boost::typeindex::type_id<S>());

   //
   // Check get<>()
   //
   BOOST_TEST(!boost::get<S>(&var));
   BOOST_TEST(!boost::get<S>(&cvar));

   const S* ptr1 = 0;
   const S* ptr2 = 0;
   try
   {
      S& r = boost::get<S>(var); // should throw
      BOOST_ERROR( "get<S> passed unexpectedly" );

      ptr1 = &r;
   }
   catch(const boost::bad_get& )
   {
      // do nothing except pass-through
   }

   try
   {
      const S& cr = boost::get<S>(var); // should throw
      BOOST_ERROR( "get<S> const passed unexpectedly" );

      ptr2 = &cr;
   }
   catch(const boost::bad_get& )
   {
      // do nothing except pass-through
   }

   BOOST_TEST(ptr1 == 0 && ptr2 == 0);
}


#endif //_JOBSH_INC_