Proton  1.1.1
Make porting easy from Python to C++11
functor.hpp
Go to the documentation of this file.
1 #ifndef PROTON_FUNCTOR_H
2 #define PROTON_FUNCTOR_H
3 
4 /** @file functor.hpp
5  * @brief functor interface
6  */
7 
8 #include <iostream>
9 #include <ctime>
10 #include <cstring>
11 #include <iomanip>
12 #include <cstdio>
13 
14 namespace proton{
15 
16 /**
17  * @addtogroup ref
18  * @{
19  * @addtogroup functor
20  * @{
21  * Due to bugs of Doxygen, template aliases can't be shown here, including:
22  * - template<typename fpT> func_ : the functor template.
23  * - template<typename fpT> fp_ : functor for normal functions.
24  * - template<typename refT, typename fpT> fm_ : functor for ref_ member functions.
25  *
26  * @see functor.cpp in examples.
27  */
28 
29 /** @example functor.cpp
30  */
31 
32 template<typename> struct fo_;
33 
34 /** functor interface for function object classes.
35  */
36 template<typename retT, typename ...argT>
37 struct fo_<retT(argT...)>{
38  virtual retT operator()(argT ...x)=0;
39 };
40 
41 /** functor template.
42  */
43 template<typename fpT>
44  using func_=ref_<fo_<fpT> >;
45 
46 template<typename> struct _fp_;
47 
48 template<typename retT, typename ...argT>
49 struct _fp_<retT(argT...) >: fo_<retT(argT...)>{
50  typedef retT (*fp_t)(argT...);
51  fp_t _f;
52 
53  _fp_()=delete;
54 
55  _fp_(fp_t f):_f(f)
56  {}
57 
58  retT operator()(argT ...x)
59  {
60  return _f(x...);
61  }
62 
63 };
64 
65 /** functor for normal functions.
66  */
67 template<typename fpT>
68  using fp_=ref_<_fp_<fpT> >;
69 
70 
71 template<typename, typename> struct _fm_;
72 
73 template<typename refT, typename retT, typename ...argT>
74 struct _fm_<refT, retT(argT...) >: fo_<retT(argT...)>{
75  typedef typename refT::obj_t obj_t;
76  typedef retT (obj_t::*fp_t)(argT...);
77 
78  refT _x;
79  fp_t _f;
80 
81  _fm_()=delete;
82 
83  _fm_(refT x, fp_t f):_x(x), _f(f)
84  {}
85 
86  retT operator()(argT ...a)
87  {
88  return ((*_x).*(_f))(a...);
89  }
90 };
91 
92 /** functor for member functions.
93  *
94  * If possible, please use interface mechanism of ref_ directly or std::mem_fn,
95  * instead of functors for member functions.
96  */
97 template<typename refT, typename fpT>
98  using fm_=ref_<_fm_<refT, fpT> >;
99 
100 /**
101  * @}
102  * @}
103  */
104 }; // namespace proton
105 
106 #endif // PROTON_FUNCTOR_H