Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / POA / DSI / Database_i.cpp
blob382c256fc04e7eaaffcc5a6c2cf8244531baa4f7
1 #include "Database_i.h"
3 #include "tao/DynamicInterface/Server_Request.h"
5 #include "tao/AnyTypeCode/NVList.h"
6 #include "tao/AnyTypeCode/TypeCode.h"
7 #include "ace/Null_Mutex.h"
9 DatabaseImpl::Simpler_Database_Malloc::Simpler_Database_Malloc ()
10 // : DATABASE_MALLOC ()
14 DatabaseImpl::Simpler_Database_Malloc::~Simpler_Database_Malloc ()
16 this->remove ();
19 DatabaseImpl::Entry::Entry (CORBA::ORB_ptr orb,
20 PortableServer::POA_ptr poa)
21 : orb_ (CORBA::ORB::_duplicate (orb)),
22 poa_ (PortableServer::POA::_duplicate (poa))
24 // Get the POA Current object reference
25 CORBA::Object_var obj =
26 this->orb_->resolve_initial_references ("POACurrent");
28 // Narrow the object reference to a POA Current reference
29 this->poa_current_ =
30 PortableServer::Current::_narrow (obj.in ());
33 DatabaseImpl::Entry::~Entry ()
37 void
38 DatabaseImpl::Entry::invoke (CORBA::ServerRequest_ptr request)
40 // The servant determines the key associated with the database
41 // entry represented by self.
42 PortableServer::ObjectId_var oid =
43 this->poa_current_->get_object_id ();
45 // Now convert the id into a string
46 CORBA::String_var key =
47 PortableServer::ObjectId_to_string (oid.in ());
49 // Get the operation name for this request
50 const char *operation =
51 request->operation ();
53 if (ACE_OS::strcmp (operation,
54 "_is_a") == 0)
56 this->is_a (request);
58 else
59 throw CORBA::NO_IMPLEMENT ();
62 void
63 DatabaseImpl::Entry::is_a (CORBA::ServerRequest_ptr request)
65 CORBA::NVList_ptr list;
66 this->orb_->create_list (0, list);
68 CORBA::Any any_1;
69 any_1._tao_set_typecode (CORBA::_tc_string);
71 list->add_value ("value",
72 any_1,
73 CORBA::ARG_IN);
75 request->arguments (list);
77 CORBA::NamedValue_ptr nv = list->item (0);
79 CORBA::Any_ptr ap = nv->value ();
80 const char *value;
81 *ap >>= value;
83 const char *object_id =
84 CORBA::_tc_Object->id ();
86 CORBA::Boolean result;
87 if (ACE_OS::strcmp (value, "IDL:Database/Employee:1.0") == 0
88 || ACE_OS::strcmp (value, "IDL:Database/Entry:1.0") == 0
89 || ACE_OS::strcmp (value, object_id) == 0)
90 result = 1;
91 else
92 result = 0;
94 CORBA::Any result_any;
95 CORBA::Any::from_boolean from_boolean (result);
96 result_any <<= from_boolean;
98 request->set_result (result_any);
101 CORBA::RepositoryId
102 DatabaseImpl::Entry::_primary_interface (const PortableServer::ObjectId &/*oid*/,
103 PortableServer::POA_ptr)
105 return 0;
108 PortableServer::POA_ptr
109 DatabaseImpl::Entry::_default_POA ()
111 return PortableServer::POA::_duplicate (this->poa_.in ());
114 DatabaseImpl::Agent::Agent (CORBA::ORB_ptr orb,
115 PortableServer::POA_ptr poa)
116 : orb_ (CORBA::ORB::_duplicate (orb)),
117 poa_ (PortableServer::POA::_duplicate (poa)),
118 common_servant_ (orb,
119 poa)
121 this->poa_->set_servant (&this->common_servant_);
124 DatabaseImpl::Agent::~Agent ()
128 Database::Entry_ptr
129 DatabaseImpl::Agent::create_entry (const char *key,
130 const char *entry_type,
131 const Database::NVPairSequence &initial_attributes)
133 // Create a new entry in the database:
134 if (ACE_OS::strcmp (entry_type, "Employee") != 0
135 || initial_attributes.length () != 2)
136 throw Database::Unknown_Type ();
138 const char *name = 0;
139 CORBA::Long id = 0;
141 const Database::NamedValue &first =
142 initial_attributes[0];
143 const Database::NamedValue &second =
144 initial_attributes[1];
146 if (ACE_OS::strcmp (first.name.in (), "name") != 0
147 || ACE_OS::strcmp (second.name.in (), "id") != 0)
148 throw Database::Unknown_Type ();
150 first.value >>= name;
151 second.value >>= id;
153 Employee *new_employee;
154 // This attempts to create a new Employee and throws an exception
155 // and returns a null value if it fails
156 ACE_NEW_THROW_EX (new_employee,
157 Employee (name, id),
158 CORBA::NO_MEMORY ());
160 // @@ Should check the return value here and throw an exception if
161 // it fails.
162 DATABASE::instance ()->bind (key,
163 new_employee);
165 ACE_DEBUG ((LM_DEBUG,
166 "New employee created with name = %C and id = %d\n",
167 name,
168 id));
170 // Creates a reference to the CORBA object used to encapsulate
171 // access to the new entry in the database. There is an interface
172 // for each entry type:
173 PortableServer::ObjectId_var obj_id =
174 PortableServer::string_to_ObjectId (key);
175 CORBA::String_var repository_id =
176 DatabaseImpl::entry_type_to_repository_id ("Entry");
178 CORBA::Object_var obj =
179 this->poa_->create_reference_with_id (obj_id.in (),
180 repository_id.in ());
182 Database::Entry_var entry = Database::Entry::_narrow (obj.in ());
184 return entry._retn ();
187 Database::Entry_ptr
188 DatabaseImpl::Agent::find_entry (const char *key,
189 const char *entry_type)
191 if (ACE_OS::strcmp (entry_type,
192 "Employee") != 0)
193 throw Database::Unknown_Type ();
195 void *temp = 0;
196 Database::Entry_var entry;
197 if (DATABASE::instance ()->find (key, temp) == 0)
199 Employee *employee =
200 reinterpret_cast<Employee *> (temp);
201 ACE_DEBUG ((LM_DEBUG,
202 "Employee with key = %C found: name = %C and id = %d\n",
203 key,
204 employee->name (),
205 employee->id ()));
207 // Creates a reference to the CORBA object used to encapsulate
208 // access to the new entry in the database. There is an
209 // interface for each entry type:
210 PortableServer::ObjectId_var obj_id =
211 PortableServer::string_to_ObjectId (key);
212 CORBA::String_var repository_id =
213 DatabaseImpl::entry_type_to_repository_id ("Entry");
214 CORBA::Object_var obj =
215 this->poa_->create_reference_with_id (obj_id.in (),
216 repository_id.in ());
218 entry = Database::Entry::_narrow (obj.in ());
220 else
222 ACE_DEBUG ((LM_DEBUG,
223 "Employee with key = %C not found\n",
224 key));
226 throw Database::Not_Found ();
229 return entry._retn ();
232 void
233 DatabaseImpl::Agent::destroy_entry (const char *key,
234 const char *entry_type)
236 if (ACE_OS::strcmp (entry_type, "Employee") != 0)
237 throw Database::Unknown_Type ();
239 void *temp = 0;
240 if (DATABASE::instance ()->unbind (key, temp) == 0)
242 Employee *employee =
243 reinterpret_cast<Employee *> (temp);
244 ACE_DEBUG ((LM_DEBUG,
245 "Employee with key = %C will be removed from the database: "
246 "name = %C and id = %d\n",
247 key,
248 employee->name (),
249 employee->id ()));
251 delete employee;
253 else
255 ACE_DEBUG ((LM_DEBUG,
256 "Employee with key = %C not found\n",
257 key));
259 throw Database::Unknown_Key ();
263 void
264 DatabaseImpl::Agent::shutdown ()
266 this->orb_->shutdown ();
269 PortableServer::POA_ptr
270 DatabaseImpl::Agent::_default_POA ()
272 return PortableServer::POA::_duplicate (this->poa_.in ());
275 char *
276 DatabaseImpl::entry_type_to_repository_id (const char *entry_type)
278 static const char *prefix = "IDL:Database/";
279 static int prefix_length = ACE_OS::strlen (prefix);
280 static const char *suffix = ":1.0";
281 static int suffix_length = ACE_OS::strlen (prefix);
283 int len =
284 prefix_length +
285 ACE_OS::strlen (entry_type) +
286 suffix_length +
289 char *result = CORBA::string_alloc (len);
290 ACE_OS::sprintf (result,
291 "%s%s%s",
292 prefix,
293 entry_type,
294 suffix);
295 return result;
298 DatabaseImpl::Employee::Employee (const char* name,
299 CORBA::Long id)
300 : id_ (id),
301 name_ (0)
303 this->name (name);
306 DatabaseImpl::Employee::~Employee ()
308 DATABASE::instance ()->free (this->name_);
311 const char *
312 DatabaseImpl::Employee::name () const
314 return this->name_;
317 void
318 DatabaseImpl::Employee::name (const char* name)
320 DATABASE::instance ()->free (this->name_);
322 this->name_ =
323 (char *) DATABASE::instance ()->malloc (ACE_OS::strlen (name) + 1);
325 ACE_OS::strcpy (this->name_,
326 name);
329 CORBA::Long
330 DatabaseImpl::Employee::id () const
332 return this->id_;
335 void
336 DatabaseImpl::Employee::id (CORBA::Long id)
338 this->id_ = id;
341 void *
342 DatabaseImpl::Employee::operator new (size_t size)
344 return DATABASE::instance ()->malloc (size);
347 void
348 DatabaseImpl::Employee::operator delete (void *pointer)
350 DATABASE::instance ()->free (pointer);
353 /// Overloaded new operator, nothrow_t variant.
354 void *
355 DatabaseImpl::Employee::operator new (size_t size, const std::nothrow_t &)
357 return DATABASE::instance ()->malloc (size);
360 void
361 DatabaseImpl::Employee::operator delete (void *ptr, const std::nothrow_t&) noexcept
363 DATABASE::instance ()->free (ptr);
366 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, DatabaseImpl::Simpler_Database_Malloc, ACE_Null_Mutex);