Orocos Real-Time Toolkit  2.8.3
CorbaConversion.hpp
Go to the documentation of this file.
1 /***************************************************************************
2 tag: Peter Soetens Mon Jun 26 13:25:58 CEST 2006 CorbaConversion.hpp
3 
4 CorbaConversion.hpp - description
5 -------------------
6 begin : Mon June 26 2006
7 copyright : (C) 2006 Peter Soetens
8 email : peter.soetens@fmtc.be
9 
10 ***************************************************************************
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU General Public *
13 * License as published by the Free Software Foundation; *
14 * version 2 of the License. *
15 * *
16 * As a special exception, you may use this file as part of a free *
17 * software library without restriction. Specifically, if other files *
18 * instantiate templates or use macros or inline functions from this *
19 * file, or you compile this file and link it with other files to *
20 * produce an executable, this file does not by itself cause the *
21 * resulting executable to be covered by the GNU General Public *
22 * License. This exception does not however invalidate any other *
23 * reasons why the executable file might be covered by the GNU General *
24 * Public License. *
25 * *
26 * This library is distributed in the hope that it will be useful, *
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29 * Lesser General Public License for more details. *
30 * *
31 * You should have received a copy of the GNU General Public *
32 * License along with this library; if not, write to the Free Software *
33 * Foundation, Inc., 59 Temple Place, *
34 * Suite 330, Boston, MA 02111-1307 USA *
35 * *
36 ***************************************************************************/
37 
38 
39 #ifndef ORO_CORBA_CONVERSION_HPP
40 #define ORO_CORBA_CONVERSION_HPP
41 
42 #include <string>
43 #include <vector>
44 #include <map>
45 
46 #include "corba.h"
47 #ifdef CORBA_IS_TAO
48 #include <tao/Version.h>
49 #if TAO_MAJOR_VERSION == 1 && TAO_MINOR_VERSION <= 4
50 #include <tao/Any.h>
51 #else // TAO 1.5 and higher
52 #include <tao/AnyTypeCode/Any.h>
53 #endif
54 #include <tao/CORBA_String.h>
55 #else
56 #include "corba.h"
57 #include <omniORB4/stringtypes.h>
58 #endif
59 
60 #include "OrocosTypesC.h"
61 #include "../../Logger.hpp"
62 #include "../../internal/DataSourceTypeInfo.hpp"
63 
64 
65 namespace RTT {
66  namespace corba {
67 
80  template<class Type>
82  {
86  typedef CORBA::Any CorbaType;
90  typedef Type StdType;
95 
103  static bool toStdType(StdType& tp, const CorbaType& cb) {
104  Logger::log() << Logger::Error << "Failing conversion of CorbaType to type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
105  return false;
106  }
107 
115  static bool toCorbaType(CorbaType& cb, const StdType& tp) {
116  Logger::log() << Logger::Error << "Failing conversion of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"to a CorbaType." <<Logger::endl;
117  return false;
118  }
119 
127  static bool update(const CORBA::Any& any, StdType tp) {
128  Logger::log() << Logger::Error << "Failing conversion of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
129  return false;
130  }
131 
138  static CORBA::Any_ptr createAny( StdType tp ) {
139  Logger::log() << Logger::Error << "Failing corba::Any creation of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
140  return new CORBA::Any();
141  }
142 
150  static bool updateAny( StdType tp, CORBA::Any& any ) {
151  Logger::log() << Logger::Error << "Failing corba::Any updating of type "<<internal::DataSourceTypeInfo<StdType>::getType()<<"." <<Logger::endl;
152  return false;
153  }
154  };
155 
160  template<class Type, class _CorbaType = Type>
162  {
163  typedef _CorbaType CorbaType;
164  typedef Type StdType;
165  static CorbaType toAny( Type t ) {
166  //Logger::log() << Logger::Debug << "Converting type "<<internal::DataSourceTypeInfo<Type>::getType()<<" to same CORBA type." <<Logger::endl;
167  return t;
168  }
169 
170  static Type& fromAny( Type& t ) {
171  return t;
172  }
173 
174  static const Type& get(const Type& t) {
175  return t;
176  }
177 
178  static bool toStdType(StdType& tp, const CorbaType& cb) {
179  tp = cb;
180  return true;
181  }
182 
183  static bool toCorbaType(CorbaType& cb, const StdType& tp) {
184  cb = tp;
185  return true;
186  }
187 
188  static bool update(const CORBA::Any& any, StdType& _value) {
189  CorbaType temp;
190  if ( any >>= temp ) {
191  _value = temp;
192  return true;
193  }
194  return false;
195  }
196 
197  static CORBA::Any_ptr createAny( const Type& t ) {
198  CORBA::Any_ptr ret = new CORBA::Any();
199  //Logger::log() << Logger::Debug << "Creating corba::Any from "<<internal::DataSourceTypeInfo<Type>::getType()<<"." <<Logger::endl;
200  *ret <<= toAny( static_cast<CorbaType>(t) );
201  return ret;
202  }
203 
204  static bool updateAny( const Type& t, CORBA::Any& any ) {
205  //Logger::log() << Logger::Debug << "Updating corba::Any from "<<internal::DataSourceTypeInfo<Type>::getType()<<"." <<Logger::endl;
206  any <<= toAny( static_cast<CorbaType>(t) );
207  return true;
208  }
209  };
210 
216  template<class T, class _Alloc>
217  struct AnyConversion< std::vector<T, _Alloc> >
218  {
220 
222  typedef std::vector<T, _Alloc> StdType;
223 
224  static bool toStdType(StdType& tp, const CorbaType& cb) {
225  bool res = true;
226  tp.resize( cb.length() );
227  for (size_t i = 0; i != cb.length(); ++i) {
228  res = res && AnyConversion<T>::toStdType(tp[i], cb[(CORBA::ULong)(i)]);
229  }
230  return res;
231  }
232 
233  static bool toCorbaType(CorbaType& cb, const StdType& tp) {
234  bool res = true;
235  cb.length( (CORBA::ULong)(tp.size()) );
236  for( size_t i = 0; i != tp.size(); ++i)
237  res = res && AnyConversion<T>::toCorbaType(cb[(CORBA::ULong)(i)], tp[i]);
238  return res;
239  }
240 
244  static bool toStdType(StdType& tp, const CORBA::Any& any) {
245  return update(any, tp);
246  }
247  static bool toCorbaType(CORBA::Any& any, const StdType& tp) {
248  return updateAny(tp, any);
249  }
250 
251  static CorbaType* toAny(const StdType& tp) {
252  CorbaType* cb = new CorbaType();
253  toCorbaType(*cb, tp);
254  return cb;
255  }
256 
257  static bool update(const CORBA::Any& any, StdType& _value) {
258  CorbaType* result;
259  if ( any >>= result ) {
260  return toStdType(_value, *result);
261  }
262  return false;
263  }
264 
265  static CORBA::Any_ptr createAny( const StdType& t ) {
266  CORBA::Any_ptr ret = new CORBA::Any();
267  *ret <<= toAny( t );
268  return ret;
269  }
270 
271  static bool updateAny( StdType const& t, CORBA::Any& any ) {
272  any <<= toAny( t );
273  return true;
274  }
275  };
276 
282  template<class T1, class T2>
283  struct AnyConversion<std::pair<T1, T2> > {
285 
287  typedef std::pair<T1, T2> StdType;
288 
289  static bool toStdType(StdType& tp, const CorbaType& cb) {
290  return AnyConversion<T1>::update(cb.t1, tp.first) && AnyConversion<T2>::update(cb.t2, tp.second);
291  }
292 
293  static bool toCorbaType(CorbaType& cb, const StdType& tp) {
294  return AnyConversion<T1>::updateAny(tp.first, cb.t1) && AnyConversion<T2>::updateAny(tp.second, cb.t2);
295  }
296 
297  static CorbaType* toAny(const StdType& tp) {
298  CorbaType* cb = new CorbaType();
299  toCorbaType(*cb, tp);
300  return cb;
301  }
302 
303  static StdType get(const CorbaType* cb) {
304  StdType tp;
305  toStdType(tp, *cb);
306  return tp;
307  }
308 
309  static bool update(const CORBA::Any& any, StdType& _value) {
310  CorbaType* result;
311  if ( any >>= result ) {
312  return toStdType(_value, *result);
313  }
314  return false;
315  }
316 
317  static CORBA::Any_ptr createAny( const StdType& t ) {
318  CORBA::Any_ptr ret = new CORBA::Any();
319  *ret <<= toAny( t );
320  return ret;
321  }
322 
323  static bool updateAny( StdType const& t, CORBA::Any& any ) {
324  any <<= toAny( t );
325  return true;
326  }
327  };
328 
334  template<class T1, class T2, class _Compare, class _Alloc>
335  struct AnyConversion<std::map<T1, T2, _Compare, _Alloc> > {
337 
339  typedef std::map<T1, T2, _Compare, _Alloc> StdType;
340 
341  static bool toStdType(StdType& tp, const CorbaType& cb) {
342  bool res = true;
343  tp.clear();
344 
345  for (size_t i = 0; i != cb.length(); ++i) {
346  std::pair<T1, T2> p;
347  res = res && AnyConversion<std::pair<T1, T2> >::toStdType(p, cb[(CORBA::ULong)(i)]);
348  tp.insert(p);
349  }
350  return res;
351  }
352 
353  static bool toCorbaType(CorbaType& cb, const StdType& tp) {
354  bool res = true;
355  cb.length(tp.size());
356  typename StdType::const_iterator it = tp.begin();
357 
358  for(size_t i = 0; i != tp.size(); i++) {
359  res = res &&AnyConversion<std::pair<T1, T2> >::toCorbaType(cb[(CORBA::ULong)(i)], *it);
360  it++;
361  }
362  return res;
363  }
364 
368  static bool toStdType(StdType& tp, const CORBA::Any& any) {
369  return update(any, tp);
370  }
371  static bool toCorbaType(CORBA::Any& any, const StdType& tp) {
372  return updateAny(tp, any);
373  }
374 
375  static CorbaType* toAny(const StdType& tp) {
376  CorbaType* cb = new CorbaType();
377  toCorbaType(*cb, tp);
378  return cb;
379  }
380 
381  static StdType get(const CorbaType* cb) {
382  StdType sb;
383  toStdType(sb, *cb);
384  return sb;
385  }
386 
387  static bool update(const CORBA::Any& any, StdType& _value) {
388  CorbaType* result;
389  if ( any >>= result ) {
390  return toStdType(_value, *result);
391  }
392  return false;
393  }
394 
395  static CORBA::Any_ptr createAny( const StdType& t ) {
396  CORBA::Any_ptr ret = new CORBA::Any();
397  *ret <<= toAny( t );
398  return ret;
399  }
400 
401  static bool updateAny( StdType const& t, CORBA::Any& any ) {
402  any <<= toAny( t );
403  return true;
404  }
405  };
406  }
407 }
408 
409 #endif
static bool toCorbaType(CorbaType &cb, const StdType &tp)
static bool toStdType(StdType &tp, const CorbaType &cb)
CORBA::Any CorbaType
corba type
static bool updateAny(StdType tp, CORBA::Any &any)
Updates an CORBA::Any object from of a C++/IDL type.
This class converts a given application-specific type to a CORBA::Any object and vice versa...
static bool toCorbaType(CorbaType &cb, const StdType &tp)
static bool toStdType(StdType &tp, const CorbaType &cb)
static bool toCorbaType(CorbaType &cb, const StdType &tp)
static bool update(const CORBA::Any &any, StdType tp)
Updates tp with the contents of any.
static bool toStdType(StdType &tp, const CorbaType &cb)
static bool toCorbaType(CorbaType &cb, const StdType &tp)
STL namespace.
static CorbaType toAny(Type t)
static bool toStdType(StdType &tp, const CORBA::Any &any)
if T is a std::vector or a std::map the associated type is CORBA::Any
static CORBA::Any_ptr createAny(const Type &t)
static bool toCorbaType(CorbaType &cb, const StdType &tp)
Updates cb with the contents of tp.
static bool toStdType(StdType &tp, const CORBA::Any &any)
if T is a std::vector or a std::map the associated type is CORBA::Any
static bool updateAny(StdType const &t, CORBA::Any &any)
static const std::string & getType()
Return the qualified type.
static CORBA::Any_ptr createAny(const StdType &t)
static bool update(const CORBA::Any &any, StdType &_value)
sequence< Pair > PairSeq
Definition: OrocosTypes.idl:23
static CorbaType * toAny(const StdType &tp)
Used for the conversion of types that are binary compatible between CORBA type and std C++ type...
static CORBA::Any_ptr createAny(const StdType &t)
static bool toStdType(StdType &tp, const CorbaType &cb)
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
static bool toStdType(StdType &tp, const CorbaType &cb)
Updates tp with the contents of cb.
static bool update(const CORBA::Any &any, StdType &_value)
sequence< any > CAnySequence
Definition: OrocosTypes.idl:21
static bool toCorbaType(CORBA::Any &any, const StdType &tp)
static bool update(const CORBA::Any &any, StdType &_value)
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
static bool updateAny(StdType const &t, CORBA::Any &any)
static CORBA::Any_ptr createAny(StdType tp)
Creates an CORBA::Any object out of a C++/IDL type.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
static Type & fromAny(Type &t)
static bool toCorbaType(CORBA::Any &any, const StdType &tp)
static bool update(const CORBA::Any &any, StdType &_value)
RTT::corba::CAnySequence sequence
sequence of corba type (used to convert stl container)
static bool updateAny(StdType const &t, CORBA::Any &any)
static bool updateAny(const Type &t, CORBA::Any &any)