cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsCommandWrite.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  Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet
6  Created on: 2004-04-30
7 
8  (C) Copyright 2004-2014 Johns Hopkins University (JHU), All Rights
9  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 _mtsCommandWrite_h
27 #define _mtsCommandWrite_h
28 
29 
32 
33 
40 template <class _classType, class _argumentType>
42 
43 public:
44  typedef _argumentType ArgumentType;
46 
48  typedef _classType ClassType;
49 
52 
55  typedef void(_classType::*ActionType)(const ArgumentType&);
56 
57 private:
59  inline mtsCommandWrite(const ThisType & CMN_UNUSED(other));
60 
61 protected:
65 
68 
69  template<bool, typename dummy=void>
71  // Default case: ArgumentType not derived from mtsGenericObjectProxy
72  public:
74  {
76  if (data == 0) {
78  }
79  (ClassInst->*Action)(*data);
81  }
82  };
83  template<typename dummy>
84  class ConditionalCast<true, dummy> {
85  // Specialization: ArgumentType is derived from mtsGenericObjectProxy (and thus also from mtsGenericObject)
86  // In this case, we may need to create a temporary Proxy object.
87  public:
89  {
90  // First, check if a Proxy object was passed.
91  const ArgumentType *data = dynamic_cast<const ArgumentType *>(&argument);
92  if (data) {
93  (ClassInst->*Action)(*data);
95  }
96  // If it isn't a Proxy, maybe it is a ProxyRef
97  typedef typename ArgumentType::RefType ArgumentRefType;
98  const ArgumentRefType *dataref = dynamic_cast<const ArgumentRefType *>(&argument);
99  if (!dataref) {
100  CMN_LOG_INIT_ERROR << "Write CallMethod could not cast from " << typeid(argument).name()
101  << " to const " << typeid(ArgumentRefType).name() << std::endl;
103  }
104  // Now, make the call using the temporary
105  ArgumentType temp;
106  temp.Assign(*dataref);
107  (ClassInst->*Action)(temp);
109  }
110  };
111 
112 private:
114  mtsCommandWrite(void): BaseType() {}
115 
116 public:
123  mtsCommandWrite(ActionType action, ClassType * classInstantiation, const std::string & name,
124  const ArgumentType & argumentPrototype):
125  BaseType(name),
126  Action(action),
127  ClassInstantiation(classInstantiation)
128  {
130  }
131 
132 
134  virtual ~mtsCommandWrite() {
135  if (this->ArgumentPrototype) {
136  delete this->ArgumentPrototype;
137  }
138  }
139 
140 
146  mtsBlockingType CMN_UNUSED(blocking)) {
147  if (this->IsEnabled()) {
149  >::CallMethod(ClassInstantiation, Action, argument);
150  }
152  }
153 
155  inline mtsExecutionResult Execute(const ArgumentType & argument,
156  mtsBlockingType CMN_UNUSED(blocking)) {
157  if (this->IsEnabled()) {
158  (ClassInstantiation->*Action)(argument);
160  }
162  }
163 
164  inline mtsExecutionResult Execute(const mtsGenericObject & argument, mtsBlockingType blocking,
165  mtsCommandWriteBase * CMN_UNUSED(finishedEventHandler)) {
166  return Execute(argument, blocking);
167  }
168 
169  /* commented in base class */
170  virtual void ToStream(std::ostream & outputStream) const {
171  outputStream << "mtsCommandWrite: ";
172  if (this->ClassInstantiation) {
173  outputStream << this->Name << "(const " << this->ArgumentPrototype->Services()->GetName() << "&) using class/object \""
174  << mtsObjectName(this->ClassInstantiation) << "\" currently "
175  << (this->IsEnabled() ? "enabled" : "disabled");
176  } else {
177  outputStream << "Not initialized properly";
178  }
179  }
180 };
181 
182 
183 
184 
185 
186 
187 
188 template <class _classType>
190 
191 public:
193 
195  typedef _classType ClassType;
196 
199 
202  typedef void(_classType::*ActionType)(const mtsGenericObject &);
203 
204 private:
206  inline mtsCommandWriteGeneric(const ThisType & CMN_UNUSED(other));
207 
208 protected:
212 
215 
216 private:
218  mtsCommandWriteGeneric(void): BaseType() {}
219 
220 public:
227  mtsCommandWriteGeneric(ActionType action, ClassType * classInstantiation, const std::string & name,
228  const mtsGenericObject * argumentPrototype):
229  BaseType(name),
230  Action(action),
231  ClassInstantiation(classInstantiation)
232  {
233  // use dynamic creation with copy constructor if argument prototype has been provided
234  if (argumentPrototype) {
235  cmnGenericObject * prototypePointer = argumentPrototype->Services()->Create(*argumentPrototype);
236  CMN_ASSERT(prototypePointer);
237  this->ArgumentPrototype = dynamic_cast<mtsGenericObject *>(prototypePointer);
239  } else {
240  this->ArgumentPrototype = 0;
241  }
242  }
243 
244 
247  if (this->ArgumentPrototype) {
248  delete this->ArgumentPrototype;
249  }
250  }
251 
252 
258  mtsBlockingType CMN_UNUSED(blocking)) {
259  if (this->IsEnabled()) {
260  (ClassInstantiation->*Action)(argument);
262  }
264  }
265 
266  inline mtsExecutionResult Execute(const mtsGenericObject & argument, mtsBlockingType blocking,
267  mtsCommandWriteBase * CMN_UNUSED(finishedEventHandler)) {
268  return Execute(argument, blocking);
269  }
270 
271  /* documented in base class */
272  void ToStream(std::ostream & outputStream) const {
273  outputStream << "mtsCommandWriteGeneric: ";
274  if (this->ClassInstantiation) {
275  outputStream << this->Name << "(const mtsGenericObject *) using class/object \""
276  << mtsObjectName(this->ClassInstantiation) << "\" currently "
277  << (this->IsEnabled() ? "enabled" : "disabled");
278  } else {
279  outputStream << "Not initialized properly";
280  }
281  }
282 };
283 
284 
285 #endif // _mtsCommandWrite_h
ActionType Action
Definition: mtsCommandWrite.h:64
std::string mtsObjectName(const mtsComponent *object)
Definition: mtsComponent.h:552
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
bool IsEnabled(void) const
Definition: mtsCommandBase.h:105
mtsCommandWrite< ClassType, ArgumentType > ThisType
Definition: mtsCommandWrite.h:51
mtsCommandWriteBase BaseType
Definition: mtsCommandWrite.h:45
ClassType * ClassInstantiation
Definition: mtsCommandWrite.h:214
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
virtual ~mtsCommandWrite()
Definition: mtsCommandWrite.h:134
Definition: mtsCommandWrite.h:189
const std::string & GetName(void) const
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType CMN_UNUSED(blocking))
Definition: mtsCommandWrite.h:145
Definition: mtsExecutionResult.h:44
mtsCommandWriteGeneric< ClassType > ThisType
Definition: mtsCommandWrite.h:198
Base class for high level objects.
Definition: cmnGenericObject.h:51
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
ActionType Action
Definition: mtsCommandWrite.h:211
virtual const cmnClassServicesBase * Services(void) const =0
Defines a base class for a command with one argument.
Definition: mtsCommandWrite.h:70
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType CMN_UNUSED(blocking))
Definition: mtsCommandWrite.h:257
std::string Name
Definition: mtsCommandBase.h:52
Definition: mtsCommandWriteBase.h:40
const mtsGenericObject * ArgumentPrototype
Definition: mtsCommandWriteBase.h:109
_argumentType ArgumentType
Definition: mtsCommandWrite.h:44
_classType ClassType
Definition: mtsCommandWrite.h:195
void ToStream(std::ostream &outputStream) const
Definition: mtsCommandWrite.h:272
mtsCommandWriteBase BaseType
Definition: mtsCommandWrite.h:192
mtsExecutionResult Execute(const ArgumentType &argument, mtsBlockingType CMN_UNUSED(blocking))
Definition: mtsCommandWrite.h:155
static T * CastArg(mtsGenericObject &arg)
Definition: mtsGenericObjectProxy.h:683
void(_classType::* ActionType)(const mtsGenericObject &)
Definition: mtsCommandWrite.h:202
static mtsExecutionResult CallMethod(ClassType *ClassInst, ActionType Action, const mtsGenericObject &argument)
Definition: mtsCommandWrite.h:73
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType blocking, mtsCommandWriteBase *CMN_UNUSED(finishedEventHandler))
Definition: mtsCommandWrite.h:266
Definition: mtsExecutionResult.h:34
Definition: mtsExecutionResult.h:44
ClassType * ClassInstantiation
Definition: mtsCommandWrite.h:67
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType blocking, mtsCommandWriteBase *CMN_UNUSED(finishedEventHandler))
Definition: mtsCommandWrite.h:164
mtsCommandWriteGeneric(ActionType action, ClassType *classInstantiation, const std::string &name, const mtsGenericObject *argumentPrototype)
Definition: mtsCommandWrite.h:227
void(_classType::* ActionType)(const ArgumentType &)
Definition: mtsCommandWrite.h:55
virtual ~mtsCommandWriteGeneric()
Definition: mtsCommandWrite.h:246
Definition: mtsCommandWrite.h:41
mtsBlockingType
Definition: mtsForwardDeclarations.h:55
Definition: mtsExecutionResult.h:44
_classType ClassType
Definition: mtsCommandWrite.h:48
virtual void ToStream(std::ostream &outputStream) const
Definition: mtsCommandWrite.h:170
virtual cmnGenericObject * Create(void) const =0
static mtsGenericObject * ConditionalCreate(const T &arg, const std::string &name)
Definition: mtsGenericObjectProxy.h:676
mtsCommandWrite(ActionType action, ClassType *classInstantiation, const std::string &name, const ArgumentType &argumentPrototype)
Definition: mtsCommandWrite.h:123
static mtsExecutionResult CallMethod(ClassType *ClassInst, ActionType Action, const mtsGenericObject &argument)
Definition: mtsCommandWrite.h:88