cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsCallableQualifiedReadMethod.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-2014 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 _mtsCallableQualifiedReadMethod_h
27 #define _mtsCallableQualifiedReadMethod_h
28 
29 
32 
33 #include <string>
34 
35 
39 template <class _classType, class _argumentType, class _resultType>
41 
42 public:
44  typedef _argumentType ArgumentType;
45  typedef _resultType ResultType;
46 
48  typedef _classType ClassType;
49 
52 
55  typedef bool(_classType::*ActionType)(const ArgumentType & argument, ResultType & result) const;
56 
57 private:
59  inline mtsCallableQualifiedReadMethod(const ThisType & CMN_UNUSED(other)) {}
60 
61 protected:
65 
68 
69  template <bool a, bool b, typename dummy = void>
71  public:
72  static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action,
73  const mtsGenericObject & argument, mtsGenericObject & result)
74  {
75  const ArgumentType * data1 = mtsGenericTypes<ArgumentType>::CastArg(argument);
76  if (data1 == 0) {
78  }
80  if (data2 == 0) {
82  }
83  if ( (classInstantiation->*action)(*data1, *data2) ) {
85  }
87  }
88  };
89 
90  template <typename dummy>
91  class ConditionalCast<true, false, dummy> {
92  public:
93  static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action,
94  const mtsGenericObject & argument, mtsGenericObject & result)
95  {
96  // First, check if a Proxy object was passed.
97  ArgumentType temp1; // in case needed
98  const ArgumentType * data1 = dynamic_cast<const ArgumentType *>(&argument);
99  // If it isn't a Proxy, maybe it is a ProxyRef
100  if (!data1) {
101  typedef typename ArgumentType::RefType ArgumentRefType;
102  const ArgumentRefType * data1ref = dynamic_cast<const ArgumentRefType *>(&argument);
103  if (!data1ref) {
104  CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast argument from " << typeid(argument).name()
105  << " to const " << typeid(ArgumentRefType).name() << std::endl;
107  }
108  temp1 = *data1ref;
109  data1 = &temp1;
110  }
112  if (data2 == 0) {
114  }
115  if ( (classInstantiation->*action)(*data1, *data2) ) {
117  }
119  }
120  };
121 
122  template <typename dummy>
123  class ConditionalCast<false, true, dummy> {
124  public:
125  static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action,
126  const mtsGenericObject & argument, mtsGenericObject & result)
127  {
128  const ArgumentType * data1 = mtsGenericTypes<ArgumentType>::CastArg(argument);
129  if (data1 == 0) {
131  }
132  // First, check if a Proxy object was passed.
133  ResultType temp2; // in case needed
134  ResultType * data2 = dynamic_cast<ResultType *>(&result);
135  // If it isn't a Proxy, maybe it is a ProxyRef
136  typedef typename ResultType::RefType ResultRefType;
137  ResultRefType * data2ref = 0;
138  if (!data2) {
139  data2ref = dynamic_cast<ResultRefType *>(&result);
140  if (!data2ref) {
141  CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast result from " << typeid(result).name()
142  << " to const " << typeid(ResultRefType).name() << std::endl;
144  }
145  data2 = &temp2;
146  }
147  if ( (classInstantiation->*action)(*data1, *data2) ) {
148  if (data2ref) {
149  *data2ref = *data2;
150  }
152  }
154  }
155  };
156 
157  template <typename dummy>
158  class ConditionalCast<true, true, dummy> {
159  public:
160  static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action,
161  const mtsGenericObject & argument, mtsGenericObject & result)
162  {
163  // First, check if a Proxy object was passed.
164  ArgumentType temp1; // in case needed
165  const ArgumentType * data1 = dynamic_cast<const ArgumentType *>(&argument);
166  // If it isn't a Proxy, maybe it is a ProxyRef
167  if (!data1) {
168  typedef typename ArgumentType::RefType ArgumentRefType;
169  const ArgumentRefType * data1ref = dynamic_cast<const ArgumentRefType *>(&argument);
170  if (!data1ref) {
171  CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast argument from " << typeid(argument).name()
172  << " to const " << typeid(ArgumentRefType).name() << std::endl;
174  }
175  temp1 = *data1ref;
176  data1 = &temp1;
177  }
178  // First, check if a Proxy object was passed.
179  ResultType temp2; // in case needed
180  ResultType * data2 = dynamic_cast<ResultType *>(&result);
181  // If it isn't a Proxy, maybe it is a ProxyRef
182  typedef typename ResultType::RefType ResultRefType;
183  ResultRefType * data2ref = 0;
184  if (!data2) {
185  data2ref = dynamic_cast<ResultRefType *>(&result);
186  if (!data2ref) {
187  CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast result from " << typeid(result).name()
188  << " to const " << typeid(ResultRefType).name() << std::endl;
190  }
191  data2 = &temp2;
192  }
193  if ( (classInstantiation->*action)(*data1, *data2) ) {
194  // Finally, copy the data to the argument
195  if (data2ref) {
196  *data2ref = temp2;
197  }
199  }
201  }
202  };
203 
204 public:
207 
213  mtsCallableQualifiedReadMethod(ActionType action, ClassType * classInstantiation):
214  BaseType(),
215  Action(action),
216  ClassInstantiation(classInstantiation)
217  {}
218 
221 
222  /* documented in base class */
226  ::CallMethod(ClassInstantiation, Action, argument, result);
227  }
228 
229  /* documented in base class */
230  void ToStream(std::ostream & outputStream) const {
231  if (this->ClassInstantiation) {
232  outputStream << "method based callable qualified read object using class/object \""
233  << mtsObjectName(this->ClassInstantiation) << "\"";
234  } else {
235  outputStream << "invalid method based callable object";
236  }
237  }
238 };
239 
240 
241 
242 
243 template <class _classType>
245 
246 public:
248 
250  typedef _classType ClassType;
251 
254 
257  typedef bool(_classType::*ActionType)(const mtsGenericObject & argument, mtsGenericObject & result) const;
258 
259 private:
262 
263 protected:
267 
270 
271 public:
274 
281  BaseType(),
282  Action(action),
283  ClassInstantiation(classInstantiation)
284  {}
285 
288 
289  /* documented in base class */
291  if ((ClassInstantiation->*Action)(argument, result) ) {
293  }
295  }
296 
297  /* documented in base class */
298  void ToStream(std::ostream & outputStream) const {
299  if (this->ClassInstantiation) {
300  outputStream << "generic method based callable qualified read object using class/object \""
301  << mtsObjectName(this->ClassInstantiation) << "\"";
302  } else {
303  outputStream << "invalid generic method based callable object";
304  }
305  }
306 };
307 
308 #endif // _mtsCallableQualifiedReadMethod_h
std::string mtsObjectName(const mtsComponent *object)
Definition: mtsComponent.h:552
mtsCallableQualifiedReadMethodGeneric< ClassType > ThisType
Definition: mtsCallableQualifiedReadMethod.h:253
Definition: mtsCallableQualifiedReadMethod.h:244
Definition: mtsCallableQualifiedReadBase.h:40
Definition: cmnTypeTraits.h:590
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
ClassType * ClassInstantiation
Definition: mtsCallableQualifiedReadMethod.h:269
virtual ~mtsCallableQualifiedReadMethod()
Definition: mtsCallableQualifiedReadMethod.h:220
virtual ~mtsCallableQualifiedReadMethodGeneric()
Definition: mtsCallableQualifiedReadMethod.h:287
bool(_classType::* ActionType)(const ArgumentType &argument, ResultType &result) const
Definition: mtsCallableQualifiedReadMethod.h:55
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, const mtsGenericObject &argument, mtsGenericObject &result)
Definition: mtsCallableQualifiedReadMethod.h:93
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsGenericObject &result)
Definition: mtsCallableQualifiedReadMethod.h:290
Definition: mtsExecutionResult.h:44
ClassType * ClassInstantiation
Definition: mtsCallableQualifiedReadMethod.h:67
void ToStream(std::ostream &outputStream) const
Definition: mtsCallableQualifiedReadMethod.h:298
void ToStream(std::ostream &outputStream) const
Definition: mtsCallableQualifiedReadMethod.h:230
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, const mtsGenericObject &argument, mtsGenericObject &result)
Definition: mtsCallableQualifiedReadMethod.h:160
Defines a base class for a callable object with a qualifer and a placeholder to retrieve data...
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, const mtsGenericObject &argument, mtsGenericObject &result)
Definition: mtsCallableQualifiedReadMethod.h:72
static T * CastArg(mtsGenericObject &arg)
Definition: mtsGenericObjectProxy.h:683
_resultType ResultType
Definition: mtsCallableQualifiedReadMethod.h:45
mtsCallableQualifiedReadBase BaseType
Definition: mtsCallableQualifiedReadMethod.h:43
_argumentType ArgumentType
Definition: mtsCallableQualifiedReadMethod.h:44
mtsCallableQualifiedReadMethodGeneric(ActionType action, ClassType *classInstantiation)
Definition: mtsCallableQualifiedReadMethod.h:280
Definition: mtsExecutionResult.h:34
Definition: mtsExecutionResult.h:44
ActionType Action
Definition: mtsCallableQualifiedReadMethod.h:64
mtsCallableQualifiedReadBase BaseType
Definition: mtsCallableQualifiedReadMethod.h:247
bool(_classType::* ActionType)(const mtsGenericObject &argument, mtsGenericObject &result) const
Definition: mtsCallableQualifiedReadMethod.h:257
mtsExecutionResult Execute(const mtsGenericObject &argument, mtsGenericObject &result)
Definition: mtsCallableQualifiedReadMethod.h:223
mtsCallableQualifiedReadMethodGeneric(void)
Definition: mtsCallableQualifiedReadMethod.h:273
_classType ClassType
Definition: mtsCallableQualifiedReadMethod.h:48
ActionType Action
Definition: mtsCallableQualifiedReadMethod.h:266
mtsCallableQualifiedReadMethod(ActionType action, ClassType *classInstantiation)
Definition: mtsCallableQualifiedReadMethod.h:213
Definition: mtsCallableQualifiedReadMethod.h:40
_classType ClassType
Definition: mtsCallableQualifiedReadMethod.h:250
Definition: mtsExecutionResult.h:44
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, const mtsGenericObject &argument, mtsGenericObject &result)
Definition: mtsCallableQualifiedReadMethod.h:125
Definition: mtsCallableQualifiedReadMethod.h:70
mtsCallableQualifiedReadMethod< ClassType, ArgumentType, ResultType > ThisType
Definition: mtsCallableQualifiedReadMethod.h:51
mtsCallableQualifiedReadMethod(void)
Definition: mtsCallableQualifiedReadMethod.h:206