Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Singleton Class Template Reference

A Singleton Adapter uses the Adapter pattern to turn ordinary classes into Singletons optimized with the Double-Checked Locking optimization pattern. More...

#include <Singleton.h>

Inheritance diagram for ACE_Singleton:

Inheritance graph
[legend]
Collaboration diagram for ACE_Singleton:

Collaboration graph
[legend]
List of all members.

Public Methods

virtual void cleanup (void *param=0)
 Cleanup method, used by <ace_cleanup_destroyer> to destroy the ACE_Singleton. More...


Static Public Methods

TYPE * instance (void)
 Global access point to the Singleton. More...

void dump (void)
 Dump the state of the object. More...


Protected Methods

 ACE_Singleton (void)
 Default constructor. More...


Static Protected Methods

ACE_Singleton< TYPE, ACE_LOCK > *& instance_i (void)
 Get pointer to the Singleton instance. More...


Protected Attributes

TYPE instance_
 Contained instance. More...


Static Protected Attributes

ACE_Singleton< TYPE, ACE_LOCK > * singleton_ = 0
 Pointer to the Singleton (ACE_Cleanup) instance. More...


Detailed Description

template<class TYPE, class ACE_LOCK>
class ACE_Singleton< TYPE, ACE_LOCK >

A Singleton Adapter uses the Adapter pattern to turn ordinary classes into Singletons optimized with the Double-Checked Locking optimization pattern.

This implementation is a slight variation on the GoF Singleton pattern. In particular, a single <ACE_Singleton<TYPE, ACE_LOCK> > instance is allocated here, not a <TYPE> instance. The reason for this is to allow registration with the ACE_Object_Manager, so that the Singleton can be cleaned up when the process exits. For this scheme to work, a (static) cleanup() function must be provided. ACE_Singleton provides one so that TYPE doesn't need to. If you want to make sure that only the singleton instance of <T> is created, and that users cannot create their own instances of <T>, do the following to class <T>: (a) Make the constructor of <T> private (or protected) (b) Make Singleton a friend of <T> Here is an example:

 * class foo
 * {
 * friend class ACE_Singleton<foo, ACE_Null_Mutex>;
 * private:
 * foo () { cout << "foo constructed" << endl; }
 * ~foo () { cout << "foo destroyed" << endl; }
 * };
 * typedef ACE_Singleton<foo, ACE_Null_Mutex> FOO;
 * 

NOTE: the best types to use for ACE_LOCK are ACE_Recursive_Thread_Mutex and ACE_Null_Mutex. ACE_Recursive_Thread_Mutex should be used in multi-threaded programs in which it is possible for more than one thread to access the <ACE_Singleton<TYPE, ACE_LOCK>> instance. ACE_Null_Mutex can be used otherwise. The reason that these types of locks are best has to do with their allocation by the ACE_Object_Manager. Single ACE_Recursive_Thread_Mutex and ACE_Null_Mutex instances are used for all ACE_Singleton instantiations. However, other types of locks are allocated per ACE_Singleton instantiation.

Definition at line 76 of file Singleton.h.


Constructor & Destructor Documentation

template<class TYPE, class ACE_LOCK>
ACE_INLINE ACE_Singleton< TYPE, ACE_LOCK >::ACE_Singleton void    [protected]
 

Default constructor.

Definition at line 10 of file Singleton.i.

00011 {
00012 }


Member Function Documentation

template<class TYPE, class ACE_LOCK>
void ACE_Singleton< TYPE, ACE_LOCK >::cleanup void *    param = 0 [virtual]
 

Cleanup method, used by <ace_cleanup_destroyer> to destroy the ACE_Singleton.

Reimplemented from ACE_Cleanup.

Definition at line 104 of file Singleton.cpp.

References instance_i.

Referenced by ACE_Unmanaged_Singleton::close.

00105 {
00106   delete this;
00107   ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
00108 }

template<class TYPE, class ACE_LOCK>
void ACE_Singleton< TYPE, ACE_LOCK >::dump void    [static]
 

Dump the state of the object.

Reimplemented in ACE_Unmanaged_Singleton.

Definition at line 26 of file Singleton.cpp.

References ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_TRACE, instance_i, and LM_DEBUG.

