summaryrefslogtreecommitdiff
path: root/TAO/tao/Object.cpp
blob: f1be9002e7dce56c68ebcbdff810925ee62f38be (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
// @(#)
//
// Copyright 1994-1995 by Sun Microsystems Inc.
// Copyright 1997-2002 by Washington University
// All Rights Reserved
//
// ORB:         CORBA::Object operations

#include "tao/Object.h"
#include "tao/Stub.h"
#include "tao/Profile.h"
#include "tao/ORB_Core.h"
#include "tao/Connector_Registry.h"
#include "tao/LocateRequest_Invocation_Adapter.h"
#include "tao/debug.h"
#include "tao/Dynamic_Adapter.h"
#include "tao/IFR_Client_Adapter.h"
#include "tao/Remote_Object_Proxy_Broker.h"
#include "tao/CDR.h"
#include "tao/SystemException.h"
#include "tao/PolicyC.h"

#include "ace/Dynamic_Service.h"
#include "ace/OS_NS_string.h"
#include "ace/CORBA_macros.h"

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

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

CORBA::Object::~Object ()
{
  if (this->protocol_proxy_)
    (void) this->protocol_proxy_->_decr_refcnt ();
}

CORBA::Object::Object (TAO_Stub * protocol_proxy,
                       CORBA::Boolean collocated,
                       TAO_Abstract_ServantBase * servant,
                       TAO_ORB_Core *orb_core)
  : refcount_ (1)
    , is_local_ (false)
    , is_evaluated_ (true)
    , ior_ (nullptr)
    , orb_core_ (orb_core)
    , protocol_proxy_ (protocol_proxy)
{
  /// This constructor should not be called when the protocol proxy is
  /// null ie. when the object is a LocalObject. Assert that
  /// requirement.
  ACE_ASSERT (this->protocol_proxy_ != nullptr);

  if (this->orb_core_ == nullptr)
    this->orb_core_ = this->protocol_proxy_->orb_core ();

  // Set the collocation marker on the stub. This may not be news to it.
  // This may also change the stub's object proxy broker.
  this->protocol_proxy_->is_collocated (collocated);

  // Set the collocated servant pointer (null if not collocated) on the stub.
  this->protocol_proxy_->collocated_servant (servant);
}

CORBA::Object::Object (IOP::IOR *ior,
                       TAO_ORB_Core *orb_core)
  : refcount_ (1)
    , is_local_ (false)
    , is_evaluated_ (false)
    , ior_ (ior)
    , orb_core_ (orb_core)
    , protocol_proxy_ (nullptr)
{
}

// Too lazy to do this check in every method properly! This is useful
// only  for lazily evaluated IOR's
#define TAO_OBJECT_IOR_EVALUATE \
if (!this->is_evaluated_) \
  { \
    ACE_GUARD (TAO_SYNCH_MUTEX , mon, this->object_init_lock_); \
      if (!this->is_evaluated_) \
        CORBA::Object::tao_object_initialize (this); \
  }

#define TAO_OBJECT_IOR_EVALUATE_RETURN \
if (!this->is_evaluated_) \
  { \
    ACE_GUARD_RETURN (TAO_SYNCH_MUTEX , mon, this->object_init_lock_, 0); \
    if (!this->is_evaluated_) \
      CORBA::Object::tao_object_initialize (this); \
  }

void
CORBA::Object::_add_ref ()
{
  ++this->refcount_;
}

void
CORBA::Object::_remove_ref ()
{
  if (--this->refcount_ == 0)
    {
      delete this;
    }
}

CORBA::ULong
CORBA::Object::_refcount_value() const
{
  return this->refcount_;
}

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

// virtual -- do not inline
CORBA::Boolean
CORBA::Object::marshal (TAO_OutputCDR &cdr)
{
  return (cdr << this);
}

/*static*/ CORBA::Boolean
CORBA::Object::marshal (const CORBA::Object_ptr x, TAO_OutputCDR &cdr)
{
  if (x == nullptr)
    {
      // NIL objrefs ... marshal as empty type hint, no elements.
      cdr.write_ulong (1);
      cdr.write_char ('\0');
      cdr.write_ulong (0);
      return (CORBA::Boolean) cdr.good_bit ();
    }

  return x->marshal (cdr);
}

#if defined (GEN_OSTREAM_OPS)

/*static*/ std::ostream &
CORBA::Object::_tao_stream (std::ostream &strm,
                            const CORBA::Object_ptr _tao_objref)
{
  return _tao_objref->_tao_stream_v (strm);
}

std::ostream &
CORBA::Object::_tao_stream_v (std::ostream &strm) const
{
  return strm << "\"IDL:omg.org/CORBA/Object:1.0\"";
}

#endif /* GEN_OSTREAM_OPS */

bool
CORBA::Object::can_convert_to_ior () const
{
  // By default, objects can not be stringified if they are local
  return !this->_is_local ();
}

char*
CORBA::Object::convert_to_ior (bool, const char*) const
{
  return nullptr;
}

TAO_Abstract_ServantBase*
CORBA::Object::_servant () const
{
  if (this->protocol_proxy_ == nullptr)
    {
      // No stub set. Should not happen.
      return nullptr;
    }

  return this->protocol_proxy_->collocated_servant ();
}

// IS_A ... ask the object if it's an instance of the type whose
// logical type ID is passed as a parameter.

CORBA::Boolean
CORBA::Object::_is_a (const char *type_id)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  // NOTE: if _stub->type_id is nonzero and we have local knowledge of
  // it, we can answer this question without a costly remote call.
  //
  // That "local knowledge" could come from stubs or skeletons linked
  // into this process in the best case, or a "near" repository in a
  // slightly worse case.  Or in a trivial case, if the ID being asked
  // about is the ID we have recorded, we don't need to ask about the
  // inheritance relationships at all!
  //
  // In real systems having local knowledge will be common, though as
  // the systems built atop ORBs become richer it'll also become
  // common to have the "real type ID" not be directly understood
  // because it's more deeply derived than any locally known types.
  //
  // XXX if type_id is that of CORBA::Object, "yes, we comply" :-)

  if (this->protocol_proxy_ == nullptr)
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy for %C\n"),
                    type_id));

      throw ::CORBA::NO_IMPLEMENT ();
    }

  if (this->_stubobj ()->type_id.in () != nullptr
      && ACE_OS::strcmp (type_id,
                         this->_stubobj ()->type_id.in ()) == 0)
    return true;

  return this->proxy_broker ()->_is_a (this, type_id);
}

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

