summaryrefslogtreecommitdiff
path: root/TAO/tao/Object_T.cpp
blob: 359910e0dafb0aa8a82fbca27867c0304770b942 (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
#ifndef TAO_OBJECT_T_CPP
#define TAO_OBJECT_T_CPP

#include "tao/Object_T.h"
#include "tao/Object.h"
#include "tao/Stub.h"
#include "tao/SystemException.h"
#include "ace/CORBA_macros.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  template<typename T>
  T *
  Narrow_Utils<T>::narrow (CORBA::Object_ptr obj,
                           const char *repo_id)
  {
    if (CORBA::is_nil (obj))
      {
        return T::_nil ();
      }

    if (!obj->_is_a (repo_id))
      {
        return T::_nil ();
      }

    return TAO::Narrow_Utils<T>::unchecked_narrow (obj);
  }

  template<typename T> T *
  Narrow_Utils<T>::unchecked_narrow (CORBA::Object_ptr obj)
  {
    if (CORBA::is_nil (obj))
      {
        return T::_nil ();
      }

    if (obj->_is_local ())
      {
        return T::_duplicate (dynamic_cast<T *> (obj));
      }

    T_ptr proxy = T::_nil ();
    try
      {
        proxy = Narrow_Utils<T>::lazy_evaluation (obj);

        if (CORBA::is_nil (proxy))
          {
            TAO_Stub* stub = obj->_stubobj ();

            if (stub != 0)
              {
                stub->_incr_refcnt ();

                bool const collocated =
                  !CORBA::is_nil (stub->servant_orb_var ().in ())
                  && stub->optimize_collocation_objects ()
                  && obj->_is_collocated ();

                ACE_NEW_RETURN (proxy,
                                T (stub,
                                   collocated,
                                   obj->_servant ()),
                                T::_nil ());
              }
          }
      }
    catch (const CORBA::Exception&)
      {
        // Swallow the exception
        return T::_nil ();
      }

    return proxy;
  }

  template<typename T>
  T *
  Narrow_Utils<T>::lazy_evaluation (CORBA::Object_ptr obj)
  {
    T_ptr default_proxy = T::_nil ();

    // Code for lazily evaluated IORs.
    if (!obj->is_evaluated ())
      {
        ACE_NEW_RETURN (default_proxy,
                        T (obj->steal_ior (),
                           obj->orb_core ()),
                        T::_nil ());
      }

    return default_proxy;
  }
}

TAO_END_VERSIONED_NAMESPACE_DECL

#endif /* TAO_OBJECT_T_CPP */