Orocos Real-Time Toolkit  2.6.0
RTTCorbaConversion.hpp
00001 /***************************************************************************
00002 tag: Peter Soetens  Mon Jun 26 13:25:58 CEST 2006  CorbaConversion.hpp
00003 
00004 CorbaConversion.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_RTT_CORBA_CONVERSION_HPP
00040 #define ORO_RTT_CORBA_CONVERSION_HPP
00041 
00042 
00043 #include "CorbaConversion.hpp"
00044 #include "OrocosTypesC.h"
00045 #include "TaskContextC.h"
00046 #include "TaskContextServer.hpp"
00047 #include "TaskContextProxy.hpp"
00048 #include "CorbaConnPolicy.hpp"
00049 
00050 namespace RTT { 
00051   namespace corba {
00052 
00053     template<>
00054     struct AnyConversion<double> : public AnyConversionHelper<double> {
00055       typedef CORBA::DoubleSeq sequence;
00056     };
00057 
00058     template<>
00059     struct AnyConversion<float> : public AnyConversionHelper<float> {
00060       typedef CORBA::FloatSeq sequence;
00061     };
00062 
00063     template<>
00064     struct AnyConversion<int> : public AnyConversionHelper<int, CORBA::Long> {
00065       typedef CORBA::LongSeq sequence;
00066     };
00067 
00068     //template<>
00069     //struct AnyConversion<long> : public AnyConversionHelper<long> {};
00070 
00071     template<>
00072     struct AnyConversion<unsigned int> : public AnyConversionHelper<unsigned int, CORBA::ULong> {
00073       typedef CORBA::ULongSeq sequence;
00074     };
00075 
00076     template<>
00077     struct AnyConversion<CORBA::Any_ptr>
00078     {
00079       typedef CORBA::Any_ptr CorbaType;
00080       typedef CORBA::Any_ptr StdType;
00081 
00082       static bool update(const CORBA::Any& any, CORBA::Any_ptr _value) {
00083         //Logger::log() << Logger::Debug << "Updating type CORBA::Any_ptr with CORBA::Any." <<Logger::endl;
00084         *_value = any;
00085         return true;
00086       }
00087 
00088       static CORBA::Any_ptr createAny( CORBA::Any_ptr t ) {
00089         //Logger::log() << Logger::Debug << "Duplicating CORBA::Any_ptr." <<Logger::endl;
00090         return new CORBA::Any(*t);
00091       }
00092 
00093       static bool updateAny( const StdType& t, CORBA::Any& any ) {
00094         //Logger::log() << Logger::Debug << "Duplicating CORBA::Any_ptr." <<Logger::endl;
00095         any <<= CORBA::Any(*t);
00096         return true;
00097       }
00098     };
00099 
00100     template<>
00101     struct AnyConversion<CORBA::Any_var>
00102     {
00103       typedef CORBA::Any_ptr CorbaType;
00104       typedef CORBA::Any_var StdType;
00105 
00106       static bool update(const CORBA::Any& any, CORBA::Any_var _value) {
00107         //Logger::log() << Logger::Debug << "Updating type CORBA::Any_var with CORBA::Any." <<Logger::endl;
00108         *_value.out() = any;
00109         return true;
00110       }
00111 
00112       static CORBA::Any_ptr createAny( CORBA::Any_var t ) {
00113         //Logger::log() << Logger::Debug << "Duplicating CORBA::Any_var." <<Logger::endl;
00114         return new CORBA::Any( t.in() );
00115       }
00116 
00117       static bool updateAny( const StdType& t, CORBA::Any& any ) {
00118         //Logger::log() << Logger::Debug << "Duplicating CORBA::Any_var." <<Logger::endl;
00119         any <<= CORBA::Any( t.in() );
00120         return true;
00121       }
00122     };
00123 
00124     template<>
00125     struct AnyConversion<bool>
00126     {
00127       typedef CORBA::Boolean CorbaType;
00128       typedef bool StdType;
00129 
00130       typedef CORBA::BooleanSeq sequence;
00131 
00132       static CORBA::Any::from_boolean toAny( bool t ) {
00133         //Logger::log() << Logger::Debug << "Converting type 'bool' to from_boolean." <<Logger::endl;
00134         return CORBA::Any::from_boolean(t);
00135       }
00136       static CORBA::Any::to_boolean fromAny( CORBA::Boolean& t ) {
00137         return CORBA::Any::to_boolean(t);
00138       }
00139       static StdType get(const CORBA::Boolean t) {
00140         return t;
00141       }
00142 
00143       static bool toStdType(StdType& tp, const CorbaType& cb) {
00144         tp = get(cb);
00145         return true;
00146       }
00147       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00148         cb = tp;
00149         return true;
00150       }
00151 
00152       static bool update(const CORBA::Any& any, StdType& _value) {
00153         CorbaType result;
00154         if ( any >>= AnyConversion<bool>::fromAny( result ) ) {
00155           _value = AnyConversion<bool>::get(result);
00156           return true;
00157         }
00158         return false;
00159       }
00160 
00161       static CORBA::Any_ptr createAny( bool t ) {
00162         CORBA::Any_ptr ret = new CORBA::Any();
00163         *ret <<= toAny( t );
00164         return ret;
00165       }
00166 
00167       static bool updateAny( bool t, CORBA::Any& any ) {
00168         any <<= toAny( t );
00169         return true;
00170       }
00171     };
00172 
00173     template<>
00174     struct AnyConversion<char>
00175     {
00176       typedef CORBA::Char CorbaType;
00177       typedef char StdType;
00178 
00179       typedef CORBA::CharSeq sequence;
00180 
00181       static CORBA::Any::from_char toAny( StdType t ) {
00182         return CORBA::Any::from_char(t);
00183       }
00184 
00185       static CORBA::Any::to_char fromAny( CorbaType& t ) {
00186         return CORBA::Any::to_char(t);
00187       }
00188 
00189       static StdType get(const CorbaType t) {
00190         return t;
00191       }
00192 
00193       static bool toStdType(StdType& tp, const CorbaType& cb) {
00194         tp = get(cb);
00195         return true;
00196       }
00197       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00198         cb = tp;
00199         return true;
00200       }
00201 
00202       static bool update(const CORBA::Any& any, StdType& _value) {
00203         CorbaType result;
00204         if ( any >>= AnyConversion<StdType>::fromAny( result ) ) {
00205           _value = AnyConversion<StdType>::get(result);
00206           return true;
00207         }
00208         return false;
00209       }
00210 
00211       static CORBA::Any_ptr createAny( char t ) {
00212         CORBA::Any_ptr ret = new CORBA::Any();
00213         *ret <<= toAny( t );
00214         return ret;
00215       }
00216 
00217       static bool updateAny( char t, CORBA::Any& any ) {
00218         any <<= toAny( t );
00219         return true;
00220       }
00221     };
00222 
00223     template<>
00224     struct AnyConversion<std::string>
00225     {
00226       typedef CORBA::StringSeq sequence;
00227 
00228       typedef const char* CorbaType;
00229       typedef std::string StdType;
00230 
00231       static CorbaType toAny(const std::string& orig) {
00232         //Logger::log() << Logger::Debug << "Converting type 'string' to const char*." <<Logger::endl;
00233         return orig.c_str();
00234       }
00235 
00236       static StdType get(const CorbaType t) {
00237         return StdType( t );
00238       }
00239 
00240       static bool toStdType(StdType& dest, const CorbaType src) {
00241         dest = get(src);
00242         return true;
00243       }
00244 
00248       template<class dummy>
00249       static bool toCorbaType(dummy dest, const StdType& src) {
00250         dest = src.c_str();
00251         return true;
00252       }
00253 
00254       static bool toCorbaType(CorbaType dest, const StdType& src) {
00255         dest = src.c_str();
00256         return true;
00257       }
00258 
00259       static bool update(const CORBA::Any& any, StdType& _value) {
00260         CorbaType result;
00261         //Logger::log() << Logger::Debug << "Updating std::string with Any." <<Logger::endl;
00262         if ( any >>= result ) {
00263           _value = result;
00264           return true;
00265         }
00266         return false;
00267       }
00268 
00269       static CORBA::Any_ptr createAny( const std::string& t ) {
00270         CORBA::Any_ptr ret = new CORBA::Any();
00271         *ret <<= toAny( t );
00272         return ret;
00273       }
00274 
00275       static bool updateAny( StdType const& t, CORBA::Any& any ) {
00276         any <<= toAny( t );
00277         return true;
00278       }
00279     };
00280 
00285     template<>
00286     struct AnyConversion<ConnPolicy>
00287     {
00288       typedef RTT::corba::CConnPolicy CorbaType;
00289       typedef RTT::ConnPolicy StdType;
00290       static CorbaType toAny(const StdType& orig) {
00291         //Logger::log() << Logger::Debug << "Converting type 'string' to const char*." <<Logger::endl;
00292         return toCORBA(orig);
00293       }
00294 
00295       static StdType get(const CorbaType t) {
00296         return toRTT( t );
00297       }
00298 
00299       static bool update(const CORBA::Any& any, StdType& _value) {
00300         CorbaType* result;
00301         //Logger::log() << Logger::Debug << "Updating std::string with Any." <<Logger::endl;
00302         if ( any >>= result ) {
00303           _value = toRTT(*result);
00304           return true;
00305         }
00306         return false;
00307       }
00308 
00309       static CORBA::Any_ptr createAny( const StdType& t ) {
00310         CORBA::Any_ptr ret = new CORBA::Any();
00311         *ret <<= toAny( t );
00312         return ret;
00313       }
00314 
00315       static bool updateAny( StdType const& t, CORBA::Any& any ) {
00316         any <<= toAny( t );
00317         return true;
00318       }
00319     };
00320 
00321     template<>
00322     struct AnyConversion<RTT::TaskContext*>
00323     {
00324       typedef RTT::corba::CTaskContext_ptr CorbaType;
00325       typedef RTT::TaskContext* StdType;
00326 
00327       static bool update(const CORBA::Any& any, StdType& _value) {
00328         RTT::corba::CTaskContext_ptr task;
00329         if ( any >>= task ) {
00330           // read-only insertion, we duplicate the _ptr in Create:
00331           _value = TaskContextProxy::Create( task );
00332           return true;
00333         }
00334         return true;
00335       }
00336 
00337       static CORBA::Any_ptr createAny( const StdType& t ) {
00338         CORBA::Any_ptr ret = new CORBA::Any();
00339         // copying insertion:
00340         *ret <<= TaskContextServer::CreateServer(t,false,false);
00341         return ret;
00342       }
00343 
00344       static bool updateAny( const StdType& t, CORBA::Any& any ) {
00345         if (t) {
00346           // copying insertion:
00347           any <<= TaskContextServer::CreateServer(t, false, false );
00348         } else {
00349           // leave any.
00350         }
00351         return true;
00352       }
00353     };
00354   }
00355 }
00356 
00357 #endif