cisst-saw
Loading...
Searching...
No Matches
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
13This software is provided "as is" under an open source license, with
14no warranty. The complete license can be found in license.txt and
15http://www.cisst.org/cisst/license.txt.
16
17--- end cisst license ---
18*/
19
20
25
26#ifndef _mtsCommandWrite_h
27#define _mtsCommandWrite_h
28
29
32
33
40template <class _classType, class _argumentType>
41class mtsCommandWrite: public mtsCommandWriteBase {
42
43public:
44 typedef _argumentType ArgumentType;
46
48 typedef _classType ClassType;
49
51 typedef mtsCommandWrite<ClassType, ArgumentType> ThisType;
52
55 typedef void(_classType::*ActionType)(const ArgumentType&);
56
57private:
59 inline mtsCommandWrite(const ThisType & CMN_UNUSED(other));
60
61protected:
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
112private:
114 mtsCommandWrite(void): BaseType() {}
115
116public:
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
135 if (this->ArgumentPrototype) {
136 delete this->ArgumentPrototype;
137 }
138 }
139
140
146 mtsBlockingType CMN_UNUSED(blocking)) {
147 if (this->IsEnabled()) {
148 return ConditionalCast<cmnIsDerivedFromTemplated<ArgumentType, mtsGenericObjectProxy>::IS_DERIVED
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
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
188template <class _classType>
189class mtsCommandWriteGeneric: public mtsCommandWriteBase {
190
191public:
193
195 typedef _classType ClassType;
196
198 typedef mtsCommandWriteGeneric<ClassType> ThisType;
199
202 typedef void(_classType::*ActionType)(const mtsGenericObject &);
203
204private:
206 inline mtsCommandWriteGeneric(const ThisType & CMN_UNUSED(other));
207
208protected:
212
215
216private:
218 mtsCommandWriteGeneric(void): BaseType() {}
219
220public:
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
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
const std::string & GetName(void) const
virtual cmnGenericObject * Create(void) const =0
Base class for high level objects.
Definition cmnGenericObject.h:53
virtual const cmnClassServicesBase * Services(void) const =0
std::string Name
Definition mtsCommandBase.h:52
bool IsEnabled(void) const
Definition mtsCommandBase.h:105
static mtsExecutionResult CallMethod(ClassType *ClassInst, ActionType Action, const mtsGenericObject &argument)
Definition mtsCommandWrite.h:88
Definition mtsCommandWrite.h:70
static mtsExecutionResult CallMethod(ClassType *ClassInst, ActionType Action, const mtsGenericObject &argument)
Definition mtsCommandWrite.h:73
mtsCommandWriteBase(void)
Definition mtsCommandWriteBase.h:49
const mtsGenericObject * ArgumentPrototype
Definition mtsCommandWriteBase.h:109
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType CMN_UNUSED(blocking))
Definition mtsCommandWrite.h:257
void(ClassType::*) ActionType(const mtsGenericObject &)
Definition mtsCommandWrite.h:202
void ToStream(std::ostream &outputStream) const
Definition mtsCommandWrite.h:272
mtsCommandWriteGeneric(ActionType action, ClassType *classInstantiation, const std::string &name, const mtsGenericObject *argumentPrototype)
Definition mtsCommandWrite.h:227
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType blocking, mtsCommandWriteBase *CMN_UNUSED(finishedEventHandler))
Definition mtsCommandWrite.h:266
virtual ~mtsCommandWriteGeneric()
Definition mtsCommandWrite.h:246
ClassType * ClassInstantiation
Definition mtsCommandWrite.h:214
mtsCommandWriteBase BaseType
Definition mtsCommandWrite.h:192
_classType ClassType
Definition mtsCommandWrite.h:195
ActionType Action
Definition mtsCommandWrite.h:211
mtsCommandWriteGeneric< ClassType > ThisType
Definition mtsCommandWrite.h:198
Definition mtsCommandWrite.h:41
void(ClassType::*) ActionType(const ArgumentType &)
Definition mtsCommandWrite.h:55
virtual void ToStream(std::ostream &outputStream) const
Definition mtsCommandWrite.h:170
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType blocking, mtsCommandWriteBase *CMN_UNUSED(finishedEventHandler))
Definition mtsCommandWrite.h:164
mtsExecutionResult Execute(const ArgumentType &argument, mtsBlockingType CMN_UNUSED(blocking))
Definition mtsCommandWrite.h:155
mtsCommandWriteBase BaseType
Definition mtsCommandWrite.h:45
mtsCommandWrite< ClassType, ArgumentType > ThisType
Definition mtsCommandWrite.h:51
_argumentType ArgumentType
Definition mtsCommandWrite.h:44
virtual ~mtsCommandWrite()
Definition mtsCommandWrite.h:134
ClassType * ClassInstantiation
Definition mtsCommandWrite.h:67
mtsCommandWrite(ActionType action, ClassType *classInstantiation, const std::string &name, const ArgumentType &argumentPrototype)
Definition mtsCommandWrite.h:123
_classType ClassType
Definition mtsCommandWrite.h:48
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsBlockingType CMN_UNUSED(blocking))
Definition mtsCommandWrite.h:145
ActionType Action
Definition mtsCommandWrite.h:64
Definition mtsExecutionResult.h:34
@ INVALID_INPUT_TYPE
Definition mtsExecutionResult.h:44
@ COMMAND_SUCCEEDED
Definition mtsExecutionResult.h:44
@ COMMAND_DISABLED
Definition mtsExecutionResult.h:44
Base class for data object in cisstMultiTask.
Definition mtsGenericObject.h:52
static T * CastArg(mtsGenericObject &arg)
Definition mtsGenericObjectProxy.h:700
static mtsGenericObject * ConditionalCreate(const T &arg, const std::string &name)
Definition mtsGenericObjectProxy.h:691
#define CMN_ASSERT(expr)
Definition cmnAssert.h:97
#define CMN_LOG_INIT_ERROR
Definition cmnLogger.h:162
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
Defines a base class for a command with one argument.
std::string mtsObjectName(const mtsComponent *object)
Definition mtsComponent.h:588
mtsBlockingType
Definition mtsForwardDeclarations.h:54
vctDynamicNArrayBase< vctDynamicNArrayOwner< _elementType, _dimension >, _elementType, _dimension > BaseType
Definition vctDynamicNArray.h:133