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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [g++.dg/] [cpp0x/] [variadic-function.C] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 693 jeremybenn
// { dg-do run }
2
// { dg-options "-std=gnu++0x" }
3
// A basic implementation of TR1's function using variadic teplates
4
// Contributed by Douglas Gregor 
5
#include 
6
 
7
template
8
class function;
9
 
10
template
11
class invoker_base
12
{
13
 public:
14
  virtual ~invoker_base() { }
15
  virtual R invoke(Args...) = 0;
16
  virtual invoker_base* clone() = 0;
17
};
18
 
19
template
20
class functor_invoker : public invoker_base
21
{
22
 public:
23
  explicit functor_invoker(const F& f) : f(f) { }
24
  R invoke(Args... args) { return f(args...); }
25
  functor_invoker* clone() { return new functor_invoker(f); }
26
 
27
 private:
28
  F f;
29
};
30
 
31
template
32
class function {
33
 public:
34
  typedef R result_type;
35
 
36
  function() : invoker (0) { }
37
 
38
  function(const function& other) : invoker(0) {
39
    if (other.invoker)
40
      invoker = other.invoker->clone();
41
  }
42
 
43
  template
44
  function(const F& f) : invoker(0) {
45
    invoker = new functor_invoker(f);
46
  }
47
 
48
  ~function() {
49
    if (invoker)
50
      delete invoker;
51
  }
52
 
53
  function& operator=(const function& other) {
54
    function(other).swap(*this);
55
    return *this;
56
  }
57
 
58
  template
59
  function& operator=(const F& f) {
60
    function(f).swap(*this);
61
    return *this;
62
  }
63
 
64
  void swap(function& other) {
65
    invoker_base* tmp = invoker;
66
    invoker = other.invoker;
67
    other.invoker = tmp;
68
  }
69
 
70
  result_type operator()(Args... args) const {
71
    assert(invoker);
72
    return invoker->invoke(args...);
73
  }
74
 
75
 private:
76
  invoker_base* invoker;
77
};
78
 
79
struct plus {
80
  template T operator()(T x, T y) { return x + y; }
81
};
82
 
83
struct multiplies {
84
  template T operator()(T x, T y) { return x * y; }
85
};
86
 
87
int main()
88
{
89
  function f1 = plus();
90
  assert(f1(3, 5) == 8);
91
 
92
  f1 = multiplies();
93
  assert(f1(3, 5) == 15);
94
 
95
  return 0;
96
}

powered by: WebSVN 2.1.0

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