CORBA::Boolean
CORBA::Object::_is_collocated () const
{
  if (this->protocol_proxy_)
    {
      return this->protocol_proxy_->is_collocated ();
    }

  return false;
}

CORBA::Boolean
CORBA::Object::_is_local () const
{
  return this->is_local_;
}

TAO_Stub *
CORBA::Object::_stubobj () const
{
  return this->protocol_proxy_;
}

TAO_Stub *
CORBA::Object::_stubobj ()
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;
  return this->protocol_proxy_;
}

CORBA::ULong
CORBA::Object::_hash (CORBA::ULong maximum)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  if (this->protocol_proxy_ != nullptr)
    return this->protocol_proxy_->hash (maximum);
  else
    {
      // Locality-constrained object.

      // Note that we reinterpret_cast to an "unsigned long" instead
      // of CORBA::ULong since we need to first cast to an integer
      // large enough to hold an address to avoid compile-time
      // warnings on some 64-bit platforms.
      const CORBA::ULong hash =
        static_cast<CORBA::ULong> (reinterpret_cast<ptrdiff_t> (this));

      return hash % maximum;
    }
}

CORBA::Boolean
CORBA::Object::_is_equivalent (CORBA::Object_ptr other_obj)
{
  if (other_obj == nullptr)
    {
      return false;
    }

  if (other_obj == this)
    {
      return true;
    }

  TAO_OBJECT_IOR_EVALUATE_RETURN;

  if (this->protocol_proxy_ != nullptr)
    return this->protocol_proxy_->is_equivalent (other_obj);

  return false;
}

// TAO's extensions

