找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 6844|回复: 1

[原]ACE_Singleton类学习

[复制链接]
发表于 2012-3-5 22:42:12 | 显示全部楼层 |阅读模式
先看一下源代码。
  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.   /// Cleanup method, used by <ace_cleanup_destroyer> to destroy the
  8.   /// ACE_Singleton.
  9.   virtual void cleanup (void *param = 0);
  10.   /// Dump the state of the object.
  11.   static void dump (void);
  12. protected:
  13.   /// Default constructor.
  14.   ACE_Singleton (void);
  15.   /// Contained instance.
  16.   TYPE instance_;
  17. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
  18.   /// Pointer to the Singleton (ACE_Cleanup) instance.
  19.   static ACE_Singleton<TYPE, ACE_LOCK> *singleton_;
  20. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
  21.   /// Get pointer to the Singleton instance.
  22.   static ACE_Singleton<TYPE, ACE_LOCK> *&instance_i (void);
  23. };
复制代码
实现文件:
  1. // Singleton.cpp,v 4.54 2005/10/28 16:14:55 ossama Exp
  2. #ifndef ACE_SINGLETON_CPP
  3. #define ACE_SINGLETON_CPP
  4. #include "ace/Singleton.h"
  5. #if !defined (ACE_LACKS_PRAGMA_ONCE)
  6. # pragma once
  7. #endif /* ACE_LACKS_PRAGMA_ONCE */
  8. #if !defined (__ACE_INLINE__)
  9. #include "ace/Singleton.inl"
  10. #endif /* __ACE_INLINE__ */
  11. #include "ace/Object_Manager.h"
  12. #include "ace/Log_Msg.h"
  13. #include "ace/Framework_Component.h"
  14. #include "ace/Guard_T.h"
  15. ACE_RCSID (ace,
  16.            Singleton,
  17.            "Singleton.cpp,v 4.54 2005/10/28 16:14:55 ossama Exp")
  18. ACE_BEGIN_VERSIONED_NAMESPACE_DECL
  19. template <class TYPE, class ACE_LOCK> void
  20. ACE_Singleton<TYPE, ACE_LOCK>::dump (void)
  21. {
  22. #if defined (ACE_HAS_DUMP)
  23.   ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::dump");
  24. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
  25.   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("instance_ = %x"),
  26.               ACE_Singleton<TYPE, ACE_LOCK>::instance_i ()));
  27.   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
  28. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
  29. #endif /* ACE_HAS_DUMP */
  30. }
  31. template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *&
  32. ACE_Singleton<TYPE, ACE_LOCK>::instance_i (void)
  33. {
  34. #if defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
  35.   // Pointer to the Singleton instance.  This works around a bug with
  36.   // G++ and it's (mis-)handling of templates and statics...
  37.   static ACE_Singleton<TYPE, ACE_LOCK> *singleton_ = 0;
  38.   return singleton_;
  39. #else
  40.   return ACE_Singleton<TYPE, ACE_LOCK>::singleton_;
  41. #endif /* ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES */
  42. }
  43. template <class TYPE, class ACE_LOCK> TYPE *
  44. ACE_Singleton<TYPE, ACE_LOCK>::instance (void)
  45. {
  46.   ACE_TRACE ("ACE_Singleton<TYPE, ACE_LOCK>::instance");
  47.   ACE_Singleton<TYPE, ACE_LOCK> *&singleton =
  48.     ACE_Singleton<TYPE, ACE_LOCK>::instance_i ();
  49.   // Perform the Double-Check pattern...
  50.   if (singleton == 0)
  51.     {
  52.       if (ACE_Object_Manager::starting_up () ||
  53.           ACE_Object_Manager::shutting_down ())
  54.         {
  55.           // The program is still starting up, and therefore assumed
  56.           // to be single threaded.  There's no need to double-check.
  57.           // Or, the ACE_Object_Manager instance has been destroyed,
  58.           // so the preallocated lock is not available.  Either way,
  59.           // don't register for destruction with the
  60.           // ACE_Object_Manager:  we'll have to leak this instance.
  61.           ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
  62.         }
  63.       else
  64.         {
  65. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  66.           // Obtain a lock from the ACE_Object_Manager.  The pointer
  67.           // is static, so we only obtain one per ACE_Singleton
  68.           // instantiation.
  69.           static ACE_LOCK *lock = 0;
  70.           if (ACE_Object_Manager::get_singleton_lock (lock) != 0)
  71.             // Failed to acquire the lock!
  72.             return 0;
  73.           ACE_GUARD_RETURN (ACE_LOCK, ace_mon, *lock, 0);
  74.           if (singleton == 0)
  75.             {
  76. #endif /* ACE_MT_SAFE */
  77.               ACE_NEW_RETURN (singleton, (ACE_Singleton<TYPE, ACE_LOCK>), 0);
  78.               // Register for destruction with ACE_Object_Manager.
  79.               ACE_Object_Manager::at_exit (singleton);
  80. #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
  81.             }
  82. #endif /* ACE_MT_SAFE */
  83.         }
  84.     }
  85.   return &singleton->instance_;
  86. }
  87. template <class TYPE, class ACE_LOCK> void
  88. ACE_Singleton<TYPE, ACE_LOCK>::cleanup (void *)
  89. {
  90.   delete this;
  91.   ACE_Singleton<TYPE, ACE_LOCK>::instance_i () = 0;
  92. }
  93. #if !defined (ACE_LACKS_STATIC_DATA_MEMBER_TEMPLATES)
  94. // Pointer to the Singleton instance.
  95. template <class TYPE, class ACE_LOCK> ACE_Singleton<TYPE, ACE_LOCK> *
  96. 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经过自己的方法和类已经很好的解决了。
总之,不管他如何实现,我们完全就是直接简单的使用。
typedef ACE_Singleton<Class, ACE_Null_Mutex> instance;
更多文章,欢迎访问:http://blog.csdn.net/wallwind


发表于 2013-11-27 11:12:38 | 显示全部楼层
ACE_Singleton对象的实例化和删除都处在你的程序、而不是平台的运行时环境的控制之下。和Boost的Singleton有所区别。:)
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-22 16:21 , Processed in 0.034874 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表