CorbaCommandFactory.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:58 CEST 2006  CorbaCommandFactory.hpp
00003 
00004                         CorbaCommandFactory.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 #ifndef ORO_CORBACOMMANDFACTORY_HPP
00040 #define ORO_CORBACOMMANDFACTORY_HPP
00041 
00042 #include "../OperationFactory.hpp"
00043 #include "CommandProxy.hpp"
00044 #include "OperationInterfaceC.h"
00045 #include "CommandProxy.hpp"
00046 
00047 namespace RTT
00048 {namespace Corba
00049 {
00050 
00055     class RTT_CORBA_API CorbaCommandFactory
00056         : public RTT::detail::OperationFactoryPart<DispatchInterface*>
00057     {
00058         std::string com;
00059         Corba::CommandInterface_var mfact;
00060         PortableServer::POA_var mpoa;
00061     public:
00062         typedef std::vector< RTT::ArgumentDescription > Descriptions;
00063         typedef std::vector<std::string> Commands;
00064         typedef std::vector<DataSourceBase::shared_ptr> Arguments;
00065 
00066         CorbaCommandFactory(const std::string& command, Corba::CommandInterface_ptr fact, PortableServer::POA_ptr the_poa)
00067             : RTT::detail::OperationFactoryPart<DispatchInterface*>("Corba Command"),
00068               com(command), mfact( Corba::CommandInterface::_duplicate(fact) ),
00069               mpoa(PortableServer::POA::_duplicate(the_poa) )
00070         {}
00071 
00072         virtual ~CorbaCommandFactory() {}
00073 
00074         virtual int arity()  const {
00075             return this->getArgumentList().size();
00076         }
00077 
00078         virtual std::string resultType() const
00079         {
00080             return "bool";
00081         }
00082 
00083         virtual std::string description() const
00084         {
00085             try {
00086                 CORBA::String_var result = mfact->getDescription( com.c_str() );
00087                 return std::string( result.in() );
00088             } catch ( Corba::NoSuchNameException& nsn ) {
00089                 throw name_not_found_exception( nsn.name.in() );
00090             }
00091             return std::string();
00092         }
00093 
00097         virtual Descriptions getArgumentList() const
00098         {
00099             Descriptions ret;
00100             try {
00101                 Corba::Descriptions_var result = mfact->getArguments( com.c_str() );
00102                 ret.reserve( result->length() );
00103                 for (size_t i=0; i!= result->length(); ++i)
00104                     ret.push_back( RTT::ArgumentDescription(std::string( result[i].name.in() ),
00105                                                        std::string( result[i].description.in() ),
00106                                                        std::string( result[i].type.in() ) ));
00107             } catch ( Corba::NoSuchNameException& nsn ) {
00108                 throw name_not_found_exception( nsn.name.in() );
00109             }
00110             return ret;
00111         }
00112 
00113         virtual DispatchInterface* produce(const Arguments& args) const
00114         {
00115             Corba::Arguments_var nargs = new Corba::Arguments();
00116             nargs->length( args.size() );
00117             for (size_t i=0; i < args.size(); ++i )
00118                 nargs[i] = (Corba::Expression_ptr)args[i]->server(ORO_CORBA_PROTOCOL_ID, mpoa.in() );
00119             try {
00120                 Corba::Command_var result = mfact->createCommand( com.c_str(), nargs.in() );
00121                 // return a DispatchInterface object:
00122                 return CommandProxy::Create( result.in() );
00123             } catch ( Corba::NoSuchNameException& nsn ) {
00124                 throw name_not_found_exception( nsn.name.in() );
00125             } catch ( Corba::WrongNumbArgException& wa ) {
00126                 throw wrong_number_of_args_exception( wa.wanted, wa.received );
00127             } catch ( Corba::WrongTypeArgException& wta ) {
00128                 throw wrong_types_of_args_exception( wta.whicharg, wta.expected.in(), wta.received.in() );
00129             }
00130             return 0; // not reached.
00131         }
00132     };
00133 }}
00134 
00135 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3