language: C++ 4.7.2 (gcc-4.7.2)
date: 888 days 8 hours ago
link:
visibility: public
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
#include <stdio.h>
#include <vector>
 
template<typename R=void, 
typename A=void,
typename F=R (*)(A)> class Method {             
protected: F method;
public   : Method(F methodPtr):method(methodPtr){ };
                   virtual R operator()(A argument) { 
                           return this->method(argument); 
                   };
                   typedef F FuncType;
};
 
template<typename A, typename F> class Method<void,A,F> {               
protected: F method;
public   : Method(F methodPtr) :method(methodPtr) { };
                   virtual void operator()(A argument) { 
                           this->method(argument); 
                   };
                   typedef F FuncType;
};
 
template<typename R, typename F> class Method<R,void,F> {               
protected: F method;
public   : Method(F methodPtr) :method(methodPtr) { };
                   virtual R operator()() { 
                           return this->method(); 
                   };
                   typedef F FuncType;
};
 
template<typename F> class Method<void,void,F> {                
protected: F method;
public   : Method(F methodPtr) :method(methodPtr) { };
                   virtual void operator()() { 
                           this->method(); 
                   };
                   typedef F FuncType;
};
 
template<typename C=void,
typename R=void, 
typename A=void,
typename F=R (C::*)(A)> 
class ClassMethod : public Method<R,A,F> {              
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
                           :Method<R,A,F>(methodPtr),owner(methodOwner){ };
                   virtual R operator()(A argument) { 
                           return ((this->owner).*(this->method))(argument); 
                   };
                   typedef F FuncType;
};
 
template<typename C, typename A, typename F> 
class ClassMethod<C,void,A,F>: public Method<void,A,F> {
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
                           :Method<void,A,F>(methodPtr),owner(methodOwner){ };
                   virtual void operator()(A argument) { 
                           ((this->owner).*(this->method))(argument); 
                   };
                   typedef F FuncType;
};
 
template<typename C, typename R, typename F> 
class ClassMethod<C,R,void,F>: public Method<R,void,F> {
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
                           :Method<R,void,F>(methodPtr),owner(methodOwner){ };
                   virtual R operator()() { 
                           return ((this->owner).*(this->method))(); 
                   };
                   typedef F FuncType;
};
 
template<typename C, typename F> 
class ClassMethod<C,void,void,F>: public Method<void,void,F> {
protected: C& owner;
public   : ClassMethod(C& methodOwner,F methodPtr) 
                           :Method<void,void,F>(methodPtr),owner(methodOwner){ };
                   virtual void operator()() { 
                           ((this->owner).*(this->method))(); 
                   };
                   typedef F FuncType;
};
 
template<typename A> class MethodList {
protected:
        std::vector< Method<void,A> > methods;
public: 
        void add(typename Method<void,A>::FuncType fp) {
                this->methods.push_back(Method<void,A>(fp));
        }
        template<class C> void add(typename C& instance,
                typename ClassMethod<C,void,A>::FuncType fp) {
                        this->methods.push_back(ClassMethod<C,void,A>(instance,fp));
                }
                void invoke(A argument) {
                        typename std::vector< Method<void,A> >::iterator it;
                        for(it=this->methods.begin() ; it!=this->methods.end() ; it++) {
                                (*it)(argument);
                        }
                }
};
 
 
void function1(int arg) {
        printf("function1(%d)",arg);
}
 
class Class1 {
public:
        void function1(int arg) {
                printf("Class1::function1(%d)",arg);
        }
};
 
int main(int argc,char* argv[] )                        
{      
        Class1 inst;
        MethodList<int> methodList;
 
        methodList.add(function1);
        methodList.add<Class1>(inst,&Class1::function1);
        methodList.invoke(123);
 
        return 0;
}   
prog.cpp:96: error: expected nested-name-specifier before ‘C’
prog.cpp:96: error: expected `(' before ‘C’
prog.cpp:97: error: expected `(' before ‘fp’
prog.cpp:97: error: variable or field ‘add’ declared void
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:126: error: expected primary-expression before ‘>’ token
prog.cpp:126: warning: left-hand operand of comma has no effect