Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / AnyTypeCode / Any.cpp
blob2af5807cb6425ffcb0082b1445aacd8be6437282
1 // -*- C++ -*-
2 #include "tao/AnyTypeCode/Any.h"
3 #include "tao/AnyTypeCode/Any_Basic_Impl.h"
4 #include "tao/AnyTypeCode/Any_Impl_T.h"
5 #include "tao/AnyTypeCode/Any_Special_Impl_T.h"
6 #include "tao/AnyTypeCode/Any_Dual_Impl_T.h"
7 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
8 #include "tao/AnyTypeCode/TypeCode.h"
9 #include "tao/AnyTypeCode/TypeCode_Constants.h"
10 #include "tao/CDR.h"
11 #include "tao/SystemException.h"
13 #include "ace/Log_Msg.h"
14 #include "ace/OS_NS_string.h"
15 #include "ace/OS_NS_wchar.h"
16 #include "ace/OS_Memory.h"
17 #include <cstring>
19 #if !defined (__ACE_INLINE__)
20 # include "tao/AnyTypeCode/Any.inl"
21 #endif /* ! __ACE_INLINE__ */
23 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
25 using namespace TAO;
27 CORBA::Any::Any (const CORBA::Any &rhs)
28 : impl_ (rhs.impl_)
30 if (this->impl_)
32 this->impl_->_add_ref ();
36 CORBA::Any::~Any ()
38 if (this->impl_)
40 this->impl_->_remove_ref ();
44 CORBA::Any &
45 CORBA::Any::operator= (const CORBA::Any &rhs)
47 if (this->impl_ != rhs.impl_)
49 if (this->impl_)
51 this->impl_->_remove_ref ();
54 this->impl_ = rhs.impl_;
56 if (this->impl_)
58 this->impl_->_add_ref ();
62 return *this;
65 void
66 CORBA::Any::replace (TAO::Any_Impl *new_impl)
68 ACE_ASSERT (new_impl != 0);
70 if (this->impl_)
72 this->impl_->_remove_ref ();
75 this->impl_ = new_impl;
78 CORBA::TypeCode_ptr
79 CORBA::Any::type () const
81 if (this->impl_)
83 return this->impl_->type ();
86 return CORBA::TypeCode::_duplicate (CORBA::_tc_null);
89 CORBA::TypeCode_ptr
90 CORBA::Any::_tao_get_typecode () const
92 if (this->impl_)
94 return this->impl_->_tao_get_typecode ();
97 return CORBA::_tc_null;
100 void
101 CORBA::Any::_tao_set_typecode (const CORBA::TypeCode_ptr tc)
103 if (!this->impl_)
105 ACE_NEW (this->impl_,
106 TAO::Unknown_IDL_Type (tc));
108 else
110 this->impl_->type (tc);
115 CORBA::Any::_tao_byte_order () const
117 if (this->impl_)
119 return this->impl_->_tao_byte_order ();
122 return TAO_ENCAP_BYTE_ORDER;
125 void
126 CORBA::Any::type (CORBA::TypeCode_ptr tc)
128 CORBA::Boolean equiv = false;
130 if (this->impl_)
132 equiv = this->impl_->_tao_get_typecode ()->equivalent (tc);
135 if (equiv)
137 this->impl_->type (tc);
139 else
141 throw ::CORBA::BAD_TYPECODE ();
145 void
146 CORBA::Any::_tao_any_destructor (void *x)
148 CORBA::Any *tmp = static_cast<CORBA::Any *> (x);
149 delete tmp;
152 CORBA::Any::to_object::to_object (CORBA::Object_out obj)
153 : ref_ (obj.ptr ())
157 CORBA::Any::to_abstract_base::to_abstract_base (CORBA::AbstractBase_ptr &obj)
158 : ref_ (obj)
162 CORBA::Any::to_value::to_value (CORBA::ValueBase *& obj)
163 : ref_ (obj)
167 CORBA::Boolean
168 CORBA::Any::checked_to_object (CORBA::Object_ptr &_tao_elem) const
170 if (!this->impl_)
172 return false;
175 return this->impl_->to_object (_tao_elem);
178 CORBA::Boolean
179 CORBA::Any::checked_to_value (CORBA::ValueBase *&_tao_elem) const
181 if (!this->impl_)
183 return false;
186 return this->impl_->to_value (_tao_elem);
189 CORBA::Boolean
190 CORBA::Any::checked_to_abstract_base (CORBA::AbstractBase_ptr &_tao_elem) const
192 if (!this->impl_)
194 return false;
197 return this->impl_->to_abstract_base (_tao_elem);
200 // ****************************************************************
202 CORBA::Any_var::Any_var (const CORBA::Any_var &r)
204 if (r.ptr_)
206 ACE_NEW (this->ptr_,
207 CORBA::Any (*r.ptr_));
211 CORBA::Any_var &
212 CORBA::Any_var::operator= (CORBA::Any *p)
214 if (this->ptr_ != p)
216 delete this->ptr_;
217 this->ptr_ = p;
220 return *this;
223 CORBA::Any_var &
224 CORBA::Any_var::operator= (const CORBA::Any_var &r)
226 delete this->ptr_;
227 this->ptr_ = nullptr;
229 if (r.ptr_)
231 ACE_NEW_RETURN (this->ptr_,
232 CORBA::Any (*r.ptr_),
233 *this);
236 return *this;
239 // ***********************************************************************
241 CORBA::Boolean
242 operator<< (TAO_OutputCDR &cdr, const CORBA::Any &any)
244 TAO::Any_Impl *impl = any.impl ();
246 if (impl)
248 return impl->marshal (cdr);
251 return (cdr << CORBA::_tc_null);
254 CORBA::Boolean
255 operator>> (TAO_InputCDR &cdr, CORBA::Any &any)
257 CORBA::TypeCode_var tc;
259 if (!(cdr >> tc.out ()))
261 return false;
266 TAO::Unknown_IDL_Type *impl = nullptr;
267 ACE_NEW_RETURN (impl,
268 TAO::Unknown_IDL_Type (tc.in ()),
269 false);
271 any.replace (impl);
272 impl->_tao_decode (cdr);
274 catch (const ::CORBA::Exception&)
276 return false;
279 return true;
282 #if defined (GEN_OSTREAM_OPS)
284 std::ostream &
285 operator<< (std::ostream &strm, const CORBA::Any &)
287 return strm << "CORBA::Any";
290 #endif /* GEN_OSTREAM_OPS */
292 // =======================================================================
294 // Insertion of the special basic types.
296 void
297 CORBA::Any::operator<<= (CORBA::Any::from_boolean b)
299 TAO::Any_Basic_Impl::insert (*this, CORBA::_tc_boolean, &b.val_);
302 void
303 CORBA::Any::operator<<= (CORBA::Any::from_octet o)
305 TAO::Any_Basic_Impl::insert (*this, CORBA::_tc_octet, &o.val_);
308 void
309 CORBA::Any::operator<<= (CORBA::Any::from_char c)
311 TAO::Any_Basic_Impl::insert (*this, CORBA::_tc_char, &c.val_);
314 void
315 CORBA::Any::operator<<= (CORBA::Any::from_wchar wc)
317 TAO::Any_Basic_Impl::insert (*this, CORBA::_tc_wchar, &wc.val_);
320 void
321 CORBA::Any::operator<<= (CORBA::Any::from_string s)
323 if (s.bound_ > 0 && s.val_ != 0 && std::strlen (s.val_) > s.bound_)
325 return;
328 TAO::Any_Special_Impl_T<
329 char,
330 CORBA::Any::from_string,
331 CORBA::Any::to_string
332 >::insert (*this,
333 TAO::Any_Impl::_tao_any_string_destructor,
334 CORBA::_tc_string,
335 s.nocopy_ ? s.val_ : CORBA::string_dup (s.val_),
336 s.bound_);
339 void
340 CORBA::Any::operator<<= (CORBA::Any::from_wstring ws)
342 if (ws.bound_ > 0 && ws.val_ != 0 && ACE_OS::wslen (ws.val_) > ws.bound_)
344 return;
347 TAO::Any_Special_Impl_T<
348 CORBA::WChar,
349 CORBA::Any::from_wstring,
350 CORBA::Any::to_wstring
351 >::insert (*this,
352 TAO::Any_Impl::_tao_any_wstring_destructor,
353 CORBA::_tc_wstring,
354 ws.nocopy_ ? ws.val_ : CORBA::wstring_dup (ws.val_),
355 ws.bound_);
358 // Insertion of the other basic types.
360 namespace CORBA
362 void
363 operator<<= (CORBA::Any &any, CORBA::Short s)
365 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_short, &s);
368 void
369 operator<<= (CORBA::Any &any, CORBA::UShort us)
371 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_ushort, &us);
374 void
375 operator<<= (CORBA::Any &any, CORBA::Long l)
377 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_long, &l);
380 void
381 operator<<= (CORBA::Any &any, CORBA::ULong ul)
383 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_ulong, &ul);
386 void
387 operator<<= (CORBA::Any &any, CORBA::LongLong ll)
389 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_longlong, &ll);
392 void
393 operator<<= (CORBA::Any &any, CORBA::ULongLong ull)
395 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_ulonglong, &ull);
398 void
399 operator<<= (CORBA::Any &any, CORBA::Float f)
401 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_float, &f);
404 void
405 operator<<= (CORBA::Any &any, CORBA::Double d)
407 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_double, &d);
410 void
411 operator<<= (CORBA::Any &any, CORBA::LongDouble ld)
413 TAO::Any_Basic_Impl::insert (any, CORBA::_tc_longdouble, &ld);
416 // Insertion of Any - copying.
417 void
418 operator<<= (CORBA::Any &any, const CORBA::Any &a)
420 TAO::Any_Dual_Impl_T<CORBA::Any>::insert_copy (
421 any,
422 CORBA::Any::_tao_any_destructor,
423 CORBA::_tc_any,
427 // Insertion of Any - non-copying.
428 void
429 operator<<= (CORBA::Any &any, CORBA::Any *a)
431 TAO::Any_Dual_Impl_T<CORBA::Any>::insert (any,
432 CORBA::Any::_tao_any_destructor,
433 CORBA::_tc_any,
437 // Insertion of TypeCode - copying.
438 void
439 operator<<= (CORBA::Any &any, CORBA::TypeCode_ptr tc)
441 CORBA::TypeCode_ptr dup = CORBA::TypeCode::_duplicate (tc);
442 any <<= &dup;
445 // Insertion of TypeCode - non-copying.
446 void
447 operator<<= (CORBA::Any &any, CORBA::TypeCode_ptr * tc)
449 TAO::Any_Impl_T<CORBA::TypeCode>::insert (
450 any,
451 CORBA::TypeCode::_tao_any_destructor,
452 CORBA::_tc_TypeCode,
453 *tc);
456 // Insertion of CORBA object - copying.
457 void
458 operator<<= (CORBA::Any &any, const CORBA::Object_ptr obj)
460 CORBA::Object_ptr dup = CORBA::Object::_duplicate (obj);
461 any <<= &dup;
464 // Insertion of CORBA object - non-copying.
465 void
466 operator<<= (CORBA::Any &any, CORBA::Object_ptr *objptr)
468 TAO::Any_Impl_T<CORBA::Object>::insert (any,
469 CORBA::Object::_tao_any_destructor,
470 CORBA::_tc_Object,
471 *objptr);
474 // These are copying versions for unbounded (w)strings. They are not inlined,
475 // to avoid use in Any.inl before definition in ORB.inl.
477 void
478 operator<<= (CORBA::Any &any, const char *s)
480 TAO::Any_Special_Impl_T<
481 char,
482 CORBA::Any::from_string,
483 CORBA::Any::to_string
484 >::insert (any,
485 TAO::Any_Impl::_tao_any_string_destructor,
486 CORBA::_tc_string,
487 CORBA::string_dup (s),
491 void
492 operator<<= (CORBA::Any &any, const CORBA::WChar *ws)
494 TAO::Any_Special_Impl_T<
495 CORBA::WChar,
496 CORBA::Any::from_wstring,
497 CORBA::Any::to_wstring
498 >::insert (any,
499 TAO::Any_Impl::_tao_any_wstring_destructor,
500 CORBA::_tc_wstring,
501 CORBA::wstring_dup (ws),
505 void
506 operator <<= (CORBA::Any &any, const std::string & str)
508 any <<= str.c_str ();
511 void
512 operator <<= (CORBA::Any &, std::string *)
514 // @todo
517 #if !defined(ACE_LACKS_STD_WSTRING)
518 void
519 operator <<= (CORBA::Any &any, const std::wstring & str)
521 any <<= str.c_str ();
524 void
525 operator <<= (CORBA::Any &, std::wstring *)
527 // @todo
529 #endif
531 // Extraction: these are safe and hence we have to check that the
532 // typecode of the Any is equal to the one we are trying to extract
533 // into.
535 // Extraction into the special basic types.
537 CORBA::Boolean
538 CORBA::Any::operator>>= (CORBA::Any::to_boolean b) const
540 return TAO::Any_Basic_Impl::extract (*this, CORBA::_tc_boolean, &b.ref_);
543 CORBA::Boolean
544 CORBA::Any::operator>>= (CORBA::Any::to_octet o) const
546 return TAO::Any_Basic_Impl::extract (*this, CORBA::_tc_octet, &o.ref_);
549 CORBA::Boolean
550 CORBA::Any::operator>>= (CORBA::Any::to_char c) const
552 return TAO::Any_Basic_Impl::extract (*this, CORBA::_tc_char, &c.ref_);
555 CORBA::Boolean
556 CORBA::Any::operator>>= (CORBA::Any::to_wchar wc) const
558 return TAO::Any_Basic_Impl::extract (*this, CORBA::_tc_wchar, &wc.ref_);
561 CORBA::Boolean
562 CORBA::Any::operator>>= (CORBA::Any::to_string s) const
564 return
565 TAO::Any_Special_Impl_T<
566 char,
567 CORBA::Any::from_string,
568 CORBA::Any::to_string
569 >::extract (*this,
570 TAO::Any_Impl::_tao_any_string_destructor,
571 CORBA::_tc_string,
572 s.val_,
573 s.bound_);
576 CORBA::Boolean
577 CORBA::Any::operator>>= (CORBA::Any::to_wstring ws) const
579 return
580 TAO::Any_Special_Impl_T<
581 CORBA::WChar,
582 CORBA::Any::from_wstring,
583 CORBA::Any::to_wstring
584 >::extract (*this,
585 TAO::Any_Impl::_tao_any_wstring_destructor,
586 CORBA::_tc_wstring,
587 ws.val_,
588 ws.bound_);
591 CORBA::Boolean
592 CORBA::Any::operator>>= (CORBA::Any::to_object obj) const
594 return this->checked_to_object (obj.ref_);
597 CORBA::Boolean
598 CORBA::Any::operator>>= (CORBA::Any::to_abstract_base obj) const
600 return this->checked_to_abstract_base (obj.ref_);
603 CORBA::Boolean
604 CORBA::Any::operator>>= (CORBA::Any::to_value obj) const
606 return this->checked_to_value (obj.ref_);
609 // Extraction into the other basic types.
611 CORBA::Boolean
612 operator>>= (const CORBA::Any &any, CORBA::Short &s)
614 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_short, std::addressof(s));
617 CORBA::Boolean
618 operator>>= (const CORBA::Any &any, CORBA::UShort &us)
620 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_ushort, std::addressof(us));
623 CORBA::Boolean
624 operator>>= (const CORBA::Any &any, CORBA::Long &l)
626 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_long, std::addressof(l));
629 CORBA::Boolean
630 operator>>= (const CORBA::Any &any, CORBA::ULong &ul)
632 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_ulong, std::addressof(ul));
635 CORBA::Boolean
636 operator>>= (const CORBA::Any &any, CORBA::LongLong &ll)
638 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_longlong, std::addressof(ll));
641 CORBA::Boolean
642 operator>>= (const CORBA::Any &any, CORBA::ULongLong &ull)
644 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_ulonglong, std::addressof(ull));
647 CORBA::Boolean
648 operator>>= (const CORBA::Any &any, CORBA::Float &f)
650 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_float, std::addressof(f));
653 CORBA::Boolean
654 operator>>= (const CORBA::Any &any, CORBA::Double &d)
656 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_double, std::addressof(d));
659 CORBA::Boolean
660 operator>>= (const CORBA::Any &any, CORBA::LongDouble &ld)
662 return TAO::Any_Basic_Impl::extract (any, CORBA::_tc_longdouble, std::addressof(ld));
665 CORBA::Boolean
666 operator>>= (const CORBA::Any &any, const CORBA::Any *&a)
668 return TAO::Any_Dual_Impl_T<CORBA::Any>::extract (
669 any,
670 CORBA::Any::_tao_any_destructor,
671 CORBA::_tc_any,
675 CORBA::Boolean
676 operator>>= (const CORBA::Any &any, const CORBA::Char *&s)
678 return
679 TAO::Any_Special_Impl_T<
680 char,
681 CORBA::Any::from_string,
682 CORBA::Any::to_string
683 >::extract (any,
684 TAO::Any_Impl::_tao_any_string_destructor,
685 CORBA::_tc_string,
690 CORBA::Boolean
691 operator>>= (const CORBA::Any &any, const CORBA::WChar *&ws)
693 return
694 TAO::Any_Special_Impl_T<
695 CORBA::WChar,
696 CORBA::Any::from_wstring,
697 CORBA::Any::to_wstring
698 >::extract (any,
699 TAO::Any_Impl::_tao_any_wstring_destructor,
700 CORBA::_tc_wstring,
705 CORBA::Boolean
706 operator>>= (const CORBA::Any &any, CORBA::TypeCode_ptr &tc)
708 return TAO::Any_Impl_T<CORBA::TypeCode>::extract (
709 any,
710 CORBA::TypeCode::_tao_any_destructor,
711 CORBA::_tc_TypeCode,
712 tc);
715 CORBA::Boolean
716 operator >>= (const CORBA::Any &any, std::string &str)
718 const char *buf = nullptr;
719 CORBA::Boolean const flag = any >>= buf;
721 if (buf)
723 str.assign (buf);
726 return flag;
729 #if !defined(ACE_LACKS_STD_WSTRING)
730 CORBA::Boolean
731 operator >>= (const CORBA::Any &any, std::wstring &str)
733 const wchar_t *buf = nullptr;
734 CORBA::Boolean const flag = any >>= buf;
736 if (buf)
738 str.assign (buf);
741 return flag;
743 #endif
747 // ================================================================
748 // Any_Impl_T template specializations.
750 namespace TAO
752 template<>
753 CORBA::Boolean
754 Any_Impl_T<CORBA::Object>::to_object (CORBA::Object_ptr &_tao_elem) const
756 _tao_elem = CORBA::Object::_duplicate (this->value_);
757 return true;
761 TAO_END_VERSIONED_NAMESPACE_DECL