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 #include "ace/os_include/os_typeinfo.h"
00021
00022 ACE_RCSID (ace,
00023 Singleton,
00024 "$Id: Singleton.cpp 84273 2009-01-30 12:55:25Z johnnyw $")
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, 0, typeid (TYPE).name ());
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 ACE_Object_Manager::remove_at_exit (this);
00112 delete this;
00113 ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00114 }
00115
00116 template <class TYPE, class ACE_LOCK> void
00117 ACE_Singleton<TYPE, ACE_LOCK>::close (void)
00118 {
00119 ACE_Singleton<TYPE, ACE_LOCK> *&singleton =
00120 ACE_Singleton<TYPE, ACE_LOCK>::instance_i ();
00121
00122 if (singleton)
00123 {
00124 singleton->cleanup ();
00125 ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00126 }
00127 }
00128
00129 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00130
00131 template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *
00132 ACE_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00133
00134 template <class TYPE, class ACE_LOCK> ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *
00135 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00136 #endif
00137
00138 template <class TYPE, class ACE_LOCK> void
00139 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::dump (void)
00140 {
00141 #if defined (ACE_HAS_DUMP)
00142 ACE_TRACE ("ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::dump");
00143
00144 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00145 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00146 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00147 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00148 #endif
00149 #endif
00150 }
00151
00152 template <class TYPE, class ACE_LOCK>
00153 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *&
00154 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00155 {
00156 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00157
00158
00159 static ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00160
00161 return singleton_;
00162 #else
00163 return ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::singleton_;
00164 #endif
00165 }
00166
00167 template <class TYPE, class ACE_LOCK> TYPE *
00168 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance (void)
00169 {
00170 ACE_TRACE ("ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance");
00171
00172 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *&singleton =
00173 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i ();
00174
00175
00176 if (singleton == 0)
00177 {
00178 if (ACE_Object_Manager::starting_up () ||
00179 ACE_Object_Manager::shutting_down ())
00180 {
00181
00182
00183
00184
00185
00186
00187
00188 ACE_NEW_RETURN (singleton, (ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>),
00189 0);
00190 }
00191 else
00192 {
00193 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00194
00195
00196
00197 static ACE_LOCK *lock = 0;
00198 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00199
00200 return 0;
00201
00202 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00203 #endif
00204
00205 if (singleton == 0)
00206 ACE_NEW_RETURN (singleton,
00207 (ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>),
00208 0);
00209 }
00210 }
00211
00212 return &singleton->instance_;
00213 }
00214
00215 template <class TYPE, class ACE_LOCK> void
00216 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::close (void)
00217 {
00218 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK> *&singleton =
00219 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i ();
00220
00221 if (singleton)
00222 {
00223 singleton->cleanup ();
00224 ACE_Unmanaged_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00225 }
00226 }
00227
00228 template <class TYPE, class ACE_LOCK> void
00229 ACE_TSS_Singleton<TYPE, ACE_LOCK>::dump (void)
00230 {
00231 #if defined (ACE_HAS_DUMP)
00232 ACE_TRACE ("ACE_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00233
00234 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00235 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00236 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00237 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00238 #endif
00239 #endif
00240 }
00241
00242 template <class TYPE, class ACE_LOCK> ACE_TSS_Singleton<TYPE, ACE_LOCK> *&
00243 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00244 {
00245 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00246
00247
00248 static ACE_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00249
00250 return singleton_;
00251 #else
00252 return ACE_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00253 #endif
00254 }
00255
00256 template <class TYPE, class ACE_LOCK> TYPE *
00257 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance (void)
00258 {
00259 ACE_TRACE ("ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00260
00261 ACE_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00262 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00263
00264
00265 if (singleton == 0)
00266 {
00267 if (ACE_Object_Manager::starting_up () ||
00268 ACE_Object_Manager::shutting_down ())
00269 {
00270
00271
00272
00273
00274
00275
00276
00277 ACE_NEW_RETURN (singleton, (ACE_TSS_Singleton<TYPE, ACE_LOCK>), 0);
00278 }
00279 else
00280 {
00281 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00282
00283
00284
00285 static ACE_LOCK *lock = 0;
00286 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00287
00288 return 0;
00289
00290 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00291
00292 if (singleton == 0)
00293 {
00294 #endif
00295 ACE_NEW_RETURN (singleton, (ACE_TSS_Singleton<TYPE, ACE_LOCK>),
00296 0);
00297
00298
00299 ACE_Object_Manager::at_exit (singleton, 0, typeid (TYPE).name ());
00300 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00301 }
00302 #endif
00303 }
00304 }
00305
00306 return ACE_TSS_GET (&singleton->instance_, TYPE);
00307 }
00308
00309 template <class TYPE, class ACE_LOCK> void
00310 ACE_TSS_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
00311 {
00312 delete this;
00313 ACE_TSS_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00314 }
00315
00316 template <class TYPE, class ACE_LOCK> void
00317 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::dump (void)
00318 {
00319 #if defined (ACE_HAS_DUMP)
00320 ACE_TRACE ("ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::dump");
00321
00322 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00323 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00324 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00325 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00326 #endif
00327 #endif
00328 }
00329
00330 template <class TYPE, class ACE_LOCK>
00331 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *&
00332 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i (void)
00333 {
00334 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00335
00336
00337 static ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00338
00339 return singleton_;
00340 #else
00341 return ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::singleton_;
00342 #endif
00343 }
00344
00345 template <class TYPE, class ACE_LOCK> TYPE *
00346 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance (void)
00347 {
00348 ACE_TRACE ("ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance");
00349
00350 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00351 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00352
00353
00354 if (singleton == 0)
00355 {
00356 if (ACE_Object_Manager::starting_up () ||
00357 ACE_Object_Manager::shutting_down ())
00358 {
00359
00360
00361
00362
00363
00364
00365
00366 ACE_NEW_RETURN (singleton,
00367 (ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>),
00368 0);
00369 }
00370 else
00371 {
00372 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00373
00374
00375
00376 static ACE_LOCK *lock = 0;
00377 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00378
00379 return 0;
00380
00381 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00382 #endif
00383
00384 if (singleton == 0)
00385 ACE_NEW_RETURN (singleton,
00386 (ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>),
00387 0);
00388 }
00389 }
00390
00391 return ACE_TSS_GET (&singleton->instance_, TYPE);
00392 }
00393
00394 template <class TYPE, class ACE_LOCK> void
00395 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::close (void)
00396 {
00397 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *&singleton =
00398 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::instance_i ();
00399
00400 if (singleton)
00401 singleton->cleanup ();
00402 }
00403
00404 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00405
00406 template <class TYPE, class ACE_LOCK> ACE_TSS_Singleton <TYPE, ACE_LOCK> *
00407 ACE_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00408
00409 template <class TYPE, class ACE_LOCK>
00410 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK> *
00411 ACE_Unmanaged_TSS_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;
00412 #endif
00413
00414
00415
00416 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00417
00418 template <class TYPE, class ACE_LOCK> ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *
00419 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::singleton_ = 0;
00420 #endif
00421
00422 template <class TYPE, class ACE_LOCK> void
00423 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::dump (void)
00424 {
00425 #if defined (ACE_HAS_DUMP)
00426 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::dump");
00427
00428 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00429 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("instance_ = %x"),
00430 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i ()));
00431 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00432 #endif
00433 #endif
00434 }
00435
00436 template <class TYPE, class ACE_LOCK>
00437 ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *&
00438 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i (void)
00439 {
00440 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i");
00441
00442 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00443
00444
00445 static ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *singleton_ = 0;
00446
00447 return singleton_;
00448 #else
00449 return ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::singleton_;
00450 #endif
00451 }
00452
00453 template <class TYPE, class ACE_LOCK> TYPE *
00454 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance (void)
00455 {
00456 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance");
00457
00458 ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *&singleton =
00459 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i ();
00460
00461
00462 if (singleton == 0)
00463 {
00464 if (ACE_Object_Manager::starting_up () ||
00465 ACE_Object_Manager::shutting_down ())
00466 {
00467
00468
00469
00470
00471
00472
00473
00474 ACE_NEW_RETURN (singleton, (ACE_DLL_Singleton_T<TYPE, ACE_LOCK>),
00475 0);
00476 }
00477 else
00478 {
00479 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00480
00481
00482
00483 static ACE_LOCK *lock = 0;
00484 if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00485
00486 return 0;
00487
00488 ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00489 #endif
00490
00491 if (singleton == 0)
00492 ACE_NEW_RETURN (singleton,
00493 (ACE_DLL_Singleton_T<TYPE, ACE_LOCK>),
00494 0);
00495 }
00496
00497 ACE_Framework_Repository::instance ()->register_component
00498 (new ACE_Framework_Component_T<ACE_DLL_Singleton_T<TYPE, ACE_LOCK> > (singleton));
00499 }
00500
00501 return &singleton->instance_;
00502 }
00503
00504 template <class TYPE, class ACE_LOCK> void
00505 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close (void)
00506 {
00507 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close");
00508
00509 ACE_DLL_Singleton_T<TYPE, ACE_LOCK> *&singleton =
00510 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::instance_i ();
00511
00512 delete singleton;
00513 singleton = 0;
00514 }
00515
00516 template <class TYPE, class ACE_LOCK> void
00517 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close_singleton (void)
00518 {
00519 ACE_TRACE ("ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close_singleton");
00520 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::close ();
00521 }
00522
00523 template <class TYPE, class ACE_LOCK> const ACE_TCHAR *
00524 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::dll_name (void)
00525 {
00526 return this->instance ()->dll_name ();
00527 }
00528
00529 template <class TYPE, class ACE_LOCK> const ACE_TCHAR *
00530 ACE_DLL_Singleton_T<TYPE, ACE_LOCK>::name (void)
00531 {
00532 return this->instance ()->name ();
00533 }
00534
00535
00536
00537
00538 template <class TYPE> const ACE_TCHAR*
00539 ACE_DLL_Singleton_Adapter_T<TYPE>::dll_name (void)
00540 {
00541
00542
00543 return ACE_TEXT("ACE");
00544 }
00545
00546 ACE_END_VERSIONED_NAMESPACE_DECL
00547
00548 #endif