summaryrefslogtreecommitdiff
path: root/TAO/tao/Valuetype/AbstractBase.cpp
blob: a28c7ae4ef73f0cfc063791f4e11d5f542e34e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// "$Id$"

#include "tao/Valuetype/AbstractBase.h"
#include "tao/Valuetype/ValueBase.h"
#include "tao/Valuetype/ValueFactory.h"
#include "tao/Stub.h"
#include "tao/ORB_Core.h"
#include "tao/Profile.h"
#include "tao/debug.h"
#include "tao/CDR.h"

#if !defined (__ACE_INLINE__)
# include "tao/Valuetype/AbstractBase.inl"
#endif /* ! __ACE_INLINE__ */

ACE_RCSID (Valuetype,
           AbstractBase,
           "$Id$")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

// ************************************************************
// These are in CORBA namespace

void
CORBA::release (CORBA::AbstractBase_ptr obj)
{
  if (obj)
    {
      obj->_remove_ref ();
    }
}

CORBA::Boolean
CORBA::is_nil (CORBA::AbstractBase_ptr obj)
{
  return (obj == 0);
}

// ************************************************************

CORBA::AbstractBase::AbstractBase (void)
  : is_objref_ (0)
  , concrete_stubobj_ (0)
  , is_collocated_ (0)
  , servant_ (0)
  , is_local_ (0)
  , equivalent_obj_ (0)
{
}

CORBA::AbstractBase::AbstractBase (const CORBA::AbstractBase &rhs)
  : is_objref_ (rhs.is_objref_)
  , concrete_stubobj_ (rhs.concrete_stubobj_)
  , is_collocated_ (rhs.is_collocated_)
  , servant_ (rhs.servant_)
  , is_local_ (rhs.is_local_)
  , equivalent_obj_ (0)
{
  if (rhs.concrete_stubobj_ != 0)
    {
      rhs.concrete_stubobj_->_incr_refcnt ();
    }

  if (!CORBA::is_nil (rhs.equivalent_obj_))
    {
      this->equivalent_obj_ =
        CORBA::Object::_duplicate (rhs.equivalent_obj_);
    }
}

CORBA::AbstractBase::AbstractBase (TAO_Stub * protocol_proxy,
                                   CORBA::Boolean collocated,
                                   TAO_Abstract_ServantBase * servant)
  : is_objref_ (1)
  , concrete_stubobj_ (protocol_proxy)
  , is_collocated_ (collocated)
  , servant_ (servant)
  , is_local_ (protocol_proxy == 0 ? 1 : 0)
  , equivalent_obj_ (0)
{
  if (this->concrete_stubobj_ != 0)
    {
      TAO_Stub *stub = this->concrete_stubobj_;

      stub->_incr_refcnt ();

      this->equivalent_obj_ =
        stub->orb_core ()->create_object (stub);
    }

}

CORBA::AbstractBase::~AbstractBase (void)
{
  if (this->concrete_stubobj_ != 0)
    {
      this->concrete_stubobj_->_decr_refcnt ();
    }
}

CORBA::AbstractBase_ptr
CORBA::AbstractBase::_duplicate (CORBA::AbstractBase_ptr obj)
{
  if (obj)
    {
      obj->_add_ref ();
    }

  if (!CORBA::is_nil (obj->equivalent_obj_.in ()))
    {
      obj->equivalent_obj_->_add_ref ();
    }

  return obj;
}

// These are non-pure virtual no-ops so we can instantiate the
// class in the CDR extraction operator. The actual management
// of the refcount will always be done in the derived class.
void
CORBA::AbstractBase::_add_ref (void)
{
}

void
CORBA::AbstractBase::_remove_ref (void)
{
}

void
CORBA::AbstractBase::_tao_any_destructor (void *x)
{
  CORBA::AbstractBase_ptr tmp = static_cast<CORBA::AbstractBase_ptr> (x);
  ::CORBA::release (tmp);
}

CORBA::Object_ptr
CORBA::AbstractBase::_to_object (void)
{
  if (!CORBA::is_nil (this->equivalent_obj_.in ()))
    return CORBA::Object::_duplicate (this->equivalent_obj_.in ());

  if (this->concrete_stubobj_ == 0)
    {
      return CORBA::Object::_nil ();
    }

  TAO_ORB_Core *orb_core = this->concrete_stubobj_->orb_core ();
  this->concrete_stubobj_->_incr_refcnt ();

  return orb_core->create_object (this->concrete_stubobj_);
}

CORBA::ValueBase *
CORBA::AbstractBase::_to_value (void)
{
  if (this->is_objref_)
    {
      return 0;
    }

  CORBA::ValueBase *retval = this->_tao_to_value ();

  if (retval != 0)
    {
      retval->_add_ref ();
    }

  return retval;
}

CORBA::Boolean
CORBA::AbstractBase::_is_a (const char *type_id
                            ACE_ENV_ARG_DECL_NOT_USED)
{
  return ! ACE_OS::strcmp (type_id,
                           "IDL:omg.org/CORBA/AbstractBase:1.0");
}

const char *
CORBA::AbstractBase::_interface_repository_id (void) const
{
  return "IDL:omg.org/CORBA/AbstractBase:1.0";
}

const char *
CORBA::AbstractBase::_tao_obv_repository_id (void) const
{
  return "IDL:omg.org/CORBA/AbstractBase:1.0";
}