00027 {
00028   ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::dump");
00029 
00030 #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00031   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("instance_ = %x"),
00032               ACE_Singleton<TYPE, ACE_LOCK>::instance_i ()));
00033   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00034 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00035 }

template<class TYPE, class ACE_LOCK>
TYPE * ACE_Singleton< TYPE, ACE_LOCK >::instance void    [static]
 

Global access point to the Singleton.

Reimplemented in ACE_Unmanaged_Singleton.

Definition at line 52 of file Singleton.cpp.

References ACE_GUARD_RETURN, ACE_NEW_RETURN, ACE_TRACE, ACE_Object_Manager::at_exit, instance_, instance_i, ACE_Object_Manager::shutting_down, and ACE_Object_Manager::starting_up.

Referenced by ACE_Based_Pointer_Basic::ACE_Based_Pointer_Basic, ACE_Remote_Token_Proxy::initiate_connection, ACE_Pagefile_Memory_Pool::map, ACE_MMAP_Memory_Pool::map_file, ACE_MMAP_Memory_Pool::release, ACE_Remote_Token_Proxy::request_reply, ACE_Remote_Token_Proxy::set_server_address, and ACE_Pagefile_Memory_Pool::unmap.

00053 {
00054   ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::instance");
00055 
00056   ACE_Singleton<TYPE, ACE_LOCK> *&singleton =
00057     ACE_Singleton<TYPE, ACE_LOCK>::instance_i ();
00058 
00059   // Perform the Double-Check pattern...
00060   if (singleton == 0)
00061     {
00062       if (ACE_Object_Manager::starting_up () ||
00063           ACE_Object_Manager::shutting_down ())
00064         {
00065           // The program is still starting up, and therefore assumed
00066           // to be single threaded.  There's no need to double-check.
00067           // Or, the ACE_Object_Manager instance has been destroyed,
00068           // so the preallocated lock is not available.  Either way,
00069           // don't register for destruction with the
00070           // ACE_Object_Manager:  we'll have to leak this instance.
00071 
00072           ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
00073         }
00074       else
00075         {
00076 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00077           // Obtain a lock from the ACE_Object_Manager.  The pointer
00078           // is static, so we only obtain one per ACE_Singleton
00079           // instantiation.
00080           static ACE_LOCK *lock = 0;
00081           if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
00082             // Failed to acquire the lock!
00083             return 0;
00084 
00085           ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
00086 
00087           if (singleton == 0)
00088             {
00089 #endif /* ACE_MT_SAFE */
00090               ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
00091 
00092               // Register for destruction with ACE_Object_Manager.
00093               ACE_Object_Manager::at_exit (singleton);
00094 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00095             }
00096 #endif /* ACE_MT_SAFE */
00097         }
00098     }
00099 
00100   return &singleton->instance_;
00101 }

template<class TYPE, class ACE_LOCK>
ACE_Singleton< TYPE, ACE_LOCK > *& ACE_Singleton< TYPE, ACE_LOCK >::instance_i void    [static, protected]
 

Get pointer to the Singleton instance.

Reimplemented in ACE_Unmanaged_Singleton.

Definition at line 38 of file Singleton.cpp.

References singleton_.

Referenced by cleanup, dump, and instance.

00039 {
00040 #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
00041   // Pointer to the Singleton instance.  This works around a bug with
00042   // G++ and it's (mis-)handling of templates and statics...
00043   static ACE_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
00044 
00045   return singleton_;
00046 #else
00047   return ACE_Singleton<TYPE, ACE_LOCK>::singleton_;
00048 #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
00049 }


Member Data Documentation

template<class TYPE, class ACE_LOCK>
TYPE ACE_Singleton::instance_ [protected]
 

Contained instance.

Definition at line 94 of file Singleton.h.

Referenced by ACE_Unmanaged_Singleton::instance, and instance.

template<class TYPE, class ACE_LOCK>
ACE_Singleton< TYPE, ACE_LOCK > * ACE_Singleton< TYPE, ACE_LOCK >::singleton_ = 0 [static, protected]
 

Pointer to the Singleton (ACE_Cleanup) instance.

Reimplemented in ACE_Unmanaged_Singleton.

Definition at line 113 of file Singleton.cpp.

Referenced by instance_i.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:56:12 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002