cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsTaskFromCallback.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Peter Kazanzides
7  Created on: 2008-09-10
8 
9  (C) Copyright 2008-2012 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 */
19 
20 
26 #ifndef _mtsTaskFromCallback_h
27 #define _mtsTaskFromCallback_h
28 
30 #include <cisstMultiTask/mtsTask.h>
31 
32 // Always include last
34 
35 #ifndef MTS_TASK_CALLBACK_CONVENTION
36 #define MTS_TASK_CALLBACK_CONVENTION
37 #endif
38 
79 {
81 
82 protected:
83  typedef mtsTask BaseType;
84 
85  /************************ Protected task data *********************/
87 
88  /********************* Methods that call user methods *****************/
89 
92  void *RunInternal(void* argument);
93 
96  void StartupInternal(void);
97 
98 public:
99  /********************* Task constructor and destructor *****************/
100 
112  mtsTaskFromCallback(const std::string & name,
113  unsigned int sizeStateTable = 256):
114  mtsTask(name, sizeStateTable), inRunInternal(false) {}
115 
117  virtual ~mtsTaskFromCallback() {}
118 
119  /********************* Methods to change task status *****************/
120  /* (use Kill method from base class) */
121 
122  /* Create a new thread (if needed). */
123  void Create(void *data = 0);
124 
126  void Start(void);
127 
129  void Suspend(void);
130 
132  void Kill(void);
133 
135  virtual void *DoCallback(void *data = 0) {
136  // The user Run() method should call SetThreadReturnValue.
137  return RunInternal(data);
138  }
139 };
140 
142 
143 
151 // PK: This class should be in a separate file, but I would like to
152 // find a better name first.
153 
155 {
157 
158 protected:
159  typedef mtsTaskFromCallback BaseType;
160 
161  enum SignatureType {INVALID = 0, VALID = 0x3242 };
162  struct CallbackData {
164  mtsTaskFromCallback* obj;
165  CallbackData() : signature(INVALID), obj(0) {}
167  };
169 
170 public:
171 
172  mtsTaskFromCallbackAdapter(const std::string & name,
173  unsigned int sizeStateTable = 256) :
174  mtsTaskFromCallback(name, sizeStateTable),
175  callbackData() { }
176 
178 
188  template <class _callbackReturnType>
189  static _callbackReturnType MTS_TASK_CALLBACK_CONVENTION CallbackAdapter(void *obj);
190 
194  virtual void *GetCallbackParameter() {
195  callbackData.signature = VALID;
196  callbackData.obj = this;
197  return static_cast<void*>(&callbackData);
198  }
199 
200 };
201 
203 
204 template <class _callbackReturnType>
205 _callbackReturnType MTS_TASK_CALLBACK_CONVENTION mtsTaskFromCallbackAdapter::CallbackAdapter(void *obj)
206 {
207  if (!obj) {
208  CMN_LOG_INIT_ERROR << "CallbackAdapter got null pointer" << std::endl;
209  return _callbackReturnType();
210  }
211  CallbackData* _this = static_cast<CallbackData*>(obj);
212  if (_this->signature != VALID) {
213  CMN_LOG_INIT_ERROR << "CallbackAdapter received invalid signature: " << _this->signature << std::endl;
214  return _callbackReturnType();
215  }
216  void *ret =_this->obj->DoCallback(0);
217  // Following won't compile if _callbackReturnType is 'void'.
218  // TBD: workaround using a type traits approach.
219  return *static_cast<_callbackReturnType *>(ret);
220 }
221 
222 
223 #endif // _mtsTaskFromCallback_h
224 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
mtsTask BaseType
Definition: mtsTaskFromCallback.h:83
Portability across compilers and operating systems tools.
virtual void * GetCallbackParameter()
Definition: mtsTaskFromCallback.h:194
Definition: mtsTaskFromCallback.h:162
bool inRunInternal
Definition: mtsTaskFromCallback.h:86
CallbackData()
Definition: mtsTaskFromCallback.h:165
Definition: mtsTaskFromCallback.h:78
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
~CallbackData()
Definition: mtsTaskFromCallback.h:166
SignatureType
Definition: mtsTaskFromCallback.h:161
virtual void Suspend(void)
mtsTaskFromCallback * obj
Definition: mtsTaskFromCallback.h:164
virtual ~mtsTaskFromCallback()
Definition: mtsTaskFromCallback.h:117
Definition: mtsTask.h:60
void Kill(void)
void Create(void)
Definition: mtsTask.h:228
mtsTaskFromCallback BaseType
Definition: mtsTaskFromCallback.h:159
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
SignatureType signature
Definition: mtsTaskFromCallback.h:163
Definition: mtsTaskFromCallback.h:154
Defines a periodic task.
mtsTaskFromCallbackAdapter(const std::string &name, unsigned int sizeStateTable=256)
Definition: mtsTaskFromCallback.h:172
~mtsTaskFromCallbackAdapter()
Definition: mtsTaskFromCallback.h:177
virtual void * DoCallback(void *data=0)
Definition: mtsTaskFromCallback.h:135
mtsTaskFromCallback(const std::string &name, unsigned int sizeStateTable=256)
Definition: mtsTaskFromCallback.h:112
Rules of exporting.
virtual void * RunInternal(void *argument)=0
CallbackData callbackData
Definition: mtsTaskFromCallback.h:168
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
#define MTS_TASK_CALLBACK_CONVENTION
Definition: mtsTaskFromCallback.h:36
virtual void StartupInternal(void)
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition: cmnClassRegisterMacros.h:202
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
virtual void Start(void)