cisst-saw
Loading...
Searching...
No Matches
mtsCallableReadMethod.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): Anton Deguet
7 Created on: 2010-09-16
8
9 (C) Copyright 2010 Johns Hopkins University (JHU), All Rights
10 Reserved.
11
12--- begin cisst license - do not edit ---
13
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://www.cisst.org/cisst/license.txt.
17
18--- end cisst license ---
19*/
20
21
26
27#ifndef _mtsCallableReadMethod_h
28#define _mtsCallableReadMethod_h
29
30
33
34#include <string>
35
36
40template <class _classType, class _resultType>
41class mtsCallableReadMethod: public mtsCallableReadBase {
42
43public:
45 typedef _resultType ResultType;
46
48 typedef _classType ClassType;
49
51 typedef mtsCallableReadMethod<ClassType, ResultType> ThisType;
52
55 typedef bool(_classType::*ActionType)(ResultType & argument) const;
56
57private:
59 inline mtsCallableReadMethod(const ThisType & CMN_UNUSED(other)) {}
60
61protected:
65
68
69 template <bool, typename _dummy = void>
71 // Default case: ResultType not derived from mtsGenericObjectProxy
72 public:
73 static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action, mtsGenericObject & argument) {
74 ResultType * argumentCasted = mtsGenericTypes<ResultType>::CastArg(argument);
75 if (argumentCasted == 0) {
77 }
78 if ( (classInstantiation->*action)(*argumentCasted) ) {
80 }
82 }
83 };
84
85 template <typename _dummy>
86 class ConditionalCast<true, _dummy> {
87 // Specialization: ResultType is derived from mtsGenericObjectProxy (and thus also from mtsGenericObject)
88 // In this case, we may need to create a temporary Proxy object.
89 public:
90 static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action, mtsGenericObject & argument) {
91 // First, check if a Proxy object was passed.
92 ResultType * argumentCasted = dynamic_cast<ResultType *>(&argument);
93 if (argumentCasted) {
94 if ( (classInstantiation->*action)(*argumentCasted) ) {
96 }
98 }
99 // If it isn't a Proxy, maybe it is a ProxyRef
100 typedef typename ResultType::RefType ResultRefType;
101 ResultRefType * dataRef = dynamic_cast<ResultRefType *>(&argument);
102 if (!dataRef) {
103 CMN_LOG_INIT_ERROR << "mtsCallableRead for " << typeid(ClassType).name()
104 << ": CallMethod could not cast from " << typeid(argument).name()
105 << " to " << typeid(ResultRefType).name()
106 << ", arg type is " << argument.Services()->GetName() << std::endl;
108 }
109 // Now, make the call using the temporary
110 ResultType temp;
111 if ( (classInstantiation->*action)(temp) ) {
112 // Finally, copy the data to the return
113 *dataRef = temp;
115 }
116 // Copy result anyway
117 *dataRef = temp;
119 }
120 };
121
122public:
125
131 mtsCallableReadMethod(ActionType action, ClassType * classInstantiation):
132 BaseType(),
133 Action(action),
134 ClassInstantiation(classInstantiation)
135 {}
136
139
140 /* documented in base class */
142 return ConditionalCast<cmnIsDerivedFromTemplated<ResultType, mtsGenericObjectProxy>::IS_DERIVED>
143 ::CallMethod(ClassInstantiation, Action, argument);
144 }
145
146 /* documented in base class */
147 void ToStream(std::ostream & outputStream) const {
148 if (this->ClassInstantiation) {
149 outputStream << "method based callable void return object using class/object \""
150 << mtsObjectName(this->ClassInstantiation) << "\"";
151 } else {
152 outputStream << "invalid method based callable object";
153 }
154 }
155};
156
157template <class _classType>
158class mtsCallableReadMethodGeneric: public mtsCallableReadBase {
159
160public:
162
164 typedef _classType ClassType;
165
167 typedef mtsCallableReadMethodGeneric<ClassType> ThisType;
168
171 typedef bool(_classType::*ActionType)(mtsGenericObject & result) const;
172
173private:
175 inline mtsCallableReadMethodGeneric(const ThisType & CMN_UNUSED(other)) {}
176
177protected:
181
184
185public:
188
194 mtsCallableReadMethodGeneric(ActionType action, ClassType * classInstantiation):
195 BaseType(),
196 Action(action),
197 ClassInstantiation(classInstantiation)
198 {}
199
202
203 /* documented in base class */
210
211 /* documented in base class */
212 void ToStream(std::ostream & outputStream) const {
213 if (this->ClassInstantiation) {
214 outputStream << "method based callable read object using class/object \""
215 << mtsObjectName(this->ClassInstantiation) << "\"";
216 } else {
217 outputStream << "invalid method based callable object";
218 }
219 }
220};
221
222#endif // _mtsCallableReadMethod_h
223
const std::string & GetName(void) const
virtual const cmnClassServicesBase * Services(void) const =0
mtsCallableReadBase(void)
Definition mtsCallableReadBase.h:44
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, mtsGenericObject &argument)
Definition mtsCallableReadMethod.h:90
Definition mtsCallableReadMethod.h:70
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, mtsGenericObject &argument)
Definition mtsCallableReadMethod.h:73
bool(ClassType::*) ActionType(mtsGenericObject &result) const
Definition mtsCallableReadMethod.h:171
virtual ~mtsCallableReadMethodGeneric()
Definition mtsCallableReadMethod.h:201
mtsExecutionResult Execute(mtsGenericObject &result)
Definition mtsCallableReadMethod.h:204
void ToStream(std::ostream &outputStream) const
Definition mtsCallableReadMethod.h:212
mtsCallableReadMethodGeneric< ClassType > ThisType
Definition mtsCallableReadMethod.h:167
ActionType Action
Definition mtsCallableReadMethod.h:180
ClassType * ClassInstantiation
Definition mtsCallableReadMethod.h:183
mtsCallableReadMethodGeneric(void)
Definition mtsCallableReadMethod.h:187
mtsCallableReadBase BaseType
Definition mtsCallableReadMethod.h:161
_classType ClassType
Definition mtsCallableReadMethod.h:164
mtsCallableReadMethodGeneric(ActionType action, ClassType *classInstantiation)
Definition mtsCallableReadMethod.h:194
mtsCallableReadMethod(ActionType action, ClassType *classInstantiation)
Definition mtsCallableReadMethod.h:131
_classType ClassType
Definition mtsCallableReadMethod.h:48
virtual ~mtsCallableReadMethod()
Definition mtsCallableReadMethod.h:138
void ToStream(std::ostream &outputStream) const
Definition mtsCallableReadMethod.h:147
mtsExecutionResult Execute(mtsGenericObject &argument)
Definition mtsCallableReadMethod.h:141
mtsCallableReadBase BaseType
Definition mtsCallableReadMethod.h:44
bool(ClassType::*) ActionType(ResultType &argument) const
Definition mtsCallableReadMethod.h:55
mtsCallableReadMethod(void)
Definition mtsCallableReadMethod.h:124
_resultType ResultType
Definition mtsCallableReadMethod.h:45
ClassType * ClassInstantiation
Definition mtsCallableReadMethod.h:67
ActionType Action
Definition mtsCallableReadMethod.h:64
mtsCallableReadMethod< ClassType, ResultType > ThisType
Definition mtsCallableReadMethod.h:51
Definition mtsExecutionResult.h:34
@ METHOD_OR_FUNCTION_FAILED
Definition mtsExecutionResult.h:44
@ INVALID_INPUT_TYPE
Definition mtsExecutionResult.h:44
@ COMMAND_SUCCEEDED
Definition mtsExecutionResult.h:44
Base class for data object in cisstMultiTask.
Definition mtsGenericObject.h:52
static T * CastArg(mtsGenericObject &arg)
Definition mtsGenericObjectProxy.h:700
#define CMN_LOG_INIT_ERROR
Definition cmnLogger.h:162
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
Defines a base class for a callable object with a placeholder to retrieve data.
std::string mtsObjectName(const mtsComponent *object)
Definition mtsComponent.h:588