OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc1/] [gcc/] [testsuite/] [g++.dg/] [cpp0x/] [variadic-bind.C] - Blame information for rev 338

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 301 jeremybenn
// { dg-options "-std=gnu++0x" }
2
// { dg-do "run" }
3
// A basic implementation of TR1's bind using variadic teplates
4
// Contributed by Douglas Gregor 
5
#include 
6
 
7
// Trivial reference_wrapper
8
template
9
struct reference_wrapper
10
{
11
  reference_wrapper(T& x) : ptr(&x) { }
12
 
13
  operator T&() const { return *ptr; }
14
 
15
  T& get() const { return *ptr; }
16
 
17
  T* ptr;
18
};
19
 
20
template reference_wrapper ref(T& x) { return x; }
21
template reference_wrapper cref(const T& x) { return x; }
22
 
23
// Simple type-traits we'll need
24
template
25
struct add_reference
26
{
27
  typedef T& type;
28
};
29
 
30
template
31
struct add_reference
32
{
33
  typedef T& type;
34
};
35
 
36
template
37
struct is_same
38
{
39
  static const bool value = false;
40
};
41
 
42
template
43
struct is_same
44
{
45
  static const bool value = true;
46
};
47
 
48
// For creating the constructor parameters of tuple<>
49
template
50
struct add_const_reference
51
{
52
  typedef const T& type;
53
};
54
 
55
template
56
struct add_const_reference
57
{
58
  typedef T& type;
59
};
60
 
61
// 6.1.3 Class template tuple: Needed for bind() implementation
62
template
63
class tuple;
64
 
65
template<> class tuple<> { };
66
 
67
template
68
class tuple
69
  : private tuple
70
{
71
  typedef tuple inherited;
72
 
73
 public:
74
  tuple() { }
75
 
76
  // implicit copy-constructor is okay
77
 
78
  tuple(typename add_const_reference::type v,
79
        typename add_const_reference::type... vtail)
80
    : m_head(v), inherited(vtail...) { }
81
 
82
  template
83
  tuple(const tuple& other)
84
    : m_head(other.head()), inherited(other.tail()) { }
85
 
86
  template
87
  tuple& operator=(const tuple& other)
88
  {
89
    m_head = other.head();
90
    tail() = other.tail();
91
    return *this;
92
  }
93
 
94
  typename add_reference::type       head()       { return m_head; }
95
  typename add_reference::type head() const { return m_head; }
96
  inherited&                               tail()       { return *this; }
97
  const inherited&                         tail() const { return *this; }
98
 
99
 protected:
100
  Head m_head;
101
};
102
 
103
template
104
struct make_tuple_result
105
{
106
  typedef T type;
107
};
108
 
109
template
110
struct make_tuple_result >
111
{
112
  typedef T& type;
113
};
114
 
115
// 6.1.3.2 Tuple creation functions
116
struct ignore_t {
117
  template ignore_t& operator=(const T&) { return *this; }
118
} ignore;
119
 
120
template
121
tuple::type...>
122
make_tuple(const Values&... values)
123
{
124
  return tuple::type...>(values...);
125
}
126
 
127
template
128
tuple tie(Values&... values)
129
{
130
  return tuple(values...);
131
}
132
 
133
// 6.1.3.3 Tuple helper classes
134
template
135
struct tuple_size;
136
 
137
template<>
138
struct tuple_size >
139
{
140
  static const __SIZE_TYPE__ value = 0;
141
};
142
 
143
template
144
struct tuple_size >
145
{
146
  static const __SIZE_TYPE__ value = 1 + tuple_size >::value;
147
};
148
 
149
template
150
struct tuple_element;
151
 
152
template
153
struct tuple_element >
154
{
155
  typedef typename tuple_element >::type type;
156
};
157
 
158
template
159
struct tuple_element<0, tuple >
160
{
161
  typedef Head type;
162
};
163
 
164
// 6.1.3.4 Element access
165
template
166
class get_impl;
167
 
