Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tao / Environment.cpp
blobcd188fd4f2b4557afed2f6862b8dd4ea33a9425c
1 #include "tao/Environment.h"
2 #include "tao/ORB_Core.h"
3 #include "tao/SystemException.h"
4 #include "tao/default_environment.h"
6 #include "ace/OS_NS_string.h"
8 #if !defined (__ACE_INLINE__)
9 # include "tao/Environment.inl"
10 #endif /* __ACE_INLINE__ */
12 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
14 CORBA::Environment::Environment (const CORBA::Environment& rhs)
15 : exception_ (nullptr)
16 , previous_ (nullptr)
18 if (rhs.exception_)
19 this->exception_ = rhs.exception_->_tao_duplicate ();
22 CORBA::Environment::Environment (TAO_ORB_Core* orb_core)
23 : exception_ (nullptr)
24 , previous_ (orb_core->default_environment ())
26 orb_core->default_environment (this);
29 CORBA::Environment&
30 CORBA::Environment::operator= (const CORBA::Environment& rhs)
32 CORBA::Environment tmp (rhs);
34 CORBA::Exception *tmp_ex = this->exception_;
35 this->exception_ = tmp.exception_;
36 tmp.exception_ = tmp_ex;
39 CORBA::Environment *tmp_env = this->previous_;
40 this->previous_ = rhs.previous_;
41 tmp.previous_ = tmp_env;
43 return *this;
46 CORBA::Environment::~Environment ()
48 this->clear ();
50 // If previous is 0 then this is the first Environment, allocated
51 // with the ORB, it shouldn't try to pop because the ORB is being
52 // destroyed also.
53 if (this->previous_ != nullptr)
54 TAO_ORB_Core_instance ()->default_environment (this->previous_);
57 void
58 CORBA::Environment::exception (CORBA::Exception *ex)
60 // @@ This does not look right, setting the exception to the
61 // contained exception is a bug, the application is only
62 // supposed to pass in a pointer to an exception that it (the
63 // application) owns, however, if we contain the exception then
64 // *WE* own it.
65 // Normally I (coryan) would remove code like this, but I feel
66 // that it is a typical example of defensive programming for the
67 // *BAD*, i.e. we are not helping the application to get better
68 // and only making the ORB bigger and slower.
69 #if 0
70 if (ex != this->exception_)
72 this->clear ();
74 #else
75 ACE_ASSERT (ex != this->exception_);
76 this->clear ();
77 #endif /* 0 */
79 this->exception_ = ex;
81 if (this->exception_ != nullptr)
82 this->exception_->_raise ();
85 void
86 CORBA::Environment::clear ()
88 delete this->exception_;
89 this->exception_ = nullptr;
92 CORBA::Environment&
93 CORBA::Environment::default_environment ()
95 // If we are using native C++ exceptions the user is *not* supposed
96 // to clear the environment every time she calls into TAO. In fact
97 // the user is not supposed to use the environment at all!
99 // But TAO is using the default environment internally, thus
100 // somebody has to clear it. Since TAO passes the environment around
101 // this function should only be called when going from the user code
102 // into TAO's code.
104 // This is not an issue when using the alternative C++ mapping (with
105 // the Environment argument) because then the user is supposed to
106 // clear the environment before calling into the ORB.
107 TAO_ORB_Core_instance ()->default_environment ()->clear ();
109 return TAO_default_environment ();
112 // Convenience -- say if the exception is a system exception or not.
115 CORBA::Environment::exception_type () const
117 // @@ Carlos, is this stuff that's properly "transformed" for EBCDIC
118 // platforms?!
119 // @@ Doug: Yes, they are used to compare against the _id() of the
120 // exception, which should have been mappend to the native
121 // codeset. Notice the "should" we haven't tried that stuff yet,
122 // and i find it hard to keep track of all the transformations
123 // going on, specially for the TypeCodes that are generated by
124 // the IDL compiler vs. the ones hard-coded in
125 // $TAO_ROOT/tao/Typecode_Constants.cpp
127 static char sysex_prefix [] = "IDL:omg.org/CORBA/";
128 static char typecode_extra [] = "TypeCode/";
130 if (!this->exception_)
132 return CORBA::NO_EXCEPTION;
135 // All exceptions currently (CORBA 2.0) defined in the CORBA scope
136 // are system exceptions ... except for a couple that are related to
137 // TypeCodes.
139 const char *id = this->exception_->_rep_id ();
141 if ((ACE_OS::strncmp (id,
142 sysex_prefix,
143 sizeof sysex_prefix - 1) == 0
144 && ACE_OS::strncmp (id + sizeof sysex_prefix - 1,
145 typecode_extra,
146 sizeof typecode_extra - 1) != 0))
147 return CORBA::SYSTEM_EXCEPTION;
148 else
149 return CORBA::USER_EXCEPTION;
152 const char*
153 CORBA::Environment::exception_id () const
155 if (this->exception_ == nullptr)
156 return nullptr;
158 return this->exception_->_rep_id ();
161 // Diagnostic utility routine: describe the exception onto the
162 // standard I/O stream passed as a parameter.
164 void
165 CORBA::Environment::print_exception (const char *info,
166 FILE *) const
168 if (this->exception_)
170 const char *id = this->exception_->_rep_id ();
172 TAOLIB_ERROR ((LM_ERROR,
173 ACE_TEXT ("TAO: (%P|%t) EXCEPTION, %C\n"),
174 info));
176 CORBA::SystemException *x2 =
177 CORBA::SystemException::_downcast (this->exception_);
179 if (x2 != nullptr)
180 x2->_tao_print_system_exception ();
181 else
182 // @@ we can use the exception's typecode to dump all the data
183 // held within it ...
185 TAOLIB_ERROR ((LM_ERROR,
186 ACE_TEXT ("TAO: (%P|%t) user exception, ID '%C'\n"),
187 id));
189 else
190 TAOLIB_ERROR ((LM_ERROR,
191 ACE_TEXT ("TAO: (%P|%t) no exception, %C\n"), info));
194 TAO_END_VERSIONED_NAMESPACE_DECL