/* -*- 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): Anton Deguet Created on: 2010-09-16 (C) Copyright 2010-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 no argument */ #ifndef _mtsCallableQualifiedReadMethod_h #define _mtsCallableQualifiedReadMethod_h #include #include #include /*! \ingroup cisstMultiTask */ template class mtsCallableQualifiedReadMethod: public mtsCallableQualifiedReadBase { public: typedef mtsCallableQualifiedReadBase BaseType; typedef _argumentType ArgumentType; typedef _resultType ResultType; /*! Typedef for the specific interface. */ typedef _classType ClassType; /*! This type. */ typedef mtsCallableQualifiedReadMethod ThisType; /*! Typedef for pointer to member function (method) of a specific class (_classType). */ typedef bool(_classType::*ActionType)(const ArgumentType & argument, ResultType & result) const; private: /*! Private copy constructor to prevent copies */ inline mtsCallableQualifiedReadMethod(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 class ConditionalCast { public: static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action, const mtsGenericObject & argument, mtsGenericObject & result) { const ArgumentType * data1 = mtsGenericTypes::CastArg(argument); if (data1 == 0) { return mtsExecutionResult::INVALID_INPUT_TYPE; } ResultType * data2 = mtsGenericTypes::CastArg(result); if (data2 == 0) { return mtsExecutionResult::INVALID_INPUT_TYPE; } if ( (classInstantiation->*action)(*data1, *data2) ) { return mtsExecutionResult::COMMAND_SUCCEEDED; } return mtsExecutionResult::METHOD_OR_FUNCTION_FAILED; } }; template class ConditionalCast { public: static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action, const mtsGenericObject & argument, mtsGenericObject & result) { // First, check if a Proxy object was passed. ArgumentType temp1; // in case needed const ArgumentType * data1 = dynamic_cast(&argument); // If it isn't a Proxy, maybe it is a ProxyRef if (!data1) { typedef typename ArgumentType::RefType ArgumentRefType; const ArgumentRefType * data1ref = dynamic_cast(&argument); if (!data1ref) { CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast argument from " << typeid(argument).name() << " to const " << typeid(ArgumentRefType).name() << std::endl; return mtsExecutionResult::INVALID_INPUT_TYPE; } temp1 = *data1ref; data1 = &temp1; } ResultType * data2 = mtsGenericTypes::CastArg(result); if (data2 == 0) { return mtsExecutionResult::INVALID_INPUT_TYPE; } if ( (classInstantiation->*action)(*data1, *data2) ) { return mtsExecutionResult::COMMAND_SUCCEEDED; } return mtsExecutionResult::METHOD_OR_FUNCTION_FAILED; } }; template class ConditionalCast { public: static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action, const mtsGenericObject & argument, mtsGenericObject & result) { const ArgumentType * data1 = mtsGenericTypes::CastArg(argument); if (data1 == 0) { return mtsExecutionResult::INVALID_INPUT_TYPE; } // First, check if a Proxy object was passed. ResultType temp2; // in case needed ResultType * data2 = dynamic_cast(&result); // If it isn't a Proxy, maybe it is a ProxyRef typedef typename ResultType::RefType ResultRefType; ResultRefType * data2ref = 0; if (!data2) { data2ref = dynamic_cast(&result); if (!data2ref) { CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast result from " << typeid(result).name() << " to const " << typeid(ResultRefType).name() << std::endl; return mtsExecutionResult::INVALID_INPUT_TYPE; } data2 = &temp2; } if ( (classInstantiation->*action)(*data1, *data2) ) { if (data2ref) { *data2ref = *data2; } return mtsExecutionResult::COMMAND_SUCCEEDED; } return mtsExecutionResult::METHOD_OR_FUNCTION_FAILED; } }; template class ConditionalCast { public: static mtsExecutionResult CallMethod(ClassType * classInstantiation, ActionType action, const mtsGenericObject & argument, mtsGenericObject & result) { // First, check if a Proxy object was passed. ArgumentType temp1; // in case needed const ArgumentType * data1 = dynamic_cast(&argument); // If it isn't a Proxy, maybe it is a ProxyRef if (!data1) { typedef typename ArgumentType::RefType ArgumentRefType; const ArgumentRefType * data1ref = dynamic_cast(&argument); if (!data1ref) { CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast argument from " << typeid(argument).name() << " to const " << typeid(ArgumentRefType).name() << std::endl; return mtsExecutionResult::INVALID_INPUT_TYPE; } temp1 = *data1ref; data1 = &temp1; } // First, check if a Proxy object was passed. ResultType temp2; // in case needed ResultType * data2 = dynamic_cast(&result); // If it isn't a Proxy, maybe it is a ProxyRef typedef typename ResultType::RefType ResultRefType; ResultRefType * data2ref = 0; if (!data2) { data2ref = dynamic_cast(&result); if (!data2ref) { CMN_LOG_INIT_ERROR << "Class mtsCallableQualifiedReadMethod: CallMethod could not cast result from " << typeid(result).name() << " to const " << typeid(ResultRefType).name() << std::endl; return mtsExecutionResult::INVALID_INPUT_TYPE; } data2 = &temp2; } if ( (classInstantiation->*action)(*data1, *data2) ) { // Finally, copy the data to the argument if (data2ref) { *data2ref = temp2; } return mtsExecutionResult::COMMAND_SUCCEEDED; } return mtsExecutionResult::METHOD_OR_FUNCTION_FAILED; } }; public: /*! The constructor. Does nothing. */ mtsCallableQualifiedReadMethod(void): BaseType(), ClassInstantiation(0) {} /*! 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 */ mtsCallableQualifiedReadMethod(ActionType action, ClassType * classInstantiation): BaseType(), Action(action), ClassInstantiation(classInstantiation) {} /*! The destructor. Does nothing */ virtual ~mtsCallableQualifiedReadMethod() {} /* documented in base class */ mtsExecutionResult Execute(const mtsGenericObject & argument, mtsGenericObject & result) { return ConditionalCast::IS_DERIVED, cmnIsDerivedFromTemplated::IS_DERIVED> ::CallMethod(ClassInstantiation, Action, argument, result); } /* documented in base class */ void ToStream(std::ostream & outputStream) const { if (this->ClassInstantiation) { outputStream << "method based callable qualified read object using class/object \"" << mtsObjectName(this->ClassInstantiation) << "\""; } else { outputStream << "invalid method based callable object"; } } }; template class mtsCallableQualifiedReadMethodGeneric: public mtsCallableQualifiedReadBase { public: typedef mtsCallableQualifiedReadBase BaseType; /*! Typedef for the specific interface. */ typedef _classType ClassType; /*! This type. */ typedef mtsCallableQualifiedReadMethodGeneric ThisType; /*! Typedef for pointer to member function (method) of a specific class (_classType). */ typedef bool(_classType::*ActionType)(const mtsGenericObject & argument, mtsGenericObject & result) const; private: /*! Private copy constructor to prevent copies */ inline mtsCallableQualifiedReadMethodGeneric(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; public: /*! The constructor. Does nothing. */ mtsCallableQualifiedReadMethodGeneric(void): BaseType(), ClassInstantiation(0) {} /*! 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 */ mtsCallableQualifiedReadMethodGeneric(ActionType action, ClassType * classInstantiation): BaseType(), Action(action), ClassInstantiation(classInstantiation) {} /*! The destructor. Does nothing */ virtual ~mtsCallableQualifiedReadMethodGeneric() {} /* documented in base class */ mtsExecutionResult Execute(const mtsGenericObject & argument, mtsGenericObject & result) { if ((ClassInstantiation->*Action)(argument, result) ) { return mtsExecutionResult::COMMAND_SUCCEEDED; } return mtsExecutionResult::METHOD_OR_FUNCTION_FAILED; } /* documented in base class */ void ToStream(std::ostream & outputStream) const { if (this->ClassInstantiation) { outputStream << "generic method based callable qualified read object using class/object \"" << mtsObjectName(this->ClassInstantiation) << "\""; } else { outputStream << "invalid generic method based callable object"; } } }; #endif // _mtsCallableQualifiedReadMethod_h