Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tao / IORManipulation / IORManipulation.cpp
blobd46abceb6a041ef8f1f24f5dc3c04bf293f6fa35
1 // -*- C++ -*-
2 #include "tao/IORManipulation/IORManipulation.h"
4 #include "tao/MProfile.h"
5 #include "tao/Profile.h"
6 #include "tao/Stub.h"
7 #include "tao/ORB_Core.h"
9 #include <memory>
10 #include "ace/OS_NS_string.h"
12 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
14 TAO_IOR_Manipulation_impl::TAO_IOR_Manipulation_impl ()
18 TAO_IOR_Manipulation_impl::~TAO_IOR_Manipulation_impl ()
22 CORBA::Object_ptr
23 TAO_IOR_Manipulation_impl::merge_iors (
24 const TAO_IOP::TAO_IOR_Manipulation::IORList & iors)
26 // we need to create a new CORBA::Object which has the union of the
27 // two profile lists. However, if any profiles are duplicates (i.e. in
28 // both lists) then an exception is raised.
30 // Determine how many profiles we have
31 // Get an estimate of the size - pfile count could change since we
32 // neither lock nor get a copy in this loop.
33 CORBA::ULong i, count=0;
34 for (i = 0; i < iors.length (); i++)
36 count += iors[i]->_stubobj ()->base_profiles ().profile_count ();
39 // make sure we have some profiles
40 if (count == 0)
41 throw TAO_IOP::EmptyProfileList ();
43 // initialize with estimated pfile count.
44 TAO_MProfile Merged_Profiles (count);
46 // get the profile lists, start by initialize the composite reference
47 // by using the first Object. Then for each subsequent Object verify
48 // they are the same type and they do not have duplicate profiles.
49 std::unique_ptr<TAO_MProfile> tmp_pfiles (iors[0]->_stubobj ()->make_profiles ());
50 if (Merged_Profiles.add_profiles (tmp_pfiles.get ())< 0)
51 throw TAO_IOP::Invalid_IOR ();
52 CORBA::String_var id =
53 CORBA::string_dup (iors[0]->_stubobj ()->type_id.in ());
55 for (i = 1; i < iors.length () ; i++)
57 // this gets a copy of the MProfile, hence the auto_ptr;
58 tmp_pfiles.reset (iors[i]->_stubobj ()->make_profiles ());
60 // check to see if any of the profiles in tmp_pfiles are already
61 // in Merged_Profiles. If so raise exception.
62 if (Merged_Profiles.is_equivalent (tmp_pfiles.get ()))
63 throw TAO_IOP::Duplicate ();
65 // If the object type_id's differ then raise an exception.
66 if (id.in () && iors[i]->_stubobj ()->type_id.in () &&
67 ACE_OS::strcmp (id.in (), iors[i]->_stubobj ()->type_id.in ()))
68 throw TAO_IOP::Invalid_IOR ();
70 // append profiles
71 if (Merged_Profiles.add_profiles (tmp_pfiles.get ()) < 0)
72 throw TAO_IOP::Invalid_IOR ();
75 // MS C++ knows nothing about reset!
76 // tmp_pfiles.reset (0); // get rid of last MProfile
77 TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
79 TAO_Stub *stub = orb_core->create_stub (id.in (), // give the id string
80 Merged_Profiles);
82 // Make the stub memory allocation exception safe for the duration
83 // of this method.
84 TAO_Stub_Auto_Ptr safe_stub (stub);
86 // Create the CORBA level proxy
87 CORBA::Object_ptr temp_obj = CORBA::Object::_nil ();
88 ACE_NEW_THROW_EX (temp_obj,
89 CORBA::Object (safe_stub.get ()),
90 CORBA::NO_MEMORY ());
92 CORBA::Object_var new_obj = temp_obj;
94 // Clean up in case of errors.
95 if (CORBA::is_nil (new_obj.in ()))
97 throw TAO_IOP::Invalid_IOR ();
100 // Release ownership of the pointers protected by the auto_ptrs since they
101 // no longer need to be protected by this point.
102 stub = safe_stub.release ();
104 return new_obj._retn ();
107 CORBA::Object_ptr
108 TAO_IOR_Manipulation_impl::add_profiles (
109 CORBA::Object_ptr ior1,
110 CORBA::Object_ptr ior2)
112 // Get an estimate of the number of profiles
113 CORBA::Object_ptr buffer [2];
114 buffer [0] = ior1;
115 buffer [1] = ior2;
116 TAO_IOP::TAO_IOR_Manipulation::IORList iors (2, 2, buffer, 0);
117 return this->merge_iors (iors);
120 CORBA::Object_ptr
121 TAO_IOR_Manipulation_impl::remove_profiles (
122 CORBA::Object_ptr group,
123 CORBA::Object_ptr ior2)
125 // First verify they are the same type!
126 CORBA::String_var id =
127 CORBA::string_dup (group->_stubobj ()->type_id.in ());
128 if (id.in () && ior2->_stubobj ()->type_id.in () &&
129 ACE_OS::strcmp (id.in (), ior2->_stubobj ()->type_id.in ()))
130 throw TAO_IOP::Invalid_IOR ();
132 // Since we are removing from group ...
133 CORBA::ULong count = group->_stubobj ()->base_profiles ().profile_count ();
135 // make sure we have some profiles
136 if (count == 0 ||
137 ior2->_stubobj ()->base_profiles ().profile_count () == 0)
138 throw TAO_IOP::EmptyProfileList ();
140 // initialize with estimated pfile count.
141 TAO_MProfile Diff_Profiles (count);
143 std::unique_ptr<TAO_MProfile> tmp_pfiles (group->_stubobj ()->make_profiles ());
144 if (Diff_Profiles.add_profiles (tmp_pfiles.get ()) < 0)
145 throw TAO_IOP::Invalid_IOR ();
147 // We are done with add_profiles.
148 // At this point, we don't do remove_profiles()
149 // immediately like before,
150 // because it could result in an
151 // Object Reference with 0 profile. And it would not pass
152 // the ::CORBA::is_nil() evaluation.
153 // Instead, we create the Object Reference right here, which is
154 // earlier than before.(Actually, I just moved some code
155 // from below up to here).
156 TAO_ORB_Core *orb_core = TAO_ORB_Core_instance ();
158 TAO_Stub *stub = orb_core->create_stub (id.in (), // give the id string
159 Diff_Profiles);
161 // Make the stub memory allocation exception safe for the duration
162 // of this method.
163 TAO_Stub_Auto_Ptr safe_stub (stub);
165 // Create the CORBA level proxy
166 CORBA::Object_ptr temp_obj = CORBA::Object::_nil ();
167 ACE_NEW_THROW_EX (temp_obj,
168 CORBA::Object (safe_stub.get ()),
169 CORBA::NO_MEMORY ());
171 CORBA::Object_var new_obj = temp_obj;
173 // Exception safety is no longer an issue by this point so release
174 // the TAO_Stub from the TAO_Stub_Auto_Ptr.
175 stub = safe_stub.release ();
177 // Clean up in case of errors.
178 if (CORBA::is_nil (new_obj.in ()))
180 throw TAO_IOP::Invalid_IOR ();
183 // Now we can remove the profiles which we want to eliminate from
184 // the Object.
185 tmp_pfiles.reset (ior2->_stubobj ()->make_profiles ());
187 TAO_MProfile& mp = stub -> base_profiles();
188 if (mp.remove_profiles (tmp_pfiles.get ()) < 0)
189 throw TAO_IOP::NotFound ();
191 // MS C++ knows nothing about reset!
192 // tmp_pfiles.reset (0); // get rid of last MProfile
194 return new_obj._retn ();
197 CORBA::Boolean
198 TAO_IOR_Manipulation_impl::set_property (
199 TAO_IOP::TAO_IOR_Property_ptr prop,
200 CORBA::Object_ptr group)
202 // make sure we have some profiles
203 if (group->_stubobj ()->base_profiles ().profile_count () == 0)
204 throw TAO_IOP::Invalid_IOR ();
206 // Call the implementation object to
207 return prop->set_property (group);
210 //@@ note awkward argument order
211 CORBA::Boolean
212 TAO_IOR_Manipulation_impl::set_primary (
213 TAO_IOP::TAO_IOR_Property_ptr prop,
214 CORBA::Object_ptr new_primary,
215 CORBA::Object_ptr group)
217 // make sure we have some profiles in GROUP
218 if (group->_stubobj ()->base_profiles ().profile_count () == 0)
219 throw TAO_IOP::Invalid_IOR ();
221 // Make sure we have only one profile in new_primary
222 // @@ Will fail if the object has been
223 /*if (new_primary->_stubobj ()->base_profiles ().profile_count () > 1)
224 throw TAO_IOP::MultiProfileList ();*/
226 // Call the callback object to do the rest of the processing.
227 return prop->set_primary (new_primary, group);
230 CORBA::Object_ptr
231 TAO_IOR_Manipulation_impl::get_primary (
232 TAO_IOP::TAO_IOR_Property_ptr prop,
233 CORBA::Object_ptr group)
235 // make sure we have some profiles in IOR
236 if (group->_stubobj ()->base_profiles ().profile_count () == 0)
237 throw TAO_IOP::NotFound ();
238 // @@ Bala: this was throwing TAO_IOP::Invalid_IOR, but it was not
239 // in the throw spec, that will result in a CORBA::UNKNOWN at
240 // run-time (if it does not crash). Any idea about what is going on
241 // here?
243 return prop->get_primary (group);
246 CORBA::Boolean
247 TAO_IOR_Manipulation_impl::is_primary_set (
248 TAO_IOP::TAO_IOR_Property_ptr prop,
249 CORBA::Object_ptr group)
251 return prop->is_primary_set (group);
254 CORBA::Boolean
255 TAO_IOR_Manipulation_impl::remove_primary_tag (
256 TAO_IOP::TAO_IOR_Property_ptr prop,
257 CORBA::Object_ptr group)
259 return prop->remove_primary_tag (group);
262 CORBA::ULong
263 TAO_IOR_Manipulation_impl::is_in_ior (
264 CORBA::Object_ptr ior1,
265 CORBA::Object_ptr ior2)
267 CORBA::ULong count = 0;
268 TAO_Profile *pfile1 = 0;
269 TAO_Profile *pfile2 = 0;
270 std::unique_ptr<TAO_MProfile> tmp_pfiles1 (ior1->_stubobj ()->make_profiles ());
271 std::unique_ptr<TAO_MProfile> tmp_pfiles2 (ior2->_stubobj ()->make_profiles ());
273 tmp_pfiles1->rewind ();
274 while ((pfile1 = tmp_pfiles1->get_next ()) != 0)
276 tmp_pfiles2->rewind ();
277 while ((pfile2 = tmp_pfiles2->get_next ()) != 0)
279 if (pfile1->is_equivalent (pfile2))
280 ++count;
284 if (count == 0)
285 throw TAO_IOP::NotFound ();
287 return count;
290 CORBA::ULong
291 TAO_IOR_Manipulation_impl::get_profile_count (CORBA::Object_ptr group)
293 CORBA::ULong const count = group->_stubobj ()->base_profiles ().profile_count ();
295 if (count == 0)
296 throw TAO_IOP::EmptyProfileList ();
298 return count;
301 TAO_END_VERSIONED_NAMESPACE_DECL