Classdesc  3.D29
function.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of Classdesc
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
13 #ifndef FUNCTION_H
14 #define FUNCTION_H
15 #include "classdesc.h"
16 #include <string>
17 
18 namespace classdesc
19 {
20 
25  namespace functional
26  {
27 
28  // these classes have either a const static member V, or a type T
30  //function object \a F
31  template <class F> struct Arity;
33  template <class F> struct Return;
35  // note - this basically duplicates std::is_member_function_pointer
36  template <class F> struct is_member_function_ptr
37  {static const bool value=false;};
38 
40  template <class F> struct is_const_method
41  {static const bool value=false;};
42 
44  template <class F> struct is_nonmember_function_ptr
45  {static const bool value=false;};
48  template <class F> struct is_function
49  {
50  static const bool value=is_member_function_ptr<F>::value||
52  };
53 
54 
55 
56 
59  template <class F, template<class> class Pred, int arity=Arity<F>::value> struct AllArgs;
60 // template <class F, template<class> class Pred> struct AnyArg
61 // {
62 // static const bool value=!AllArgs<F,Not<Pred> >::value;
63 // };
65 
68  template <class F, int> struct Arg;
69 
70  template <class C, class M> class bound_method;
71 
72  template <class T> struct Fdummy {Fdummy(int) {} };
73 
74 
90  template <class C, class M>
91  struct Arity<bound_method<C,M> >
92  {
93  static const int V=bound_method<C,M>::arity;
94  static const int value=bound_method<C,M>::arity;
95  };
96 
97  template <class C, class M>
98  struct Return<bound_method<C,M> >
99  {
100  typedef typename bound_method<C,M>::Ret T;
101  };
102 
103  template <class C, class M, int i>
104  struct Arg<bound_method<C,M>,i>
105  {
107  typedef T type;
108  };
109 
110 #include "functiondb.h"
111 
112  //std::bind1st is no good, because it assume the functional
113  // has a first_argument_type member. So roll our own
114  template <class F, class X, int Arity>
115  class binder1st
116  {
117  F f;
118  X x;
119  public:
120  static const int arity=Arity;
121  typedef typename Return<F>::T Ret;
122  binder1st(const F& f, const X& x): f(f), x(x) {}
123  template <class Y>
124  binder1st<binder1st<F,X,Arity>,Y,Arity-1> operator()(const Y& y) const
125  {return binder1st<binder1st<F,X,Arity>,Y,Arity-1>(*this, y);}
126  };
127 
128  template <class F, class X>
129  class binder1st<F,X,0>
130  {
131  F f;
132  X x;
133  public:
134  static const int arity=0;
135  typedef typename Return<F>::T Ret;
136  binder1st(const F& f, const X& x): f(f), x(x) {}
137  typename Return<F>::T operator()() const
138  {return f(x);}
139  };
140 
141  template <class F, class X, int A>
142  struct Arity<binder1st<F,X,A> >
143  {
144  static const int V=A;
145  };
146 
147  template <class F, class X, int A>
148  struct Return<binder1st<F,X,A> >
149  {
150  typedef typename binder1st<F,X,A>::Ret T;
151  };
152 
153  template <class F, class X>
154  binder1st<F,X, Arity<F>::V-1> bind1st(const F& f, const X& x, dummy<0> dum=0)
155  {
157  }
158 
159  template <class O, class M>
161  bind1st(const M& member, O& obj, dummy<1> dum=0)
162  {
163  return bound_method<O,M>(obj,member);
164  }
165 
166  template <class F, class Args>
167  typename enable_if<
169  void
170  >::T
171  apply(typename Return<F>::T* r, F f, const Args& args, dummy<0> d=0)
172  {*r=apply_nonvoid_fn(f,args);}
173 
174  template <class F, class Args>
175  typename enable_if<is_void<typename Return<F>::T>, void>::T
176  apply(void* r, F f, Args& args, dummy<1> d=0)
177  {apply_void_fn(f,args);}
178 
179 
180 
182  }
183 
184 }
185 #endif
Definition: function.h:72
Definition: classdesc.h:351
Definition: function.h:48
Definition: function.h:59
Definition: function.h:115
is_member_function_ptr::value is true if F is a member function pointer
Definition: function.h:36
Definition: function.h:70
Arity::V (or ::value) is the number of arguments of
Definition: function.h:31
Return::T (or ::type) is the return type of F
Definition: function.h:33
is_nonmember_function_ptr::value is true if F is an ordinary function pointer
Definition: function.h:44
controlled template specialisation: stolen from boost::enable_if.
Definition: classdesc.h:274
Definition: classdesc.h:291
Definition: function.h:68
is_const_method::value is true if F is a pointer to a const method
Definition: function.h:40