Service_Types.cpp

Go to the documentation of this file.
00001 // $Id: Service_Types.cpp 79367 2007-08-16 16:35:29Z mesnier_p $
00002 
00003 #include "ace/Service_Types.h"
00004 
00005 #if !defined (__ACE_INLINE__)
00006 #include "ace/Service_Types.inl"
00007 #endif /* __ACE_INLINE__ */
00008 
00009 #include "ace/Stream_Modules.h"
00010 #include "ace/Stream.h"
00011 #include "ace/OS_NS_stdio.h"
00012 #include "ace/OS_NS_string.h"
00013 
00014 
00015 ACE_RCSID (ace,
00016            Service_Types,
00017            "$Id: Service_Types.cpp 79367 2007-08-16 16:35:29Z mesnier_p $")
00018 
00019   ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00020 
00021 typedef ACE_Stream<ACE_SYNCH> MT_Stream;
00022 typedef ACE_Module<ACE_SYNCH> MT_Module;
00023 typedef ACE_Task<ACE_SYNCH> MT_Task;
00024 
00025 ACE_ALLOC_HOOK_DEFINE(ACE_Service_Type_Impl)
00026 
00027   void
00028 ACE_Service_Type_Impl::dump (void) const
00029 {
00030 #if defined (ACE_HAS_DUMP)
00031   ACE_TRACE ("ACE_Service_Type_Impl::dump");
00032 #endif /* ACE_HAS_DUMP */
00033 }
00034 
00035 ACE_Service_Type_Impl::ACE_Service_Type_Impl (void *so,
00036                                               const ACE_TCHAR *s_name,
00037                                               u_int f,
00038                                               ACE_Service_Object_Exterminator gobbler)
00039   : name_ (0),
00040     obj_ (so),
00041     gobbler_ (gobbler),
00042     flags_ (f)
00043 {
00044   ACE_TRACE ("ACE_Service_Type_Impl::ACE_Service_Type_Impl");
00045   this->name (s_name);
00046 }
00047 
00048 ACE_Service_Type_Impl::~ACE_Service_Type_Impl (void)
00049 {
00050   ACE_TRACE ("ACE_Service_Type_Impl::~ACE_Service_Type_Impl");
00051 
00052   // It's ok to call this, even though we may have already deleted it
00053   // in the fini() method since it would then be NULL.
00054   delete [] const_cast <ACE_TCHAR *> (this->name_);
00055 }
00056 
00057 int
00058 ACE_Service_Type_Impl::fini (void) const
00059 {
00060   ACE_TRACE ("ACE_Service_Type_Impl::fini");
00061   if (ACE::debug ())
00062     ACE_DEBUG ((LM_DEBUG,
00063                 ACE_TEXT ("(%P|%t) ST::fini - destroying %s, flags = %d\n"),
00064                 this->name_,
00065                 this->flags_));
00066 
00067   delete [] const_cast <ACE_TCHAR *> (this->name_);
00068   (const_cast <ACE_Service_Type_Impl *> (this))->name_ = 0;
00069 
00070   if (ACE_BIT_ENABLED (this->flags_,
00071                        ACE_Service_Type::DELETE_OBJ))
00072     {
00073       if (gobbler_ != 0)
00074         gobbler_ (this->object ());
00075       else
00076         // Cast to remove const-ness.
00077         operator delete ((void *) this->object ());
00078     }
00079 
00080   if (ACE_BIT_ENABLED (this->flags_,
00081                        ACE_Service_Type::DELETE_THIS))
00082     delete const_cast <ACE_Service_Type_Impl *> (this);
00083 
00084   return 0;
00085 }
00086 
00087 ACE_Service_Object_Type::ACE_Service_Object_Type (void *so,
00088                                                   const ACE_TCHAR *s_name,
00089                                                   u_int f,
00090                                                   ACE_Service_Object_Exterminator gobbler)
00091   : ACE_Service_Type_Impl (so, s_name, f, gobbler)
00092   , initialized_ (-1)
00093 {
00094   ACE_TRACE ("ACE_Service_Object_Type::ACE_Service_Object_Type");
00095 }
00096 
00097 int
00098 ACE_Service_Object_Type::init (int argc, ACE_TCHAR *argv[]) const
00099 {
00100   ACE_TRACE ("ACE_Service_Object_Type::init");
00101 
00102   void * const obj = this->object ();
00103 
00104   ACE_Service_Object * const so =
00105     static_cast<ACE_Service_Object *> (obj);
00106 
00107   if (so == 0)
00108     return -1;
00109 
00110   this->initialized_ = so->init (argc, argv);
00111 
00112   return this->initialized_;
00113 }
00114 
00115 int
00116 ACE_Service_Object_Type::fini (void) const
00117 {
00118   ACE_TRACE ("ACE_Service_Object_Type::fini");
00119 
00120   void * const obj = this->object ();
00121 
00122   ACE_Service_Object * const so =
00123     static_cast<ACE_Service_Object *> (obj);
00124 
00125   // Call fini() if an only if, the object was successfuly
00126   // initialized, i.e. init() returned 0. This is necessary to
00127   // maintain the ctor/dtor-like semantics for init/fini.
00128   if (so && initialized_ == 0)
00129       so->fini ();
00130 
00131   return ACE_Service_Type_Impl::fini ();
00132 }
00133 
00134 ACE_Service_Object_Type::~ACE_Service_Object_Type (void)
00135 {
00136   ACE_TRACE ("ACE_Service_Object_Type::~ACE_Service_Object_Type");
00137 }
00138 
00139 int
00140 ACE_Service_Object_Type::suspend (void) const
00141 {
00142   ACE_TRACE ("ACE_Service_Object_Type::suspend");
00143   return static_cast<ACE_Service_Object *> (this->object ())->suspend ();
00144 }
00145 
00146 int
00147 ACE_Service_Object_Type::resume (void) const
00148 {
00149   ACE_TRACE ("ACE_Service_Object_Type::resume");
00150   return static_cast<ACE_Service_Object *> (this->object ())->resume ();
00151 }
00152 
00153 int
00154 ACE_Service_Object_Type::info (ACE_TCHAR **str, size_t len) const
00155 {
00156   ACE_TRACE ("ACE_Service_Object_Type::info");
00157   return static_cast<ACE_Service_Object *> (this->object ())->info (str, len);
00158 }
00159 
00160 ACE_ALLOC_HOOK_DEFINE(ACE_Module_Type)
00161 
00162   void
00163 ACE_Module_Type::dump (void) const
00164 {
00165 #if defined (ACE_HAS_DUMP)
00166   ACE_TRACE ("ACE_Module_Type::dump");
00167 #endif /* ACE_HAS_DUMP */
00168 }
00169 
00170 ACE_Module_Type::ACE_Module_Type (void *m,
00171                                   const ACE_TCHAR *m_name,
00172                                   u_int f)
00173   : ACE_Service_Type_Impl (m, m_name, f)
00174 {
00175   ACE_TRACE ("ACE_Module_Type::ACE_Module_Type");
00176 }
00177 
00178 ACE_Module_Type::~ACE_Module_Type (void)
00179 {
00180   ACE_TRACE ("ACE_Module_Type::~ACE_Module_Type");
00181 }
00182 
00183 int
00184 ACE_Module_Type::init (int argc, ACE_TCHAR *argv[]) const
00185 {
00186   ACE_TRACE ("ACE_Module_Type::init");
00187   void *obj = this->object ();
00188   MT_Module *mod = (MT_Module *) obj;
00189   MT_Task *reader = mod->reader ();
00190   MT_Task *writer = mod->writer ();
00191 
00192   if (reader->init (argc, argv) == -1
00193       || writer->init (argc, argv) == -1)
00194     return -1;
00195   else
00196     return 0;
00197 }
00198 
00199 int
00200 ACE_Module_Type::suspend (void) const
00201 {
00202   ACE_TRACE ("ACE_Module_Type::suspend");
00203   void *obj = this->object ();
00204   MT_Module *mod = (MT_Module *) obj;
00205   MT_Task *reader = mod->reader ();
00206   MT_Task *writer = mod->writer ();
00207 
00208   if (reader->suspend () == -1
00209       || writer->suspend () == -1)
00210     return -1;
00211   else
00212     return 0;
00213 }
00214 
00215 int
00216 ACE_Module_Type::resume (void) const
00217 {
00218   ACE_TRACE ("ACE_Module_Type::resume");
00219   void *obj = this->object ();
00220   MT_Module *mod = (MT_Module *) obj;
00221   MT_Task *reader = mod->reader ();
00222   MT_Task *writer = mod->writer ();
00223 
00224   if (reader->resume () == -1
00225       || writer->resume () == -1)
00226     return -1;
00227   else
00228     return 0;
00229 }
00230 
00231 // Note, these operations are somewhat too familiar with the
00232 // implementation of ACE_Module and ACE_Module::close...
00233 
00234 int
00235 ACE_Module_Type::fini (void) const
00236 {
00237   ACE_TRACE ("ACE_Module_Type::fini");
00238 
00239   void *obj = this->object ();
00240   MT_Module *mod = (MT_Module *) obj;
00241   MT_Task *reader = mod->reader ();
00242   MT_Task *writer = mod->writer ();
00243 
00244   if (reader != 0)
00245     reader->fini ();
00246 
00247   if (writer != 0)
00248     writer->fini ();
00249 
00250   // Close the module and delete the memory.
00251   mod->close (MT_Module::M_DELETE);
00252   return ACE_Service_Type_Impl::fini ();
00253 }
00254 
00255 int
00256 ACE_Module_Type::info (ACE_TCHAR **str, size_t len) const
00257 {
00258   ACE_TRACE ("ACE_Module_Type::info");
00259   ACE_TCHAR buf[BUFSIZ];
00260 
00261   ACE_OS::sprintf (buf,
00262                    ACE_TEXT ("%s\t %s"),
00263                    this->name (),
00264                    ACE_TEXT ("# ACE_Module\n"));
00265 
00266   if (*str == 0 && (*str = ACE_OS::strdup (buf)) == 0)
00267     return -1;
00268   else
00269     ACE_OS::strsncpy (*str, buf, len);
00270   return static_cast<int> (ACE_OS::strlen (buf));
00271 }
00272 
00273 void
00274 ACE_Module_Type::link (ACE_Module_Type *n)
00275 {
00276   ACE_TRACE ("ACE_Module_Type::link");
00277   this->link_ = n;
00278 }
00279 
00280 ACE_Module_Type *
00281 ACE_Module_Type::link (void) const
00282 {
00283   ACE_TRACE ("ACE_Module_Type::link");
00284   return this->link_;
00285 }
00286 
00287 ACE_ALLOC_HOOK_DEFINE(ACE_Stream_Type)
00288 
00289   void
00290 ACE_Stream_Type::dump (void) const
00291 {
00292 #if defined (ACE_HAS_DUMP)
00293   ACE_TRACE ("ACE_Stream_Type::dump");
00294 #endif /* ACE_HAS_DUMP */
00295 }
00296 
00297 int
00298 ACE_Stream_Type::init (int, ACE_TCHAR *[]) const
00299 {
00300   ACE_TRACE ("ACE_Stream_Type::init");
00301   return 0;
00302 }
00303 
00304 int
00305 ACE_Stream_Type::suspend (void) const
00306 {
00307   ACE_TRACE ("ACE_Stream_Type::suspend");
00308 
00309   for (ACE_Module_Type *m = this->head_;
00310        m != 0;
00311        m = m->link ())
00312     m->suspend ();
00313 
00314   return 0;
00315 }
00316 
00317 int
00318 ACE_Stream_Type::resume (void) const
00319 {
00320   ACE_TRACE ("ACE_Stream_Type::resume");
00321 
00322   for (ACE_Module_Type *m = this->head_;
00323        m != 0;
00324        m = m->link ())
00325     m->resume ();
00326 
00327   return 0;
00328 }
00329 
00330 ACE_Stream_Type::ACE_Stream_Type (void *s,
00331                                   const ACE_TCHAR *s_name,
00332                                   u_int f)
00333   : ACE_Service_Type_Impl (s, s_name, f),
00334     head_ (0)
00335 {
00336   ACE_TRACE ("ACE_Stream_Type::ACE_Stream_Type");
00337 }
00338 
00339 ACE_Stream_Type::~ACE_Stream_Type (void)
00340 {
00341   ACE_TRACE ("ACE_Stream_Type::~ACE_Stream_Type");
00342 }
00343 
00344 int
00345 ACE_Stream_Type::info (ACE_TCHAR **str, size_t len) const
00346 {
00347   ACE_TRACE ("ACE_Stream_Type::info");
00348   ACE_TCHAR buf[BUFSIZ];
00349 
00350   ACE_OS::sprintf (buf,
00351                    ACE_TEXT ("%s\t %s"),
00352                    this->name (),
00353                    ACE_TEXT ("# STREAM\n"));
00354 
00355   if (*str == 0 && (*str = ACE_OS::strdup (buf)) == 0)
00356     return -1;
00357   else
00358     ACE_OS::strsncpy (*str, buf, len);
00359   return static_cast<int> (ACE_OS::strlen (buf));
00360 }
00361 
00362 int
00363 ACE_Stream_Type::fini (void) const
00364 {
00365   ACE_TRACE ("ACE_Stream_Type::fini");
00366   void *obj = this->object ();
00367   MT_Stream *str = (MT_Stream *) obj;
00368 
00369   for (ACE_Module_Type *m = this->head_; m != 0; )
00370     {
00371       ACE_Module_Type *t = m->link ();
00372 
00373       // Final arg is an indication to *not* delete the Module.
00374       str->remove (m->name (),
00375                    MT_Module::M_DELETE_NONE);
00376 
00377       // Finalize the Module (this may delete it, but we don't really
00378       // care since we don't access it again).
00379       m->fini ();
00380       m = t;
00381     }
00382 
00383   str->close ();
00384   return ACE_Service_Type_Impl::fini ();
00385 }
00386 
00387 // Locate and remove <mod_name> from the ACE_Stream.
00388 
00389 int
00390 ACE_Stream_Type::remove (ACE_Module_Type *mod)
00391 {
00392   ACE_TRACE ("ACE_Stream_Type::remove");
00393 
00394   ACE_Module_Type *prev = 0;
00395   void *obj = this->object ();
00396   MT_Stream *str = (MT_Stream *) obj;
00397   int result = 0;
00398 
00399   for (ACE_Module_Type *m = this->head_; m != 0; )
00400     {
00401       // We need to do this first so we don't bomb out if we delete m!
00402       ACE_Module_Type *link = m->link ();
00403 
00404       if (m == mod)
00405         {
00406           if (prev == 0)
00407             this->head_ = link;
00408           else
00409             prev->link (link);
00410 
00411           // Final arg is an indication to *not* delete the Module.
00412           if (str->remove (m->name (),
00413                            MT_Module::M_DELETE_NONE) == -1)
00414             result = -1;
00415 
00416           // This call may end up deleting m, which is ok since we
00417           // don't access it again!
00418           m->fini ();
00419         }
00420       else
00421         prev = m;
00422 
00423       m = link;
00424     }
00425 
00426   return result;
00427 }
00428 
00429 int
00430 ACE_Stream_Type::push (ACE_Module_Type *new_module)
00431 {
00432   ACE_TRACE ("ACE_Stream_Type::push");
00433   void *obj = this->object ();
00434   MT_Stream *str = (MT_Stream *) obj;
00435 
00436   new_module->link (this->head_);
00437   this->head_ = new_module;
00438   obj = new_module->object ();
00439   return str->push ((MT_Module *) obj);
00440 }
00441 
00442 ACE_Module_Type *
00443 ACE_Stream_Type::find (const ACE_TCHAR *mod_name) const
00444 {
00445   ACE_TRACE ("ACE_Stream_Type::find");
00446 
00447   for (ACE_Module_Type *m = this->head_;
00448        m != 0;
00449        m = m->link ())
00450     if (ACE_OS::strcmp (m->name (), mod_name) == 0)
00451       return m;
00452 
00453   return 0;
00454 }
00455 
00456 // @@@ Eliminated ommented out explicit template instantiation code
00457 
00458 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Sun Jan 27 12:05:37 2008 for ACE by doxygen 1.3.6