TAO::ObjectKey *
CORBA::Object::_key ()
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  if (this->_stubobj () && this->_stubobj ()->profile_in_use ())
    return this->_stubobj ()->profile_in_use ()->_key ();

  if (TAO_debug_level > 2)
    {
      TAOLIB_ERROR ((LM_ERROR,
                  ACE_TEXT ("TAO (%P|%t) Null object key return from ")
                  ACE_TEXT ("profile in use\n")));
    }

  throw ::CORBA::INTERNAL (
    CORBA::SystemException::_tao_minor_code (
      0,
      EINVAL),
    CORBA::COMPLETED_NO);
}

void
CORBA::Object::_proxy_broker (TAO::Object_Proxy_Broker *proxy_broker)
{
  this->protocol_proxy_->object_proxy_broker (proxy_broker);
}

CORBA::Boolean
CORBA::Object::is_nil_i (CORBA::Object_ptr obj)
{
  // If the profile length is zero for a non-evaluated IOR it is a
  // null-object.
  if ((!obj->is_evaluated ()) && obj->ior ().profiles.length () == 0)
    return true;

  // To accommodate new definitions.
  if (obj->orb_core_)
    {
      return obj->orb_core_->object_is_nil (obj);
    }

  return false;
}

#if (TAO_HAS_MINIMUM_CORBA == 0)

#if !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
void
CORBA::Object::_create_request (CORBA::Context_ptr ctx,
                                const char *operation,
                                CORBA::NVList_ptr arg_list,
                                CORBA::NamedValue_ptr result,
                                CORBA::Request_ptr &request,
                                CORBA::Flags req_flags)
{
  TAO_OBJECT_IOR_EVALUATE;

  // Since we don't really support Context, anything but a null pointer
  // is a no-no.
  // Neither can we create a request object from locality constrained
  // object references.
  if (ctx != nullptr || this->protocol_proxy_ == nullptr)
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy for %C\n"),
                    operation));

      throw ::CORBA::NO_IMPLEMENT ();
    }

  TAO_Dynamic_Adapter *dynamic_adapter =
    ACE_Dynamic_Service<TAO_Dynamic_Adapter>::instance (
        TAO_ORB_Core::dynamic_adapter_name ()
      );

  dynamic_adapter->create_request (
                       this,
                       this->protocol_proxy_->orb_core ()-> orb (),
                       operation,
                       arg_list,
                       result,
                       nullptr,
                       request,
                       req_flags);
}
#endif

#if !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
void
CORBA::Object::_create_request (CORBA::Context_ptr ctx,
                                const char *operation,
                                CORBA::NVList_ptr arg_list,
                                CORBA::NamedValue_ptr result,
                                CORBA::ExceptionList_ptr exceptions,
                                CORBA::ContextList_ptr,
                                CORBA::Request_ptr &request,
                                CORBA::Flags req_flags)
{
  TAO_OBJECT_IOR_EVALUATE;

  // Since we don't really support Context, anything but a null pointer
  // is a no-no.
  // Neither can we create a request object from locality constrained
  // object references.
  if (ctx != nullptr || this->protocol_proxy_ == nullptr)
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy for %C\n"),
                    operation));

      throw ::CORBA::NO_IMPLEMENT ();
    }

  TAO_Dynamic_Adapter *dynamic_adapter =
    ACE_Dynamic_Service<TAO_Dynamic_Adapter>::instance (
        TAO_ORB_Core::dynamic_adapter_name ()
      );

  dynamic_adapter->create_request (
                       this,
                       this->protocol_proxy_->orb_core ()-> orb (),
                       operation,
                       arg_list,
                       result,
                       exceptions,
                       request,
                       req_flags);
}
#endif

#if !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO)
CORBA::Request_ptr
CORBA::Object::_request (const char *operation)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;
  if (this->protocol_proxy_)
    {
      TAO_Dynamic_Adapter *dynamic_adapter =
        ACE_Dynamic_Service<TAO_Dynamic_Adapter>::instance (
            TAO_ORB_Core::dynamic_adapter_name ());

      return dynamic_adapter->request (
                                  this,
                                  this->protocol_proxy_->orb_core ()->orb (),
                                  operation);
    }
  else
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy for %C\n"),
                    operation));

      throw ::CORBA::NO_IMPLEMENT ();
    }
}
#endif

