MIRA
Runnable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 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 
47 #ifndef _MIRA_RUNNABLE_H_
48 #define _MIRA_RUNNABLE_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/enable_shared_from_this.hpp>
52 #endif
53 
54 #include <error/LoggingAux.h>
55 #include <fw/Status.h>
56 
57 namespace mira {
58 
60 
61 //fwd decls.
62 class IRunnable;
64 
66 typedef boost::shared_ptr<IRunnable> IRunnablePtr;
67 
72 class IRunnable : public boost::enable_shared_from_this<IRunnable>
73 {
74 public:
75 
76  virtual ~IRunnable() {}
77 
78 protected:
79  friend class DispatcherThread;
80 
85  virtual void run(DispatcherThread* dispatcher) = 0;
86 
87 };
88 
90 
92 {
93 public:
94 
96  mDiagnosticsModule(module) {}
97 
99  mDiagnosticsModule = module;
100  }
102 
103 protected:
104 
106 };
107 typedef boost::shared_ptr<DiagnosticRunnable> DiagnosticRunnablePtr;
108 
110 
114 template <typename F>
116 {
117 public:
118  FunctionRunnable(F&& fn, DiagnosticsModulePtr module = NULL) :
119  DiagnosticRunnable(module), function(fn) {}
120 
121  virtual void run(DispatcherThread* dispatcher) {
122 
123  try {
124  function();
125  }
126  catch(Exception& ex) {
127  if (!mDiagnosticsModule ||
128  mDiagnosticsModule->error("Runnable", ex.message(),
129  "Exception while invoking Runnable")) {
130  MIRA_LOG_EXCEPTION(ERROR,ex) << "Exception:\n";
131  }
132  }
133  catch(std::exception& ex) {
134  if (!mDiagnosticsModule ||
135  mDiagnosticsModule->error("Runnable", ex.what(),
136  "Exception while invoking Runnable")) {
137  MIRA_LOG_EXCEPTION(ERROR,ex) << "Exception:\n";
138  }
139  }
140  }
141 
142  F function;
143 
144 };
145 
147 
148 } // namespace
149 
150 #endif
F function
Definition: Runnable.h:142
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Runnable that wraps any function pointer.
Definition: Runnable.h:115
virtual ~IRunnable()
Definition: Runnable.h:76
DiagnosticRunnable(DiagnosticsModulePtr module=NULL)
Definition: Runnable.h:95
Base for all runnable classes that can be signaled when they need to run again.
Definition: Runnable.h:72
Auxiliary logging macros for special entities like exceptions, etc.
virtual void run(DispatcherThread *dispatcher)
Called from dispatcher thread this runnable is attached to, whenever the runnable should be executed...
Definition: Runnable.h:121
std::string message() const MIRA_NOEXCEPT_OR_NOTHROW
Similar to what().
virtual void run(DispatcherThread *dispatcher)=0
Called from dispatcher thread this runnable is attached to, whenever the runnable should be executed...
void setDiagnosticsModule(DiagnosticsModulePtr module)
Definition: Runnable.h:98
boost::shared_ptr< DiagnosticRunnable > DiagnosticRunnablePtr
Definition: Runnable.h:107
DiagnosticsModulePtr mDiagnosticsModule
Definition: Runnable.h:105
Definition: Runnable.h:91
boost::shared_ptr< IRunnable > IRunnablePtr
a runnable pointer
Definition: Runnable.h:63
Base class for exceptions.
Definition: Exception.h:199
Class that can be used whenever you want to have ONE thread where several handlers are assigned to...
Definition: DispatcherThread.h:131
FunctionRunnable(F &&fn, DiagnosticsModulePtr module=NULL)
Definition: Runnable.h:118
#define MIRA_LOG_EXCEPTION(level, ex)
Log the specified exception, including all information that the exception object carries.
Definition: LoggingAux.h:107
bool error(const std::string &category, const std::string &message, const std::string &trText="")
Signal an error in a category.
DiagnosticsModulePtr getDiagnosticsModule()
Definition: Runnable.h:101
Definition: LoggingCore.h:75
Base class for modules that want to use diagnostics and set the current status.
Definition: Status.h:138
Status and status management classes used for diagnostics.