aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Kinnunen <fluxgen@fluxbox.org>2010-03-18 09:45:33 (GMT)
committerHenrik Kinnunen <fluxgen@fluxbox.org>2010-03-18 09:45:33 (GMT)
commit0504de454a0a3bf06466029940af480e3caaa8b2 (patch)
tree0c4507b4fe965cbe2080329ef9bad443c14fa6aa
parent5a91ada3c6c9f199b3646465206d4b2b613cad3e (diff)
downloadfluxbox-0504de454a0a3bf06466029940af480e3caaa8b2.zip
fluxbox-0504de454a0a3bf06466029940af480e3caaa8b2.tar.bz2
Added MemFunIgnoreArgs which ignores aditional arguments.
For example connecting a function that takes two arguments to a signal that emits three arguments: struct Functor { void show(int a, int b); }; Functor f; Signal<void, int, int, int> s3; s3.connect(MemFunIgnoreArgs(f, &Functor::show));
-rw-r--r--src/FbTk/MemFun.hh90
-rw-r--r--src/tests/testSignals.cc23
2 files changed, 111 insertions, 2 deletions
diff --git a/src/FbTk/MemFun.hh b/src/FbTk/MemFun.hh
index 4c834dd..c2c6b20 100644
--- a/src/FbTk/MemFun.hh
+++ b/src/FbTk/MemFun.hh
@@ -38,7 +38,9 @@ public:
38 void operator ()() { 38 void operator ()() {
39 (m_obj.*m_action)(); 39 (m_obj.*m_action)();
40 } 40 }
41 41 void call() {
42 (m_obj.*m_action)();
43 }
42private: 44private:
43 Object& m_obj; 45 Object& m_obj;
44 Action m_action; 46 Action m_action;
@@ -133,6 +135,92 @@ MemFun( Object& obj, ReturnType (Object:: *action)(Arg1, Arg2, Arg3) ) {
133 return MemFun3<ReturnType, Object, Arg1, Arg2, Arg3>(obj, action); 135 return MemFun3<ReturnType, Object, Arg1, Arg2, Arg3>(obj, action);
134} 136}
135 137
138/// Ignores all arguments
139template <typename ReturnType, typename Object>
140class MemFun0IgnoreArgs: public MemFun0<ReturnType, Object> {
141public:
142 typedef MemFun0<ReturnType, Object> BaseType;
143
144 MemFun0IgnoreArgs(Object& obj,
145 typename BaseType::Action action):
146 BaseType(obj, action) {
147 }
148
149 template <typename IgnoreType1, typename IgnoreType2, typename IgnoreType3>
150 void operator ()(IgnoreType1&, IgnoreType2&, IgnoreType3&) {
151 BaseType::operator ()();
152 }
153
154 template <typename IgnoreType1, typename IgnoreType2>
155 void operator ()(IgnoreType1&, IgnoreType2&) {
156 BaseType::operator ()();
157 }
158
159 template <typename IgnoreType1>
160 void operator ()(IgnoreType1&) {
161 BaseType::operator ()();
162 }
163};
164
165/// Ignores second and third argument
166template <typename ReturnType, typename Object, typename Arg1>
167class MemFun1IgnoreArgs: public MemFun1<ReturnType, Object, Arg1> {
168public:
169 typedef MemFun1<ReturnType, Object, Arg1> BaseType;
170
171 MemFun1IgnoreArgs(Object& obj, typename BaseType::Action& action):
172 BaseType(obj, action) {
173 }
174
175 template <typename IgnoreType1, typename IgnoreType2>
176 void operator ()(Arg1 arg1, IgnoreType1&, IgnoreType2&) {
177 BaseType::operator ()(arg1);
178 }
179
180 template <typename IgnoreType>
181 void operator ()(Arg1 arg1, IgnoreType&) {
182 BaseType::operator ()(arg1);
183 }
184};
185
186/// Takes two arguments but ignores the third
187template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
188class MemFun2IgnoreArgs: public MemFun2<ReturnType, Object, Arg1, Arg2> {
189public:
190 typedef MemFun2<ReturnType, Object, Arg1, Arg2> BaseType;
191
192 MemFun2IgnoreArgs(Object& obj, typename BaseType::Action& action):
193 BaseType(obj, action) {
194 }
195
196 template < typename IgnoreType >
197 void operator ()(Arg1 arg1, Arg2 arg2, IgnoreType&) {
198 BaseType::operator ()(arg1, arg2);
199 }
200};
201
202/// Creates functor that ignores all arguments.
203template <typename ReturnType, typename Object>
204MemFun0IgnoreArgs<ReturnType, Object>
205MemFunIgnoreArgs( Object& obj, ReturnType (Object:: *action)() ) {
206 return MemFun0IgnoreArgs<ReturnType, Object>(obj, action);
207}
208
209/// Creates functor that ignores second and third argument.
210template <typename ReturnType, typename Object, typename Arg1>
211MemFun1IgnoreArgs<ReturnType, Object, Arg1>
212MemFunIgnoreArgs( Object& obj, ReturnType (Object:: *action)(Arg1) ) {
213 return MemFun1IgnoreArgs<ReturnType, Object, Arg1>(obj, action);
214}
215
216/// Creates functor that ignores third argument.
217template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
218MemFun2IgnoreArgs<ReturnType, Object, Arg1, Arg2>
219MemFunIgnoreArgs( Object& obj, ReturnType (Object:: *action)(Arg1,Arg2) ) {
220 return MemFun2IgnoreArgs<ReturnType, Object, Arg1, Arg2>(obj, action);
221}
222
223
136} // namespace FbTk 224} // namespace FbTk
137 225
138#endif // FBTK_MEM_FUN_HH 226#endif // FBTK_MEM_FUN_HH
diff --git a/src/tests/testSignals.cc b/src/tests/testSignals.cc
index 86096bf..6d68d3a 100644
--- a/src/tests/testSignals.cc
+++ b/src/tests/testSignals.cc
@@ -21,7 +21,8 @@ struct OneArgument {
21}; 21};
22 22
23struct TwoArguments { 23struct TwoArguments {
24 void operator ()( int value, const string& message ) { 24 template <typename T1, typename T2>
25 void operator ()( const T1& value, const T2& message ) {
25 cout << "Two arguments, (1) = " << value << ", (2) = " << message << endl; 26 cout << "Two arguments, (1) = " << value << ", (2) = " << message << endl;
26 } 27 }
27}; 28};
@@ -51,6 +52,9 @@ struct FunctionClass {
51 void showMessage( int value, const string& message ) { 52 void showMessage( int value, const string& message ) {
52 cout << "(" << value << "): " << message << endl; 53 cout << "(" << value << "): " << message << endl;
53 } 54 }
55 void showMessage2( const string& message1, const string& message2) {
56 cout << "(" << message1 << ", " << message2 << ")" << endl;
57 }
54 void threeArgs( int value, const string& str, double pi ) { 58 void threeArgs( int value, const string& str, double pi ) {
55 cout << "(" << value << "): " << str << ", pi = " << pi << endl; 59 cout << "(" << value << "): " << str << ", pi = " << pi << endl;
56 } 60 }
@@ -118,4 +122,21 @@ int main() {
118 three_args.clear(); 122 three_args.clear();
119 three_args.connect(MemFun(obj, &FunctionClass::threeArgs)); 123 three_args.connect(MemFun(obj, &FunctionClass::threeArgs));
120 three_args.emit(9, "nine", 3.141592); 124 three_args.emit(9, "nine", 3.141592);
125
126 // Test ignore signals
127 {
128 cout << "----------- Testing ignoring arguments for signal." << endl;
129 using FbTk::MemFunIgnoreArgs;
130 // Create a signal that emits with three arguments, and connect
131 // sinks that takes less than three arguments.
132 Signal<void, string, string, float> more_args;
133 more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::print));
134 more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::takeIt));
135 more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::showMessage2));
136 more_args.emit("This should be visible for takeIt(string)",
137 "Visible to the two args function.",
138 2.9);
139
140 }
141
121} 142}