DynStruct_i.cpp

Go to the documentation of this file.
00001 // $Id: DynStruct_i.cpp 77785 2007-03-23 13:09:39Z johnnyw $
00002 
00003 #include "tao/DynamicAny/DynStruct_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            DynStruct_i,
00015            "$Id: DynStruct_i.cpp 77785 2007-03-23 13:09:39Z johnnyw $")
00016 
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 TAO_DynStruct_i::TAO_DynStruct_i (void)
00020 {
00021 }
00022 
00023 TAO_DynStruct_i::~TAO_DynStruct_i (void)
00024 {
00025 }
00026 
00027 void
00028 TAO_DynStruct_i::check_typecode (CORBA::TypeCode_ptr tc)
00029 {
00030   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc);
00031 
00032   if (kind == CORBA::tk_struct || kind == CORBA::tk_except)
00033     {
00034       return;
00035     }
00036 
00037   throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
00038 }
00039 
00040 void
00041 TAO_DynStruct_i::init_common (void)
00042 {
00043   this->ref_to_component_ = false;
00044   this->container_is_destroying_ = false;
00045   this->has_components_ = true;
00046   this->destroyed_ = false;
00047   this->component_count_ = static_cast <CORBA::ULong> (this->da_members_.size ());
00048   this->current_position_ = this->component_count_ ? 0 : -1;
00049 }
00050 
00051 void
00052 TAO_DynStruct_i::init (const CORBA::Any& any)
00053 {
00054   CORBA::TypeCode_ptr tc = any._tao_get_typecode ();
00055   this->check_typecode (tc);
00056 
00057   this->type_ = CORBA::TypeCode::_duplicate (tc);
00058 
00059   this->set_from_any (any);
00060 }
00061 
00062 
00063 // This code is common to from_any() and the init() overload that takes
00064 // an Any argument.
00065 void
00066 TAO_DynStruct_i::set_from_any (const CORBA::Any & any)
00067 {
00068   // member_type() does not work with aliased type codes.
00069   CORBA::TypeCode_var unaliased_tc =
00070     TAO_DynAnyFactory::strip_alias (any._tao_get_typecode ());
00071 
00072   CORBA::ULong numfields =
00073     unaliased_tc->member_count ();
00074 
00075   // Resize the array.
00076   this->da_members_.size (numfields);
00077 
00078   this->init_common ();
00079 
00080   // Get the CDR stream of the Any, if there isn't one, make one.
00081   TAO::Any_Impl *impl = any.impl ();
00082   TAO_OutputCDR out;
00083   TAO_InputCDR in (static_cast<ACE_Message_Block *> (0));
00084   TAO::Unknown_IDL_Type *unk = 0;
00085 
00086   if (impl->encoded ())
00087     {
00088       unk = dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00089 
00090       if (!unk)
00091         throw CORBA::INTERNAL ();
00092 
00093       in = unk->_tao_get_cdr ();
00094     }
00095   else
00096     {
00097       impl->marshal_value (out);
00098       TAO_InputCDR tmp_in (out);
00099       in = tmp_in;
00100     }
00101 
00102   // If we have an exception type, unmarshal the repository ID.
00103   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (any._tao_get_typecode ());
00104 
00105   if (kind == CORBA::tk_except)
00106     {
00107       CORBA::String_var str;
00108       in >> str.out ();
00109     }
00110 
00111   for (CORBA::ULong i = 0; i < numfields; i++)
00112     {
00113       CORBA::TypeCode_var field_tc =
00114         unaliased_tc->member_type (i);
00115 
00116       CORBA::Any field_any;
00117       TAO_InputCDR unk_in (in);
00118       ACE_NEW (unk,
00119                TAO::Unknown_IDL_Type (field_tc.in (),
00120                                       unk_in));
00121       field_any.replace (unk);
00122 
00123       // This recursive step will call the correct constructor
00124       // based on the type of field_any.
00125       this->da_members_[i] =
00126         TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
00127           field_any._tao_get_typecode (),
00128           field_any);
00129 
00130       // Move to the next field in the CDR stream.
00131       (void) TAO_Marshal_Object::perform_skip (field_tc.in (), &in);
00132     }
00133 }
00134 
00135 void
00136 TAO_DynStruct_i::init (CORBA::TypeCode_ptr tc)
00137 {
00138   this->check_typecode (tc);
00139 
00140   this->type_ = CORBA::TypeCode::_duplicate (tc);
00141 
00142   // member_type() does not work with aliased type codes.
00143   CORBA::TypeCode_var unaliased_tc =
00144   TAO_DynAnyFactory::strip_alias (this->type_.in ());
00145 
00146   this->component_count_ =
00147     unaliased_tc->member_count ();
00148 
00149   // Resize the array.
00150   this->da_members_.size (this->component_count_);
00151 
00152   this->init_common ();
00153 
00154   CORBA::TypeCode_var mtype;
00155 
00156   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00157     {
00158       mtype = unaliased_tc->member_type (i);
00159 
00160       // Recursively initialize each member.
00161       this->da_members_[i] =
00162         TAO::MakeDynAnyUtils::make_dyn_any_t<CORBA::TypeCode_ptr> (
00163           mtype.in (),
00164           mtype.in ());
00165     }
00166 }
00167 
00168 // ****************************************************************
00169 
00170 TAO_DynStruct_i *
00171 TAO_DynStruct_i::_narrow (CORBA::Object_ptr _tao_objref)
00172 {
00173   if (CORBA::is_nil (_tao_objref))
00174     {
00175       return 0;
00176     }
00177 
00178   return dynamic_cast<TAO_DynStruct_i *> (_tao_objref);
00179 }
00180 
00181 // ****************************************************************
00182 
00183 DynamicAny::FieldName
00184 TAO_DynStruct_i::current_member_name (void)
00185 {
00186   if (this->destroyed_)
00187     {
00188       throw ::CORBA::OBJECT_NOT_EXIST ();
00189     }
00190 
00191   // Is this an empty exception?
00192   if (this->component_count_ == 0)
00193     {
00194       throw DynamicAny::DynAny::TypeMismatch ();
00195     }
00196 
00197   if (this->current_position_ == -1)
00198     {
00199       throw DynamicAny::DynAny::InvalidValue ();
00200     }
00201 
00202   // member_name() does not work with aliased type codes.
00203   CORBA::TypeCode_var unaliased_tc =
00204     TAO_DynAnyFactory::strip_alias (this->type_.in ());
00205   const char *name =
00206     unaliased_tc->member_name (this->current_position_);
00207 
00208   return CORBA::string_dup (name);
00209 }
00210 
00211 // Returns the unaliased TCKind.
00212 CORBA::TCKind
00213 TAO_DynStruct_i::current_member_kind (void)
00214 {
00215   if (this->destroyed_)
00216     {
00217       throw ::CORBA::OBJECT_NOT_EXIST ();
00218     }
00219 
00220   // Is this an empty exception?
00221   if (this->component_count_ == 0)
00222     {
00223       throw DynamicAny::DynAny::TypeMismatch ();
00224     }
00225 
00226   if (this->current_position_ == -1)
00227     {
00228       throw DynamicAny::DynAny::InvalidValue ();
00229     }
00230 
00231   // member_type() does not work with aliased type codes.
00232   CORBA::TypeCode_var unaliased_tc =
00233     TAO_DynAnyFactory::strip_alias (this->type_.in ());
00234   CORBA::TypeCode_var tc =
00235   unaliased_tc->member_type (this->current_position_);
00236 
00237   CORBA::TCKind retval = TAO_DynAnyFactory::unalias (tc.in ());
00238 
00239   return retval;
00240 }
00241 
00242 DynamicAny::NameValuePairSeq *
00243 TAO_DynStruct_i::get_members (void)
00244 {
00245   if (this->destroyed_)
00246     {
00247       throw ::CORBA::OBJECT_NOT_EXIST ();
00248     }
00249 
00250   DynamicAny::NameValuePairSeq* members;
00251   ACE_NEW_THROW_EX (members,
00252                     DynamicAny::NameValuePairSeq (this->component_count_),
00253                     CORBA::NO_MEMORY ());
00254 
00255   // We must do this explicitly.
00256   members->length (this->component_count_);
00257 
00258   DynamicAny::NameValuePairSeq_var safe_retval = members;
00259   CORBA::Any_var temp;
00260   CORBA::TypeCode_var unaliased_tc;
00261 
00262   CORBA::TCKind const tc_kind =
00263     this->type_->kind ();
00264 
00265   if (tc_kind == CORBA::tk_alias)
00266     {
00267       unaliased_tc = TAO_DynAnyFactory::strip_alias (this->type_.in ());
00268     }
00269   else
00270     {
00271       unaliased_tc = CORBA::TypeCode::_duplicate (this->type_.in ());
00272     }
00273 
00274   // Assign name and value to each pearl on the string.
00275   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00276     {
00277       safe_retval[i].id =
00278         CORBA::string_dup (unaliased_tc->member_name (i));
00279 
00280       temp = this->da_members_[i]->to_any ();
00281 
00282       safe_retval[i].value = temp.in ();
00283     }
00284 
00285   return safe_retval._retn ();
00286 }
00287 
00288 void
00289 TAO_DynStruct_i::set_members (const DynamicAny::NameValuePairSeq & values)
00290 {
00291   if (this->destroyed_)
00292     {
00293       throw ::CORBA::OBJECT_NOT_EXIST ();
00294     }
00295 
00296   CORBA::ULong length = values.length ();
00297 
00298   // Check for length match.
00299   if (length != this->component_count_)
00300     {
00301       throw DynamicAny::DynAny::InvalidValue ();
00302     }
00303 
00304   CORBA::TypeCode_var value_tc;
00305   CORBA::TypeCode_var my_tc;
00306   CORBA::Boolean equivalent;
00307 
00308   // member_type() does not work with aliased type codes.
00309   CORBA::TypeCode_var unaliased_tc =
00310     TAO_DynAnyFactory::strip_alias (this->type_.in ()
00311                                    );
00312 
00313   for (CORBA::ULong i = 0; i < length; ++i)
00314     {
00315       // Check for type and name match.
00316       my_tc = unaliased_tc->member_type (i);
00317 
00318       value_tc = values[i].value.type ();
00319 
00320       equivalent = my_tc->equivalent (value_tc.in ());
00321 
00322       if (!equivalent)
00323         {
00324           throw DynamicAny::DynAny::TypeMismatch ();
00325         }
00326 
00327       this->da_members_[i]->destroy ();
00328 
00329       this->da_members_[i] =
00330         TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
00331           values[i].value._tao_get_typecode (),
00332           values[i].value);
00333     }
00334 
00335   this->current_position_ = length ? 0 : -1;
00336 }
00337 
00338 DynamicAny::NameDynAnyPairSeq *
00339 TAO_DynStruct_i::get_members_as_dyn_any (void)
00340 {
00341   if (this->destroyed_)
00342     {
00343       throw ::CORBA::OBJECT_NOT_EXIST ();
00344     }
00345 
00346   DynamicAny::NameDynAnyPairSeq * members;
00347   ACE_NEW_THROW_EX (members,
00348                     DynamicAny::NameDynAnyPairSeq (this->component_count_),
00349                     CORBA::NO_MEMORY ());
00350 
00351   // We must do this explicitly.
00352   members->length (this->component_count_);
00353 
00354   DynamicAny::NameDynAnyPairSeq_var safe_retval = members;
00355 
00356   // member_name() does not work with aliased type codes.
00357   CORBA::TypeCode_var unaliased_tc =
00358     TAO_DynAnyFactory::strip_alias (this->type_.in ());
00359 
00360   // Assign name and value to each pearl on the string.
00361   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00362     {
00363       safe_retval[i].id = CORBA::string_dup (unaliased_tc->member_name (i));
00364 
00365       // A deep copy is made only by copy() (CORBA 2.4.2 section 9.2.3.6).
00366       // Set the flag so the caller can't destroy.
00367       this->set_flag (this->da_members_[i].in (), 0);
00368 
00369       safe_retval[i].value =
00370         DynamicAny::DynAny::_duplicate (this->da_members_[i].in ());
00371     }
00372 
00373   return safe_retval._retn ();
00374 }
00375 
00376 void
00377 TAO_DynStruct_i::set_members_as_dyn_any (
00378     const DynamicAny::NameDynAnyPairSeq & values
00379 
00380   )
00381 {
00382   if (this->destroyed_)
00383     {
00384       throw ::CORBA::OBJECT_NOT_EXIST ();
00385     }
00386 
00387   CORBA::ULong length = values.length ();
00388 
00389   // Check for length match.
00390   if (length != this->component_count_)
00391     {
00392       throw DynamicAny::DynAny::InvalidValue ();
00393     }
00394 
00395   CORBA::TypeCode_var value_tc;
00396   CORBA::TypeCode_var my_tc;
00397   CORBA::Boolean equivalent;
00398 
00399   // member_type() does not work with aliased type codes.
00400   CORBA::TypeCode_var unaliased_tc =
00401     TAO_DynAnyFactory::strip_alias (this->type_.in ()
00402                                    );
00403 
00404   for (CORBA::ULong i = 0; i < length; ++i)
00405     {
00406       // Check for type and name match.
00407       my_tc = unaliased_tc->member_type (i);
00408 
00409       value_tc = values[i].value->type ();
00410 
00411       equivalent = my_tc->equivalent (value_tc.in ());
00412 
00413       if (!equivalent)
00414         {
00415           throw DynamicAny::DynAny::TypeMismatch ();
00416         }
00417 
00418       this->da_members_[i]->destroy ();
00419 
00420       this->da_members_[i] =
00421         values[i].value->copy ();
00422     }
00423 
00424   this->current_position_ = length ? 0 : -1;
00425 }
00426 
00427 // ****************************************************************
00428 
00429 void
00430 TAO_DynStruct_i::from_any (const CORBA::Any & any)
00431 {
00432   if (this->destroyed_)
00433     {
00434       throw ::CORBA::OBJECT_NOT_EXIST ();
00435     }
00436 
00437   CORBA::TypeCode_var tc = any.type ();
00438   CORBA::Boolean equivalent =
00439     this->type_->equivalent (tc.in ()
00440                             );
00441 
00442   if (equivalent)
00443     {
00444       // Get the CDR stream of the Any, if there isn't one, make one.
00445       TAO::Any_Impl *impl = any.impl ();
00446       TAO_OutputCDR out;
00447       TAO_InputCDR in (static_cast<ACE_Message_Block *> (0));
00448       TAO::Unknown_IDL_Type *unk = 0;
00449 
00450       if (impl->encoded ())
00451         {
00452           unk = dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
00453 
00454           if (!unk)
00455             throw CORBA::INTERNAL ();
00456 
00457           in = unk->_tao_get_cdr ();
00458         }
00459       else
00460         {
00461           impl->marshal_value (out);
00462           TAO_InputCDR tmp_in (out);
00463           in = tmp_in;
00464         }
00465 
00466       // If we have an exception type, unmarshal the repository ID.
00467       CORBA::TCKind kind =
00468         TAO_DynAnyFactory::unalias (this->type_.in ());
00469 
00470       if (kind == CORBA::tk_except)
00471         {
00472           CORBA::String_var str;
00473           in >> str.out ();
00474         }
00475 
00476       CORBA::TypeCode_var field_tc;
00477       CORBA::TypeCode_var unaliased =
00478         TAO::unaliased_typecode (this->type_.in ()
00479                                 );
00480 
00481       for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00482         {
00483           field_tc = unaliased->member_type (i);
00484 
00485           CORBA::Any field_any;
00486           TAO_InputCDR unk_in (in);
00487           TAO::Unknown_IDL_Type *unk = 0;
00488           ACE_NEW (unk,
00489                    TAO::Unknown_IDL_Type (field_tc.in (),
00490                                           unk_in));
00491           field_any.replace (unk);
00492 
00493           this->da_members_[i]->destroy ();
00494 
00495           this->da_members_[i] =
00496             TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
00497               field_any._tao_get_typecode (),
00498               field_any);
00499 
00500           // Move to the next field in the CDR stream.
00501           (void) TAO_Marshal_Object::perform_skip (field_tc.in (), &in);
00502         }
00503 
00504       this->current_position_ = this->component_count_ ? 0 : -1;
00505     }
00506   else
00507     {
00508       throw DynamicAny::DynAny::TypeMismatch ();
00509     }
00510 }
00511 
00512 CORBA::Any_ptr
00513 TAO_DynStruct_i::to_any (void)
00514 {
00515   if (this->destroyed_)
00516     {
00517       throw ::CORBA::OBJECT_NOT_EXIST ();
00518     }
00519 
00520   TAO_OutputCDR out_cdr;
00521 
00522   // If we have an exception type, marshal the repository ID.
00523   CORBA::TCKind kind = TAO_DynAnyFactory::unalias (this->type_.in ());
00524 
00525   if (kind == CORBA::tk_except)
00526     {
00527       out_cdr << this->type_->id ();
00528     }
00529 
00530   TAO::Any_Impl *field_impl = 0;
00531   TAO::Unknown_IDL_Type *field_unk = 0;
00532   TAO_InputCDR field_in_cdr (static_cast<ACE_Message_Block *> (0));
00533 
00534   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00535     {
00536       CORBA::TypeCode_var field_tc =
00537         this->da_members_[i]->type ();
00538 
00539       // Recursive step.
00540       CORBA::Any_var field_any =
00541         this->da_members_[i]->to_any ();
00542 
00543       TAO_OutputCDR field_out_cdr;
00544       field_impl = field_any->impl ();
00545 
00546       if (field_impl->encoded ())
00547         {
00548           field_unk =
00549             dynamic_cast<TAO::Unknown_IDL_Type *> (field_impl);
00550 
00551           if (!field_unk)
00552             throw CORBA::INTERNAL ();
00553 
00554           field_in_cdr = field_unk->_tao_get_cdr ();
00555         }
00556       else
00557         {
00558           field_impl->marshal_value (field_out_cdr);
00559           TAO_InputCDR tmp (field_out_cdr);
00560           field_in_cdr = tmp;
00561         }
00562 
00563       (void) TAO_Marshal_Object::perform_append (field_tc.in (),
00564                                                  &field_in_cdr,
00565                                                  &out_cdr);
00566     }
00567 
00568   TAO_InputCDR in_cdr (out_cdr);
00569 
00570   CORBA::Any_ptr retval = 0;
00571   ACE_NEW_THROW_EX (retval,
00572                     CORBA::Any,
00573                     CORBA::NO_MEMORY ());
00574 
00575   TAO::Unknown_IDL_Type *unk = 0;
00576   ACE_NEW_THROW_EX (unk,
00577                     TAO::Unknown_IDL_Type (this->type_.in (),
00578                                            in_cdr),
00579                     CORBA::NO_MEMORY ());
00580 
00581   retval->replace (unk);
00582   return retval;
00583 }
00584 
00585 CORBA::Boolean
00586 TAO_DynStruct_i::equal (DynamicAny::DynAny_ptr rhs)
00587 {
00588   if (this->destroyed_)
00589     {
00590       throw ::CORBA::OBJECT_NOT_EXIST ();
00591     }
00592 
00593   CORBA::TypeCode_var tc = rhs->type ();
00594 
00595   if (!tc->equivalent (this->type_.in ()))
00596     {
00597       return false;
00598     }
00599 
00600   DynamicAny::DynAny_var tmp;
00601   CORBA::Boolean member_equal;
00602 
00603   for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00604     {
00605       rhs->seek (static_cast <CORBA::Long> (i));
00606 
00607       tmp = rhs->current_component ();
00608 
00609       // Recursive step.
00610       member_equal = tmp->equal (this->da_members_[i].in ());
00611 
00612       if (!member_equal)
00613         {
00614           return 0;
00615         }
00616     }
00617 
00618   return true;
00619 }
00620 
00621 void
00622 TAO_DynStruct_i::destroy (void)
00623 {
00624   if (this->destroyed_)
00625     {
00626       throw ::CORBA::OBJECT_NOT_EXIST ();
00627     }
00628 
00629   if (!this->ref_to_component_ || this->container_is_destroying_)
00630     {
00631       // Do a deep destroy.
00632       for (CORBA::ULong i = 0; i < this->component_count_; ++i)
00633         {
00634           this->set_flag (da_members_[i].in (), 1);
00635 
00636           this->da_members_[i]->destroy ();
00637         }
00638 
00639       this->destroyed_ = 1;
00640     }
00641 }
00642 
00643 DynamicAny::DynAny_ptr
00644 TAO_DynStruct_i::current_component (void)
00645 {
00646   if (this->destroyed_)
00647     {
00648       throw ::CORBA::OBJECT_NOT_EXIST ();
00649     }
00650 
00651   if (this->component_count_ == 0)
00652     {
00653       // Empty exception - no component.
00654       throw DynamicAny::DynAny::TypeMismatch ();
00655     }
00656 
00657   if (this->current_position_ == -1)
00658     {
00659       return DynamicAny::DynAny::_nil ();
00660     }
00661 
00662   CORBA::ULong index = static_cast <CORBA::ULong> (this->current_position_);
00663 
00664   this->set_flag (this->da_members_[index].in (), 0);
00665 
00666   return DynamicAny::DynAny::_duplicate (this->da_members_[index].in ());
00667 }
00668 
00669 TAO_END_VERSIONED_NAMESPACE_DECL

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