Any_Dual_Impl_T.cpp

Go to the documentation of this file.
00001 // $Id: Any_Dual_Impl_T.cpp 77409 2007-02-26 23:48:49Z ossama $
00002 
00003 #ifndef TAO_ANY_DUAL_IMPL_T_CPP
00004 #define TAO_ANY_DUAL_IMPL_T_CPP
00005 
00006 #include "tao/AnyTypeCode/Any_Dual_Impl_T.h"
00007 #include "tao/AnyTypeCode/Any.h"
00008 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
00009 #include "tao/AnyTypeCode/Marshal.h"
00010 #include "tao/CORBA_String.h"
00011 #include "tao/SystemException.h"
00012 #include "tao/CDR.h"
00013 #include "tao/AnyTypeCode/TypeCode.h"
00014 
00015 #include "ace/Auto_Ptr.h"
00016 #include "ace/OS_Memory.h"
00017 
00018 #if !defined (__ACE_INLINE__)
00019 # include "tao/AnyTypeCode/Any_Dual_Impl_T.inl"
00020 #endif /* ! __ACE_INLINE__ */
00021 
00022 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00023 
00024 template<typename T>
00025 TAO::Any_Dual_Impl_T<T>::Any_Dual_Impl_T (_tao_destructor destructor,
00026                                           CORBA::TypeCode_ptr tc,
00027                                           T * const val)
00028   : Any_Impl (destructor,
00029               tc),
00030     value_ (val)
00031 {
00032 }
00033 
00034 template<typename T> void
00035 TAO::Any_Dual_Impl_T<T>::value (const T & val)
00036 {
00037   ACE_NEW (this->value_,
00038            T (val));
00039 }
00040 
00041 template<typename T>
00042 TAO::Any_Dual_Impl_T<T>::Any_Dual_Impl_T (_tao_destructor destructor,
00043                                           CORBA::TypeCode_ptr tc,
00044                                           const T & val)
00045   : Any_Impl (destructor,
00046               tc)
00047 {
00048   this->value (val);
00049 }
00050 
00051 template<typename T>
00052 TAO::Any_Dual_Impl_T<T>::Any_Dual_Impl_T (CORBA::TypeCode_ptr tc)
00053   : Any_Impl (0,
00054               tc)
00055 {
00056 }
00057 
00058 template<typename T>
00059 TAO::Any_Dual_Impl_T<T>::~Any_Dual_Impl_T (void)
00060 {
00061 }
00062 
00063 template<typename T>
00064 void
00065 TAO::Any_Dual_Impl_T<T>::insert (CORBA::Any & any,
00066                                  _tao_destructor destructor,
00067                                  CORBA::TypeCode_ptr tc,
00068                                  T * const value)
00069 {
00070   Any_Dual_Impl_T<T> *new_impl = 0;
00071   ACE_NEW (new_impl,
00072            Any_Dual_Impl_T (destructor,
00073                             tc,
00074                             value));
00075   any.replace (new_impl);
00076 }
00077 
00078 template<typename T>
00079 void
00080 TAO::Any_Dual_Impl_T<T>::insert_copy (CORBA::Any & any,
00081                                       _tao_destructor destructor,
00082                                       CORBA::TypeCode_ptr tc,
00083                                       const T & value)
00084 {
00085   Any_Dual_Impl_T<T> *new_impl = 0;
00086   ACE_NEW (new_impl,
00087            Any_Dual_Impl_T (destructor,
00088                             tc,
00089                             value));
00090   any.replace (new_impl);
00091 }
00092 
00093 template<typename T>
00094 CORBA::Boolean
00095 TAO::Any_Dual_Impl_T<T>::extract (const CORBA::Any & any,
00096                                   _tao_destructor destructor,
00097                                   CORBA::TypeCode_ptr tc,
00098                                   const T *& _tao_elem)
00099 {
00100   _tao_elem = 0;
00101 
00102   try
00103     {
00104       CORBA::TypeCode_ptr any_tc = any._tao_get_typecode ();
00105       CORBA::Boolean const _tao_equiv = any_tc->equivalent (tc);
00106 
00107       if (_tao_equiv == false)
00108         {
00109           return false;
00110         }
00111 
00112       TAO::Any_Impl * const impl = any.impl ();
00113 
00114       if (impl && !impl->encoded ())
00115         {
00116           TAO::Any_Dual_Impl_T<T> * const narrow_impl =
00117             dynamic_cast <TAO::Any_Dual_Impl_T<T> *> (impl);
00118 
00119           if (narrow_impl == 0)
00120             {
00121               return false;
00122             }
00123 
00124           _tao_elem = narrow_impl->value_;
00125           return true;
00126         }
00127 
00128       T *empty_value = 0;
00129       ACE_NEW_RETURN (empty_value,
00130                       T,
00131                       false);
00132       TAO::Any_Dual_Impl_T<T> *replacement = 0;
00133       ACE_NEW_RETURN (replacement,
00134                       TAO::Any_Dual_Impl_T<T> (destructor,
00135                                                any_tc,
00136                                                empty_value),
00137                       false);
00138 
00139       auto_ptr<TAO::Any_Dual_Impl_T<T> > replacement_safety (replacement);
00140 
00141       // We know this will work since the unencoded case is covered above.
00142       TAO::Unknown_IDL_Type * const unk =
00143         dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00144 
00145       if (!unk)
00146         return false;
00147 
00148       // We don't want the rd_ptr of unk to move, in case it is
00149       // shared by another Any. This copies the state, not the buffer.
00150       TAO_InputCDR for_reading (unk->_tao_get_cdr ());
00151 
00152       CORBA::Boolean const good_decode =
00153         replacement->demarshal_value (for_reading);
00154 
00155       if (good_decode)
00156         {
00157           _tao_elem = replacement->value_;
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 template<typename T>
00174 CORBA::Boolean
00175 TAO::Any_Dual_Impl_T<T>::marshal_value (TAO_OutputCDR &cdr)
00176 {
00177   return (cdr << *this->value_);
00178 }
00179 
00180 template<typename T>
00181 const void *
00182 TAO::Any_Dual_Impl_T<T>::value (void) const
00183 {
00184   return this->value_;
00185 }
00186 
00187 template<typename T>
00188 void
00189 TAO::Any_Dual_Impl_T<T>::free_value (void)
00190 {
00191   if (this->value_destructor_ != 0)
00192     {
00193       (*this->value_destructor_) (this->value_);
00194       this->value_destructor_ = 0;
00195     }
00196 
00197   ::CORBA::release (this->type_);
00198   this->value_ = 0;
00199 }
00200 
00201 template<typename T>
00202 void
00203 TAO::Any_Dual_Impl_T<T>::_tao_decode (TAO_InputCDR &cdr)
00204 {
00205   if (! this->demarshal_value (cdr))
00206     {
00207       throw ::CORBA::MARSHAL ();
00208     }
00209 }
00210 
00211 TAO_END_VERSIONED_NAMESPACE_DECL
00212 
00213 #endif /* TAO_ANY_DUAL_IMPL_T_CPP */

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