Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tao / DynamicAny / DynSequence_i.cpp
blob749a3b79e932cec1d9b8cad44a836825c8a03302
1 // -*- C++ -*-
2 #include "tao/DynamicAny/DynSequence_i.h"
3 #include "tao/DynamicAny/DynAnyFactory.h"
4 #include "tao/DynamicAny/DynAnyUtils_T.h"
6 #include "tao/AnyTypeCode/Marshal.h"
7 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
8 #include "tao/AnyTypeCode/AnyTypeCode_methods.h"
10 #include "tao/CDR.h"
12 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
14 TAO_DynSequence_i::TAO_DynSequence_i (CORBA::Boolean allow_truncation)
15 : TAO_DynCommon (allow_truncation)
19 TAO_DynSequence_i::~TAO_DynSequence_i ()
23 void
24 TAO_DynSequence_i::init_common ()
26 this->ref_to_component_ = false;
27 this->container_is_destroying_ = false;
28 this->has_components_ = true;
29 this->destroyed_ = false;
30 this->current_position_ = -1;
31 this->component_count_ = static_cast<CORBA::ULong> (this->da_members_.size ());
34 void
35 TAO_DynSequence_i::init (const CORBA::Any& any)
37 CORBA::TypeCode_var tc = any.type ();
39 CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc.in ());
41 if (kind != CORBA::tk_sequence)
43 throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
46 this->type_ = tc;
48 // Get the CDR stream of the Any, if there isn't one, make one.
49 TAO::Any_Impl *impl = any.impl ();
50 CORBA::ULong length;
51 TAO_OutputCDR out;
52 TAO_InputCDR cdr (static_cast<ACE_Message_Block *> (0));
54 if (impl->encoded ())
56 TAO::Unknown_IDL_Type * const unk =
57 dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
59 if (!unk)
60 throw CORBA::INTERNAL ();
62 cdr = unk->_tao_get_cdr ();
64 else
66 impl->marshal_value (out);
67 TAO_InputCDR tmp_in (out);
68 cdr = tmp_in;
71 // If the any is a sequence, first 4 bytes of cdr hold the
72 // length.
73 cdr.read_ulong (length);
75 // Resize the array.
76 this->da_members_.size (length);
78 this->init_common ();
80 // Get the type of the sequence elments.
81 CORBA::TypeCode_var field_tc =
82 this->get_element_type ();
84 for (CORBA::ULong i = 0; i < length; ++i)
86 CORBA::Any field_any;
87 TAO_InputCDR unk_in (cdr);
88 TAO::Unknown_IDL_Type *field_unk = 0;
89 ACE_NEW (field_unk,
90 TAO::Unknown_IDL_Type (field_tc.in (), unk_in));
91 field_any.replace (field_unk);
93 // This recursive step will call the correct constructor
94 // based on the type of field_any.
95 this->da_members_[i] =
96 TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
97 field_any._tao_get_typecode (),
98 field_any,
99 this->allow_truncation_ );
101 // Move to the next field in the CDR stream.
102 (void) TAO_Marshal_Object::perform_skip (field_tc.in (), &cdr);
106 void
107 TAO_DynSequence_i::init (CORBA::TypeCode_ptr tc)
109 CORBA::TCKind kind = TAO_DynAnyFactory::unalias (tc);
111 if (kind != CORBA::tk_sequence)
113 throw DynamicAny::DynAnyFactory::InconsistentTypeCode ();
116 // Empty sequence.
117 this->da_members_.size (0);
119 this->init_common ();
121 this->type_ = CORBA::TypeCode::_duplicate (tc);
124 // ****************************************************************
126 TAO_DynSequence_i *
127 TAO_DynSequence_i::_narrow (CORBA::Object_ptr _tao_objref)
129 if (CORBA::is_nil (_tao_objref))
131 return 0;
134 return dynamic_cast<TAO_DynSequence_i *> (_tao_objref);
137 // ****************************************************************
139 CORBA::TypeCode_ptr
140 TAO_DynSequence_i::get_element_type ()
142 CORBA::TypeCode_var element_type =
143 CORBA::TypeCode::_duplicate (this->type_.in ());
145 // Strip away aliases (if any) on top of the outer type.
146 CORBA::TCKind kind = element_type->kind ();
148 while (kind != CORBA::tk_sequence)
150 element_type = element_type->content_type ();
152 kind = element_type->kind ();
155 // Return the content type.
156 CORBA::TypeCode_ptr retval = element_type->content_type ();
158 return retval;
161 // = Functions specific to DynSequence.
163 CORBA::ULong
164 TAO_DynSequence_i::get_length ()
166 if (this->destroyed_)
168 throw ::CORBA::OBJECT_NOT_EXIST ();
171 return this->component_count_;
174 void
175 TAO_DynSequence_i::set_length (CORBA::ULong length)
177 if (this->destroyed_)
179 throw ::CORBA::OBJECT_NOT_EXIST ();
182 // CORBA::TypeCode::length() does not accept aliased type codes.
183 CORBA::TypeCode_var stripped_tc =
184 TAO_DynAnyFactory::strip_alias (this->type_.in ());
186 CORBA::ULong bound = stripped_tc->length ();
188 if (bound > 0 && length > bound)
190 throw DynamicAny::DynAny::InvalidValue ();
193 // CORBA 2.3.1 has several explicit rules about resetting the
194 // current position, depending on the current value of the
195 // current position, the current size, and the new length.
196 if (length == 0)
198 this->current_position_ = -1;
200 else if (length > this->component_count_)
202 if (this->current_position_ == -1)
204 // Set it to the first new slot.
205 this->current_position_ = static_cast<CORBA::Long> (this->component_count_);
208 else if (length < this->component_count_)
210 // If the current position will no longer exist..
211 if (this->current_position_ >= static_cast<CORBA::Long> (length))
213 this->current_position_ = -1;
217 if (length > this->component_count_)
219 // Grow array first, then initialize new members.
220 this->da_members_.size (length);
222 CORBA::TypeCode_var elemtype =
223 stripped_tc->content_type ();
225 for (CORBA::ULong i = this->component_count_; i < length; ++i)
227 this->da_members_[i] =
228 TAO::MakeDynAnyUtils::make_dyn_any_t<CORBA::TypeCode_ptr> (
229 elemtype.in (),
230 elemtype.in (),
231 this->allow_truncation_ );
234 else if (length < this->component_count_)
236 // Destroy any dangling members first, then shrink array.
237 for (CORBA::ULong j = length; j < this->component_count_; ++j)
239 this->da_members_[j]->destroy ();
242 this->da_members_.size (length);
245 // Now we can update component_count_.
246 this->component_count_ = length;
249 DynamicAny::AnySeq *
250 TAO_DynSequence_i::get_elements ()
252 if (this->destroyed_)
254 throw ::CORBA::OBJECT_NOT_EXIST ();
257 CORBA::ULong length = static_cast<CORBA::ULong> (this->da_members_.size ());
259 DynamicAny::AnySeq *elements;
260 ACE_NEW_THROW_EX (elements,
261 DynamicAny::AnySeq (length),
262 CORBA::NO_MEMORY ());
264 elements->length (length);
265 DynamicAny::AnySeq_var safe_retval = elements;
267 // Initialize each Any.
268 for (CORBA::ULong i = 0; i < length; ++i)
270 CORBA::Any_var tmp =
271 this->da_members_[i]->to_any ();
274 safe_retval[i] = tmp.in ();
277 return safe_retval._retn ();
280 void
281 TAO_DynSequence_i::set_elements (const DynamicAny::AnySeq & value)
283 if (this->destroyed_)
285 throw ::CORBA::OBJECT_NOT_EXIST ();
288 CORBA::TypeCode_var stripped_tc =
289 TAO_DynAnyFactory::strip_alias (this->type_.in ());
291 CORBA::ULong length = value.length ();
292 CORBA::ULong bound = stripped_tc->length ();
294 if (bound > 0 && length > bound)
296 throw DynamicAny::DynAny::InvalidValue ();
299 // CORBA 2.4.2.
300 if (length == 0)
302 this->current_position_ = -1;
304 else
306 this->current_position_ = 0;
309 // If the array grows, we must do it now.
310 if (length > this->component_count_)
312 this->da_members_.size (length);
315 CORBA::TypeCode_var element_type = this->get_element_type ();
317 CORBA::TypeCode_var value_tc;
319 for (CORBA::ULong i = 0; i < length; ++i)
321 // Check each arg element for type match.
322 value_tc = value[i].type ();
323 CORBA::Boolean equivalent =
324 value_tc->equivalent (element_type.in ());
326 if (equivalent)
328 // Destroy any existing members.
329 if (i < this->component_count_)
331 this->da_members_[i]->destroy ();
334 this->da_members_[i] =
335 TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
336 value[i]._tao_get_typecode (),
337 value[i],
338 this->allow_truncation_ );
340 else
342 throw DynamicAny::DynAny::TypeMismatch ();
346 // Destroy any dangling members.
347 for (CORBA::ULong j = length; j < this->component_count_; ++j)
349 this->da_members_[j]->destroy ();
352 // If the array shrinks, we must wait until now to do it.
353 if (length < this->component_count_)
355 this->da_members_.size (length);
358 // Now we can update component_count_.
359 this->component_count_ = length;
362 DynamicAny::DynAnySeq *
363 TAO_DynSequence_i::get_elements_as_dyn_any ()
365 if (this->destroyed_)
367 throw ::CORBA::OBJECT_NOT_EXIST ();
370 DynamicAny::DynAnySeq *retval = 0;
371 ACE_NEW_THROW_EX (retval,
372 DynamicAny::DynAnySeq (this->component_count_),
373 CORBA::NO_MEMORY ());
375 retval->length (this->component_count_);
376 DynamicAny::DynAnySeq_var safe_retval (retval);
378 for (CORBA::ULong i = 0; i < this->component_count_; ++i)
380 // A deep copy is made only by copy() (CORBA 2.4.2 section 9.2.3.6).
381 // Set the flag so the caller can't destroy.
382 this->set_flag (this->da_members_[i].in (), 0);
384 safe_retval[i] =
385 DynamicAny::DynAny::_duplicate (this->da_members_[i].in ());
388 return safe_retval._retn ();
391 void
392 TAO_DynSequence_i::set_elements_as_dyn_any (
393 const DynamicAny::DynAnySeq & values)
395 if (this->destroyed_)
397 throw ::CORBA::OBJECT_NOT_EXIST ();
400 CORBA::TypeCode_var stripped_tc =
401 TAO_DynAnyFactory::strip_alias (this->type_.in ());
403 CORBA::ULong length = values.length ();
404 CORBA::ULong bound = stripped_tc->length ();
406 if (bound > 0 && length > bound)
408 throw DynamicAny::DynAny::InvalidValue ();
411 // If the array grows, we must do it now.
412 if (length > this->component_count_)
414 this->da_members_.size (length);
417 CORBA::TypeCode_var element_type =
418 this->get_element_type ();
420 CORBA::TypeCode_var val_type;
421 CORBA::Boolean equivalent;
423 for (CORBA::ULong i = 0; i < length; ++i)
425 val_type = values[i]->type ();
427 equivalent = val_type->equivalent (element_type.in ());
429 if (equivalent)
431 // Destroy any existing members.
432 if (i < this->component_count_)
434 this->da_members_[i]->destroy ();
437 this->da_members_[i] =
438 values[i]->copy ();
440 else
442 throw DynamicAny::DynAny::TypeMismatch ();
446 // Destroy any dangling members.
447 for (CORBA::ULong j = length; j < this->component_count_; ++j)
449 this->da_members_[j]->destroy ();
452 // If the array shrinks, we must wait until now to do it.
453 if (length < this->component_count_)
455 this->da_members_.size (length);
458 // Now we can update component_count_.
459 this->component_count_ = length;
462 // ****************************************************************
464 void
465 TAO_DynSequence_i::from_any (const CORBA::Any & any)
467 if (this->destroyed_)
469 throw ::CORBA::OBJECT_NOT_EXIST ();
472 CORBA::TypeCode_var tc = any.type ();
473 CORBA::Boolean equivalent =
474 this->type_.in ()->equivalent (tc.in ());
476 if (equivalent)
478 // Get the CDR stream of the Any, if there isn't one, make one.
479 TAO::Any_Impl *impl = any.impl ();
480 TAO_OutputCDR out;
481 TAO_InputCDR cdr (static_cast<ACE_Message_Block *> (0));
483 if (impl->encoded ())
485 TAO::Unknown_IDL_Type * const unk =
486 dynamic_cast<TAO::Unknown_IDL_Type *> (impl);
488 if (!unk)
489 throw CORBA::INTERNAL ();
491 cdr = unk->_tao_get_cdr ();
493 else
495 impl->marshal_value (out);
496 TAO_InputCDR tmp_in (out);
497 cdr = tmp_in;
501 CORBA::ULong arg_length;
503 // If the any is a sequence, first 4 bytes of cdr hold the
504 // length.
505 cdr.read_ulong (arg_length);
507 // If the array grows, we must do it now.
508 if (arg_length > this->component_count_)
510 this->da_members_.size (arg_length);
513 CORBA::TypeCode_var field_tc =
514 this->get_element_type ();
516 for (CORBA::ULong i = 0; i < arg_length; ++i)
518 CORBA::Any field_any;
519 TAO_InputCDR unk_in (cdr);
520 TAO::Unknown_IDL_Type *field_unk = 0;
521 ACE_NEW (field_unk,
522 TAO::Unknown_IDL_Type (field_tc.in (),
523 unk_in));
524 field_any.replace (field_unk);
526 if (i < this->component_count_)
528 this->da_members_[i]->destroy ();
531 this->da_members_[i] =
532 TAO::MakeDynAnyUtils::make_dyn_any_t<const CORBA::Any&> (
533 field_any._tao_get_typecode (),
534 field_any,
535 this->allow_truncation_ );
537 // Move to the next field in the CDR stream.
538 (void) TAO_Marshal_Object::perform_skip (field_tc.in (), &cdr);
541 // Destroy any dangling members.
542 for (CORBA::ULong j = arg_length; j < this->component_count_; ++j)
544 this->da_members_[j]->destroy ();
547 // If the array shrinks, we must wait until now to do it.
548 if (arg_length < this->component_count_)
550 this->da_members_.size (arg_length);
553 // Now we can update component_count_.
554 this->component_count_ = arg_length;
556 this->current_position_ = arg_length ? 0 : -1;
558 else
560 throw DynamicAny::DynAny::TypeMismatch ();
564 CORBA::Any_ptr
565 TAO_DynSequence_i::to_any ()
567 if (this->destroyed_)
569 throw ::CORBA::OBJECT_NOT_EXIST ();
572 TAO_OutputCDR out_cdr;
573 out_cdr.write_ulong (this->component_count_);
575 CORBA::TypeCode_var field_tc =
576 this->get_element_type ();
578 for (CORBA::ULong i = 0; i < this->component_count_; ++i)
580 // Recursive step
581 CORBA::Any_var field_any =
582 this->da_members_[i]->to_any ();
584 TAO::Any_Impl *field_impl = field_any->impl ();
585 TAO_OutputCDR field_out;
586 TAO_InputCDR field_cdr (static_cast<ACE_Message_Block *> (0));
588 if (field_impl->encoded ())
590 TAO::Unknown_IDL_Type * const field_unk =
591 dynamic_cast<TAO::Unknown_IDL_Type *> (field_impl);
593 if (!field_unk)
594 throw CORBA::INTERNAL ();
596 field_cdr = field_unk->_tao_get_cdr ();
598 else
600 field_impl->marshal_value (field_out);
601 TAO_InputCDR tmp_in (field_out);
602 field_cdr = tmp_in;
605 (void) TAO_Marshal_Object::perform_append (field_tc.in (),
606 &field_cdr,
607 &out_cdr);
610 TAO_InputCDR in_cdr (out_cdr);
612 CORBA::Any_ptr retval = 0;
613 ACE_NEW_THROW_EX (retval,
614 CORBA::Any,
615 CORBA::NO_MEMORY ());
617 TAO::Unknown_IDL_Type *unk = 0;
618 ACE_NEW_THROW_EX (unk,
619 TAO::Unknown_IDL_Type (this->type_.in (),
620 in_cdr),
621 CORBA::NO_MEMORY ());
623 retval->replace (unk);
624 return retval;
627 CORBA::Boolean
628 TAO_DynSequence_i::equal (DynamicAny::DynAny_ptr rhs)
630 if (this->destroyed_)
632 throw ::CORBA::OBJECT_NOT_EXIST ();
635 CORBA::TypeCode_var tc = rhs->type ();
637 CORBA::Boolean equivalent = tc->equivalent (this->type_.in ());
639 if (!equivalent)
641 return false;
644 if (rhs->component_count () != this->component_count_)
646 return false;
649 DynamicAny::DynAny_var tmp;
650 CORBA::Boolean member_equal;
652 for (CORBA::ULong i = 0; i < this->component_count_; ++i)
654 rhs->seek (static_cast<CORBA::Long> (i));
656 tmp = rhs->current_component ();
658 // Recursive step.
659 member_equal = tmp->equal (this->da_members_[i].in ());
661 if (!member_equal)
663 return false;
667 return true;
670 void
671 TAO_DynSequence_i::destroy ()
673 if (this->destroyed_)
675 throw ::CORBA::OBJECT_NOT_EXIST ();
678 if (!this->ref_to_component_ || this->container_is_destroying_)
680 // Do a deep destroy.
681 for (CORBA::ULong i = 0; i < this->component_count_; ++i)
683 this->set_flag (da_members_[i].in (), 1);
685 this->da_members_[i]->destroy ();
688 this->destroyed_ = 1;
692 DynamicAny::DynAny_ptr
693 TAO_DynSequence_i::current_component ()
695 if (this->destroyed_)
697 throw ::CORBA::OBJECT_NOT_EXIST ();
700 if (this->current_position_ == -1)
702 return DynamicAny::DynAny::_nil ();
705 CORBA::ULong index = static_cast<CORBA::ULong> (this->current_position_);
707 this->set_flag (this->da_members_[index].in (), 0);
709 return DynamicAny::DynAny::_duplicate (this->da_members_[index].in ());
712 TAO_END_VERSIONED_NAMESPACE_DECL