// NON_EXISTENT ... send a simple call to the object, which will
// either elicit a false response or a OBJECT_NOT_EXIST exception.  In
// the latter case, return true.

CORBA::Boolean
CORBA::Object::_non_existent ()
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  CORBA::Boolean retval = false;

  try
    {
      retval = this->proxy_broker ()->_non_existent (this);
    }
  catch (const ::CORBA::OBJECT_NOT_EXIST&)
    {
      retval = true;
    }

  return retval;
}


#if ! defined (CORBA_E_COMPACT) && ! defined (CORBA_E_MICRO)
CORBA::InterfaceDef_ptr
CORBA::Object::_get_interface ()
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;
  return this->proxy_broker ()->_get_interface (this);
}

CORBA::Object_ptr
CORBA::Object::_get_component ()
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;
  return this->proxy_broker ()->_get_component (this);
}
#endif

char*
CORBA::Object::_repository_id ()
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;
  return this->proxy_broker ()->_repository_id (this);
}

#endif /* TAO_HAS_MINIMUM_CORBA */

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

// @@ Does it make sense to support policy stuff for locality constrained
//    objects?  Also, does it make sense to bind policies with stub object?
//    - nw.

#if (TAO_HAS_CORBA_MESSAGING == 1)

CORBA::Policy_ptr
CORBA::Object::_get_policy (CORBA::PolicyType type)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  if (this->protocol_proxy_)
    return this->protocol_proxy_->get_policy (type);
  else
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy in _get_policy\n")));

      throw ::CORBA::NO_IMPLEMENT ();
    }
}

CORBA::Policy_ptr
CORBA::Object::_get_cached_policy (TAO_Cached_Policy_Type type)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  if (this->protocol_proxy_)
    return this->protocol_proxy_->get_cached_policy (type);
  else
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy in _get_policy\n")));

      throw ::CORBA::NO_IMPLEMENT ();
    }
}

CORBA::Object_ptr
CORBA::Object::_set_policy_overrides (
  const CORBA::PolicyList & policies,
  CORBA::SetOverrideType set_add)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  if (!this->protocol_proxy_)
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy in _get_policy\n")));

      throw ::CORBA::NO_IMPLEMENT ();
    }

  TAO_Stub* stub =
    this->protocol_proxy_->set_policy_overrides (policies, set_add);

  TAO_Stub_Auto_Ptr safe_stub (stub);

  CORBA::Object_ptr obj = CORBA::Object::_nil ();

  ACE_NEW_THROW_EX (obj,
                    CORBA::Object (stub,
                                  this->_is_collocated ()),
                    CORBA::NO_MEMORY (
                      CORBA::SystemException::_tao_minor_code (
                        0,
                        ENOMEM),
                      CORBA::COMPLETED_MAYBE));

  // If the stub is collocated and we don't have a collocated server we need
  // to reinitialize it to get it.
  if (stub->is_collocated () && stub->collocated_servant () == nullptr)
    {
      obj->orb_core ()->reinitialize_object (stub);
    }

  (void) safe_stub.release ();

  return obj;
}

CORBA::PolicyList *
CORBA::Object::_get_policy_overrides (const CORBA::PolicyTypeSeq & types)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;
  if (this->protocol_proxy_)
    return this->protocol_proxy_->get_policy_overrides (types);
  else
    {
      if (TAO_debug_level > 0)
        TAOLIB_ERROR ((LM_ERROR,
                    ACE_TEXT ("TAO (%P|%t) - No protocol proxy in _get_policy\n")));

      throw ::CORBA::NO_IMPLEMENT ();
    }
}

