Any_Basic_Impl.cpp

Go to the documentation of this file.
00001 // $Id: Any_Basic_Impl.cpp 77409 2007-02-26 23:48:49Z ossama $
00002 
00003 #include "tao/AnyTypeCode/Any_Basic_Impl.h"
00004 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
00005 #include "tao/AnyTypeCode/Any.h"
00006 #include "tao/AnyTypeCode/TypeCode.h"
00007 #include "tao/CDR.h"
00008 #include "tao/SystemException.h"
00009 
00010 #include "ace/Auto_Ptr.h"
00011 #include "ace/OS_NS_string.h"
00012 
00013 ACE_RCSID (AnyTypeCode,
00014            Any_Basic_Impl,
00015            "$Id: Any_Basic_Impl.cpp 77409 2007-02-26 23:48:49Z ossama $")
00016 
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 namespace TAO
00020 {
00021   Any_Basic_Impl::Any_Basic_Impl (CORBA::TypeCode_ptr tc,
00022                                   void *value)
00023     : Any_Impl (0, tc),
00024       kind_ (CORBA::tk_null)
00025   {
00026     this->kind_ = TAO::unaliased_kind (tc);
00027 
00028     switch (this->kind_)
00029     {
00030       case CORBA::tk_short:
00031         this->u_.s = *static_cast<CORBA::Short *> (value);
00032         break;
00033       case CORBA::tk_ushort:
00034         this->u_.us = *static_cast<CORBA::UShort *> (value);
00035         break;
00036       case CORBA::tk_long:
00037         this->u_.l = *static_cast<CORBA::Long *> (value);
00038         break;
00039       case CORBA::tk_ulong:
00040          this->u_.ul = *static_cast<CORBA::ULong *> (value);
00041         break;
00042       case CORBA::tk_float:
00043         this->u_.f = *static_cast<CORBA::Float *> (value);
00044         break;
00045       case CORBA::tk_double:
00046         this->u_.d = *static_cast<CORBA::Double *> (value);
00047         break;
00048       case CORBA::tk_boolean:
00049         this->u_.b = *static_cast<CORBA::Boolean *> (value);
00050         break;
00051       case CORBA::tk_char:
00052         this->u_.c = *static_cast<CORBA::Char *> (value);
00053         break;
00054       case CORBA::tk_octet:
00055         this->u_.o = *static_cast<CORBA::Octet *> (value);
00056         break;
00057       case CORBA::tk_longlong:
00058         this->u_.ll = *static_cast<CORBA::LongLong *> (value);
00059         break;
00060 #if !defined (ACE_LACKS_LONGLONG_T)
00061       case CORBA::tk_ulonglong:
00062   #if !defined (ACE_LACKS_UNSIGNEDLONGLONG_T)
00063         this->u_.ull = *static_cast<CORBA::ULongLong *> (value);
00064   #else
00065         this->u_.ull = *static_cast<CORBA::LongLong *> (value);
00066   #endif
00067         break;
00068 #endif
00069       case CORBA::tk_longdouble:
00070         this->u_.ld = *static_cast<CORBA::LongDouble *> (value);
00071         break;
00072       case CORBA::tk_wchar:
00073         this->u_.wc = *static_cast<CORBA::WChar *> (value);
00074         break;
00075       default:
00076         break;
00077     }
00078   }
00079 
00080   Any_Basic_Impl::~Any_Basic_Impl (void)
00081   {
00082   }
00083 
00084   void
00085   Any_Basic_Impl::insert (CORBA::Any &any,
00086                           CORBA::TypeCode_ptr tc,
00087                           const void *value)
00088   {
00089     Any_Basic_Impl *new_impl = 0;
00090     ACE_NEW (new_impl,
00091              Any_Basic_Impl (tc,
00092                              const_cast<void *> (value)));
00093     any.replace (new_impl);
00094   }
00095 
00096   CORBA::Boolean
00097   Any_Basic_Impl::extract (const CORBA::Any &any,
00098                            CORBA::TypeCode_ptr tc,
00099                            void *_tao_elem)
00100   {
00101     try
00102       {
00103         CORBA::TypeCode_ptr any_tc = any._tao_get_typecode ();
00104         CORBA::Boolean const _tao_equiv =
00105           any_tc->equivalent (tc);
00106 
00107         if (!_tao_equiv)
00108           {
00109             return false;
00110           }
00111 
00112         TAO::Any_Impl * const impl = any.impl ();
00113 
00114         if (impl && !impl->encoded ())
00115           {
00116             TAO::Any_Basic_Impl * const narrow_impl =
00117               dynamic_cast<TAO::Any_Basic_Impl *> (impl);
00118 
00119             if (narrow_impl == 0)
00120               {
00121                 return false;
00122               }
00123 
00124             Any_Basic_Impl::assign_value (_tao_elem, narrow_impl);
00125             return true;
00126           }
00127 
00128         TAO::Any_Basic_Impl *replacement =
00129           TAO::Any_Basic_Impl::create_empty (any_tc);
00130 
00131         auto_ptr<TAO::Any_Basic_Impl> replacement_safety (replacement);
00132 
00133         // We know this will work since the unencoded case is covered above.
00134         TAO::Unknown_IDL_Type * const unk =
00135           dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00136 
00137         if (!unk)
00138           return false;
00139 
00140         // Get the kind of the type where we are extracting in ie. the
00141         // aliased  type if there are any. Passing the aliased kind
00142         // will not help.
00143         CORBA::TCKind const tck = tc->kind ();
00144 
00145         // We don't want the rd_ptr of unk to move, in case it is
00146         // shared by another Any. This copies the state, not the buffer.
00147         TAO_InputCDR for_reading (unk->_tao_get_cdr ());
00148 
00149         CORBA::Boolean const good_decode =
00150           replacement->demarshal_value (for_reading,
00151                                         static_cast<CORBA::Long> (tck));
00152 
00153         if (good_decode)
00154           {
00155             Any_Basic_Impl::assign_value (_tao_elem,
00156                                           replacement,
00157                                           tck);
00158             const_cast<CORBA::Any &> (any).replace (replacement);
00159             replacement_safety.release ();
00160             return true;
00161           }
00162 
00163         // Duplicated by Any_Impl base class constructor.
00164         ::CORBA::release (any_tc);
00165       }
00166     catch (const ::CORBA::Exception&)
00167       {
00168       }
00169 
00170     return false;
00171   }
00172 
00173   CORBA::Boolean
00174   Any_Basic_Impl::marshal_value (TAO_OutputCDR &cdr)
00175   {
00176     CORBA::TCKind const tckind = static_cast<CORBA::TCKind> (this->kind_);
00177 
00178     switch (tckind)
00179     {
00180       case CORBA::tk_short:
00181         return cdr << this->u_.s;
00182       case CORBA::tk_ushort:
00183         return cdr << this->u_.us;
00184       case CORBA::tk_long:
00185         return cdr << this->u_.l;
00186       case CORBA::tk_ulong:
00187         return cdr << this->u_.ul;
00188       case CORBA::tk_float:
00189         return cdr << this->u_.f;
00190       case CORBA::tk_double:
00191         return cdr << this->u_.d;
00192       case CORBA::tk_boolean:
00193         return cdr << CORBA::Any::from_boolean (this->u_.b);
00194       case CORBA::tk_char:
00195         return cdr << CORBA::Any::from_char (this->u_.c);
00196       case CORBA::tk_octet:
00197         return cdr << CORBA::Any::from_octet (this->u_.o);
00198       case CORBA::tk_longlong:
00199         return cdr << this->u_.ll;
00200 #if !defined (ACE_LACKS_LONGLONG_T)
00201       case CORBA::tk_ulonglong:
00202         return cdr << this->u_.ull;
00203 #endif
00204       case CORBA::tk_longdouble:
00205         return cdr << this->u_.ld;
00206       case CORBA::tk_wchar:
00207         return cdr << CORBA::Any::from_wchar (this->u_.wc);
00208       default:
00209         return false;
00210     }
00211   }
00212 
00213   CORBA::Boolean
00214   Any_Basic_Impl::demarshal_value (TAO_InputCDR &cdr)
00215   {
00216     return this->demarshal_value (cdr,
00217                                   this->kind_);
00218   }
00219 
00220   CORBA::Boolean
00221   Any_Basic_Impl::demarshal_value (TAO_InputCDR &cdr,
00222                                    CORBA::Long tck)
00223   {
00224     CORBA::TCKind const tckind = static_cast<CORBA::TCKind> (tck);
00225     switch (tckind)
00226     {
00227       case CORBA::tk_short:
00228         return cdr >> this->u_.s;
00229       case CORBA::tk_ushort:
00230         return cdr >> this->u_.us;
00231       case CORBA::tk_long:
00232         return cdr >> this->u_.l;
00233       case CORBA::tk_ulong:
00234         return cdr >> this->u_.ul;
00235       case CORBA::tk_float:
00236         return cdr >> this->u_.f;
00237       case CORBA::tk_double:
00238         return cdr >> this->u_.d;
00239       case CORBA::tk_boolean:
00240         return cdr >> CORBA::Any::to_boolean (this->u_.b);
00241       case CORBA::tk_char:
00242         return cdr >> CORBA::Any::to_char (this->u_.c);
00243       case CORBA::tk_octet:
00244         return cdr >> CORBA::Any::to_octet (this->u_.o);
00245       case CORBA::tk_longlong:
00246         return cdr >> this->u_.ll;
00247 #if !defined (ACE_LACKS_LONGLONG_T)
00248       case CORBA::tk_ulonglong:
00249         return cdr >> this->u_.ull;
00250 #endif
00251       case CORBA::tk_longdouble:
00252         return cdr >> this->u_.ld;
00253       case CORBA::tk_wchar:
00254         return cdr >> CORBA::Any::to_wchar (this->u_.wc);
00255       default:
00256         return 0;
00257     }
00258   }
00259 
00260   void
00261   Any_Basic_Impl::_tao_decode (TAO_InputCDR &cdr)
00262   {
00263     if (! this->demarshal_value (cdr))
00264       {
00265         throw ::CORBA::MARSHAL ();
00266       }
00267   }
00268 
00269   Any_Basic_Impl *
00270   Any_Basic_Impl::create_empty (CORBA::TypeCode_ptr tc)
00271   {
00272     CORBA::TCKind const kind = tc->kind ();
00273 
00274     TAO::Any_Basic_Impl * retval = 0;
00275 
00276     switch (kind)
00277     {
00278       case CORBA::tk_longlong:
00279         {
00280           CORBA::LongLong tmp = ACE_CDR_LONGLONG_INITIALIZER;
00281           ACE_NEW_RETURN (retval,
00282                           TAO::Any_Basic_Impl (tc, &tmp),
00283                           0);
00284         }
00285 
00286         break;
00287       case CORBA::tk_longdouble:
00288         {
00289           CORBA::LongDouble tmp = ACE_CDR_LONG_DOUBLE_INITIALIZER;
00290           ACE_NEW_RETURN (retval,
00291                           TAO::Any_Basic_Impl (tc, &tmp),
00292                           0);
00293         }
00294 
00295         break;
00296       default:
00297         {
00298           CORBA::ULongLong tmp = 0;
00299           ACE_NEW_RETURN (retval,
00300                           TAO::Any_Basic_Impl (tc, &tmp),
00301                           0);
00302         }
00303 
00304         break;
00305     }
00306 
00307     return retval;
00308   }
00309 
00310   void
00311   Any_Basic_Impl::assign_value (void *dest, Any_Basic_Impl *src)
00312   {
00313     Any_Basic_Impl::assign_value (dest,
00314                                   src,
00315                                   src->kind_);
00316   }
00317 
00318   void
00319   Any_Basic_Impl::assign_value (void *dest,
00320                                 Any_Basic_Impl *src,
00321                                 CORBA::Long tck)
00322   {
00323     CORBA::TCKind const kind = static_cast<CORBA::TCKind> (tck);
00324 
00325     switch (kind)
00326       {
00327       case CORBA::tk_short:
00328         *static_cast<CORBA::Short *> (dest) = src->u_.s;
00329         break;
00330       case CORBA::tk_ushort:
00331         *static_cast<CORBA::UShort *> (dest) = src->u_.us;
00332         break;
00333       case CORBA::tk_long:
00334         *static_cast<CORBA::Long *> (dest) = src->u_.l;
00335         break;
00336       case CORBA::tk_ulong:
00337         *static_cast<CORBA::ULong *> (dest) = src->u_.ul;
00338         break;
00339       case CORBA::tk_float:
00340         *static_cast<CORBA::Float *> (dest) = src->u_.f;
00341         break;
00342       case CORBA::tk_double:
00343         *static_cast<CORBA::Double *> (dest) = src->u_.d;
00344         break;
00345       case CORBA::tk_boolean:
00346         *static_cast<CORBA::Boolean *> (dest) = src->u_.b;
00347         break;
00348       case CORBA::tk_char:
00349         *static_cast<CORBA::Char *> (dest) = src->u_.c;
00350         break;
00351       case CORBA::tk_octet:
00352         *static_cast<CORBA::Octet *> (dest) = src->u_.o;
00353         break;
00354       case CORBA::tk_longlong:
00355         *static_cast<CORBA::LongLong *> (dest) = src->u_.ll;
00356         break;
00357 #if !defined (ACE_LACKS_LONGLONG_T)
00358       case CORBA::tk_ulonglong:
00359   #if !defined (ACE_LACKS_UNSIGNEDLONGLONG_T)
00360         *static_cast<CORBA::ULongLong *> (dest) = src->u_.ull;
00361         break;
00362   #else
00363         *static_cast<CORBA::LongLong *> (dest) = src->u_.ull;
00364   #endif
00365 #endif
00366       case CORBA::tk_longdouble:
00367         *static_cast<CORBA::LongDouble *> (dest) = src->u_.ld;
00368         break;
00369       case CORBA::tk_wchar:
00370         *static_cast<CORBA::WChar *> (dest) = src->u_.wc;
00371         break;
00372       default:
00373         break;
00374       }
00375   }
00376 }
00377 
00378 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 13:21:06 2008 for TAO_AnyTypeCode by doxygen 1.3.6