File size: 10,245 Bytes
68ce2fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
/*
Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet
Created on: 2004-04-30
(C) Copyright 2004-2014 Johns Hopkins University (JHU), All Rights
Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/
/*!
\file
\brief Defines a command with one argument
*/
#ifndef _mtsCommandWrite_h
#define _mtsCommandWrite_h
#include <cisstMultiTask/mtsCommandWriteBase.h>
#include <cisstMultiTask/mtsGenericObjectProxy.h>
/*!
\ingroup cisstMultiTask
A templated version of command object with one argument for
execute. The template argument is the interface type whose method is
contained in the command object. */
template <class _classType, class _argumentType>
class mtsCommandWrite: public mtsCommandWriteBase {
public:
typedef _argumentType ArgumentType;
typedef mtsCommandWriteBase BaseType;
/*! Typedef for the specific interface. */
typedef _classType ClassType;
/*! This type. */
typedef mtsCommandWrite<ClassType, ArgumentType> ThisType;
/*! Typedef for pointer to member function of the specific interface
class. */
typedef void(_classType::*ActionType)(const ArgumentType&);
private:
/*! Private copy constructor to prevent copies */
inline mtsCommandWrite(const ThisType & CMN_UNUSED(other));
protected:
/*! The pointer to member function of the receiver class that
is to be invoked for a particular instance of the command*/
ActionType Action;
/*! Stores the receiver object of the command */
ClassType * ClassInstantiation;
template<bool, typename dummy=void>
class ConditionalCast {
// Default case: ArgumentType not derived from mtsGenericObjectProxy
public:
static mtsExecutionResult CallMethod(ClassType *ClassInst, ActionType Action, const mtsGenericObject &argument)
{
const ArgumentType * data = mtsGenericTypes<ArgumentType>::CastArg(argument);
if (data == 0) {
return mtsExecutionResult::INVALID_INPUT_TYPE;
}
(ClassInst->*Action)(*data);
return mtsExecutionResult::COMMAND_SUCCEEDED;
}
};
template<typename dummy>
class ConditionalCast<true, dummy> {
// Specialization: ArgumentType is derived from mtsGenericObjectProxy (and thus also from mtsGenericObject)
// In this case, we may need to create a temporary Proxy object.
public:
static mtsExecutionResult CallMethod(ClassType *ClassInst, ActionType Action, const mtsGenericObject &argument)
{
// First, check if a Proxy object was passed.
const ArgumentType *data = dynamic_cast<const ArgumentType *>(&argument);
if (data) {
(ClassInst->*Action)(*data);
return mtsExecutionResult::COMMAND_SUCCEEDED;
}
// If it isn't a Proxy, maybe it is a ProxyRef
typedef typename ArgumentType::RefType ArgumentRefType;
const ArgumentRefType *dataref = dynamic_cast<const ArgumentRefType *>(&argument);
if (!dataref) {
CMN_LOG_INIT_ERROR << "Write CallMethod could not cast from " << typeid(argument).name()
<< " to const " << typeid(ArgumentRefType).name() << std::endl;
return mtsExecutionResult::INVALID_INPUT_TYPE;
}
// Now, make the call using the temporary
ArgumentType temp;
temp.Assign(*dataref);
(ClassInst->*Action)(temp);
return mtsExecutionResult::COMMAND_SUCCEEDED;
}
};
private:
/*! The constructor. Does nothing */
mtsCommandWrite(void): BaseType() {}
public:
/*! The constructor.
\param action Pointer to the member function that is to be called
by the invoker of the command
\param classInstantiation Pointer to the receiver of the command
\param name A string to identify the command. */
mtsCommandWrite(ActionType action, ClassType * classInstantiation, const std::string & name,
const ArgumentType & argumentPrototype):
BaseType(name),
Action(action),
ClassInstantiation(classInstantiation)
{
this->ArgumentPrototype = mtsGenericTypes<ArgumentType>::ConditionalCreate(argumentPrototype, name);
}
/*! The destructor. Does nothing */
virtual ~mtsCommandWrite() {
if (this->ArgumentPrototype) {
delete this->ArgumentPrototype;
}
}
/*! The execute method. Calling the execute method from the invoker
applies the operation on the receiver.
\param obj The data passed to the operation method
*/
mtsExecutionResult Execute(const mtsGenericObject & argument,
mtsBlockingType CMN_UNUSED(blocking)) {
if (this->IsEnabled()) {
return ConditionalCast<cmnIsDerivedFromTemplated<ArgumentType, mtsGenericObjectProxy>::IS_DERIVED
>::CallMethod(ClassInstantiation, Action, argument);
}
return mtsExecutionResult::COMMAND_DISABLED;
}
/*! Direct execute can be used for mtsMulticastCommandWrite */
inline mtsExecutionResult Execute(const ArgumentType & argument,
mtsBlockingType CMN_UNUSED(blocking)) {
if (this->IsEnabled()) {
(ClassInstantiation->*Action)(argument);
return mtsExecutionResult::COMMAND_SUCCEEDED;
}
return mtsExecutionResult::COMMAND_DISABLED;
}
inline mtsExecutionResult Execute(const mtsGenericObject & argument, mtsBlockingType blocking,
mtsCommandWriteBase * CMN_UNUSED(finishedEventHandler)) {
return Execute(argument, blocking);
}
/* commented in base class */
virtual void ToStream(std::ostream & outputStream) const {
outputStream << "mtsCommandWrite: ";
if (this->ClassInstantiation) {
outputStream << this->Name << "(const " << this->ArgumentPrototype->Services()->GetName() << "&) using class/object \""
<< mtsObjectName(this->ClassInstantiation) << "\" currently "
<< (this->IsEnabled() ? "enabled" : "disabled");
} else {
outputStream << "Not initialized properly";
}
}
};
template <class _classType>
class mtsCommandWriteGeneric: public mtsCommandWriteBase {
public:
typedef mtsCommandWriteBase BaseType;
/*! Typedef for the specific interface. */
typedef _classType ClassType;
/*! This type. */
typedef mtsCommandWriteGeneric<ClassType> ThisType;
/*! Typedef for pointer to member function of the specific interface
class. */
typedef void(_classType::*ActionType)(const mtsGenericObject &);
private:
/*! Private copy constructor to prevent copies */
inline mtsCommandWriteGeneric(const ThisType & CMN_UNUSED(other));
protected:
/*! The pointer to member function of the receiver class that
is to be invoked for a particular instance of the command*/
ActionType Action;
/*! Stores the receiver object of the command */
ClassType * ClassInstantiation;
private:
/*! The constructor. Does nothing */
mtsCommandWriteGeneric(void): BaseType() {}
public:
/*! The constructor.
\param action Pointer to the member function that is to be called
by the invoker of the command
\param classInstantiation Pointer to the receiver of the command
\param name A string to identify the command. */
mtsCommandWriteGeneric(ActionType action, ClassType * classInstantiation, const std::string & name,
const mtsGenericObject * argumentPrototype):
BaseType(name),
Action(action),
ClassInstantiation(classInstantiation)
{
// use dynamic creation with copy constructor if argument prototype has been provided
if (argumentPrototype) {
cmnGenericObject * prototypePointer = argumentPrototype->Services()->Create(*argumentPrototype);
CMN_ASSERT(prototypePointer);
this->ArgumentPrototype = dynamic_cast<mtsGenericObject *>(prototypePointer);
CMN_ASSERT(this->ArgumentPrototype);
} else {
this->ArgumentPrototype = 0;
}
}
/*! The destructor. Does nothing */
virtual ~mtsCommandWriteGeneric() {
if (this->ArgumentPrototype) {
delete this->ArgumentPrototype;
}
}
/*! The execute method. Calling the execute method from the invoker
applies the operation on the receiver.
\param obj The data passed to the operation method
*/
mtsExecutionResult Execute(const mtsGenericObject & argument,
mtsBlockingType CMN_UNUSED(blocking)) {
if (this->IsEnabled()) {
(ClassInstantiation->*Action)(argument);
return mtsExecutionResult::COMMAND_SUCCEEDED;
}
return mtsExecutionResult::COMMAND_DISABLED;
}
inline mtsExecutionResult Execute(const mtsGenericObject & argument, mtsBlockingType blocking,
mtsCommandWriteBase * CMN_UNUSED(finishedEventHandler)) {
return Execute(argument, blocking);
}
/* documented in base class */
void ToStream(std::ostream & outputStream) const {
outputStream << "mtsCommandWriteGeneric: ";
if (this->ClassInstantiation) {
outputStream << this->Name << "(const mtsGenericObject *) using class/object \""
<< mtsObjectName(this->ClassInstantiation) << "\" currently "
<< (this->IsEnabled() ? "enabled" : "disabled");
} else {
outputStream << "Not initialized properly";
}
}
};
#endif // _mtsCommandWrite_h
|