CORBA::Boolean
CORBA::Object::_validate_connection (
  CORBA::PolicyList_out inconsistent_policies)
{
  TAO_OBJECT_IOR_EVALUATE_RETURN;

  inconsistent_policies = nullptr;
  CORBA::Boolean retval = true;

#if (TAO_HAS_MINIMUM_CORBA == 0)
  // Note that the OBJECT_NOT_EXIST exception should be propagated to
  // the caller rather than return false, which is why we do not use
  // CORBA::Object::_non_existent().  This behavior is consistent
  // with the non-collocated case.
  if (this->_is_collocated ())
      return !(this->proxy_broker ()->_non_existent (this));

  TAO::LocateRequest_Invocation_Adapter tao_call (this);
  try
    {
      tao_call.invoke ();
    }
  catch (const ::CORBA::INV_POLICY&)
    {
      inconsistent_policies = tao_call.get_inconsistent_policies ();
      retval = false;
    }
#else
  retval = false;
#endif /* TAO_HAS_MINIMUM_CORBA */

  return retval;
}

#endif /* TAO_HAS_CORBA_MESSAGING == 1 */


CORBA::ORB_ptr
CORBA::Object::_get_orb ()
{
  if (this->orb_core_ != nullptr)
    {
      return CORBA::ORB::_duplicate (this->orb_core_->orb ());
    }
  else
    {
      TAO_OBJECT_IOR_EVALUATE_RETURN;
      if (this->protocol_proxy_)
        return CORBA::ORB::_duplicate (this->protocol_proxy_->orb_core ()->orb ());
      else
        {
          if (TAO_debug_level > 0)
            TAOLIB_ERROR ((LM_ERROR,
                        ACE_TEXT ("TAO (%P|%t) - No protocol proxy in _get_policy\n")));

          throw ::CORBA::NO_IMPLEMENT ();
        }
    }
}

TAO::Object_Proxy_Broker *
CORBA::Object::proxy_broker () const
{
  // Paranoid check. We *should* never access the proxy_broker
  // when the object has not been initialised so there *should*
  // always be a stub, but just in case...
  if (this->protocol_proxy_)
    {
      return this->protocol_proxy_->object_proxy_broker ();
    }

  // We have no stub. We cannot be collocated.
  return the_tao_remote_object_proxy_broker ();
}

/*****************************************************************
 * Global Functions
 ****************************************************************/

CORBA::Boolean
operator<< (TAO_OutputCDR& cdr, const CORBA::Object* x)
{
  if (x == nullptr)
    {
      // NIL objrefs ... marshal as empty type hint, no elements.
      cdr.write_ulong (1);
      cdr.write_char ('\0');
      cdr.write_ulong (0);
      return (CORBA::Boolean) cdr.good_bit ();
    }

  if (!x->is_evaluated ())
    {
      // @@ This is too inefficient. Need to speed this up if this is
      // a bottle neck.
      cdr << const_cast<IOP::IOR &> (x->ior ());
      return cdr.good_bit ();
    }

   TAO_Stub *stubobj = x->_stubobj ();

   if (stubobj == nullptr)
     return false;

  return (stubobj->marshal (cdr));
}

