DynSequence_i.cpp

Go to the documentation of this file.
00001 // $Id: DynSequence_i.cpp 77411 2007-02-27 00:01:57Z ossama $
00002 
00003 #include "tao/DynamicAny/DynSequence_i.h"
00004 #include "tao/DynamicAny/DynAnyFactory.h"
00005 #include "tao/DynamicAny/DynAnyUtils_T.h"
00006 
00007 #include "tao/AnyTypeCode/Marshal.h"
00008 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
00009 #include "tao/AnyTypeCode/AnyTypeCode_methods.h"
00010 
00011 #include "tao/CDR.h"
00012 
00013 ACE_RCSID (DynamicAny,
00014            DynSequence_i,
00015            "$Id: DynSequence_i.cpp 77411 2007-02-27 00:01:57Z ossama $")
00016 
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 TAO_DynSequence_i::TAO_DynSequence_i (void)
00020 {
00021 }
00022 
00023 TAO_DynSequence_i::~TAO_DynSequence_i (void)
00024 {
00025 }
00026 
00027 void
00028 TAO_DynSequence_i::init_common (void)
00029 {
00030   this->ref_to_component_ = false;
00031   this->container_is_destroying_ = false;
00032   this->has_components_ = true;
00033   this->destroyed_ = false;
00034   this->current_position_ = -1;
00035   this->component_count_ = static_cast<CORBA::ULong> (this->da_members_.size ());
00036 }
00037 
00038 void
00039 TAO_DynSequence_i::init (const CORBA::Any& any)
00040 {
00041   CORBA::TypeCode_var tc = any.type ();
00042 
00043   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc.in ());
00044 
00045   if (kind != CORBA::tk_sequence)
00046     {
00047       throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
00048     }
00049 
00050   this->type_ = tc;
00051 
00052   // Get the CDR stream of the Any, if there isn't one, make one.
00053   TAO::Any_Impl *impl = any.impl ();
00054   CORBA::ULong length;
00055   TAO_OutputCDR out;
00056   TAO_InputCDR cdr (static_cast<ACE_Message_Block *> (0));
00057 
00058   if (impl->encoded ())
00059     {
00060       TAO::Unknown_IDL_Type * const unk =
00061         dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00062 
00063       if (!unk)
00064         throw CORBA::INTERNAL ();
00065 
00066       cdr = unk->_tao_get_cdr ();
00067     }
00068   else
00069     {
00070       impl->marshal_value (out);
00071       TAO_InputCDR tmp_in (out);
00072       cdr = tmp_in;
00073     }
00074 
00075   // If the any is a sequence, first 4 bytes of cdr hold the
00076   // length.
00077   cdr.read_ulong (length);
00078 
00079   // Resize the array.
00080   this->da_members_.size (length);
00081 
00082   this->init_common ();
00083 
00084   // Get the type of the sequence elments.
00085   CORBA::TypeCode_var field_tc =
00086     this->get_element_type ();
00087 
00088   for (CORBA::ULong i = 0; i < length; ++i)
00089     {
00090       CORBA::Any field_any;
00091       TAO_InputCDR unk_in (cdr);
00092       TAO::Unknown_IDL_Type *field_unk = 0;
00093       ACE_NEW (field_unk,
00094                TAO::Unknown_IDL_Type (field_tc.in (), unk_in));
00095       field_any.replace (field_unk);
00096 
00097       // This recursive step will call the correct constructor
00098       // based on the type of field_any.
00099       this->da_members_[i] =
00100         TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
00101           field_any._tao_get_typecode (), field_any);
00102 
00103       // Move to the next field in the CDR stream.
00104       (void) TAO_Marshal_Object::perform_skip (field_tc.in (), &cdr);
00105     }
00106 }
00107 
00108 void
00109 TAO_DynSequence_i::init (CORBA::TypeCode_ptr tc)
00110 {
00111   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc);
00112 
00113   if (kind != CORBA::tk_sequence)
00114     {
00115       throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
00116     }
00117 
00118   // Empty sequence.
00119   this->da_members_.size (0);
00120 
00121   this->init_common ();
00122 
00123   this->type_ = CORBA::TypeCode::_duplicate (tc);
00124 }
00125 
00126 // ****************************************************************
00127 
00128 TAO_DynSequence_i *
00129 TAO_DynSequence_i::_narrow (CORBA::Object_ptr _tao_objref)
00130 {
00131   if (CORBA::is_nil (_tao_objref))
00132     {
00133       return 0;
00134     }
00135 
00136   return dynamic_cast<TAO_DynSequence_i *> (_tao_objref);
00137 }
00138 
00139 // ****************************************************************
00140 
00141 CORBA::TypeCode_ptr
00142 TAO_DynSequence_i::get_element_type (void)
00143 {
00144   CORBA::TypeCode_var element_type =
00145     CORBA::TypeCode::_duplicate (this->type_.in ());
00146 
00147   // Strip away aliases (if any) on top of the outer type.
00148   CORBA::TCKind kind = element_type->kind ();
00149 
00150   while (kind != CORBA::tk_sequence)
00151     {
00152       element_type = element_type->content_type ();
00153 
00154       kind = element_type->kind ();
00155     }
00156 
00157   // Return the content type.
00158   CORBA::TypeCode_ptr retval = element_type->content_type ();
00159 
00160   return retval;
00161 }
00162 
00163 // = Functions specific to DynSequence.
00164 
00165 CORBA::ULong
00166 TAO_DynSequence_i::get_length (void)
00167 {
00168   if (this->destroyed_)
00169     {
00170       throw ::CORBA::OBJECT_NOT_EXIST ();
00171     }
00172 
00173   return this->component_count_;
00174 }
00175 
00176 void
00177 TAO_DynSequence_i::set_length (CORBA::ULong length)
00178 {
00179   if (this->destroyed_)
00180     {
00181       throw ::CORBA::OBJECT_NOT_EXIST ();
00182     }
00183 
00184   // CORBA::TypeCode::length() does not accept aliased type codes.
00185   CORBA::TypeCode_var stripped_tc =
00186     TAO_DynAnyFactory::strip_alias (this->type_.in ());
00187 
00188   CORBA::ULong bound = stripped_tc->length ();
00189 
00190   if (bound > 0 && length > bound)
00191     {
00192       throw DynamicAny::DynAny::InvalidValue ();
00193     }
00194 
00195   // CORBA 2.3.1 has several explicit rules about resetting the
00196   // current position, depending on the current value of the
00197   // current position, the current size, and the new length.
00198   if (length == 0)
00199     {
00200       this->current_position_ = -1;
00201     }
00202   else if (length > this->component_count_)
00203     {
00204       if (this->current_position_ == -1)
00205         {
00206           // Set it to the first new slot.
00207           this->current_position_ = static_cast<CORBA::Long> (this->component_count_);
00208         }
00209     }
00210   else if (length < this->component_count_)
00211     {
00212       // If the current position will no longer exist..
00213       if (this->current_position_ >= static_cast<CORBA::Long> (length))
00214         {
00215           this->current_position_ = -1;
00216         }
00217     }
00218 
00219   if (length > this->component_count_)
00220     {
00221       // Grow array first, then initialize new members.
00222       this->da_members_.size (length);
00223 
00224       CORBA::TypeCode_var elemtype =
00225         stripped_tc->content_type ();
00226 
00227       for (CORBA::ULong i = this->component_count_; i < length; ++i)
00228         {
00229           this->da_members_[i] =
00230         TAO::MakeDynAnyUtils::make_dyn_any_t<CORBA::TypeCode_ptr> (
00231           elemtype.in (),
00232           elemtype.in ());
00233         }
00234     }
00235   else if (length < this->component_count_)
00236     {
00237       // Destroy any dangling members first, then shrink array.
00238       for (CORBA::ULong j = length; j < this->component_count_; ++j)
00239         {
00240           this->da_members_[j]->destroy ();
00241         }
00242 
00243       this->da_members_.size (length);
00244     }
00245 
00246   // Now we can update component_count_.
00247   this->component_count_ = length;
00248 }
00249 
00250 DynamicAny::AnySeq *
00251 TAO_DynSequence_i::get_elements (void)
00252 {
00253   if (this->destroyed_)
00254     {
00255       throw ::CORBA::OBJECT_NOT_EXIST ();
00256     }
00257 
00258   CORBA::ULong length = static_cast<CORBA::ULong> (this->da_members_.size ());
00259 
00260   DynamicAny::AnySeq *elements;
00261   ACE_NEW_THROW_EX (elements,
00262                     DynamicAny::AnySeq (length),
00263                     CORBA::NO_MEMORY ());
00264 
00265   elements->length (length);
00266   DynamicAny::AnySeq_var safe_retval = elements;
00267 
00268   // Initialize each Any.
00269   for (CORBA::ULong i = 0; i < length; ++i)
00270     {
00271       CORBA::Any_var tmp =
00272         this->da_members_[i]->to_any ();
00273 
00274 
00275       safe_retval[i] = tmp.in ();
00276     }
00277 
00278   return safe_retval._retn ();
00279 }
00280 
00281 void
00282 TAO_DynSequence_i::set_elements (const DynamicAny::AnySeq & value)
00283 {
00284   if (this->destroyed_)
00285     {
00286       throw ::CORBA::OBJECT_NOT_EXIST ();
00287     }
00288 
00289   CORBA::TypeCode_var stripped_tc =
00290     TAO_DynAnyFactory::strip_alias (this->type_.in ());
00291 
00292   CORBA::ULong length = value.length ();
00293   CORBA::ULong bound = stripped_tc->length ();
00294 
00295   if (bound > 0 && length > bound)
00296     {
00297       throw DynamicAny::DynAny::InvalidValue ();
00298     }
00299 
00300   // CORBA 2.4.2.
00301   if (length == 0)
00302     {
00303       this->current_position_ = -1;
00304     }
00305   else
00306     {
00307       this->current_position_ = 0;
00308     }
00309 
00310   // If the array grows, we must do it now.
00311   if (length > this->component_count_)
00312     {
00313       this->da_members_.size (length);
00314     }
00315 
00316   CORBA::TypeCode_var element_type = this->get_element_type ();
00317 
00318   CORBA::TypeCode_var value_tc;
00319 
00320   for (CORBA::ULong i = 0; i < length; ++i)
00321     {
00322       // Check each arg element for type match.
00323       value_tc = value[i].type ();
00324       CORBA::Boolean equivalent =
00325         value_tc->equivalent (element_type.in ());
00326 
00327       if (equivalent)
00328         {
00329           // Destroy any existing members.
00330           if (i < this->component_count_)
00331             {
00332               this->da_members_[i]->destroy ();
00333             }
00334 
00335           this->da_members_[i] =
00336             TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
00337               value[i]._tao_get_typecode (),
00338               value[i]);
00339         }
00340       else
00341         {
00342           throw DynamicAny::DynAny::TypeMismatch ();
00343         }
00344     }
00345 
00346   // Destroy any dangling members.
00347   for (CORBA::ULong j = length; j < this->component_count_; ++j)
00348     {
00349       this->da_members_[j]->destroy ();
00350     }
00351 
00352   // If the array shrinks, we must wait until now to do it.
00353   if (length < this->component_count_)
00354     {
00355       this->da_members_.size (length);
00356     }
00357 
00358   // Now we can update component_count_.
00359   this->component_count_ = length;
00360 }
00361 
00362 DynamicAny::DynAnySeq *
00363 TAO_DynSequence_i::get_elements_as_dyn_any (void)
00364 {
00365   if (this->destroyed_)
00366     {
00367       throw ::CORBA::OBJECT_NOT_EXIST ();
00368     }
00369 
00370   DynamicAny::DynAnySeq *retval = 0;
00371   ACE_NEW_THROW_EX (retval,
00372                     DynamicAny::DynAnySeq (this->component_count_),
00373                     CORBA::NO_MEMORY ());
00374 
00375   retval->length (this->component_count_);
00376   DynamicAny::DynAnySeq_var safe_retval (retval);
00377 
00378   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00379     {
00380       // A deep copy is made only by copy() (CORBA 2.4.2 section 9.2.3.6).
00381       // Set the flag so the caller can't destroy.
00382       this->set_flag (this->da_members_[i].in (), 0);
00383 
00384       safe_retval[i] =
00385         DynamicAny::DynAny::_duplicate (this->da_members_[i].in ());
00386     }
00387 
00388   return safe_retval._retn ();
00389 }
00390 
00391 void
00392 TAO_DynSequence_i::set_elements_as_dyn_any (
00393   const DynamicAny::DynAnySeq & values)
00394 {
00395   if (this->destroyed_)
00396     {
00397       throw ::CORBA::OBJECT_NOT_EXIST ();
00398     }
00399 
00400   CORBA::TypeCode_var stripped_tc =
00401     TAO_DynAnyFactory::strip_alias (this->type_.in ());
00402 
00403   CORBA::ULong length = values.length ();
00404   CORBA::ULong bound = stripped_tc->length ();
00405 
00406   if (bound > 0 && length > bound)
00407     {
00408       throw DynamicAny::DynAny::InvalidValue ();
00409     }
00410 
00411   // If the array grows, we must do it now.
00412   if (length > this->component_count_)
00413     {
00414       this->da_members_.size (length);
00415     }
00416 
00417   CORBA::TypeCode_var element_type =
00418     this->get_element_type ();
00419 
00420   CORBA::TypeCode_var val_type;
00421   CORBA::Boolean equivalent;
00422 
00423   for (CORBA::ULong i = 0; i < length; ++i)
00424     {
00425       val_type = values[i]->type ();
00426 
00427       equivalent = val_type->equivalent (element_type.in ());
00428 
00429       if (equivalent)
00430         {
00431           // Destroy any existing members.
00432           if (i < this->component_count_)
00433             {
00434               this->da_members_[i]->destroy ();
00435             }
00436 
00437           this->da_members_[i] =
00438             values[i]->copy ();
00439         }
00440       else
00441         {
00442           throw DynamicAny::DynAny::TypeMismatch ();
00443         }
00444     }
00445 
00446   // Destroy any dangling members.
00447   for (CORBA::ULong j = length; j < this->component_count_; ++j)
00448     {
00449       this->da_members_[j]->destroy ();
00450     }
00451 
00452   // If the array shrinks, we must wait until now to do it.
00453   if (length < this->component_count_)
00454     {
00455       this->da_members_.size (length);
00456     }
00457 
00458   // Now we can update component_count_.
00459   this->component_count_ = length;
00460 }
00461 
00462 // ****************************************************************
00463 
00464 void
00465 TAO_DynSequence_i::from_any (const CORBA::Any & any)
00466 {
00467   if (this->destroyed_)
00468     {
00469       throw ::CORBA::OBJECT_NOT_EXIST ();
00470     }
00471 
00472   CORBA::TypeCode_var tc = any.type ();
00473   CORBA::Boolean equivalent =
00474     this->type_.in ()->equivalent (tc.in ());
00475 
00476   if (equivalent)
00477     {
00478       // Get the CDR stream of the Any, if there isn't one, make one.
00479       TAO::Any_Impl *impl = any.impl ();
00480       TAO_OutputCDR out;
00481       TAO_InputCDR cdr (static_cast<ACE_Message_Block *> (0));
00482 
00483       if (impl->encoded ())
00484         {
00485           TAO::Unknown_IDL_Type * const unk =
00486             dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00487 
00488           if (!unk)
00489             throw CORBA::INTERNAL ();
00490 
00491           cdr = unk->_tao_get_cdr ();
00492         }
00493       else
00494         {
00495           impl->marshal_value (out);
00496           TAO_InputCDR tmp_in (out);
00497           cdr = tmp_in;
00498         }
00499 
00500 
00501       CORBA::ULong arg_length;
00502 
00503       // If the any is a sequence, first 4 bytes of cdr hold the
00504       // length.
00505       cdr.read_ulong (arg_length);
00506 
00507       // If the array grows, we must do it now.
00508       if (arg_length > this->component_count_)
00509         {
00510           this->da_members_.size (arg_length);
00511         }
00512 
00513       CORBA::TypeCode_var field_tc =
00514         this->get_element_type ();
00515 
00516       for (CORBA::ULong i = 0; i < arg_length; ++i)
00517         {
00518           CORBA::Any field_any;
00519           TAO_InputCDR unk_in (cdr);
00520           TAO::Unknown_IDL_Type *field_unk = 0;
00521           ACE_NEW (field_unk,
00522                    TAO::Unknown_IDL_Type (field_tc.in (),
00523                                           unk_in));
00524           field_any.replace (field_unk);
00525 
00526           if (i < this->component_count_)
00527             {
00528               this->da_members_[i]->destroy ();
00529             }
00530 
00531           this->da_members_[i] =
00532             TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
00533               field_any._tao_get_typecode (),
00534               field_any);
00535 
00536           // Move to the next field in the CDR stream.
00537           (void) TAO_Marshal_Object::perform_skip (field_tc.in (), &cdr);
00538         }
00539 
00540       // Destroy any dangling members.
00541       for (CORBA::ULong j = arg_length; j < this->component_count_; ++j)
00542         {
00543           this->da_members_[j]->destroy ();
00544         }
00545 
00546       // If the array shrinks, we must wait until now to do it.
00547       if (arg_length < this->component_count_)
00548         {
00549           this->da_members_.size (arg_length);
00550         }
00551 
00552       // Now we can update component_count_.
00553       this->component_count_ = arg_length;
00554 
00555       this->current_position_ = arg_length ? 0 : -1;
00556     }
00557   else
00558     {
00559       throw DynamicAny::DynAny::TypeMismatch ();
00560     }
00561 }
00562 
00563 CORBA::Any_ptr
00564 TAO_DynSequence_i::to_any (void)
00565 {
00566   if (this->destroyed_)
00567     {
00568       throw ::CORBA::OBJECT_NOT_EXIST ();
00569     }
00570 
00571   TAO_OutputCDR out_cdr;
00572   out_cdr.write_ulong (this->component_count_);
00573 
00574   CORBA::TypeCode_var field_tc =
00575     this->get_element_type ();
00576 
00577   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00578     {
00579       // Recursive step
00580       CORBA::Any_var field_any =
00581         this->da_members_[i]->to_any ();
00582 
00583       TAO::Any_Impl *field_impl = field_any->impl ();
00584       TAO_OutputCDR field_out;
00585       TAO_InputCDR field_cdr (static_cast<ACE_Message_Block *> (0));
00586 
00587       if (field_impl->encoded ())
00588         {
00589           TAO::Unknown_IDL_Type * const field_unk =
00590             dynamic_cast<TAO::Unknown_IDL_Type *> (field_impl);
00591 
00592           if (!field_unk)
00593             throw CORBA::INTERNAL ();
00594 
00595           field_cdr = field_unk->_tao_get_cdr ();
00596         }
00597       else
00598         {
00599           field_impl->marshal_value (field_out);
00600           TAO_InputCDR tmp_in (field_out);
00601           field_cdr = tmp_in;
00602         }
00603 
00604       (void) TAO_Marshal_Object::perform_append (field_tc.in (),
00605                                                  &field_cdr,
00606                                                  &out_cdr);
00607     }
00608 
00609   TAO_InputCDR in_cdr (out_cdr);
00610 
00611   CORBA::Any_ptr retval = 0;
00612   ACE_NEW_THROW_EX (retval,
00613                     CORBA::Any,
00614                     CORBA::NO_MEMORY ());
00615 
00616   TAO::Unknown_IDL_Type *unk = 0;
00617   ACE_NEW_THROW_EX (unk,
00618                     TAO::Unknown_IDL_Type (this->type_.in (),
00619                                            in_cdr),
00620                     CORBA::NO_MEMORY ());
00621 
00622   retval->replace (unk);
00623   return retval;
00624 }
00625 
00626 CORBA::Boolean
00627 TAO_DynSequence_i::equal (DynamicAny::DynAny_ptr rhs)
00628 {
00629   if (this->destroyed_)
00630     {
00631       throw ::CORBA::OBJECT_NOT_EXIST ();
00632     }
00633 
00634   CORBA::TypeCode_var tc = rhs->type ();
00635 
00636   CORBA::Boolean equivalent = tc->equivalent (this->type_.in ());
00637 
00638   if (!equivalent)
00639     {
00640       return false;
00641     }
00642 
00643   if (rhs->component_count () != this->component_count_)
00644     {
00645       return false;
00646     }
00647 
00648   DynamicAny::DynAny_var tmp;
00649   CORBA::Boolean member_equal;
00650 
00651   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00652     {
00653       rhs->seek (static_cast<CORBA::Long> (i));
00654 
00655       tmp = rhs->current_component ();
00656 
00657       // Recursive step.
00658       member_equal = tmp->equal (this->da_members_[i].in ());
00659 
00660       if (!member_equal)
00661         {
00662           return false;
00663         }
00664     }
00665 
00666   return true;
00667 }
00668 
00669 void
00670 TAO_DynSequence_i::destroy (void)
00671 {
00672   if (this->destroyed_)
00673     {
00674       throw ::CORBA::OBJECT_NOT_EXIST ();
00675     }
00676 
00677   if (!this->ref_to_component_ || this->container_is_destroying_)
00678     {
00679       // Do a deep destroy.
00680       for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00681         {
00682           this->set_flag (da_members_[i].in (), 1);
00683 
00684           this->da_members_[i]->destroy ();
00685         }
00686 
00687       this->destroyed_ = 1;
00688     }
00689 }
00690 
00691 DynamicAny::DynAny_ptr
00692 TAO_DynSequence_i::current_component (void)
00693 {
00694   if (this->destroyed_)
00695     {
00696       throw ::CORBA::OBJECT_NOT_EXIST ();
00697     }
00698 
00699   if (this->current_position_ == -1)
00700     {
00701       return DynamicAny::DynAny::_nil ();
00702     }
00703 
00704   CORBA::ULong index = static_cast<CORBA::ULong> (this->current_position_);
00705 
00706   this->set_flag (this->da_members_[index].in (), 0);
00707 
00708   return DynamicAny::DynAny::_duplicate (this->da_members_[index].in ());
00709 }
00710 
00711 TAO_END_VERSIONED_NAMESPACE_DECL

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