ACE_Singleton类学习

[cpp]  view plain  copy
  1. 先看一下源代码。  
[cpp]  view plain  copy
  1. template <class TYPE, class ACE_LOCK>  
  2. class ACE_Singleton : public ACE_Cleanup  
  3. {  
  4. public:  
  5.   /// Global access point to the Singleton.  
  6.   static TYPE *instance (void);  
  7.   
  8.   /// Cleanup method, used by <ace_cleanup_destroyer> to destroy the  
  9.   /// ACE_Singleton.  
  10.   virtual void cleanup (void *param = 0);  
  11.   
  12.   /// Dump the state of the object.  
  13.   static void dump (void);  
  14.   
  15. protected:  
  16.   /// Default constructor.  
  17.   ACE_Singleton (void);  
  18.   
  19.   /// Contained instance.  
  20.   TYPE instance_;  
  21.   
  22. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)  
  23.   /// Pointer to the Singleton (ACE_Cleanup) instance.  
  24.   static ACE_Singleton<TYPE, ACE_LOCK> *singleton_;  
  25. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */  
  26.   
  27.   /// Get pointer to the Singleton instance.  
  28.   static ACE_Singleton<TYPE, ACE_LOCK> *&instance_i (void);  
  29. };  

实现文件:

[cpp]  view plain  copy
  1. // Singleton.cpp,v 4.54 2005/10/28 16:14:55 ossama Exp  
  2.   
  3. #ifndef ACE_SINGLETON_CPP  
  4. #define ACE_SINGLETON_CPP  
  5.   
  6. #include "ace/Singleton.h"  
  7.   
  8. #if !defined (ACE_LACKS_PRAGMA_ONCE)  
  9. # pragma once  
  10. #endif /* ACE_LACKS_PRAGMA_ONCE */  
  11.   
  12. #if !defined (__ACE_INLINE__)  
  13. #include "ace/Singleton.inl"  
  14. #endif /* __ACE_INLINE__ */  
  15.   
  16. #include "ace/Object_Manager.h"  
  17. #include "ace/Log_Msg.h"  
  18. #include "ace/Framework_Component.h"  
  19. #include "ace/Guard_T.h"  
  20.   
  21. ACE_RCSID (ace,  
  22.            Singleton,  
  23.            "Singleton.cpp,v 4.54 2005/10/28 16:14:55 ossama Exp")  
  24.   
  25.   
  26. ACE_BEGIN_VERSIONED_NAMESPACE_DECL  
  27.   
  28. template <class TYPE, class ACE_LOCK> void  
  29. ACE_Singleton<TYPE, ACE_LOCK>::dump (void)  
  30. {  
  31. #if defined (ACE_HAS_DUMP)  
  32.   ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::dump");  
  33.   
  34. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)  
  35.   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("instance_ = %x"),  
  36.               ACE_Singleton<TYPE, ACE_LOCK>::instance_i ()));  
  37.   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));  
  38. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */  
  39. #endif /* ACE_HAS_DUMP */  
  40. }  
  41.   
  42. template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *&  
  43. ACE_Singleton<TYPE, ACE_LOCK>::instance_i (void)  
  44. {  
  45. #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)  
  46.   // Pointer to the Singleton instance.  This works around a bug with  
  47.   // G++ and it's (mis-)handling of templates and statics...  
  48.   static ACE_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;  
  49.   
  50.   return singleton_;  
  51. #else  
  52.   return ACE_Singleton<TYPE, ACE_LOCK>::singleton_;  
  53. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */  
  54. }  
  55.   
  56. template <class TYPE, class ACE_LOCK> TYPE *  
  57. ACE_Singleton<TYPE, ACE_LOCK>::instance (void)  
  58. {  
  59.   ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::instance");  
  60.   
  61.   ACE_Singleton<TYPE, ACE_LOCK> *&singleton =  
  62.     ACE_Singleton<TYPE, ACE_LOCK>::instance_i ();  
  63.   
  64.   // Perform the Double-Check pattern...  
  65.   if (singleton == 0)  
  66.     {  
  67.       if (ACE_Object_Manager::starting_up () ||  
  68.           ACE_Object_Manager::shutting_down ())  
  69.         {  
  70.           // The program is still starting up, and therefore assumed  
  71.           // to be single threaded.  There's no need to double-check.  
  72.           // Or, the ACE_Object_Manager instance has been destroyed,  
  73.           // so the preallocated lock is not available.  Either way,  
  74.           // don't register for destruction with the  
  75.           // ACE_Object_Manager:  we'll have to leak this instance.  
  76.   
  77.           ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);  
  78.         }  
  79.       else  
  80.         {  
  81. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)  
  82.           // Obtain a lock from the ACE_Object_Manager.  The pointer  
  83.           // is static, so we only obtain one per ACE_Singleton  
  84.           // instantiation.  
  85.           static ACE_LOCK *lock = 0;  
  86.           if (ACE_Object_Manager::get_singleton_lock (lock) != 0)  
  87.             // Failed to acquire the lock!  
  88.             return 0;  
  89.   
  90.           ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);  
  91.   
  92.           if (singleton == 0)  
  93.             {  
  94. #endif /* ACE_MT_SAFE */  
  95.               ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);  
  96.   
  97.               // Register for destruction with ACE_Object_Manager.  
  98.               ACE_Object_Manager::at_exit (singleton);  
  99. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)  
  100.             }  
  101. #endif /* ACE_MT_SAFE */  
  102.         }  
  103.     }  
  104.   
  105.   return &singleton->instance_;  
  106. }  
  107.   
  108. template <class TYPE, class ACE_LOCK> void  
  109. ACE_Singleton<TYPE, ACE_LOCK>::cleanup (void *)  
  110. {  
  111.   delete this;  
  112.   ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;  
  113. }  
  114.   
  115. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)  
  116. // Pointer to the Singleton instance.  
  117. template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *  
  118. ACE_Singleton<TYPE, ACE_LOCK>::singleton_ = 0;  

 


class ACE_Singleton : public ACE_Cleanup

 继承了ACE_Cleanup,这个类自动完成了对对象的清理工作。就是实现了

template <class TYPE, class ACE_LOCK> voidACE_Singleton<TYPE, ACE_LOCK>::cleanup (void *){  delete this;  ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;}

还有就是对程序的使用了ACE_Object_Manager 释放程序的实例。

ACE_Object_Manager::at_exit (singleton);

因此当你使用ACE_Singleton 大可以放心的使用,对象管理ACE经过自己的方法和类已经很好的解决了。

总之,不管他如何实现,我们完全就是直接简单的使用。

[cpp]  view plain  copy
  1. typedef ACE_Singleton<Class, ACE_Null_Mutex> instance;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值