Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / Valuetype / AbstractBase.cpp
blob62531ec55521cfde16494f13272c219347aadc73
1 // ""
3 #include "tao/Valuetype/AbstractBase.h"
4 #include "tao/Valuetype/ValueBase.h"
5 #include "tao/Valuetype/ValueFactory.h"
6 #include "tao/AnyTypeCode/Objref_TypeCode_Static.h"
7 #include "tao/Stub.h"
8 #include "tao/ORB_Core.h"
9 #include "tao/Profile.h"
10 #include "tao/debug.h"
11 #include "tao/CDR.h"
13 #if !defined (__ACE_INLINE__)
14 # include "tao/Valuetype/AbstractBase.inl"
15 #endif /* ! __ACE_INLINE__ */
17 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
19 CORBA::AbstractBase::AbstractBase ()
20 : is_objref_ (false)
21 , refcount_ (1)
22 , is_collocated_ (false)
23 , servant_ (nullptr)
24 , equivalent_obj_ (CORBA::Object::_nil ())
28 CORBA::AbstractBase::AbstractBase (const CORBA::AbstractBase &rhs)
29 : is_objref_ (rhs.is_objref_)
30 , refcount_ (1)
31 , is_collocated_ (rhs.is_collocated_)
32 , servant_ (rhs.servant_)
33 , equivalent_obj_ (CORBA::Object::_nil ())
35 if (this->is_objref_)
37 // Need to duplicate equivalent obj only if it's objref.
38 this->equivalent_obj_ =
39 CORBA::Object::_duplicate (rhs.equivalent_obj_.in ());
43 CORBA::AbstractBase::AbstractBase (TAO_Stub * protocol_proxy,
44 CORBA::Boolean collocated,
45 TAO_Abstract_ServantBase * servant)
46 : is_objref_ (true)
47 , refcount_ (1)
48 , is_collocated_ (collocated)
49 , servant_ (servant)
50 , equivalent_obj_ (this->create_object (protocol_proxy))
54 void
55 CORBA::AbstractBase::_add_ref ()
57 ++this->refcount_;
59 // This is required by the C++ Mapping 1.2.
60 if (this->is_objref_)
62 CORBA::Object::_duplicate (this->equivalent_obj_.in ());
66 void
67 CORBA::AbstractBase::_remove_ref ()
69 // This is required by the C++ Mapping 1.2.
70 if (this->is_objref_)
72 CORBA::release (this->equivalent_obj_.in ());
75 if (--this->refcount_ == 0)
77 // If this object is going to be deleted here then the reference to
78 // equivalent_obj_ that it owned is already released a few lines above.
79 this->equivalent_obj_._retn ();
81 delete this;
85 void
86 CORBA::AbstractBase::_tao_any_destructor (void *x)
88 CORBA::AbstractBase_ptr tmp = static_cast<CORBA::AbstractBase_ptr> (x);
89 ::CORBA::release (tmp);
92 CORBA::Object_ptr
93 CORBA::AbstractBase::_to_object ()
95 if (!this->is_objref_)
97 return CORBA::Object::_nil ();
100 return CORBA::Object::_duplicate (this->equivalent_obj_.in ());
103 CORBA::ValueBase *
104 CORBA::AbstractBase::_to_value ()
106 if (this->is_objref_)
108 return nullptr;
111 CORBA::ValueBase *retval = this->_tao_to_value ();
113 if (retval != nullptr)
115 retval->_add_ref ();
118 return retval;
121 CORBA::Boolean
122 CORBA::AbstractBase::_is_a (const char *type_id)
124 if (this->is_objref_)
126 if (!CORBA::is_nil (this->equivalent_obj_.in ()))
128 return this->equivalent_obj_->_is_a (type_id);
132 return (ACE_OS::strcmp (type_id, "IDL:omg.org/CORBA/AbstractBase:1.0") == 0);
135 const char *
136 CORBA::AbstractBase::_interface_repository_id () const
138 return "IDL:omg.org/CORBA/AbstractBase:1.0";
141 const char *
142 CORBA::AbstractBase::_tao_obv_repository_id () const
144 return "IDL:omg.org/CORBA/AbstractBase:1.0";
147 CORBA::Boolean
148 operator<< (TAO_OutputCDR &strm, const CORBA::AbstractBase_ptr abs)
150 CORBA::Boolean discriminator = true;
152 // We marshal a null abstract interface ptr as a discriminator
153 // plus null object reference (see CORBA::Object::marshal()
154 // and operator << for CORBA::Object).
155 if (CORBA::is_nil (abs))
157 // Marshal discriminator, then empty type hint.
158 strm << ACE_OutputCDR::from_boolean (discriminator);
159 return strm << CORBA::Object::_nil ();
162 if (abs->_is_objref ())
164 if (strm << ACE_OutputCDR::from_boolean (discriminator))
166 TAO_Stub *stubobj = abs->_stubobj ();
168 if (stubobj == 0)
170 return false;
173 // STRING, a type ID hint
174 if ((strm << stubobj->type_id.in ()) == 0)
176 return false;
179 const TAO_MProfile& mprofile = stubobj->base_profiles ();
181 CORBA::ULong const profile_count = mprofile.profile_count ();
183 if ((strm << profile_count) == 0)
185 return false;
188 // @@ The MProfile should be locked during this iteration, is there
189 // anyway to achieve that?
190 for (CORBA::ULong i = 0; i < profile_count; ++i)
192 const TAO_Profile *p = mprofile.get_profile (i);
194 if (p->encode (strm) == 0)
196 return false;
200 return strm.good_bit ();
203 else
205 discriminator = false;
207 if (strm << ACE_OutputCDR::from_boolean (discriminator))
209 CORBA::ULong value_tag = TAO_OBV_GIOP_Flags::Value_tag_base
210 | TAO_OBV_GIOP_Flags::Type_info_single;
212 if ((strm.write_ulong (value_tag)) == 0)
214 return false;
217 if ((strm << abs->_tao_obv_repository_id ()) == 0)
219 return false;
222 return abs->_tao_marshal_v (strm);
226 return false;
229 CORBA::Boolean
230 operator>> (TAO_InputCDR &strm, CORBA::AbstractBase_ptr &abs)
232 abs = nullptr;
233 CORBA::Boolean discriminator = false;
234 ACE_InputCDR::to_boolean tb (discriminator);
235 TAO_ORB_Core *orb_core = nullptr;
237 if (strm >> tb)
239 if (!discriminator)
241 CORBA::ULong value_tag;
243 if (!strm.read_ulong (value_tag))
245 return false;
248 if (TAO_OBV_GIOP_Flags::is_null_ref (value_tag))
250 // Ok, null reference unmarshaled.
251 return true;
254 if (!TAO_OBV_GIOP_Flags::is_value_tag (value_tag))
256 TAOLIB_DEBUG ((LM_DEBUG,
257 ACE_TEXT ("operator>> CORBA::AbstractBase ")
258 ACE_TEXT ("not value_tag\n")));
259 return false;
262 CORBA::String_var repo_id_stream;
264 // It would be more efficient not to copy the string)
265 if (strm.read_string (repo_id_stream.inout ()) == 0)
267 return false;
270 orb_core = strm.orb_core ();
272 if (orb_core == 0)
274 orb_core = TAO_ORB_Core_instance ();
276 if (TAO_debug_level > 0)
278 TAOLIB_DEBUG ((LM_WARNING,
279 "TAO (%P|%t) WARNING: extracting "
280 "valuetype using default ORB_Core\n"));
284 CORBA::ValueFactory_var factory =
285 orb_core->orb ()->lookup_value_factory (repo_id_stream.in ());
287 // We should throw an exception, if there were an appropriate one.
288 if (factory.in() == 0)
290 TAOLIB_ERROR ((LM_ERROR,
291 ACE_TEXT ("(%N:%l): The following unknown type was received: `%s'."),
292 repo_id_stream.in ()));
293 return false;
296 abs = factory->create_for_unmarshal_abstract ();
298 return (abs == 0 ? false : abs->_tao_unmarshal_v (strm));
300 else
302 CORBA::Object_var generic_objref;
304 if (strm >> generic_objref.inout ())
306 if (CORBA::is_nil (generic_objref.in ()))
308 return true;
311 TAO_Stub *concrete_stubobj = generic_objref->_stubobj ();
313 bool const collocated =
314 !CORBA::is_nil (concrete_stubobj->servant_orb_var ().in ())
315 && concrete_stubobj->optimize_collocation_objects ()
316 && generic_objref->_is_collocated ();
318 ACE_NEW_RETURN (abs,
319 CORBA::AbstractBase (
320 concrete_stubobj,
321 collocated,
322 generic_objref->_servant ()),
323 false);
324 return true;
329 return false;
332 #if defined (GEN_OSTREAM_OPS)
334 std::ostream&
335 operator<< (std::ostream &strm,
336 CORBA::AbstractBase_ptr _tao_objref)
338 return CORBA::AbstractBase::_tao_stream (strm, _tao_objref);
341 #endif /* GEN_OSTREAM_OPS */
343 CORBA::Boolean
344 CORBA::AbstractBase::_tao_marshal_v (TAO_OutputCDR &) const
346 return false;
349 CORBA::Boolean
350 CORBA::AbstractBase::_tao_unmarshal_v (TAO_InputCDR &)
352 return false;
355 CORBA::Boolean
356 CORBA::AbstractBase::_tao_match_formal_type (ptrdiff_t ) const
358 return false;
361 #if defined (GEN_OSTREAM_OPS)
363 std::ostream &
364 CORBA::AbstractBase::_tao_stream (std::ostream &strm,
365 const CORBA::AbstractBase_ptr _tao_objref)
367 return _tao_objref->_tao_stream_v (strm);
370 std::ostream &
371 CORBA::AbstractBase::_tao_stream_v (std::ostream &strm) const
373 return strm << "\"IDL:omg.org/CORBA/AbstractBase:1.0\"";
376 #endif /* GEN_OSTREAM_OPS */
378 CORBA::ValueBase *
379 CORBA::AbstractBase::_tao_to_value ()
381 return nullptr;
384 CORBA::Object_ptr
385 CORBA::AbstractBase::create_object (TAO_Stub *stub)
387 if (stub)
389 stub->_incr_refcnt ();
391 TAO_Stub_Auto_Ptr safe_stub (stub);
393 CORBA::Object_ptr obj =
394 stub->orb_core ()->create_object (stub);
396 safe_stub.release ();
398 return obj;
401 return CORBA::Object::_nil ();
404 // ================== Typecode initializations ==================
406 namespace TAO
408 namespace TypeCode
410 char const tc_object_id[] = "IDL:omg.org/CORBA/AbstractBase:1.0";
411 char const tc_object_name[] = "AbstractBase";
412 Objref<char const *,
413 TAO::Null_RefCount_Policy>
414 tc_AbstractBase (CORBA::tk_abstract_interface,
415 tc_object_id,
416 tc_object_name);
420 namespace CORBA
422 TypeCode_ptr const _tc_AbstractBase = &TAO::TypeCode::tc_AbstractBase;
425 TAO_END_VERSIONED_NAMESPACE_DECL