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 "../DataSource.hpp"
00043 #include "OperationsC.h"
00044 #include "CORBAExpression.hpp"
00045 #include "../Logger.hpp"
00046 #include "../DataSources.hpp"
00047 
00048 namespace RTT
00049 {namespace Corba
00050 {
00051 
00055     class ExpressionProxy
00056         : public DataSourceBase
00057     {
00058     public:
00059         typedef boost::intrusive_ptr<ExpressionProxy> shared_ptr;
00060 
00066         static ExpressionProxy::shared_ptr Create(::RTT::Corba::Expression_ptr expr);
00067 
00073         static DataSourceBase::shared_ptr CreateDataSource(::RTT::Corba::Expression_ptr expr);
00074 
00075 
00082         template<class T>
00083         static DataSource<T>* NarrowDataSource(::RTT::Corba::Expression_ptr expr) {
00084 
00085             CORBA::Any_var any = expr->value();
00086             typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00087             ReferenceDataSource<T> rds( target );
00088             rds.ref();
00089             if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any.in() ) ) {
00090                 Logger::log() <<Logger::Debug<< "Found valid conversion from server "<< expr->getType()
00091                               <<" to local "<< DataSource<T>::GetType()<<Logger::endl;
00092                 return new CORBAExpression<T>( expr );
00093             }
00094             return 0; // not convertible.
00095         }
00096 
00103         template<class T>
00104         static DataSource<T>* NarrowConstant( const CORBA::Any& any) {
00105             // C++ language forces partial T specialisation using classes, not possible
00106             // with functions:
00107             return CreateConstantHelper<T>::Create( any );
00108         }
00109 
00116         template<class T>
00117         static AssignableDataSource<T>* NarrowAssignableDataSource( ::RTT::Corba::Expression_ptr expr) {
00118 
00119             Corba::AssignableExpression_var ret = Corba::AssignableExpression::_narrow( expr );
00120             if ( !CORBA::is_nil(ret) ) {
00121                 CORBA::Any_var any = ret->value();
00122                 typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00123                 ReferenceDataSource<T> rds( target );
00124                 rds.ref();
00125                 if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any.in() ) ) {
00126                     Logger::log() <<Logger::Debug<< "Found valid assignment conversion from server "<< ret->getType()
00127                                   <<" to local "<< DataSource<T>::GetType()<<Logger::endl;
00128                     return new CORBAAssignableExpression<T>( ret._retn() );
00129                 }
00130             }
00131             return 0; // not convertible.
00132         }
00133 
00139         template<class T>
00140         DataSource<T>* narrowDataSource() const {
00141             return NarrowDataSource<T>( mdata );
00142         }
00143 
00148         DataSource<void>* narrowDataSource() const {
00149             return new CORBAExpression<void>( mdata.in() );
00150         }
00151 
00157         template<class T>
00158         AssignableDataSource<T>* narrowAssignableDataSource() const {
00159             return NarrowAssignableDataSource<T>( mdata.in() );
00160         }
00161 
00167         //virtual Corba::Expression_ptr createExpression() const;
00168 
00169         virtual int serverProtocol() const
00170         {
00171             return ORO_CORBA_PROTOCOL_ID;
00172         }
00173 
00174         virtual bool evaluate() const {
00175             return mdata->evaluate();
00176         }
00177 
00178         virtual DataSourceBase* clone() const {
00179             return new ExpressionProxy( mdata.in() );
00180         }
00181 
00182         virtual DataSourceBase* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00183             alreadyCloned[this] = const_cast<ExpressionProxy*>(this);
00184             return alreadyCloned[this];
00185         }
00186 
00187         virtual std::string getType() const { return std::string( mdata->getType() ); }
00188 
00189         virtual const TypeInfo* getTypeInfo() const { return detail::DataSourceTypeInfo<detail::UnknownType>::getTypeInfo(); }
00190 
00191         virtual std::string getTypeName() const { return std::string( mdata->getTypeName() ); }
00192 
00193         virtual void* createBlob(int p) {
00194             if (p == ORO_CORBA_PROTOCOL_ID)
00195                 return mdata->value();
00196             return 0;
00197         }
00198 
00199         virtual void* getBlob(int p) {
00200             if (p == ORO_CORBA_PROTOCOL_ID)
00201                 return mdata->get();
00202             return 0;
00203         }
00204 
00205         virtual void* server(int p, void* arg) {
00206             if (p == ORO_CORBA_PROTOCOL_ID)
00207                 return Corba::Expression::_duplicate(mdata.in());
00208             return 0;
00209         }
00210 
00211         virtual void* server(int p, void* arg) const {
00212             if (p == ORO_CORBA_PROTOCOL_ID)
00213                 return Corba::Expression::_duplicate(mdata.in());
00214             return 0;
00215         }
00216 
00217         virtual void* method(int p, MethodC* , void* arg) {
00218             if (p == ORO_CORBA_PROTOCOL_ID)
00219                 return Corba::Method::_narrow( mdata.in() );
00220             return 0;
00221         }
00222 
00223     private:
00224         template<class T>
00225         struct CreateConstantHelper
00226         {
00227             static DataSource<T>* Create(const CORBA::Any& any) {
00228 
00229                 typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00230                 ReferenceDataSource<T> rds( target );
00231                 rds.ref();
00232                 if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any ) ) {
00233                     Logger::log() <<Logger::Debug<< "Found valid conversion from CORBA::Any "
00234                                   <<" to local constant "<< DataSource<T>::GetType()<<Logger::endl;
00235                     return new ConstantDataSource<T>( target );
00236                 }
00237                 return 0; // not convertible.
00238             }
00239         };
00240 
00241         template<class T>
00242         struct CreateConstantHelper<T&>
00243         {
00244             static DataSource<T&>* Create(const CORBA::Any& any) {
00245                 return 0; // not convertible.
00246             }
00247         };
00248 
00249         template<class T>
00250         struct CreateConstantHelper<const T&>
00251         {
00252             static DataSource<const T&>* Create(const CORBA::Any& any) {
00253 
00254                 typename DataSource<T>::value_t target = typename DataSource<T>::value_t();
00255                 ReferenceDataSource<T> rds( target );
00256                 rds.ref();
00257                 if ( rds.updateBlob(ORO_CORBA_PROTOCOL_ID, &any ) ) {
00258                     Logger::log() <<Logger::Debug<< "Found valid conversion from CORBA::Any "
00259                                   <<" to local constant "<< DataSource<const T&>::GetType()<<Logger::endl;
00260                     return new ConstantDataSource<const T&>( target );
00261                 }
00262                 return 0; // not convertible.
00263             }
00264         };
00265 
00266     protected:
00267         typedef std::map<Corba::Expression_ptr, ExpressionProxy::shared_ptr> EMap;
00268         typedef std::map<Corba::Expression_ptr, DataSourceBase::shared_ptr> DMap;
00269 
00270         static EMap proxies;
00271         static DMap dproxies;
00272 
00277         ExpressionProxy( ::RTT::Corba::Expression_ptr t );
00278 
00279         Corba::Expression_var mdata;
00280 
00281     };
00282 
00283 }}
00284 
00285 #endif

Generated on Tue Aug 25 14:17:21 2009 for Orocos Real-Time Toolkit by  doxygen 1.5.8