diff options
author | levine <levine@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-09-22 18:15:37 +0000 |
---|---|---|
committer | levine <levine@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1997-09-22 18:15:37 +0000 |
commit | eda79b335ab42b4273c2c1f193d66f22d264ba6b (patch) | |
tree | 26231935ecf2dbfb6ebb12c6f5b7213710fd2498 | |
parent | 2af513c4e333e134946d837933fea066a25a4034 (diff) | |
download | ATCD-eda79b335ab42b4273c2c1f193d66f22d264ba6b.tar.gz |
removed instance (TYPE *) member function because it wasn't being used. And, it allows us to store the contained instances_ as an object instead of a pointer, saving a dynamic memory allocation on construction.
-rw-r--r-- | ace/Singleton.cpp | 48 | ||||
-rw-r--r-- | ace/Singleton.h | 23 | ||||
-rw-r--r-- | ace/Singleton.i | 7 |
3 files changed, 25 insertions, 53 deletions
diff --git a/ace/Singleton.cpp b/ace/Singleton.cpp index f1d28f8a4c4..640114918f7 100644 --- a/ace/Singleton.cpp +++ b/ace/Singleton.cpp @@ -20,7 +20,7 @@ ACE_Singleton<TYPE, LOCK>::dump (void) #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) ACE_DEBUG ((LM_DEBUG, "instance_ = %x", - ACE_Singleton<TYPE, LOCK>::instance_i ())); + ACE_Singleton<TYPE, LOCK>::instance_i ())); ACE_Singleton<TYPE, LOCK>::singleton_lock_i ().dump (); ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP)); #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */ @@ -69,40 +69,20 @@ ACE_Singleton<TYPE, LOCK>::instance (void) if (singleton == 0) { - ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, LOCK>), 0); + ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, LOCK>), 0); // Register for destruction with ACE_Object_Manager. ACE_Object_Manager::at_exit (singleton); } } - return singleton->instance_; -} - -template <class TYPE, class LOCK> TYPE * -ACE_Singleton<TYPE, LOCK>::instance (TYPE *new_instance) -{ - ACE_TRACE ("ACE_Singleton::set_instance"); - - ACE_GUARD_RETURN (LOCK, ace_mon, (ACE_Singleton<TYPE, LOCK>::singleton_lock_i ()), 0); - - ACE_Singleton<TYPE, LOCK> *&singleton = ACE_Singleton<TYPE, LOCK>::instance_i (); - TYPE *old_instance = singleton->instance_; - singleton->instance_ = new_instance; - - return old_instance; + return &singleton->instance_; } template <class TYPE, class LOCK> void ACE_Singleton<TYPE, LOCK>::cleanup (void *) { - if (this->instance_ != 0) - { - delete this->instance_; - this->instance_ = 0; - - delete this; - } + delete this; } #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) @@ -121,7 +101,7 @@ ACE_TSS_Singleton<TYPE, LOCK>::dump (void) ACE_TRACE ("ACE_TSS_Singleton<TYPE, LOCK>::dump"); #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) - ACE_DEBUG ((LM_DEBUG, "instance_ = %x", singleton_->instance_)); + ACE_DEBUG ((LM_DEBUG, "instance_ = %x", &singleton_->instance_)); ace_singleton_lock_.dump(); ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP)); #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */ @@ -147,14 +127,14 @@ ACE_TSS_Singleton<TYPE, LOCK>::instance (void) ACE_GUARD_RETURN (LOCK, ace_mon, ace_singleton_lock_, 0); if (singleton_ == 0) - { + { ACE_NEW_RETURN (singleton_, (ACE_TSS_Singleton<TYPE, LOCK>), 0); #if 0 /* ACE_Object_Manager::at_thread_exit () is not implemented yet. */ // Register for destruction with ACE_Object_Manager. ACE_Object_Manager::at_thread_exit (instance_); #endif /* 0 */ - } + } } #else @@ -165,19 +145,19 @@ ACE_TSS_Singleton<TYPE, LOCK>::instance (void) ACE_GUARD_RETURN (LOCK, ace_mon, (ACE_TSS_Singleton<TYPE, LOCK>::ace_singleton_lock_), 0); if (singleton_ == 0) - { + { ACE_NEW_RETURN (singleton_, (ACE_TSS_Singleton<TYPE, LOCK>), 0); #if 0 /* ACE_Object_Manager::at_thread_exit () is not implemented yet. */ // Register for destruction with ACE_Object_Manager. ACE_Object_Manager::at_thread_exit (instance_); #endif /* 0 */ - } + } } #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */ - return ACE_TSS_GET (singleton_->instance_, TYPE); + return ACE_TSS_GET (&singleton_->instance_, TYPE); } template <class TYPE, class LOCK> void @@ -185,13 +165,7 @@ ACE_TSS_Singleton<TYPE, LOCK>::cleanup (void *) { ACE_TRACE ("ACE_TSS_Singleton::cleanup"); - if (this->instance_ != 0) - { - delete this->instance_; - this->instance_ = 0; - - delete this; - } + delete this; } #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) diff --git a/ace/Singleton.h b/ace/Singleton.h index 2cd031e904f..e186923fb26 100644 --- a/ace/Singleton.h +++ b/ace/Singleton.h @@ -5,7 +5,7 @@ // // = LIBRARY // ace -// +// // = FILENAME // Singleton.h // @@ -14,7 +14,7 @@ // = AUTHOR // Tim Harrison (harrison@cs.wustl.edu), Douglas C. Schmidt, Chris // Lahey, Rich Christy, and David Levine. -// +// // ============================================================================ #if !defined (ACE_SINGLETON_H) @@ -27,8 +27,8 @@ class ACE_Singleton : public ACE_Cleanup // = TITLE // A Singleton Adapter the uses the Adapter pattern to turn // ordinary classes into Singletons optimized with the - // Double-Checked Locking optimization pattern. - // + // Double-Checked Locking optimization pattern. + // // = DESCRIPTION // This implementation is a slight variation on the GoF // Singleton pattern. In particular, a single @@ -44,9 +44,6 @@ public: static TYPE *instance (void); // Global access point to the Singleton. - static TYPE *instance (TYPE *); - // Set the Singleton instance. - virtual void cleanup (void *param = 0); // Cleanup method, used by <ace_cleanup_destroyer> to destroy the // <ACE_Singleton>. @@ -58,15 +55,15 @@ protected: ACE_Singleton (void); // Default constructor. - TYPE *instance_; - // Contained instance pointer. + TYPE instance_; + // Contained instance. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) static ACE_Singleton<TYPE, LOCK> *singleton_; // Pointer to the Singleton (ACE_Cleanup) instance. static LOCK ace_singleton_lock_; - // Lock the creation of the singleton. + // Lock the creation of the singleton. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */ static ACE_Singleton<TYPE, LOCK> *&instance_i (void); @@ -110,15 +107,15 @@ protected: ACE_TSS_Singleton (void); // Default constructor. - ACE_TSS<TYPE> *instance_; - // Contained instance pointer. + ACE_TSS<TYPE> instance_; + // Contained instance. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES) static ACE_TSS_Singleton<TYPE, LOCK> *singleton_; // Pointer to the Singleton (ACE_Cleanup) instance. static LOCK ace_singleton_lock_; - // Lock the creation of the singleton. + // Lock the creation of the singleton. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */ }; diff --git a/ace/Singleton.i b/ace/Singleton.i index 4aac1ef0853..d3ac6fe3e33 100644 --- a/ace/Singleton.i +++ b/ace/Singleton.i @@ -3,17 +3,18 @@ // Singleton.i -// Default constructor. +// Default constructors. +// +// Note: don't explicitly initialize "instance_", because TYPE may not +// have a default constructor. Let the compiler figure it out . . . template <class TYPE, class LOCK> ACE_INLINE ACE_Singleton<TYPE, LOCK>::ACE_Singleton (void) - : instance_ (new TYPE) { } template <class TYPE, class LOCK> ACE_INLINE ACE_TSS_Singleton<TYPE, LOCK>::ACE_TSS_Singleton (void) - : instance_ (new ACE_TSS<TYPE>) { } |