Any_Special_Impl_T.cpp

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

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