CorbaMethodFactory.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:59 CEST 2006  CorbaMethodFactory.hpp
00003 
00004                         CorbaMethodFactory.hpp -  description
00005                            -------------------
00006     begin                : Mon June 26 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 
00040 #ifndef ORO_CORBAMETHODFACTORY_HPP
00041 #define ORO_CORBAMETHODFACTORY_HPP
00042 
00043 #include "../OperationInterface.hpp"
00044 #include "../FactoryExceptions.hpp"
00045 #include "ExpressionProxy.hpp"
00046 #include "OperationInterfaceC.h"
00047 
00048 namespace RTT
00049 {namespace Corba
00050 {
00051 
00057     class RTT_CORBA_API CorbaMethodFactory
00058         : public RTT::detail::OperationFactoryPart<DataSourceBase*>
00059     {
00060         Corba::MethodInterface_var mfact;
00061         PortableServer::POA_var mpoa;
00062         std::string method;
00063     public:
00064         typedef std::vector<DataSourceBase::shared_ptr> Arguments;
00065         typedef std::vector<std::string> Members;
00066         typedef std::vector< RTT::ArgumentDescription > Descriptions;
00067 
00068         CorbaMethodFactory( const std::string& method_name, Corba::MethodInterface_ptr fact, PortableServer::POA_ptr the_poa )
00069             : RTT::detail::OperationFactoryPart<DataSourceBase*>("Corba Method"),
00070               mfact(Corba::MethodInterface::_duplicate(fact) ),
00071               mpoa(PortableServer::POA::_duplicate(the_poa)),
00072               method(method_name)
00073         {}
00074 
00075         virtual ~CorbaMethodFactory() {}
00076 
00077         virtual int arity()  const {
00078             return this->getArgumentList().size();
00079         }
00080 
00081         virtual std::string resultType() const {
00082             try {
00083                 CORBA::String_var result = mfact->getResultType( method.c_str() );
00084                 return std::string( result.in() );
00085             } catch ( Corba::NoSuchNameException& nsn ) {
00086                 throw name_not_found_exception( nsn.name.in() );
00087             }
00088             return std::string();
00089         }
00090 
00091         virtual std::string description() const {
00092             try {
00093                 CORBA::String_var result = mfact->getDescription( method.c_str() );
00094                 return std::string( result.in() );
00095             } catch ( Corba::NoSuchNameException& nsn ) {
00096                 throw name_not_found_exception( nsn.name.in() );
00097             }
00098             return std::string();
00099         }
00100 
00101         virtual std::vector< RTT::ArgumentDescription > getArgumentList() const {
00102             Descriptions ret;
00103             try {
00104                 Corba::Descriptions_var result = mfact->getArguments( method.c_str() );
00105                 ret.reserve( result->length() );
00106                 for (size_t i=0; i!= result->length(); ++i)
00107                     ret.push_back( RTT::ArgumentDescription(std::string( result[i].name.in() ),
00108                                                        std::string( result[i].description.in() ),
00109                                                        std::string( result[i].type.in() ) ));
00110             } catch ( Corba::NoSuchNameException& nsn ) {
00111                 throw name_not_found_exception( nsn.name.in() );
00112             }
00113             return ret;
00114         }
00115 
00116         virtual DataSourceBase* produce( const Arguments& args ) const {
00117             Corba::Arguments_var nargs = new Corba::Arguments();
00118             nargs->length( args.size() );
00119             for (size_t i=0; i < args.size(); ++i )
00120                 nargs[i] = (Corba::Expression_ptr)args[i]->server(ORO_CORBA_PROTOCOL_ID, mpoa.in() );
00121             try {
00122                 Corba::Expression_var result = mfact->createMethod( method.c_str(), nargs.in() );
00123                 return ExpressionProxy::CreateDataSource( result._retn() ).get();
00124             } catch ( Corba::NoSuchNameException& nsn ) {
00125                 throw name_not_found_exception( nsn.name.in() );
00126             } catch ( Corba::WrongNumbArgException& wa ) {
00127                 throw wrong_number_of_args_exception( wa.wanted, wa.received );
00128             } catch ( Corba::WrongTypeArgException& wta ) {
00129                 throw wrong_types_of_args_exception( wta.whicharg, wta.expected.in(), wta.received.in() );
00130             }
00131             return 0; // not reached.
00132         }
00133     };
00134 
00135 }}
00136 
00137 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3