00001
00002
00003 #ifndef ACE_SINGLETON_CPP
00004 #define ACE_SINGLETON_CPP
00005
00006 #include "ace/Singleton.h"
00007
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif
00011
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/Singleton.inl"
00014 #endif
00015
00016 #include "ace/Object_Manager.h"
00017 #include "ace/Log_Msg.h"
00018 #include "ace/Framework_Component.h"
00019 #include "ace/Guard_T.h"
00020
00021 ACE_RCSID (ace,
00022 Singleton,
00023 "$Id: Singleton.cpp 79134 2007-07-31 18:23:50Z johnnyw $")
00024
00025
00026 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00027
00028 template <class TYPE, class ACE_LOCK> void
00029 ACE_Singleton<TYPE, ACE_LOCK>::dump (void)
00030 {
00031 #if defined (ACE_HAS_DUMP)
00032 ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::dump");
00033
00034 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00035 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00036 ACE_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00037 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00038 #endif
00039 #endif
00040 }
00041
00042 template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *&
00043 ACE_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00044 {
00045 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00046
00047
00048 static ACE_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00049
00050 return singleton_;
00051 #else
00052 return ACE_Singleton<TYPE, ACE_LOCK>::singleton_;
00053 #endif
00054 }
00055
00056 template <class TYPE, class ACE_LOCK> TYPE *
00057 ACE_Singleton<TYPE, ACE_LOCK>::instance (void)
00058 {
00059 ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::instance");
00060
00061 ACE_Singleton<TYPE, ACE_LOCK> *&singleton =
00062 ACE_Singleton<TYPE, ACE_LOCK>::instance_i ();
00063
00064
00065 if (singleton == 0)
00066 {
00067 if (ACE_Object_Manager::starting_up () ||
00068 ACE_Object_Manager::shutting_down ())
00069 {
00070
00071
00072
00073
00074
00075
00076
00077 ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
00078 }
00079 else
00080 {
00081 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00082
00083
00084
00085 static ACE_LOCK *lock = 0;
00086 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00087
00088 return 0;
00089
00090 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00091
00092 if (singleton == 0)
00093 {
00094 #endif
00095 ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
00096
00097
00098 ACE_Object_Manager::at_exit (singleton);
00099 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00100 }
00101 #endif
00102 }
00103 }
00104
00105 return &singleton->instance_;
00106 }
00107
00108 template <class TYPE, class ACE_LOCK> void
00109 ACE_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00110 {
00111 delete this;
00112 ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00113 }
00114
00115 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00116
00117 template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *
00118 ACE_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00119
00120 template <class TYPE, class ACE_LOCK> ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *
00121 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00122 #endif
00123
00124 template <class TYPE, class ACE_LOCK> void
00125 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::dump (void)
00126 {
00127 #if defined (ACE_HAS_DUMP)
00128 ACE_TRACE ("ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::dump");
00129
00130 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00131 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00132 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00133 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00134 #endif
00135 #endif
00136 }
00137
00138 template <class TYPE, class ACE_LOCK>
00139 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *&
00140 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00141 {
00142 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00143
00144
00145 static ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00146
00147 return singleton_;
00148 #else
00149 return ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::singleton_;
00150 #endif
00151 }
00152
00153 template <class TYPE, class ACE_LOCK> TYPE *
00154 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance (void)
00155 {
00156 ACE_TRACE ("ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance");
00157
00158 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *&singleton =
00159 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i ();
00160
00161
00162 if (singleton == 0)
00163 {
00164 if (ACE_Object_Manager::starting_up () ||
00165 ACE_Object_Manager::shutting_down ())
00166 {
00167
00168
00169
00170
00171
00172
00173
00174 ACE_NEW_RETURN (singleton, (ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>),
00175 0);
00176 }
00177 else
00178 {
00179 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00180
00181
00182
00183 static ACE_LOCK *lock = 0;
00184 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00185
00186 return 0;
00187
00188 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00189 #endif
00190
00191 if (singleton == 0)
00192 ACE_NEW_RETURN (singleton,
00193 (ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>),
00194 0);
00195 }
00196 }
00197
00198 return &singleton->instance_;
00199 }
00200
00201 template <class TYPE, class ACE_LOCK> void
00202 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::close (void)
00203 {
00204 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *&singleton =
00205 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i ();
00206
00207 if (singleton)
00208 {
00209 singleton->cleanup ();
00210 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00211 }
00212 }
00213
00214 template <class TYPE, class ACE_LOCK> void
00215 ACE_TSS_Singleton<TYPE, ACE_LOCK>::dump (void)
00216 {
00217 #if defined (ACE_HAS_DUMP)
00218 ACE_TRACE ("ACE_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00219
00220 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00221 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00222 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00223 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00224 #endif
00225 #endif
00226 }
00227
00228 template <class TYPE, class ACE_LOCK> ACE_TSS_Singleton<TYPE, ACE_LOCK> *&
00229 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00230 {
00231 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00232
00233
00234 static ACE_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00235
00236 return singleton_;
00237 #else
00238 return ACE_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00239 #endif
00240 }
00241
00242 template <class TYPE, class ACE_LOCK> TYPE *
00243 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance (void)
00244 {
00245 ACE_TRACE ("ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00246
00247 ACE_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00248 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00249
00250
00251 if (singleton == 0)
00252 {
00253 if (ACE_Object_Manager::starting_up () ||
00254 ACE_Object_Manager::shutting_down ())
00255 {
00256
00257
00258
00259
00260
00261
00262
00263 ACE_NEW_RETURN (singleton, (ACE_TSS_Singleton<TYPE, ACE_LOCK>), 0);
00264 }
00265 else
00266 {
00267 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00268
00269
00270
00271 static ACE_LOCK *lock = 0;
00272 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00273
00274 return 0;
00275
00276 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00277
00278 if (singleton == 0)
00279 {
00280 #endif
00281 ACE_NEW_RETURN (singleton, (ACE_TSS_Singleton<TYPE, ACE_LOCK>),
00282 0);
00283
00284
00285 ACE_Object_Manager::at_exit (singleton);
00286 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00287 }
00288 #endif
00289 }
00290 }
00291
00292 return ACE_TSS_GET (&singleton->instance_, TYPE);
00293 }
00294
00295 template <class TYPE, class ACE_LOCK> void
00296 ACE_TSS_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00297 {
00298 delete this;
00299 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00300 }
00301
00302 template <class TYPE, class ACE_LOCK> void
00303 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::dump (void)
00304 {
00305 #if defined (ACE_HAS_DUMP)
00306 ACE_TRACE ("ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00307
00308 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00309 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00310 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00311 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00312 #endif
00313 #endif
00314 }
00315
00316 template <class TYPE, class ACE_LOCK>
00317 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *&
00318 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00319 {
00320 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00321
00322
00323 static ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00324
00325 return singleton_;
00326 #else
00327 return ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00328 #endif
00329 }
00330
00331 template <class TYPE, class ACE_LOCK> TYPE *
00332 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance (void)
00333 {
00334 ACE_TRACE ("ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00335
00336 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00337 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00338
00339
00340 if (singleton == 0)
00341 {
00342 if (ACE_Object_Manager::starting_up () ||
00343 ACE_Object_Manager::shutting_down ())
00344 {
00345
00346
00347
00348
00349
00350
00351
00352 ACE_NEW_RETURN (singleton,
00353 (ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>),
00354 0);
00355 }
00356 else
00357 {
00358 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00359
00360
00361
00362 static ACE_LOCK *lock = 0;
00363 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00364
00365 return 0;
00366
00367 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00368 #endif
00369
00370 if (singleton == 0)
00371 ACE_NEW_RETURN (singleton,
00372 (ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>),
00373 0);
00374 }
00375 }
00376
00377 return ACE_TSS_GET (&singleton->instance_, TYPE);
00378 }
00379
00380 template <class TYPE, class ACE_LOCK> void
00381 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::close (void)
00382 {
00383 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00384 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00385
00386 if (singleton)
00387 singleton->cleanup ();
00388 }
00389
00390 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00391
00392 template <class TYPE, class ACE_LOCK> ACE_TSS_Singleton <TYPE, ACE_LOCK> *
00393 ACE_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00394
00395 template <class TYPE, class ACE_LOCK>
00396 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *
00397 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00398 #endif
00399
00400
00401
00402 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00403
00404 template <class TYPE, class ACE_LOCK> ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *
00405 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::singleton_ = 0;
00406 #endif
00407
00408 template <class TYPE, class ACE_LOCK> void
00409 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::dump (void)
00410 {
00411 #if defined (ACE_HAS_DUMP)
00412 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::dump");
00413
00414 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00415 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00416 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i ()));
00417 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00418 #endif
00419 #endif
00420 }
00421
00422 template <class TYPE, class ACE_LOCK>
00423 ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *&
00424 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i (void)
00425 {
00426 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i");
00427
00428 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00429
00430
00431 static ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *singleton_ = 0;
00432
00433 return singleton_;
00434 #else
00435 return ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::singleton_;
00436 #endif
00437 }
00438
00439 template <class TYPE, class ACE_LOCK> TYPE *
00440 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance (void)
00441 {
00442 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance");
00443
00444 ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *&singleton =
00445 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i ();
00446
00447
00448 if (singleton == 0)
00449 {
00450 if (ACE_Object_Manager::starting_up () ||
00451 ACE_Object_Manager::shutting_down ())
00452 {
00453
00454
00455
00456
00457
00458
00459
00460 ACE_NEW_RETURN (singleton, (ACE_DLL_Singleton_T<TYPE, ACE_LOCK>),
00461 0);
00462 }
00463 else
00464 {
00465 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00466
00467
00468
00469 static ACE_LOCK *lock = 0;
00470 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00471
00472 return 0;
00473
00474 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00475 #endif
00476
00477 if (singleton == 0)
00478 ACE_NEW_RETURN (singleton,
00479 (ACE_DLL_Singleton_T<TYPE, ACE_LOCK>),
00480 0);
00481 }
00482
00483 ACE_Framework_Repository::instance ()->register_component
00484 (new ACE_Framework_Component_T<ACE_DLL_Singleton_T<TYPE, ACE_LOCK> > (singleton));
00485 }
00486
00487 return &singleton->instance_;
00488 }
00489
00490 template <class TYPE, class ACE_LOCK> void
00491 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close (void)
00492 {
00493 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close");
00494
00495 ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *&singleton =
00496 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i ();
00497
00498 delete singleton;
00499 singleton = 0;
00500 }
00501
00502 template <class TYPE, class ACE_LOCK> void
00503 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close_singleton (void)
00504 {
00505 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close_singleton");
00506 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close ();
00507 }
00508
00509 template <class TYPE, class ACE_LOCK> const ACE_TCHAR *
00510 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::dll_name (void)
00511 {
00512 return this->instance ()->dll_name ();
00513 }
00514
00515 template <class TYPE, class ACE_LOCK> const ACE_TCHAR *
00516 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::name (void)
00517 {
00518 return this->instance ()->name ();
00519 }
00520
00521
00522
00523
00524 template <class TYPE> const ACE_TCHAR*
00525 ACE_DLL_Singleton_Adapter_T<TYPE>::dll_name (void)
00526 {
00527
00528
00529 return ACE_TEXT("ACE");
00530 }
00531
00532 ACE_END_VERSIONED_NAMESPACE_DECL
00533
00534 #endif