00001 #ifndef _SYN_SINGLETON_H_
00002 #define _SYN_SINGLETON_H_
00003
00004 #include <stdlib.h> // for NULL, duh...
00005
00006 namespace syn
00007 {
00008
00015 template< class singleClass >
00016 class Singleton
00017 {
00018 public:
00029 inline static singleClass* instance( void )
00030 {
00031 // WARNING! race condition possibility, creation of static vars
00032 // are not thread safe. This is only an issue when creating
00033 // your first thread, since it uses a singleton thread manager,
00034 // the two threads might both try to call instance at the same time
00035 // which then the creation of the following mutex would not be certain.
00036 //static vpr::Mutex singleton_lock1;
00037 static singleClass* the_instance1 = NULL;
00038
00039 if (NULL == the_instance1)
00040 {
00041 //vpr::Guard<vpr::Mutex> guard( singleton_lock1 );
00042 if (NULL == the_instance1)
00043 { the_instance1 = new singleClass; }
00044 }
00045 return the_instance1;
00046 }
00047
00048 protected:
00053 Singleton()
00054 {
00055 }
00056
00058 virtual ~Singleton()
00059 {
00060 }
00061 };
00062 }; // end of namespace vpr
00063
00064
00065 //
00066 // Non-locking version
00067 //
00068 /*
00069 #define vprSingletonHeader( TYPE ) \
00070 public: \
00071 static TYPE* instance( void ) \
00072 { \
00073 static TYPE* the_instance = NULL; \
00074 if (the_instance == NULL) \
00075 { \
00076 std::cout << "Creating instance of: TYPE" << std::endl; \
00077 the_instance = new TYPE; \
00078 } \
00079 return the_instance; \
00080 }
00081
00082 #define vprSingletonHeaderWithInitFunc( TYPE, INIT_FUNC_NAME ) \
00083 public: \
00084 static TYPE* instance( void ) \
00085 { \
00086 static TYPE* the_instance = NULL; \
00087 if (the_instance == NULL) \
00088 { \
00089 std::cout << "Creating instance of: TYPE" << std::endl; \
00090 the_instance = new TYPE; \
00091 the_instance->INIT_FUNC_NAME(); \
00092 } \
00093 return the_instance; \
00094 }
00095
00096 #define vprSingletonImp( TYPE ) ;
00097 */
00098
00099 #endif
1.2.8.1 written by Dimitri van Heesch,
© 1997-2001