00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 #ifndef ORO_CORBA_CONVERSION_HPP
00040 #define ORO_CORBA_CONVERSION_HPP
00041 
00042 #include <string>
00043 #include <vector>
00044 #include <map>
00045 
00046 #include "corba.h"
00047 #ifdef CORBA_IS_TAO
00048 #include <tao/Version.h>
00049 #if TAO_MAJOR_VERSION == 1 && TAO_MINOR_VERSION <= 4
00050 #include <tao/Any.h>
00051 #else // TAO 1.5 and higher
00052 #include <tao/AnyTypeCode/Any.h>
00053 #endif
00054 #include <tao/CORBA_String.h>
00055 #else
00056 #include "corba.h"
00057 #include <omniORB4/stringtypes.h>
00058 #endif
00059 
00060 #include "OrocosTypesC.h"
00061 #include "../../Logger.hpp"
00062 #include "../../internal/DataSourceTypeInfo.hpp"
00063 
00064 
00065 namespace RTT { 
00066   namespace corba {
00067 
00080     template<class Type>
00081     struct AnyConversion
00082     {
00086       typedef CORBA::Any CorbaType;
00090       typedef Type StdType;
00094       typedef RTT::corba::CAnySequence sequence;
00095 
00103       static bool toStdType(StdType& tp, const CorbaType& cb) {
00104         Logger::log() << Logger::Error << "Failing conversion of CorbaType to type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
00105         return false;
00106       }
00107 
00115       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00116         Logger::log() << Logger::Error << "Failing conversion of  type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"to a CorbaType." <<Logger::endl;
00117         return false;
00118       }
00119 
00127       static bool update(const CORBA::Any& any, StdType tp) {
00128         Logger::log() << Logger::Error << "Failing conversion of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
00129         return false;
00130       }
00131 
00138       static CORBA::Any_ptr createAny( StdType tp ) {
00139         Logger::log() << Logger::Error << "Failing corba::Any creation of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
00140         return new CORBA::Any();
00141       }
00142 
00150       static bool updateAny( StdType tp, CORBA::Any& any ) {
00151         Logger::log() << Logger::Error << "Failing corba::Any updating of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
00152         return false;
00153       }
00154     };
00155 
00160     template<class Type, class _CorbaType = Type>
00161     struct AnyConversionHelper
00162     {
00163       typedef _CorbaType CorbaType;
00164       typedef Type StdType;
00165       static CorbaType toAny( Type t ) {
00166         
00167         return t;
00168       }
00169 
00170       static Type& fromAny( Type& t ) {
00171         return t;
00172       }
00173 
00174       static const Type& get(const Type& t) {
00175         return t;
00176       }
00177 
00178       static bool toStdType(StdType& tp, const CorbaType& cb) {
00179         tp = cb;
00180         return true;
00181       }
00182 
00183       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00184         cb = tp;
00185         return true;
00186       }
00187 
00188       static bool update(const CORBA::Any& any, StdType& _value) {
00189         CorbaType temp;
00190         if ( any >>= temp ) {
00191           _value = temp;
00192           return true;
00193         }
00194         return false;
00195       }
00196 
00197       static CORBA::Any_ptr createAny( const Type& t ) {
00198         CORBA::Any_ptr ret = new CORBA::Any();
00199         
00200         *ret <<= toAny( static_cast<CorbaType>(t) );
00201         return ret;
00202       }
00203 
00204       static bool updateAny( const Type& t, CORBA::Any& any ) {
00205         
00206         any <<= toAny( static_cast<CorbaType>(t) );
00207         return true;
00208       }
00209     };
00210 
00216     template<class T>
00217     struct AnyConversion< std::vector<T> >
00218     {
00219       typedef RTT::corba::CAnySequence sequence;
00220 
00221       typedef typename AnyConversion<T>::sequence CorbaType;
00222       typedef std::vector<T> StdType;
00223 
00224       static bool toStdType(StdType& tp, const CorbaType& cb) {
00225         bool res = true;
00226         tp.resize( cb.length() );
00227         for (size_t i = 0; i != cb.length(); ++i) {
00228           res = res && AnyConversion<T>::toStdType(tp[i], cb[(CORBA::ULong)(i)]);
00229         }
00230         return res;
00231       }
00232 
00233       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00234         bool res = true;
00235         cb.length( (CORBA::ULong)(tp.size()) );
00236         for( size_t i = 0; i != tp.size(); ++i) 
00237           res = res && AnyConversion<T>::toCorbaType(cb[(CORBA::ULong)(i)], tp[i]);
00238         return res;
00239       }
00240 
00244       static bool toStdType(StdType& tp, const CORBA::Any& any) {
00245         return update(any, tp);
00246       }
00247       static bool toCorbaType(CORBA::Any& any, const StdType& tp) {
00248         return updateAny(tp, any);
00249       }
00250 
00251       static CorbaType* toAny(const StdType& tp) {
00252         CorbaType* cb = new CorbaType();
00253         toCorbaType(*cb, tp);
00254         return cb;
00255       }
00256 
00257       static bool update(const CORBA::Any& any, StdType& _value) {
00258         CorbaType* result;
00259         if ( any >>= result ) {
00260           return toStdType(_value, *result);
00261         }
00262         return false;
00263       }
00264 
00265       static CORBA::Any_ptr createAny( const StdType& t ) {
00266         CORBA::Any_ptr ret = new CORBA::Any();
00267         *ret <<= toAny( t );
00268         return ret;
00269       }
00270 
00271       static bool updateAny( StdType const& t, CORBA::Any& any ) {
00272         any <<= toAny( t );
00273         return true;
00274       }
00275     };
00276 
00282     template<class T1, class T2>
00283     struct AnyConversion<std::pair<T1, T2> > {
00284       typedef RTT::corba::PairSeq sequence;
00285 
00286       typedef RTT::corba::Pair CorbaType;
00287       typedef std::pair<T1, T2> StdType;
00288 
00289       static bool toStdType(StdType& tp, const CorbaType& cb) {
00290         return AnyConversion<T1>::update(cb.t1, tp.first) && AnyConversion<T2>::update(cb.t2, tp.second);
00291       }
00292 
00293       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00294         return AnyConversion<T1>::updateAny(tp.first, cb.t1) && AnyConversion<T2>::updateAny(tp.second, cb.t2);
00295       }
00296 
00297       static CorbaType* toAny(const StdType& tp) {
00298         CorbaType* cb = new CorbaType();
00299         toCorbaType(*cb, tp);
00300         return cb;
00301       }
00302 
00303       static StdType get(const CorbaType* cb) {
00304         StdType tp;
00305         toStdType(tp, *cb);
00306         return tp;
00307       }
00308 
00309       static bool update(const CORBA::Any& any, StdType& _value) {
00310         CorbaType* result;
00311         if ( any >>= result ) {
00312           return toStdType(_value, *result);
00313         }
00314         return false;
00315       }
00316 
00317       static CORBA::Any_ptr createAny( const StdType& t ) {
00318         CORBA::Any_ptr ret = new CORBA::Any();
00319         *ret <<= toAny( t );
00320         return ret;
00321       }
00322 
00323       static bool updateAny( StdType const& t, CORBA::Any& any ) {
00324         any <<= toAny( t );
00325         return true;
00326       }
00327     };
00328 
00334     template<class T1, class T2>
00335     struct AnyConversion<std::map<T1, T2> > {
00336       typedef RTT::corba::CAnySequence sequence;
00337 
00338       typedef RTT::corba::PairSeq CorbaType;
00339       typedef std::map<T1, T2> StdType;
00340 
00341       static bool toStdType(StdType& tp, const CorbaType& cb) {
00342         bool res = true;
00343         tp.clear();
00344 
00345         for (size_t i = 0; i != cb.length(); ++i) {
00346           std::pair<T1, T2> p;
00347           res = res && AnyConversion<std::pair<T1, T2> >::toStdType(p, cb[(CORBA::ULong)(i)]);
00348           tp.insert(p);
00349         }
00350         return res;
00351       }
00352 
00353       static bool toCorbaType(CorbaType& cb, const StdType& tp) {
00354         bool res = true;
00355         cb.length(tp.size());
00356         typename StdType::const_iterator it = tp.begin();
00357 
00358         for(size_t i = 0; i != tp.size(); i++) {
00359           res = res &&AnyConversion<std::pair<T1, T2> >::toCorbaType(cb[(CORBA::ULong)(i)], *it);
00360           it++;
00361         }
00362         return res;
00363       }
00364 
00368       static bool toStdType(StdType& tp, const CORBA::Any& any) {
00369         return update(any, tp);
00370       }
00371       static bool toCorbaType(CORBA::Any& any, const StdType& tp) {
00372         return updateAny(tp, any);
00373       }
00374 
00375       static CorbaType* toAny(const StdType& tp) {
00376         CorbaType* cb = new CorbaType();
00377         toCorbaType(*cb, tp);
00378         return cb;
00379       }
00380 
00381       static StdType get(const CorbaType* cb) {
00382         StdType sb;
00383         toStdType(sb, *cb);
00384         return sb;
00385       }
00386 
00387       static bool update(const CORBA::Any& any, StdType& _value) {
00388         CorbaType* result;
00389         if ( any >>= result ) {
00390           return toStdType(_value, *result);
00391         }
00392         return false;
00393       }
00394 
00395       static CORBA::Any_ptr createAny( const StdType& t ) {
00396         CORBA::Any_ptr ret = new CORBA::Any();
00397         *ret <<= toAny( t );
00398         return ret;
00399       }
00400 
00401       static bool updateAny( StdType const& t, CORBA::Any& any ) {
00402         any <<= toAny( t );
00403         return true;
00404       }
00405     };
00406   }
00407 }
00408 
00409 #endif