Service_Types.cpp

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

Generated on Tue Feb 2 17:18:42 2010 for ACE by  doxygen 1.4.7