cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
27 #ifndef _mtsCallableReadMethod_h
28 #define _mtsCallableReadMethod_h
29 
30 
33 
34 #include <string>
35 
36 
40 template <class _classType, class _resultType>
42 
43 public:
45  typedef _resultType ResultType;
46 
48  typedef _classType ClassType;
49 
52 
55  typedef bool(_classType::*ActionType)(ResultType & argument) const;
56 
57 private:
59  inline mtsCallableReadMethod(const ThisType & CMN_UNUSED(other)) {}
60 
61 protected:
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 
122 public:
125 
131  mtsCallableReadMethod(ActionType action, ClassType * classInstantiation):
132  BaseType(),
133  Action(action),
134  ClassInstantiation(classInstantiation)
135  {}
136 
139 
140  /* documented in base class */
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 
157 template <class _classType>
159 
160 public:
162 
164  typedef _classType ClassType;
165 
168 
171  typedef bool(_classType::*ActionType)(mtsGenericObject & result) const;
172 
173 private:
175  inline mtsCallableReadMethodGeneric(const ThisType & CMN_UNUSED(other)) {}
176 
177 protected:
181 
184 
185 public:
188 
194  mtsCallableReadMethodGeneric(ActionType action, ClassType * classInstantiation):
195  BaseType(),
196  Action(action),
197  ClassInstantiation(classInstantiation)
198  {}
199 
202 
203  /* documented in base class */
205  if ((ClassInstantiation->*Action)(result) ) {
207  }
209  }
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 
bool(_classType::* ActionType)(mtsGenericObject &result) const
Definition: mtsCallableReadMethod.h:171
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, mtsGenericObject &argument)
Definition: mtsCallableReadMethod.h:73
std::string mtsObjectName(const mtsComponent *object)
Definition: mtsComponent.h:552
void ToStream(std::ostream &outputStream) const
Definition: mtsCallableReadMethod.h:212
virtual ~mtsCallableReadMethodGeneric()
Definition: mtsCallableReadMethod.h:201
mtsCallableReadMethodGeneric(void)
Definition: mtsCallableReadMethod.h:187
ActionType Action
Definition: mtsCallableReadMethod.h:180
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
virtual ~mtsCallableReadMethod()
Definition: mtsCallableReadMethod.h:138
ClassType * ClassInstantiation
Definition: mtsCallableReadMethod.h:67
_resultType ResultType
Definition: mtsCallableReadMethod.h:45
mtsExecutionResult Execute(mtsGenericObject &result)
Definition: mtsCallableReadMethod.h:204
const std::string & GetName(void) const
mtsCallableReadBase BaseType
Definition: mtsCallableReadMethod.h:161
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
mtsCallableReadMethodGeneric(ActionType action, ClassType *classInstantiation)
Definition: mtsCallableReadMethod.h:194
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
virtual const cmnClassServicesBase * Services(void) const =0
void ToStream(std::ostream &outputStream) const
Definition: mtsCallableReadMethod.h:147
Definition: mtsExecutionResult.h:44
Definition: mtsCallableReadMethod.h:158
_classType ClassType
Definition: mtsCallableReadMethod.h:48
mtsCallableReadMethod(ActionType action, ClassType *classInstantiation)
Definition: mtsCallableReadMethod.h:131
Definition: mtsCallableReadMethod.h:70
mtsCallableReadMethodGeneric< ClassType > ThisType
Definition: mtsCallableReadMethod.h:167
Definition: mtsCallableReadMethod.h:41
bool(_classType::* ActionType)(ResultType &argument) const
Definition: mtsCallableReadMethod.h:55
mtsCallableReadBase BaseType
Definition: mtsCallableReadMethod.h:44
static T * CastArg(mtsGenericObject &arg)
Definition: mtsGenericObjectProxy.h:683
mtsExecutionResult Execute(mtsGenericObject &argument)
Definition: mtsCallableReadMethod.h:141
Definition: mtsExecutionResult.h:34
Definition: mtsExecutionResult.h:44
_classType ClassType
Definition: mtsCallableReadMethod.h:164
static mtsExecutionResult CallMethod(ClassType *classInstantiation, ActionType action, mtsGenericObject &argument)
Definition: mtsCallableReadMethod.h:90
mtsCallableReadMethod< ClassType, ResultType > ThisType
Definition: mtsCallableReadMethod.h:51
ClassType * ClassInstantiation
Definition: mtsCallableReadMethod.h:183
Definition: mtsExecutionResult.h:44
ActionType Action
Definition: mtsCallableReadMethod.h:64
Defines a base class for a callable object with a placeholder to retrieve data.
Definition: mtsCallableReadBase.h:39
mtsCallableReadMethod(void)
Definition: mtsCallableReadMethod.h:124