168
template
169
class get_impl >
170
{
171
  typedef typename tuple_element >::type Element;
172
  typedef typename add_reference::type RJ;
173
  typedef typename add_const_reference::type PJ;
174
  typedef get_impl > Next;
175
 
176
 public:
177
  static RJ get(tuple& t)
178
  { return Next::get(t.tail()); }
179
 
180
  static PJ get(const tuple& t)
181
  { return Next::get(t.tail()); }
182
};
183
 
184
template
185
class get_impl<0, tuple >
186
{
187
  typedef typename add_reference::type RJ;
188
  typedef typename add_const_reference::type PJ;
189
 
190
 public:
191
  static RJ get(tuple& t)       { return t.head(); }
192
  static PJ get(const tuple& t) { return t.head(); }
193
};
194
 
195
template
196
typename add_reference<
197
           typename tuple_element >::type
198
         >::type
199
get(tuple& t)
200
{
201
  return get_impl >::get(t);
202
}
203
 
204
template
205
typename add_const_reference<
206
           typename tuple_element >::type
207
         >::type
208
get(const tuple& t)
209
{
210
  return get_impl >::get(t);
211
}
212
 
213
// 6.1.3.5 Relational operators
214
inline bool operator==(const tuple<>&, const tuple<>&) { return true; }
215
 
216
template
217
bool operator==(const tuple& t, const tuple& u)
218
{
219
  return t.head() == u.head() && t.tail() == u.tail();
220
}
221
 
222
template
223
bool operator!=(const tuple& t, const tuple& u)
224
{
225
  return !(t == u);
226
}
227
 
228
inline bool operator<(const tuple<>&, const tuple<>&) { return false; }
229
 
230
template
231
bool operator<(const tuple& t, const tuple& u)
232
{
233
  return (t.head() < u.head() ||
234
          (!(t.head() < u.head()) && t.tail() < u.tail()));
235
}
236
 
237
template
238
bool operator>(const tuple& t, const tuple& u)
239
{
240
  return u < t;
241
}
242
 
243
template
244
bool operator<=(const tuple& t, const tuple& u)
245
{
246
  return !(u < t);
247
}
248
 
249
template
250
bool operator>=(const tuple& t, const tuple& u)
251
{
252
  return !(t < u);
253
}
254
 
255
// enable_if, the breakfast of champions
256
template
257
struct enable_if {
258
  typedef Type type;
259
};
260
 
261
template
262
struct enable_if { };
263
 
264
// 3.6 Function object binders
265
 
266
// 3.6.1 Class template is_bind_expression
267
template
268
struct is_bind_expression {
269
  static const bool value = false;
270
};
271
 
272
// 3.6.2 Class template is_placeholder
273
template
274
struct is_placeholder {
275
  static const int value = 0;
276
};
277
 
278
// 3.6.3 Function template bind
279
template struct placeholder {} ;
280
 
281
template struct int_c { };
282
 
283
// A tuple of integer values
284
template struct int_tuple {};
285
 
286
// make_indexes_impl is a helper for make_indexes
287
template
288
struct make_indexes_impl;
289
 
290
 
291
template
292
struct make_indexes_impl, T, Types...>
293
{
294
  typedef typename make_indexes_impl
295
                                     int_tuple,
296
                                     Types...>::type type;
297
};
298
 
299
template
300
struct make_indexes_impl > {
301
  typedef int_tuple type;
302
};
303
 
304
// make_indexes takes a variable-length number of N types and
305
// generates an int_tuple that contains <0, 1, 2, ..., N-1>. These can
306
// be used as indexes for tuple's get or tuple_element operation.
307
template
308
struct make_indexes : make_indexes_impl<0, int_tuple<>, Types...> { };
309
 
310
// Get the Ith tuple element, but only if I is in bounds.
311
template
312
struct safe_tuple_element{ };
313
 
314
template
315
struct safe_tuple_element,
316
         typename enable_if<(I >= 0 &&
317
                             I < tuple_size >::value)
318
                            >::type>
319
{
320
  typedef typename tuple_element >::type type;
321
};
322
 
323
// mu maps a bound argument to an actual argument, given a tuple of
324
// the arguments passed to the function object returned by bind().
325
 
326
// Return the stored reference from reference_wrapper
327
template
328
inline T& mu(reference_wrapper& bound_arg, const tuple&)
329
{
330
  return bound_arg.get();
331
}
332
 
