ExpressionProxy.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:58 CEST 2006  ExpressionProxy.hpp
00003 
00004                         ExpressionProxy.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_CORBA_EXPRESSIONPROXY_HPP
00040 #define ORO_CORBA_EXPRESSIONPROXY_HPP
00041 
00042 #include "../rtt-config.h"
00043 #include "corba.h"
00044 #ifdef CORBA_IS_TAO
00045 #include <tao/PortableServer/PortableServer.h>
00046 #else
00047 #include <omniORB4/poa.h>
00048 #endif
00049 #include "OperationsC.h"
00050 #include "../DataSource.hpp"
00051 #include "CORBAExpression.hpp"
00052 #include "../Logger.hpp"
00053 #include "../DataSources.hpp"
00054 
00055 namespace RTT
00056 {namespace Corba
00057 {
00058 
00062     class RTT_CORBA_API ExpressionProxy
00063         : public DataSourceBase
00064     {
00065     public:
00066         typedef boost::intrusive_ptr<ExpressionProxy> shared_ptr;
00067 
00073         static ExpressionProxy::shared_ptr Create(::RTT::Corba::Expression_ptr expr);
00074 
00080         static DataSourceBase::shared_ptr CreateDataSource(::RTT::Corba::Expression_ptr expr);
00081 
00082 
00089         template<class T>
00090         static DataSource<T>* NarrowDataSource(::RTT::Corba::Expression_ptr expr) {
00091 
00092             CORBA::Any_var any = expr->value();
00093             typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00094             ReferenceDataSource<T> rds( target );
00095             rds.ref();
00096             if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any.in() ) ) {
00097                 Logger::log() <<Logger::Debug<< "Found valid conversion from server "<< expr->getType()
00098                               <<" to local "<< DataSource<T>::GetType()<<Logger::endl;
00099                 return new CORBAExpression<T>( expr );
00100             }
00101             return 0; // not convertible.
00102         }
00103 
00110         template<class T>
00111         static DataSource<T>* NarrowConstant( const CORBA::Any& any) {
00112             // C++ language forces partial T specialisation using classes, not possible
00113             // with functions:
00114             return CreateConstantHelper<T>::Create( any );
00115         }
00116 
00123         template<class T>
00124         static AssignableDataSource<T>* NarrowAssignableDataSource( ::RTT::Corba::Expression_ptr expr) {
00125 
00126             Corba::AssignableExpression_var ret = Corba::AssignableExpression::_narrow( expr );
00127             if ( !CORBA::is_nil(ret) ) {
00128                 CORBA::Any_var any = ret->value();
00129                 typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00130                 ReferenceDataSource<T> rds( target );
00131                 rds.ref();
00132                 if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any.in() ) ) {
00133                     Logger::log() <<Logger::Debug<< "Found valid assignment conversion from server "<< ret->getType()
00134                                   <<" to local "<< DataSource<T>::GetType()<<Logger::endl;
00135                     return new CORBAAssignableExpression<T>( ret._retn() );
00136                 }
00137             }
00138             return 0; // not convertible.
00139         }
00140 
00146         template<class T>
00147         DataSource<T>* narrowDataSource() const {
00148             return NarrowDataSource<T>( mdata );
00149         }
00150 
00155         DataSource<void>* narrowDataSource() const {
00156             return new CORBAExpression<void>( mdata.in() );
00157         }
00158 
00164         template<class T>
00165         AssignableDataSource<T>* narrowAssignableDataSource() const {
00166             return NarrowAssignableDataSource<T>( mdata.in() );
00167         }
00168 
00174         //virtual Corba::Expression_ptr createExpression() const;
00175 
00176         virtual int serverProtocol() const
00177         {
00178             return ORO_CORBA_PROTOCOL_ID;
00179         }
00180 
00181         virtual bool evaluate() const {
00182             return mdata->evaluate();
00183         }
00184 
00185         virtual DataSourceBase* clone() const {
00186             return new ExpressionProxy( mdata.in() );
00187         }
00188 
00189         virtual DataSourceBase* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00190             alreadyCloned[this] = const_cast<ExpressionProxy*>(this);
00191             return alreadyCloned[this];
00192         }
00193 
00194         virtual std::string getType() const { return std::string( mdata->getType() ); }
00195 
00196         virtual const TypeInfo* getTypeInfo() const { return detail::DataSourceTypeInfo<detail::UnknownType>::getTypeInfo(); }
00197 
00198         virtual std::string getTypeName() const { return std::string( mdata->getTypeName() ); }
00199 
00200         virtual void* createBlob(int p) {
00201             if (p == ORO_CORBA_PROTOCOL_ID)
00202                 return mdata->value();
00203             return 0;
00204         }
00205 
00206         virtual void* getBlob(int p) {
00207             if (p == ORO_CORBA_PROTOCOL_ID)
00208                 return mdata->get();
00209             return 0;
00210         }
00211 
00212         virtual void* server(int p, void* arg) {
00213             if (p == ORO_CORBA_PROTOCOL_ID)
00214                 return Corba::Expression::_duplicate(mdata.in());
00215             return 0;
00216         }
00217 
00218         virtual void* server(int p, void* arg) const {
00219             if (p == ORO_CORBA_PROTOCOL_ID)
00220                 return Corba::Expression::_duplicate(mdata.in());
00221             return 0;
00222         }
00223 
00224         virtual void* method(int p, MethodC* , void* arg) {
00225             if (p == ORO_CORBA_PROTOCOL_ID)
00226                 return Corba::Method::_narrow( mdata.in() );
00227             return 0;
00228         }
00229 
00230     private:
00231         template<class T>
00232         struct CreateConstantHelper
00233         {
00234             static DataSource<T>* Create(const CORBA::Any& any) {
00235 
00236                 typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00237                 ReferenceDataSource<T> rds( target );
00238                 rds.ref();
00239                 if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any ) ) {
00240                     Logger::log() <<Logger::Debug<< "Found valid conversion from CORBA::Any "
00241                                   <<" to local constant "<< DataSource<T>::GetType()<<Logger::endl;
00242                     return new ConstantDataSource<T>( target );
00243                 }
00244                 return 0; // not convertible.
00245             }
00246         };
00247 
00248         template<class T>
00249         struct CreateConstantHelper<T&>
00250         {
00251             static DataSource<T&>* Create(const CORBA::Any& any) {
00252                 return 0; // not convertible.
00253             }
00254         };
00255 
00256         template<class T>
00257         struct CreateConstantHelper<const T&>
00258         {
00259             static DataSource<const T&>* Create(const CORBA::Any& any) {
00260 
00261                 typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00262                 ReferenceDataSource<T> rds( target );
00263                 rds.ref();
00264                 if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any ) ) {
00265                     Logger::log() <<Logger::Debug<< "Found valid conversion from CORBA::Any "
00266                                   <<" to local constant "<< DataSource<const T&>::GetType()<<Logger::endl;
00267                     return new ConstantDataSource<const T&>( target );
00268                 }
00269                 return 0; // not convertible.
00270             }
00271         };
00272 
00273     protected:
00274         typedef std::map<Corba::Expression_ptr, ExpressionProxy::shared_ptr> EMap;
00275         typedef std::map<Corba::Expression_ptr, DataSourceBase::shared_ptr> DMap;
00276 
00277         static EMap proxies;
00278         static DMap dproxies;
00279 
00284         ExpressionProxy( ::RTT::Corba::Expression_ptr t );
00285 
00286         Corba::Expression_var mdata;
00287 
00288     };
00289 
00290 }}
00291 
00292 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3