/*static*/ void
CORBA::Object::tao_object_initialize (CORBA::Object *obj)
{
  CORBA::ULong const profile_count =
    obj->ior_->profiles.length ();

  // Assumption is that after calling this method, folks should test
  // for protocol_proxy_ or whatever to make sure that things have
  // been initialized!
  if (profile_count == 0)
    return;

  // get a profile container to store all profiles in the IOR.
  TAO_MProfile mp (profile_count);

  TAO_ORB_Core *&orb_core = obj->orb_core_;
  if (orb_core == nullptr)
    {
      orb_core = TAO_ORB_Core_instance ();
      if (TAO_debug_level > 0)
        {
          TAOLIB_DEBUG ((LM_WARNING,
                      ACE_TEXT ("TAO (%P|%t) - Object::tao_object_initialize ")
                      ACE_TEXT ("WARNING: extracting object from ")
                      ACE_TEXT ("default ORB_Core\n")));
        }
    }

  TAO_Stub *objdata = nullptr;

  try
    {
      TAO_Connector_Registry *connector_registry =
        orb_core->connector_registry ();

      for (CORBA::ULong i = 0; i != profile_count; ++i)
        {
          IOP::TaggedProfile &tpfile = obj->ior_->profiles[i];

          // NOTE: This is a place for optimizations. Here we have an
          // 2 allocations and 2 copies. Future optimizations should
          // target this place.
          TAO_OutputCDR o_cdr;

          o_cdr << tpfile;

          TAO_InputCDR cdr (o_cdr,
                            orb_core->input_cdr_buffer_allocator (),
                            orb_core->input_cdr_dblock_allocator (),
                            orb_core->input_cdr_msgblock_allocator (),
                            orb_core);

          TAO_Profile *pfile = connector_registry->create_profile (cdr);

          if (pfile != nullptr)
            {
              if (mp.give_profile (pfile) == -1)
              {
                TAOLIB_ERROR ((LM_ERROR,
                            ACE_TEXT ("TAO (%P|%t) ERROR: give_profile\n")
                            ACE_TEXT (" returned -1\n")));
              }
            }
        }

      // Make sure we got some profiles!
      if (mp.profile_count () != profile_count)
        {
          // @@ This occurs when profile creation fails when decoding the
          //    profile from the IOR.
          TAOLIB_ERROR ((LM_ERROR,
                      ACE_TEXT ("TAO (%P|%t) ERROR: XXXXX Could not create all ")
                      ACE_TEXT ("profiles while extracting object\n")
                      ACE_TEXT ("TAO (%P|%t) ERROR: reference from the ")
                      ACE_TEXT ("CDR stream.\n")));
        }


      objdata = orb_core->create_stub (obj->ior_->type_id.in (), mp);
    }
  catch (const ::CORBA::Exception& ex)
    {
      if (TAO_debug_level > 0)
        ex._tao_print_exception (
          ACE_TEXT ("TAO - ERROR creating stub ")
          ACE_TEXT ("object when demarshaling object ")
          ACE_TEXT ("reference."));

      return;
    }

  TAO_Stub_Auto_Ptr safe_objdata (objdata);

  // This call will set the stub proxy broker if necessary
  if (orb_core->initialize_object (safe_objdata.get (), obj) == -1)
    return;

  obj->protocol_proxy_ = objdata;

  obj->is_evaluated_ = true;

  // Release the contents of the ior to keep memory consumption down.
  obj->ior_ = nullptr;

  // Transfer ownership to the CORBA::Object
  (void) safe_objdata.release ();
}