333
// Unwrap a tuple into separate arguments and forward to the function
334
// object f.
335
template
336
inline typename F::result_type
337
unwrap_and_forward(F& f, int_tuple, const tuple& args)
338
{
339
  return f(get(args)...);
340
}
341
 
342
// Evaluate the inner bind expression
343
template
344
inline typename enable_if::value,
345
                          typename Bound::result_type>::type
346
mu(Bound& bound_arg, const tuple& args)
347
{
348
  typedef typename make_indexes::type Indexes;
349
  return unwrap_and_forward(bound_arg, Indexes(), args);
350
}
351
 
352
// Retrieve the Ith argument from args
353
template
354
inline typename safe_tuple_element::value - 1,
355
                                   tuple >::type
356
mu(Bound& bound_arg, const tuple& args)
357
{
358
  return get::value-1>(args);
359
}
360
 
361
// Return the stored value.
362
template
363
struct is_reference_wrapper {
364
  static const bool value = false;
365
};
366
 
367
template
368
struct is_reference_wrapper > {
369
  static const bool value = true;
370
};
371
 
372
template
373
inline typename enable_if<(!is_bind_expression::value
374
                           && !is_placeholder::value
375
                           && !is_reference_wrapper::value),
376
                          Bound&>::type
377
mu(Bound& bound_arg, const tuple&)
378
{
379
  return bound_arg;
380
}
381
 
382
//
383
template
384
typename F::result_type
385
apply_functor(F& f, tuple& bound_args, int_tuple,
386
              const tuple& args)
387
{
388
  return f(mu(get(bound_args), args)...);
389
}
390
 
391
template
392
class bound_functor
393
{
394
  typedef typename make_indexes::type indexes;
395
 
396
 public:
397
  typedef typename F::result_type result_type;
398
 
399
  explicit bound_functor(const F& f, const BoundArgs&... bound_args)
400
    : f(f), bound_args(bound_args...) { }
401
 
402
  template
403
  typename F::result_type operator()(Args&... args) {
404
    return apply_functor(f, bound_args, indexes(), tie(args...));
405
  }
406
 
407
 private:
408
  F f;
409
  tuple bound_args;
410
};
411
 
412
template
413
struct is_bind_expression > {
414
  static const bool value = true;
415
};
416
 
417
template
418
inline bound_functor
419
bind(const F& f, const BoundArgs&... bound_args)
420
{
421
  return bound_functor(f, bound_args...);
422
}
423
 
424
 
425
// 3.6.4 Placeholders
426
template
427
struct is_placeholder > {
428
  static const int value = I;
429
};
430
 
431
placeholder<1> _1;
432
placeholder<2> _2;
433
placeholder<3> _3;
434
placeholder<4> _4;
435
placeholder<5> _5;
436
placeholder<6> _6;
437
placeholder<7> _7;
438
placeholder<8> _8;
439
placeholder<9> _9;
440
 
441
// Test code
442
template
443
struct plus {
444
  typedef T result_type;
445
 
446
  T operator()(T x, T y) { return x + y; }
447
};
448
 
449
template
450
struct multiplies {
451
  typedef T result_type;
452
 
453
  T operator()(T x, T y) { return x * y; }
454
};
455
 
456
template
457
struct negate {
458
  typedef T result_type;
459
 
460
  T operator()(T x) { return -x; }
461
};
462
 
463
int main()
464
{
465
  int seventeen = 17;
466
  int forty_two = 42;
467
 
468
  assert(bind(plus(), _1, _2)(seventeen, forty_two) == 59);
469
  assert(bind(plus(), _1, _1)(seventeen, forty_two) == 34);
470
  assert(bind(plus(), _2, _1)(seventeen, forty_two) == 59);
471
  assert(bind(plus(), 5, _1)(seventeen, forty_two) == 22);
472
  assert(bind(plus(), ref(seventeen), _2)(seventeen, forty_two) == 59);
473
  assert(bind(plus(), bind(multiplies(), 3, _1), _2)(seventeen, forty_two)
474
         == 93);
475
  return 0;
476
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.