CORBA::Boolean
operator<< (TAO_OutputCDR &strm, const CORBA::AbstractBase_ptr abs)
{
  CORBA::Boolean discriminator = true;

  // We marshal a null abstract interface ptr as a discriminator
  // plus null object reference (see CORBA::Object::marshal()
  // and operator << for CORBA::Object).
  if (CORBA::is_nil (abs))
    {
      // Marshal discriminator, then empty type hint.
      strm << ACE_OutputCDR::from_boolean (discriminator);
      strm.write_ulong (1);
      strm.write_char ('\0');
      strm.write_ulong (0);
      return (CORBA::Boolean) strm.good_bit ();
    }

  if (abs->_is_objref ())
    {
      if (strm << ACE_OutputCDR::from_boolean (discriminator))
        {
          TAO_Stub *stubobj = abs->_stubobj ();

          if (stubobj == 0)
            {
              return false;
            }

          // STRING, a type ID hint
          if ((strm << stubobj->type_id.in ()) == 0)
            {
              return false;
            }

          const TAO_MProfile& mprofile = stubobj->base_profiles ();

          CORBA::ULong const profile_count = mprofile.profile_count ();

          if ((strm << profile_count) == 0)
            {
              return false;
            }

          // @@ The MProfile should be locked during this iteration, is there
          // anyway to achieve that?
          for (CORBA::ULong i = 0; i < profile_count; ++i)
            {
              const TAO_Profile *p = mprofile.get_profile (i);

              if (p->encode (strm) == 0)
                {
                  return false;
                }
            }

          return (CORBA::Boolean) strm.good_bit ();
        }
    }
  else
    {
      discriminator = false;

      if (strm << ACE_OutputCDR::from_boolean (discriminator))
        {
          CORBA::Boolean retval = true;

          CORBA::ULong value_tag = TAO_OBV_GIOP_Flags::Value_tag_base
                                   | TAO_OBV_GIOP_Flags::Type_info_single;

          retval = strm.write_ulong (value_tag);

          if (retval == 0)
            {
              return retval;
            }

          retval = strm << abs->_tao_obv_repository_id ();

          if (retval == 0)
            {
              return retval;
            }

          return abs->_tao_marshal_v (strm);
        }
    }

  return false;
}

CORBA::Boolean
operator>> (TAO_InputCDR &strm, CORBA::AbstractBase_ptr &abs)
{
  abs = 0;
  CORBA::Boolean discriminator = 0;
  ACE_InputCDR::to_boolean tb (discriminator);
  TAO_ORB_Core *orb_core = 0;

  if (strm >> tb)
    {
      if (discriminator == 0)
        {
          CORBA::ULong value_tag;

          if (!strm.read_ulong (value_tag))
            {
              return false;
            }

          if (TAO_OBV_GIOP_Flags::is_null_ref (value_tag))
            {
              // Ok, null reference unmarshaled.
              return true;
            }

          if (!TAO_OBV_GIOP_Flags::is_value_tag (value_tag))
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("operator>> CORBA::AbstractBase ")
                          ACE_TEXT ("not value_tag\n")));
              return false;
            }

          CORBA::String_var repo_id_stream;

          // It would be more efficient not to copy the string)
          if (strm.read_string (repo_id_stream.inout ()) == 0)
            {
              return false;
            }

          orb_core = strm.orb_core ();

          if (orb_core == 0)
            {
              orb_core = TAO_ORB_Core_instance ();

              if (TAO_debug_level > 0)
                {
                  ACE_DEBUG ((LM_WARNING,
                              "TAO (%P|%t) WARNING: extracting "
                              "valuetype using default ORB_Core\n"));
                }
            }

          CORBA::ValueFactory_var factory =
            orb_core->orb ()->lookup_value_factory (repo_id_stream.in ());

          // We should throw an exception, if there were an appropriate one.
          if (factory.in() == 0)
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%N:%l): The following unknown type was received: `%s'."),
                          repo_id_stream.in ()));
              return false;
            }

          abs = factory->create_for_unmarshal_abstract ();

          return abs->_tao_unmarshal_v (strm);
        }
      else
        {
          CORBA::Object_var generic_objref;

          if (strm >> generic_objref.inout ())
            {
              TAO_Stub *concrete_stubobj = generic_objref->_stubobj ();

              CORBA::Boolean const stores_orb =
                ! CORBA::is_nil (concrete_stubobj->servant_orb_var ().in ());

              if (stores_orb)
                {
                  orb_core =
                    concrete_stubobj->servant_orb_var ()->orb_core ();
                }

              CORBA::Boolean const collocated =
                orb_core != 0
                && orb_core->optimize_collocation_objects ()
                && generic_objref->_is_collocated ();

              ACE_NEW_RETURN (abs,
                              CORBA::AbstractBase (
                                concrete_stubobj,
                                collocated,
                                generic_objref->_servant ()),
                              false);
              return true;
            }
        }
    }

  return false;
}

CORBA::Boolean
CORBA::AbstractBase::_tao_marshal_v (TAO_OutputCDR &) const
{
  return false;
}

CORBA::Boolean
CORBA::AbstractBase::_tao_unmarshal_v (TAO_InputCDR &)
{
  return false;
}

CORBA::ValueBase *
CORBA::AbstractBase::_tao_to_value (void)
{
  return 0;
}

CORBA::Object_ptr
CORBA::AbstractBase::equivalent_objref (void)
{
  if (CORBA::is_nil (this->equivalent_obj_.in ()))
    {
      if (this->concrete_stubobj_ != 0)
        {
          TAO_ORB_Core *orb_core =
            this->concrete_stubobj_->orb_core ();

          this->concrete_stubobj_->_incr_refcnt ();

          this->equivalent_obj_ =
            orb_core->create_object (this->concrete_stubobj_);
        }
    }

  return this->equivalent_obj_.in ();
}


TAO_END_VERSIONED_NAMESPACE_DECL