MIRA
SignalBinder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_SIGNALBINDER_H_
49 #define _MIRA_SIGNALBINDER_H_
50 
51 #ifndef Q_MOC_RUN
52 #include <boost/function.hpp>
53 #endif
54 
55 #include <QObject>
56 
57 #include <utils/Bind.h>
58 
59 namespace mira {
60 
62 
85 class SignalBinder : public QObject
86 {
87  Q_OBJECT
88 public:
89  SignalBinder(QObject* parent) :
90  QObject(parent) {}
91 
92 public:
93 
95  void bind(boost::function<void()> f) { mVoid = f; }
96 
98  void bindInt(boost::function<void(int)> f) { mInt = f; }
99 
101  void bindQString(boost::function<void(const QString&)> f) { mQString = f; }
102 
103 public slots:
104 
105  void slot() { if(mVoid) mVoid(); }
106  void slot(int val) { if(mInt) mInt(val); }
107  void slot(const QString& val) { if(mQString) mQString(val); }
108 
109 public:
110 
111 
117  static SignalBinder* connect(QObject* sender, const char* signal,
118  boost::function<void()> function)
119  {
120  SignalBinder* binder = new SignalBinder(sender);
121  binder->bind(function);
122  binder->QObject::connect(sender,signal, binder, SLOT(slot()));
123  return binder;
124  }
125 
131  template<typename Class>
132  static SignalBinder* connect(QObject* sender, const char* signal,
133  void (Class::*method)(), Class* instance) {
134  return connect(sender,signal,(boost::function<void()>)boost::bind(method, instance));
135  }
136 
142  static SignalBinder* connect(QObject* sender, const char* signal,
143  boost::function<void(int)> function)
144  {
145  SignalBinder* binder = new SignalBinder(sender);
146  binder->bindInt(function);
147  binder->QObject::connect(sender,signal, binder, SLOT(slot(int)));
148  return binder;
149  }
150 
156  template<typename Class>
157  static SignalBinder* connect(QObject* sender, const char* signal,
158  void (Class::*method)(int), Class* instance) {
159  return connect(sender,signal,(boost::function<void(int)>)boost::bind(method, instance, _1));
160  }
161 
167  static SignalBinder* connect(QObject* sender, const char* signal,
168  boost::function<void(const QString&)> function)
169  {
170  SignalBinder* binder = new SignalBinder(sender);
171  binder->bindQString(function);
172  binder->QObject::connect(sender,signal, binder, SLOT(slot(const QString&)));
173  return binder;
174  }
175 
181  template<typename Class>
182  static SignalBinder* connect(QObject* sender, const char* signal,
183  void (Class::*method)(const QString&), Class* instance) {
184  return connect(sender,signal,(boost::function<void(const QString&)>)boost::bind(method, instance, _1));
185  }
186 
187 private:
188 
189  boost::function<void()> mVoid;
190  boost::function<void(int)> mInt;
191  boost::function<void(const QString&)> mQString;
192 };
193 
195 
196 }
197 
198 #endif
static SignalBinder * connect(QObject *sender, const char *signal, void(Class::*method)(const QString &), Class *instance)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified &#39;met...
Definition: SignalBinder.h:182
Class that provides different slots with different parameters.
Definition: SignalBinder.h:85
static SignalBinder * connect(QObject *sender, const char *signal, boost::function< void(int)> function)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified func...
Definition: SignalBinder.h:142
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
SignalBinder(QObject *parent)
Definition: SignalBinder.h:89
Class object which supports some kind of class reflection.
Definition: Class.h:97
void slot()
Definition: SignalBinder.h:105
void bindQString(boost::function< void(const QString &)> f)
binds the given function to the slot "slot(const QString&)"
Definition: SignalBinder.h:101
void bindInt(boost::function< void(int)> f)
binds the given function to the slot "slot(int)"
Definition: SignalBinder.h:98
void slot(int val)
Definition: SignalBinder.h:106
static SignalBinder * connect(QObject *sender, const char *signal, boost::function< void()> function)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified func...
Definition: SignalBinder.h:117
void slot(const QString &val)
Definition: SignalBinder.h:107
Wrapper for boost/bind.
static SignalBinder * connect(QObject *sender, const char *signal, boost::function< void(const QString &)> function)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified func...
Definition: SignalBinder.h:167
void bind(boost::function< void()> f)
binds the given function to the slot "slot()"
Definition: SignalBinder.h:95
static SignalBinder * connect(QObject *sender, const char *signal, void(Class::*method)(int), Class *instance)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified &#39;met...
Definition: SignalBinder.h:157
static SignalBinder * connect(QObject *sender, const char *signal, void(Class::*method)(), Class *instance)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified &#39;met...
Definition: SignalBinder.h:132