CORBA::Boolean
operator>> (TAO_InputCDR& cdr, CORBA::Object*& x)
{
  bool lazy_strategy = false;
  TAO_ORB_Core *orb_core = cdr.orb_core ();

  if (orb_core == nullptr)
    {
      orb_core = TAO_ORB_Core_instance ();
      if (TAO_debug_level > 0)
        {
          TAOLIB_DEBUG ((LM_WARNING,
                      ACE_TEXT ("TAO (%P|%t) WARNING: extracting object from ")
                      ACE_TEXT ("default ORB_Core\n")));
        }
    }
  else
    {
      if (orb_core->resource_factory ()->resource_usage_strategy () ==
          TAO_Resource_Factory::TAO_LAZY)
        {
          lazy_strategy = true;
        }
    }

  if (!lazy_strategy)
    {
      // If the user has set up a eager strategy..
      CORBA::String_var type_hint;
      if (!(cdr >> type_hint.inout ()))
        return false;

      CORBA::ULong profile_count;
      if (!(cdr >> profile_count))
        return false;

      if (profile_count == 0)
        {
          x = CORBA::Object::_nil ();
          return (CORBA::Boolean) cdr.good_bit ();
        }

      // get a profile container to store all profiles in the IOR.
      TAO_MProfile mp (profile_count);

      TAO_ORB_Core *orb_core = cdr.orb_core ();
      if (orb_core == nullptr)
        {
          orb_core = TAO_ORB_Core_instance ();
          if (TAO_debug_level > 0)
            {
              TAOLIB_DEBUG ((LM_WARNING,
                          ACE_TEXT ("TAO (%P|%t) - Object::tao_object_initialize ")
                          ACE_TEXT ("WARNING: extracting object from ")
                          ACE_TEXT ("default ORB_Core\n")));
            }
        }

      // Ownership of type_hint is given to TAO_Stub
      // TAO_Stub will make a copy of mp!
      TAO_Stub *objdata = nullptr;

      try
        {
          TAO_Connector_Registry *connector_registry =
            orb_core->connector_registry ();

          for (CORBA::ULong i = 0; i != profile_count && cdr.good_bit (); ++i)
            {
              TAO_Profile *pfile = connector_registry->create_profile (cdr);
              if (pfile != nullptr)
                {
                  if (mp.give_profile (pfile) == -1)
                    {
                      TAOLIB_ERROR ((LM_ERROR,
                                  ACE_TEXT ("TAO (%P|%t) ERROR: give_profile\n")
                                  ACE_TEXT (" returned -1\n")));
                    }
                }
            }

          // Make sure we got some profiles!
          if (mp.profile_count () != profile_count)
            {
              // @@ This occurs when profile creation fails when decoding the
              //    profile from the IOR.
              TAOLIB_ERROR_RETURN ((LM_ERROR,
                                 ACE_TEXT ("TAO (%P|%t) - ERROR: Could not create all ")
                                 ACE_TEXT ("profiles while extracting object [%d, %d] ")
                                 ACE_TEXT ("reference from the CDR stream.\n"),
                                 mp.profile_count (), profile_count),
                                false);
            }

          objdata = orb_core->create_stub (type_hint.in (), mp);
        }
      catch (const ::CORBA::Exception& ex)
        {
          if (TAO_debug_level > 0)
            ex._tao_print_exception (
              ACE_TEXT ("TAO (%P|%t) - ERROR creating stub ")
              ACE_TEXT ("object when demarshaling object ")
              ACE_TEXT ("reference.\n"));

          return false;
        }

      TAO_Stub_Auto_Ptr safe_objdata (objdata);

      x = orb_core->create_object (safe_objdata.get ());
      if (!x)
        {
          return false;
        }

      // Transfer ownership to the CORBA::Object
      (void) safe_objdata.release ();
    }
  else
    {
      // Lazy strategy!
      IOP::IOR *ior = nullptr;

      ACE_NEW_RETURN (ior,
                      IOP::IOR (),
                      false);

      if (!(cdr >> *ior))
        {
          // Can't extract the IOR, so delete the already allocated
          // ior to not have a memory leak
          delete ior;
          return false;
        }

      ACE_NEW_NORETURN (x,
                        CORBA::Object (ior, orb_core));

      if (x == nullptr)
        {
          // Can't allocate a CORBA Object so delete first the
          // memory we already allocated before we return
          delete ior;
          return false;
        }
    }

  return (CORBA::Boolean) cdr.good_bit ();
}

#if defined (GEN_OSTREAM_OPS)

std::ostream&
operator<< (std::ostream &strm, CORBA::Object_ptr _tao_objref)
{
  return CORBA::Object::_tao_stream (strm, _tao_objref);
}

#endif /* GEN_OSTREAM_OPS */

// =========================================================
// Traits specializations for CORBA::Object.
namespace TAO
{
  void In_Object_Argument_Cloner_T<CORBA::InterfaceDef_ptr>::duplicate
                                              (CORBA::InterfaceDef_ptr)
  {
  }

  void In_Object_Argument_Cloner_T<CORBA::InterfaceDef_ptr>::release
                                              (CORBA::InterfaceDef_ptr)
  {
  }

  CORBA::Object_ptr
  Objref_Traits<CORBA::Object>::duplicate (CORBA::Object_ptr p)
  {
    return CORBA::Object::_duplicate (p);
  }

  void
  Objref_Traits<CORBA::Object>::release (CORBA::Object_ptr p)
  {
    ::CORBA::release (p);
  }

  CORBA::Object_ptr
  Objref_Traits<CORBA::Object>::nil ()
  {
    return CORBA::Object::_nil ();
  }

  CORBA::Boolean
  Objref_Traits<CORBA::Object>::marshal (const CORBA::Object_ptr p,
                                         TAO_OutputCDR & cdr)
  {
    return ::CORBA::Object::marshal (p, cdr);
  }
} // close TAO namespace

TAO::Object_Proxy_Broker * (*_TAO_Object_Proxy_Broker_Factory_function_pointer) () = nullptr;

TAO_END_VERSIONED_NAMESPACE_DECL