CorbaConversion.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_CORBA_CONVERSION_HPP
00040 #define ORO_CORBA_CONVERSION_HPP
00041 
00042 #include <string>
00043 #include <vector>
00044 
00045 #include "corba.h"
00046 #ifdef CORBA_IS_TAO
00047 #include <tao/Version.h>
00048 #if TAO_MAJOR_VERSION == 1 && TAO_MINOR_VERSION <= 4
00049 #include <tao/Any.h>
00050 #else // TAO 1.5 and higher
00051 #include <tao/AnyTypeCode/Any.h>
00052 #endif
00053 #include <tao/CORBA_String.h>
00054 #else
00055 #include "corba.h"
00056 #include <omniORB4/stringtypes.h>
00057 #endif
00058 
00059 #include "OrocosTypesC.h"
00060 #include "../Logger.hpp"
00061 #include "../DataSourceTypeInfo.hpp"
00062 #include "AttributesC.h"
00063 #include "CorbaLib.hpp"
00064 
00065 namespace RTT
00066 {
00079     template<class Type>
00080     struct AnyConversion
00081     {
00082         typedef CORBA::Any CorbaType;
00083         typedef Type StdType;
00084 
00092         static bool update(const CORBA::Any& any, StdType tp) {
00093             Logger::log() << Logger::Debug << "Failing conversion of type "<<detail::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
00094             return false;
00095         }
00096 
00103         static CORBA::Any_ptr createAny( StdType tp ) {
00104             Logger::log() << Logger::Error << "Failing Corba::Any creation of type "<<detail::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
00105             return new CORBA::Any();
00106         }
00107     };
00108 
00109     template<class Type, class _CorbaType = Type>
00110     struct AnyConversionHelper
00111     {
00112         typedef _CorbaType CorbaType;
00113         typedef Type StdType;
00114         static CorbaType toAny( Type t ) {
00115             //Logger::log() << Logger::Debug << "Converting type "<<detail::DataSourceTypeInfo<Type>::getType()<<" to same CORBA type." <<Logger::endl;
00116             return t;
00117         }
00118         static Type& fromAny( Type& t ) {
00119             return t;
00120         }
00121         static const Type& get(const Type& t) {
00122             return t;
00123         }
00124 
00125         static bool update(const CORBA::Any& any, StdType& _value) {
00126         CorbaType temp;
00127             if ( any >>= temp ) {
00128         _value = temp;
00129                 return true;
00130             }
00131             return false;
00132         }
00133 
00134         static CORBA::Any_ptr createAny( const Type& t ) {
00135             CORBA::Any_ptr ret = new CORBA::Any();
00136             //Logger::log() << Logger::Debug << "Creating Corba::Any from "<<detail::DataSourceTypeInfo<Type>::getType()<<"." <<Logger::endl;
00137             *ret <<= toAny( static_cast<CorbaType>(t) );
00138             return ret;
00139         }
00140 
00141     };
00142 
00143     template<>
00144     struct RTT_CORBA_API AnyConversion<double> : public AnyConversionHelper<double> {};
00145 
00146     template<>
00147     struct RTT_CORBA_API AnyConversion<float> : public AnyConversionHelper<float> {};
00148 
00149     template<>
00150     struct RTT_CORBA_API AnyConversion<int> : public AnyConversionHelper<int, CORBA::Long> {};
00151 
00152     //template<>
00153     //struct RTT_CORBA_API AnyConversion<long> : public AnyConversionHelper<long> {};
00154 
00155     template<>
00156     struct RTT_CORBA_API AnyConversion<unsigned int> : public AnyConversionHelper<unsigned int, CORBA::ULong> {};
00157 
00158     template<>
00159     struct RTT_CORBA_API AnyConversion<CORBA::Any_ptr>
00160     {
00161         typedef CORBA::Any_ptr CorbaType;
00162         typedef CORBA::Any_ptr StdType;
00163 
00164         static bool update(const CORBA::Any& any, CORBA::Any_ptr _value) {
00165             //Logger::log() << Logger::Debug << "Updating type CORBA::Any_ptr with CORBA::Any." <<Logger::endl;
00166             *_value = any;
00167             return true;
00168         }
00169 
00170         static CORBA::Any_ptr createAny( CORBA::Any_ptr t ) {
00171             //Logger::log() << Logger::Debug << "Duplicating CORBA::Any_ptr." <<Logger::endl;
00172             return new CORBA::Any(*t);
00173         }
00174     };
00175 
00176     template<>
00177     struct RTT_CORBA_API AnyConversion<CORBA::Any_var>
00178     {
00179         typedef CORBA::Any_ptr CorbaType;
00180         typedef CORBA::Any_var StdType;
00181 
00182         static bool update(const CORBA::Any& any, CORBA::Any_var _value) {
00183             //Logger::log() << Logger::Debug << "Updating type CORBA::Any_var with CORBA::Any." <<Logger::endl;
00184             *_value.out() = any;
00185             return true;
00186         }
00187 
00188         static CORBA::Any_ptr createAny( CORBA::Any_var t ) {
00189             //Logger::log() << Logger::Debug << "Duplicating CORBA::Any_var." <<Logger::endl;
00190             return new CORBA::Any( t.in() );
00191         }
00192     };
00193 
00194     template<>
00195     struct RTT_CORBA_API AnyConversion<PropertyBag>
00196     {
00197         typedef Corba::AttributeInterface_ptr CorbaType;
00198         typedef PropertyBag StdType;
00199 
00200         static bool update(const CORBA::Any& any, StdType& _value);
00201         static CORBA::Any_ptr createAny( StdType t );
00202     };
00203 
00204     template<>
00205     struct RTT_CORBA_API AnyConversion<bool>
00206     {
00207         typedef CORBA::Boolean CorbaType;
00208         typedef bool StdType;
00209         static CORBA::Any::from_boolean toAny( bool t ) {
00210             //Logger::log() << Logger::Debug << "Converting type 'bool' to from_boolean." <<Logger::endl;
00211             return CORBA::Any::from_boolean(t);
00212         }
00213         static CORBA::Any::to_boolean fromAny( CORBA::Boolean& t ) {
00214             return CORBA::Any::to_boolean(t);
00215         }
00216         static StdType get(const CORBA::Boolean t) {
00217             return t;
00218         }
00219 
00220         static bool update(const CORBA::Any& any, StdType& _value) {
00221             CorbaType result;
00222             if ( any >>= AnyConversion<bool>::fromAny( result ) ) {
00223                 _value = AnyConversion<bool>::get(result);
00224                 return true;
00225             }
00226             return false;
00227         }
00228 
00229         static CORBA::Any_ptr createAny( bool t ) {
00230             CORBA::Any_ptr ret = new CORBA::Any();
00231             *ret <<= toAny( t );
00232             return ret;
00233         }
00234     };
00235 
00236     template<>
00237     struct RTT_CORBA_API AnyConversion<char>
00238     {
00239         typedef CORBA::Char CorbaType;
00240         typedef char StdType;
00241         static CORBA::Any::from_char toAny( StdType t ) {
00242             return CORBA::Any::from_char(t);
00243         }
00244         static CORBA::Any::to_char fromAny( CorbaType& t ) {
00245             return CORBA::Any::to_char(t);
00246         }
00247         static StdType get(const CorbaType t) {
00248             return t;
00249         }
00250 
00251         static bool update(const CORBA::Any& any, StdType& _value) {
00252             CorbaType result;
00253             if ( any >>= AnyConversion<StdType>::fromAny( result ) ) {
00254                 _value = AnyConversion<StdType>::get(result);
00255                 return true;
00256             }
00257             return false;
00258         }
00259 
00260         static CORBA::Any_ptr createAny( char t ) {
00261             CORBA::Any_ptr ret = new CORBA::Any();
00262             *ret <<= toAny( t );
00263             return ret;
00264         }
00265     };
00266 
00267     template<>
00268     struct RTT_CORBA_API AnyConversion<std::string>
00269     {
00270         typedef const char* CorbaType;
00271         typedef std::string StdType;
00272         static CorbaType toAny(const std::string& orig) {
00273             //Logger::log() << Logger::Debug << "Converting type 'string' to const char*." <<Logger::endl;
00274             return orig.c_str();
00275         }
00276 
00277         static StdType get(const CorbaType t) {
00278             return StdType( t );
00279         }
00280 
00281         static bool update(const CORBA::Any& any, StdType& _value) {
00282             CorbaType result;
00283             //Logger::log() << Logger::Debug << "Updating std::string with Any." <<Logger::endl;
00284             if ( any >>= result ) {
00285                 _value = result;
00286                 return true;
00287             }
00288             return false;
00289         }
00290 
00291         static CORBA::Any_ptr createAny( const std::string& t ) {
00292             CORBA::Any_ptr ret = new CORBA::Any();
00293             *ret <<= toAny( t );
00294             return ret;
00295         }
00296 
00297     };
00298 
00299     template<>
00300     struct RTT_CORBA_API AnyConversion< std::vector<double> >
00301     {
00302         typedef Corba::DoubleSequence CorbaType;
00303         typedef std::vector<double> StdType;
00304         static CorbaType* toAny(const std::vector<double>& orig) {
00305             //Logger::log() << Logger::Debug << "Converting type 'std::vector<double>' to sequence<CORBA::Double>." <<Logger::endl;
00306             CorbaType* ret = new CorbaType();
00307             ret->length( (CORBA::ULong)(orig.size()) );
00308             for( size_t i = 0; i != orig.size(); ++i)
00309                 (*ret)[(CORBA::ULong)(i)] = orig[i];
00310             return ret;
00311         }
00312 
00313         static StdType get(const CorbaType* t) {
00314             StdType ret;
00315             ret.resize( t->length() );
00316             for( size_t i = 0; i != t->length(); ++i)
00317                 ret[i] = (*t)[(CORBA::ULong)(i)];
00318             return ret;
00319         }
00320 
00321         static bool update(const CORBA::Any& any, StdType& _value) {
00322             CorbaType* result;
00323             if ( any >>= result ) {
00324                 _value.resize( result->length() );
00325                 for( size_t i = 0; i != result->length(); ++i)
00326                     _value[i] = (*result)[(CORBA::ULong)(i)];
00327                 return true;
00328             }
00329             return false;
00330         }
00331 
00332         static CORBA::Any_ptr createAny( const std::vector<double>& t ) {
00333             CORBA::Any_ptr ret = new CORBA::Any();
00334             *ret <<= toAny( t );
00335             return ret;
00336         }
00337 
00338     };
00339 
00340 
00341 }
00342 
00343 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3