DynEnum_i.cpp

Go to the documentation of this file.
00001 // $Id: DynEnum_i.cpp 78077 2007-04-17 16:13:20Z dai_y $
00002 
00003 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
00004 #include "tao/AnyTypeCode/AnyTypeCode_methods.h"
00005 #include "tao/DynamicAny/DynEnum_i.h"
00006 #include "tao/DynamicAny/DynAnyFactory.h"
00007 #include "tao/CDR.h"
00008 
00009 #include "ace/OS_NS_string.h"
00010 
00011 
00012 ACE_RCSID (DynamicAny,
00013            DynEnum_i,
00014            "$Id: DynEnum_i.cpp 78077 2007-04-17 16:13:20Z dai_y $")
00015 
00016 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00017 
00018 TAO_DynEnum_i::TAO_DynEnum_i (void)
00019 {
00020 }
00021 
00022 TAO_DynEnum_i::~TAO_DynEnum_i (void)
00023 {
00024 }
00025 
00026 void
00027 TAO_DynEnum_i::init_common (void)
00028 {
00029   this->ref_to_component_ = false;
00030   this->container_is_destroying_ = false;
00031   this->has_components_ = false;
00032   this->destroyed_ = false;
00033   this->current_position_ = -1;
00034   this->component_count_ = 0;
00035 }
00036 
00037 void
00038 TAO_DynEnum_i::init (const CORBA::Any &any)
00039 {
00040   CORBA::TypeCode_var tc = any.type ();
00041   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc.in ());
00042 
00043   if (kind != CORBA::tk_enum)
00044     {
00045       throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
00046     }
00047 
00048   this->type_ = tc;
00049 
00050   TAO::Any_Impl *impl = any.impl ();
00051 
00052   if (impl->encoded ())
00053     {
00054       TAO::Unknown_IDL_Type * const unk =
00055         dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00056 
00057       if (!unk)
00058         throw CORBA::INTERNAL ();
00059 
00060       // We don't want unk's rd_ptr to move, in case we are shared by
00061       // another Any, so we use this to copy the state, not the buffer.
00062       TAO_InputCDR for_reading (unk->_tao_get_cdr ());
00063       for_reading.read_ulong (this->value_);
00064     }
00065   else
00066     {
00067       TAO_OutputCDR out;
00068       impl->marshal_value (out);
00069       TAO_InputCDR in (out);
00070       in.read_ulong (this->value_);
00071     }
00072 
00073   this->init_common ();
00074 }
00075 
00076 void
00077 TAO_DynEnum_i::init (CORBA::TypeCode_ptr tc)
00078 {
00079   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc);
00080 
00081   if (kind != CORBA::tk_enum)
00082     {
00083       throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
00084     }
00085 
00086   this->type_ = CORBA::TypeCode::_duplicate (tc);
00087 
00088   this->value_ = 0;
00089 
00090   this->init_common ();
00091 }
00092 
00093 // ****************************************************************
00094 
00095 TAO_DynEnum_i *
00096 TAO_DynEnum_i::_narrow (CORBA::Object_ptr _tao_objref)
00097 {
00098   if (CORBA::is_nil (_tao_objref))
00099     {
00100       return 0;
00101     }
00102 
00103   return dynamic_cast<TAO_DynEnum_i *> (_tao_objref);
00104 }
00105 
00106 // ****************************************************************
00107 
00108 char *
00109 TAO_DynEnum_i::get_as_string (void)
00110 {
00111   CORBA::TypeCode_var ct = TAO_DynAnyFactory::strip_alias (this->type_.in ());
00112 
00113   const char *retval = ct.in ()->member_name (this->value_);
00114 
00115   return CORBA::string_dup (retval);
00116 }
00117 
00118 void
00119 TAO_DynEnum_i::set_as_string (const char *value_as_string)
00120 {
00121   CORBA::TypeCode_var ct = TAO_DynAnyFactory::strip_alias (this->type_.in ());
00122 
00123   CORBA::ULong count = ct.in ()->member_count ();
00124 
00125   CORBA::ULong i;
00126   const char *temp = 0;
00127 
00128   for (i = 0; i < count; ++i)
00129     {
00130       temp = ct.in ()->member_name (i);
00131 
00132       if (!ACE_OS::strcmp (value_as_string, temp))
00133         {
00134           break;
00135         }
00136     }
00137 
00138   if (i < count)
00139     {
00140       this->value_ = i;
00141     }
00142   else
00143     {
00144       throw DynamicAny::DynAny::InvalidValue ();
00145     }
00146 }
00147 
00148 CORBA::ULong
00149 TAO_DynEnum_i::get_as_ulong (void)
00150 {
00151   return this->value_;
00152 }
00153 
00154 void
00155 TAO_DynEnum_i::set_as_ulong (CORBA::ULong value_as_ulong)
00156 {
00157   CORBA::TypeCode_var ct = TAO_DynAnyFactory::strip_alias (this->type_.in ());
00158   CORBA::ULong const max = ct.in ()->member_count ();
00159 
00160   if (value_as_ulong < max)
00161     {
00162       this->value_ = value_as_ulong;
00163     }
00164   else
00165     {
00166       throw DynamicAny::DynAny::InvalidValue ();
00167     }
00168 }
00169 
00170 // ****************************************************************
00171 
00172 void
00173 TAO_DynEnum_i::from_any (const CORBA::Any& any)
00174 {
00175   CORBA::TypeCode_var tc = any.type ();
00176   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc.in ());
00177 
00178   if (kind == CORBA::tk_enum)
00179     {
00180       // Get the CDR stream of the Any, if there isn't one, make one.
00181       TAO::Any_Impl *impl = any.impl ();
00182 
00183       if (impl->encoded ())
00184         {
00185           TAO::Unknown_IDL_Type * const unk =
00186             dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00187 
00188           if (!unk)
00189             throw CORBA::INTERNAL ();
00190 
00191           // We don't want unk's rd_ptr to move, in case we are shared by
00192           // another Any, so we use this to copy the state, not the buffer.
00193           TAO_InputCDR for_reading (unk->_tao_get_cdr ());
00194           for_reading.read_ulong (this->value_);
00195         }
00196       else
00197         {
00198           TAO_OutputCDR out;
00199           impl->marshal_value (out);
00200           TAO_InputCDR in (out);
00201           in.read_ulong (this->value_);
00202         }
00203     }
00204   else
00205     {
00206       throw DynamicAny::DynAny::TypeMismatch ();
00207     }
00208 }
00209 
00210 CORBA::Any_ptr
00211 TAO_DynEnum_i::to_any (void)
00212 {
00213   TAO_OutputCDR out_cdr;
00214 
00215   out_cdr.write_ulong (this->value_);
00216 
00217   CORBA::Any *retval;
00218   ACE_NEW_THROW_EX (retval,
00219                     CORBA::Any,
00220                     CORBA::NO_MEMORY ());
00221 
00222   TAO_InputCDR in_cdr (out_cdr);
00223   TAO::Unknown_IDL_Type *unk = 0;
00224   ACE_NEW_THROW_EX (unk,
00225                     TAO::Unknown_IDL_Type (this->type_.in (),
00226                                            in_cdr),
00227                     CORBA::NO_MEMORY ());
00228 
00229   retval->replace (unk);
00230   return retval;
00231 }
00232 
00233 CORBA::Boolean
00234 TAO_DynEnum_i::equal (DynamicAny::DynAny_ptr rhs)
00235 {
00236   CORBA::TypeCode_var tc = rhs->type ();
00237 
00238   CORBA::Boolean equivalent = tc->equivalent (this->type_.in ());
00239 
00240   if (!equivalent)
00241     {
00242       return false;
00243     }
00244 
00245   CORBA::Any_var any = rhs->to_any ();
00246 
00247   TAO::Any_Impl *impl = any->impl ();
00248   CORBA::ULong value;
00249 
00250   if (impl->encoded ())
00251     {
00252       TAO::Unknown_IDL_Type * const unk =
00253         dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00254 
00255       if (!unk)
00256         throw CORBA::INTERNAL ();
00257 
00258       // We don't want unk's rd_ptr to move, in case we are shared by
00259       // another Any, so we use this to copy the state, not the buffer.
00260       TAO_InputCDR for_reading (unk->_tao_get_cdr ());
00261       for_reading.read_ulong (value);
00262     }
00263   else
00264     {
00265       TAO_OutputCDR out;
00266       impl->marshal_value (out);
00267       TAO_InputCDR in (out);
00268       in.read_ulong (value);
00269     }
00270 
00271   return value == this->value_;
00272 }
00273 
00274 void
00275 TAO_DynEnum_i::destroy (void)
00276 {
00277   if (this->destroyed_)
00278     {
00279       throw ::CORBA::OBJECT_NOT_EXIST ();
00280     }
00281 
00282   if (!this->ref_to_component_ || this->container_is_destroying_)
00283     {
00284       this->destroyed_ = true;
00285     }
00286 }
00287 
00288 DynamicAny::DynAny_ptr
00289 TAO_DynEnum_i::current_component (void)
00290 {
00291   if (this->destroyed_)
00292     {
00293       throw ::CORBA::OBJECT_NOT_EXIST ();
00294     }
00295 
00296   throw DynamicAny::DynAny::TypeMismatch ();
00297 }
00298 
00299 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 13:36:28 2008 for TAO_DynamicAny by doxygen 1.3.6