Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Service_Types.cpp
blob5a630e69d9f3873cbf4830bb0cf313aeddac77af
1 #include "ace/Service_Types.h"
3 #if !defined (__ACE_INLINE__)
4 #include "ace/Service_Types.inl"
5 #endif /* __ACE_INLINE__ */
7 #include "ace/Stream_Modules.h"
8 #include "ace/Stream.h"
9 #include "ace/OS_NS_stdio.h"
10 #include "ace/OS_NS_string.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 using MT_Stream = ACE_Stream<ACE_MT_SYNCH>;
15 using MT_Module = ACE_Module<ACE_MT_SYNCH>;
16 using MT_Task = ACE_Task<ACE_MT_SYNCH>;
18 ACE_ALLOC_HOOK_DEFINE(ACE_Service_Type_Impl)
20 void
21 ACE_Service_Type_Impl::dump () const
23 #if defined (ACE_HAS_DUMP)
24 ACE_TRACE ("ACE_Service_Type_Impl::dump");
25 #endif /* ACE_HAS_DUMP */
28 ACE_Service_Type_Impl::ACE_Service_Type_Impl (void *so,
29 const ACE_TCHAR *s_name,
30 u_int f,
31 ACE_Service_Object_Exterminator gobbler,
32 int stype)
33 : name_ (0),
34 obj_ (so),
35 gobbler_ (gobbler),
36 flags_ (f),
37 service_type_ (stype)
39 ACE_TRACE ("ACE_Service_Type_Impl::ACE_Service_Type_Impl");
40 this->name (s_name);
43 ACE_Service_Type_Impl::~ACE_Service_Type_Impl ()
45 ACE_TRACE ("ACE_Service_Type_Impl::~ACE_Service_Type_Impl");
47 // It's ok to call this, even though we may have already deleted it
48 // in the fini() method since it would then be NULL.
49 #if defined (ACE_HAS_ALLOC_HOOKS)
50 ACE_Allocator::instance()->free(const_cast <ACE_TCHAR *> (this->name_));
51 #else
52 delete [] const_cast <ACE_TCHAR *> (this->name_);
53 #endif /* ACE_HAS_ALLOC_HOOKS */
56 int
57 ACE_Service_Type_Impl::fini () const
59 ACE_TRACE ("ACE_Service_Type_Impl::fini");
61 #if defined (ACE_HAS_ALLOC_HOOKS)
62 ACE_Allocator::instance()->free(const_cast <ACE_TCHAR *> (this->name_));
63 #else
64 delete [] const_cast <ACE_TCHAR *> (this->name_);
65 #endif /* ACE_HAS_ALLOC_HOOKS */
66 (const_cast <ACE_Service_Type_Impl *> (this))->name_ = 0;
68 if (ACE_BIT_ENABLED (this->flags_,
69 ACE_Service_Type::DELETE_OBJ))
71 if (gobbler_ != 0)
72 gobbler_ (this->object ());
73 else
74 // Cast to remove const-ness.
75 operator delete ((void *) this->object ());
78 if (ACE_BIT_ENABLED (this->flags_,
79 ACE_Service_Type::DELETE_THIS))
80 delete const_cast <ACE_Service_Type_Impl *> (this);
82 return 0;
85 ACE_Service_Object_Type::ACE_Service_Object_Type (void *so,
86 const ACE_TCHAR *s_name,
87 u_int f,
88 ACE_Service_Object_Exterminator gobbler,
89 int stype)
90 : ACE_Service_Type_Impl (so, s_name, f, gobbler, stype)
91 , initialized_ (-1)
93 ACE_TRACE ("ACE_Service_Object_Type::ACE_Service_Object_Type");
96 int
97 ACE_Service_Object_Type::init (int argc, ACE_TCHAR *argv[]) const
99 ACE_TRACE ("ACE_Service_Object_Type::init");
101 void * const obj = this->object ();
103 ACE_Service_Object * const so =
104 static_cast<ACE_Service_Object *> (obj);
106 if (so == 0)
107 return -1;
109 this->initialized_ = so->init (argc, argv);
111 return this->initialized_;
115 ACE_Service_Object_Type::fini () const
117 ACE_TRACE ("ACE_Service_Object_Type::fini");
119 void * const obj = this->object ();
121 ACE_Service_Object * const so =
122 static_cast<ACE_Service_Object *> (obj);
124 // Call fini() if an only if, the object was successfully
125 // initialized, i.e. init() returned 0. This is necessary to
126 // maintain the ctor/dtor-like semantics for init/fini.
127 if (so != 0 && this->initialized_ == 0)
128 so->fini ();
130 return ACE_Service_Type_Impl::fini ();
133 ACE_Service_Object_Type::~ACE_Service_Object_Type ()
135 ACE_TRACE ("ACE_Service_Object_Type::~ACE_Service_Object_Type");
139 ACE_Service_Object_Type::suspend () const
141 ACE_TRACE ("ACE_Service_Object_Type::suspend");
142 return static_cast<ACE_Service_Object *> (this->object ())->suspend ();
146 ACE_Service_Object_Type::resume () const
148 ACE_TRACE ("ACE_Service_Object_Type::resume");
149 return static_cast<ACE_Service_Object *> (this->object ())->resume ();
153 ACE_Service_Object_Type::info (ACE_TCHAR **str, size_t len) const
155 ACE_TRACE ("ACE_Service_Object_Type::info");
156 return static_cast<ACE_Service_Object *> (this->object ())->info (str, len);
159 ACE_ALLOC_HOOK_DEFINE(ACE_Module_Type)
161 void
162 ACE_Module_Type::dump () const
164 #if defined (ACE_HAS_DUMP)
165 ACE_TRACE ("ACE_Module_Type::dump");
166 #endif /* ACE_HAS_DUMP */
169 ACE_Module_Type::ACE_Module_Type (void *m,
170 const ACE_TCHAR *m_name,
171 u_int f,
172 int stype)
173 : ACE_Service_Type_Impl (m, m_name, f, 0, stype)
174 , link_ (0)
176 ACE_TRACE ("ACE_Module_Type::ACE_Module_Type");
179 ACE_Module_Type::~ACE_Module_Type ()
181 ACE_TRACE ("ACE_Module_Type::~ACE_Module_Type");
185 ACE_Module_Type::init (int argc, ACE_TCHAR *argv[]) const
187 ACE_TRACE ("ACE_Module_Type::init");
188 void *obj = this->object ();
189 MT_Module *mod = (MT_Module *) obj;
191 // Change the Module's name to what's in the svc.conf file. We must
192 // do this so the names match up so everything shuts down properly
193 // during the call to ACE_Stream_Type::fini which calls
194 // MT_Stream::remove([name]) for all the modules. If the calls to
195 // remove fail, we end up with a double delete during
196 // shutdown. Bugzilla #3847
198 mod->name (this->name_);
199 MT_Task *reader = mod->reader ();
200 MT_Task *writer = mod->writer ();
202 if (reader->init (argc, argv) == -1
203 || writer->init (argc, argv) == -1)
204 return -1;
205 else
206 return 0;
210 ACE_Module_Type::suspend () const
212 ACE_TRACE ("ACE_Module_Type::suspend");
213 void *obj = this->object ();
214 MT_Module *mod = (MT_Module *) obj;
215 MT_Task *reader = mod->reader ();
216 MT_Task *writer = mod->writer ();
218 if (reader->suspend () == -1
219 || writer->suspend () == -1)
220 return -1;
221 else
222 return 0;
226 ACE_Module_Type::resume () const
228 ACE_TRACE ("ACE_Module_Type::resume");
229 void *obj = this->object ();
230 MT_Module *mod = (MT_Module *) obj;
231 MT_Task *reader = mod->reader ();
232 MT_Task *writer = mod->writer ();
234 if (reader->resume () == -1
235 || writer->resume () == -1)
236 return -1;
237 else
238 return 0;
241 // Note, these operations are somewhat too familiar with the
242 // implementation of ACE_Module and ACE_Module::close...
245 ACE_Module_Type::fini () const
247 ACE_TRACE ("ACE_Module_Type::fini");
248 void *obj = this->object ();
249 MT_Module *mod = (MT_Module *) obj;
250 MT_Task *reader = mod->reader ();
251 MT_Task *writer = mod->writer ();
253 if (reader != 0)
254 reader->fini ();
256 if (writer != 0)
257 writer->fini ();
259 // Close the module and delete the memory.
260 mod->close (MT_Module::M_DELETE);
261 return ACE_Service_Type_Impl::fini ();
265 ACE_Module_Type::info (ACE_TCHAR **str, size_t len) const
267 ACE_TRACE ("ACE_Module_Type::info");
268 ACE_TCHAR buf[BUFSIZ];
270 ACE_OS::snprintf (buf, BUFSIZ,
271 ACE_TEXT ("%s\t %s"),
272 this->name (),
273 ACE_TEXT ("# ACE_Module\n"));
275 if (*str == 0 && (*str = ACE_OS::strdup (buf)) == 0)
276 return -1;
277 else
278 ACE_OS::strsncpy (*str, buf, len);
279 return static_cast<int> (ACE_OS::strlen (buf));
282 void
283 ACE_Module_Type::link (ACE_Module_Type *n)
285 ACE_TRACE ("ACE_Module_Type::link");
286 this->link_ = n;
289 ACE_Module_Type *
290 ACE_Module_Type::link () const
292 ACE_TRACE ("ACE_Module_Type::link");
293 return this->link_;
296 ACE_ALLOC_HOOK_DEFINE(ACE_Stream_Type)
298 void
299 ACE_Stream_Type::dump () const
301 #if defined (ACE_HAS_DUMP)
302 ACE_TRACE ("ACE_Stream_Type::dump");
303 #endif /* ACE_HAS_DUMP */
307 ACE_Stream_Type::init (int, ACE_TCHAR *[]) const
309 ACE_TRACE ("ACE_Stream_Type::init");
310 return 0;
314 ACE_Stream_Type::suspend () const
316 ACE_TRACE ("ACE_Stream_Type::suspend");
318 for (ACE_Module_Type *m = this->head_;
319 m != 0;
320 m = m->link ())
321 m->suspend ();
323 return 0;
327 ACE_Stream_Type::resume () const
329 ACE_TRACE ("ACE_Stream_Type::resume");
331 for (ACE_Module_Type *m = this->head_;
332 m != 0;
333 m = m->link ())
334 m->resume ();
336 return 0;
339 ACE_Stream_Type::ACE_Stream_Type (void *s,
340 const ACE_TCHAR *s_name,
341 u_int f,
342 int stype)
343 : ACE_Service_Type_Impl (s, s_name, f, 0, stype),
344 head_ (0)
346 ACE_TRACE ("ACE_Stream_Type::ACE_Stream_Type");
349 ACE_Stream_Type::~ACE_Stream_Type ()
351 ACE_TRACE ("ACE_Stream_Type::~ACE_Stream_Type");
355 ACE_Stream_Type::info (ACE_TCHAR **str, size_t len) const
357 ACE_TRACE ("ACE_Stream_Type::info");
358 ACE_TCHAR buf[BUFSIZ];
360 ACE_OS::snprintf (buf, BUFSIZ,
361 ACE_TEXT ("%s\t %s"),
362 this->name (),
363 ACE_TEXT ("# STREAM\n"));
365 if (*str == 0 && (*str = ACE_OS::strdup (buf)) == 0)
366 return -1;
367 else
368 ACE_OS::strsncpy (*str, buf, len);
369 return static_cast<int> (ACE_OS::strlen (buf));
373 ACE_Stream_Type::fini () const
375 ACE_TRACE ("ACE_Stream_Type::fini");
376 void *obj = this->object ();
377 MT_Stream *str = (MT_Stream *) obj;
379 for (ACE_Module_Type *m = this->head_; m != 0;)
381 ACE_Module_Type *t = m->link ();
383 // Final arg is an indication to *not* delete the Module.
384 str->remove (m->name (),
385 MT_Module::M_DELETE_NONE);
386 m = t;
388 str->close ();
390 return ACE_Service_Type_Impl::fini ();
393 // Locate and remove <mod_name> from the ACE_Stream.
396 ACE_Stream_Type::remove (ACE_Module_Type *mod)
398 ACE_TRACE ("ACE_Stream_Type::remove");
400 ACE_Module_Type *prev = 0;
401 void *obj = this->object ();
402 MT_Stream *str = (MT_Stream *) obj;
403 int result = 0;
405 for (ACE_Module_Type *m = this->head_; m != 0; )
407 // We need to do this first so we don't bomb out if we delete m!
408 ACE_Module_Type *link = m->link ();
410 if (m == mod)
412 if (prev == 0)
413 this->head_ = link;
414 else
415 prev->link (link);
417 // Final arg is an indication to *not* delete the Module.
418 if (str->remove (m->name (),
419 MT_Module::M_DELETE_NONE) == -1)
420 result = -1;
422 // Do not call m->fini (); as this will result in a double delete
423 // of the ACE_Module_type when ACE_Service_Repository::fini is called
425 else
426 prev = m;
428 m = link;
431 return result;
435 ACE_Stream_Type::push (ACE_Module_Type *new_module)
437 ACE_TRACE ("ACE_Stream_Type::push");
438 void *obj = this->object ();
439 MT_Stream *str = (MT_Stream *) obj;
441 new_module->link (this->head_);
442 this->head_ = new_module;
443 obj = new_module->object ();
444 return str->push ((MT_Module *) obj);
447 ACE_Module_Type *
448 ACE_Stream_Type::find (const ACE_TCHAR *module_name) const
450 ACE_TRACE ("ACE_Stream_Type::find");
452 for (ACE_Module_Type *m = this->head_;
453 m != 0;
454 m = m->link ())
455 if (ACE_OS::strcmp (m->name (), module_name) == 0)
456 return m;
458 return 0;
461 // @@@ Eliminated ommented out explicit template instantiation code
463 ACE_END_VERSIONED_NAMESPACE_DECL