CorbaConversion.hpp
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
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
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
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
00153
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
00166 *_value = any;
00167 return true;
00168 }
00169
00170 static CORBA::Any_ptr createAny( CORBA::Any_ptr t ) {
00171
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
00184 *_value.out() = any;
00185 return true;
00186 }
00187
00188 static CORBA::Any_ptr createAny( CORBA::Any_var t ) {
00189
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
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
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
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
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