贴上前些天无聊写的一份智能指针代码: )

  1. #ifndef __smart_ptr_h__
  2. #define __smart_ptr_h__
  3. //
  4. // @author: jasonshg@gmail.com
  5. //
  6. // @date: 2008.08.05
  7. //
  8. // @compiler: ms vc7.1 / dev cpp5.2
  9. //
  10. // @description:
  11. //  智能指针,采用引用计数管理生存周期,支持拷贝操作,可用于stl容器中.
  12. //  并在模拟指针语法时.封装了线程锁的操作.
  13. //
  14. #ifndef USE_INNER_GUARD
  15. #define USE_INNER_GUARD // 是否在operator->时采用inner_guard保护.
  16. #endif // macro USE_INNER_GUARD end.
  17. #include <iostream>
  18. namespace rhl_tools
  19. {
  20. #ifdef _WINDOWS_
  21.     class default_locker // ms windows os thread lock
  22.     {
  23.     public:
  24.         typedef default_locker* point;
  25.     public:
  26.         default_locker()
  27.         {
  28.             ::InitializeCriticalSection(&ion_lock);
  29.         }
  30.         ~default_locker()
  31.         {
  32.             ::DeleteCriticalSection(&ion_lock);
  33.         }
  34.     public:
  35.         inline void lock()
  36.         {
  37.             ::EnterCriticalSection(&ion_lock);
  38.         }
  39.         inline void unlock()
  40.         {
  41.             ::LeaveCriticalSection(&ion_lock);
  42.         }
  43.     private:
  44.         ::CRITICAL_SECTION section_lock;
  45.     }; // class default_locker end.
  46. #else // macro _WINDOWS_ else.
  47.     class default_locker // null lock
  48.     {
  49.     public:
  50.         typedef default_locker* point;
  51.     public:
  52.         default_locker()
  53.         {}
  54.         ~default_locker()
  55.         {}
  56.     public:
  57.         inline void lock() const
  58.         {}
  59.         inline void unlock() const
  60.         {}
  61.     private:
  62.     }; // class default_locker end.
  63. #endif // macro _WINDOWS_ end.
  64. }; // namespace rhl_tools end.
  65. namespace rhl_tools
  66. {
  67.     template<class point> // 删除某个指针.(采用模板的方式,可随需要特化出自己的版本.)
  68.     inline void delete_pointer(point& p)
  69.     {
  70.         delete p;
  71.         p = NULL;
  72.     }
  73.     namespace private_detail
  74.     {
  75.         const std::size_t min_ref_count = 1; // 最小引用计数,小于此计数者将被销毁.
  76.         template<class count_type>
  77.         inline void construct_counter(typename count_type::point& p)
  78.         {
  79.             try
  80.             {
  81.                 p = new count_type(min_ref_count);
  82.             }
  83.             catch(...)
  84.             {
  85.                 throw std::bad_alloc("counter construct failed.");
  86.             }
  87.         }
  88.         template<class count_type>
  89.         inline void destroy_counter(typename count_type::point& p)
  90.         {
  91.             delete p;
  92.             p = NULL;
  93.         }
  94.         template<class lock_type>
  95.         inline void construct_locker(typename lock_type::point& p)
  96.         {
  97.             try
  98.             {
  99.                 p = new lock_type;
  100.             }
  101.             catch(...)
  102.             {
  103.                 throw std::bad_alloc("locker construct failed.");
  104.             }
  105.         }
  106.         template<class lock_type>
  107.         inline void destroy_locker(typename lock_type::point& p)
  108.         {
  109.             delete p;
  110.             p = NULL;
  111.         }
  112.         template<class T>
  113.         class default_counter // 默认的引用计数器.
  114.         {
  115.         public:
  116.             typedef default_counter* point;
  117.             typedef T count_t;
  118.         public:
  119.             default_counter(count_t default_count)
  120.                 : _count(default_count)
  121.             {}
  122.             ~default_counter()
  123.             {}
  124.         public:
  125.             inline count_t get_count() const
  126.             {
  127.                 return _count;
  128.             }
  129.             inline count_t increase()
  130.             {
  131.                 return ++_count;
  132.             }
  133.             inline count_t decrease()
  134.             {
  135.                 return --_count;
  136.             }
  137.         private:
  138.             count_t     _count;
  139.         }; // class default_counter end.
  140.     }; // namespace private_detail end.
  141.     template<class obj_type, class lock_policy = default_locker>
  142.     class smart_ptr
  143.     {
  144.     public:
  145.         typedef smart_ptr<obj_type, lock_policy> self_type;
  146.         typedef obj_type* point;
  147.         typedef obj_type& reference;
  148.         typedef std::size_t count_t;
  149.         typedef private_detail::default_counter<count_t> counter_type;
  150.         typedef typename counter_type::point counter_point;
  151.         typedef lock_policy locker_type;
  152.         typedef typename locker_type::point locker_point;
  153.         typedef point (self_type::* ptr_to_bool)(void);
  154.     private:
  155.         class inner_guard
  156.         {
  157.         public:
  158.             inner_guard(self_type& self)
  159.                 : _refer_self(self)
  160.             {
  161.                 _refer_self._locker->lock();
  162.             }
  163.             ~inner_guard()
  164.             {
  165.                 _refer_self._locker->unlock();
  166.             }
  167.         public:
  168.             inline point operator ->()
  169.             {
  170.                 return _refer_self._pointer;
  171.             }
  172.         private:
  173.             self_type&  _refer_self;
  174.         }; // class inner_guard end.
  175.     public:
  176.         explicit smart_ptr(point p = NULL)  // 失败则会抛出bad_exception异常.
  177.             : _pointer(p), _counter(NULL), _locker(NULL)
  178.         {
  179.             if(p == NULL)
  180.                 return;
  181.             private_detail::construct_counter<counter_type>(_counter);
  182.             private_detail::construct_locker<locker_type>(_locker);
  183.         }
  184.         ~smart_ptr()
  185.         {
  186.             this->check_delete();
  187.         }
  188.         inline smart_ptr(const self_type& other)
  189.             : _pointer(NULL), _counter(NULL), _locker(NULL)
  190.         {
  191.             *this = other; // 直接采用operator=操作.
  192.         }
  193.         smart_ptr& operator =(const self_type& other)
  194.         {
  195.             if(_pointer == other._pointer)
  196.                 return *this;
  197.             this->check_delete(); // 赋值时需检查原有的指针是否应删除.
  198.             _pointer = other._pointer;
  199.             _counter = other._counter;
  200.             _locker = other._locker;
  201.             inner_guard guard(*this); // 放置守卫,保证引用计数在多线程环境下正常运转.
  202.             _counter->increase();
  203.             return *this;
  204.         }
  205.         inline operator ptr_to_bool() const // 判断指针是否有效.(借鉴了boost::shared_ptr的处理方式.)
  206.         {
  207.             return _pointer == NULL ? NULL : &self_type::get_point;
  208.         }
  209.     public:
  210.         inline point get_point() // 获得原指针
  211.         {
  212.             return _pointer;
  213.         }
  214.         inline count_t use_count() const // 查询当前引用计数
  215.         {
  216.             return _counter->get_count();
  217.         }
  218. #ifdef USE_INNER_GUARD
  219.         inner_guard operator ->()
  220.         {
  221.             return inner_guard(*this); // 奇技淫巧,何足道哉.
  222.         }
  223. #else
  224.         inline point operator ->()
  225.         {
  226.             return _pointer;
  227.         }
  228. #endif // macro USE_INNER_LOCK end.
  229.         reference operator *()
  230.         {
  231.             return *_pointer;
  232.         }
  233.     private:
  234.         void check_delete()
  235.         {
  236.             if(_pointer == NULL || _counter == NULL || _locker == NULL)
  237.                 return;
  238.             count_t ref_count = 0;
  239.             {inner_guard guard(*this); // 放置守卫,保证引用计数在多线程环境下正常运转.
  240.                 ref_count = _counter->decrease();
  241.             }
  242.             if(ref_count < private_detail::min_ref_count)
  243.             {
  244.                 delete_pointer<point>(_pointer);
  245.                 private_detail::destroy_counter<counter_type>(_counter);
  246.                 private_detail::destroy_locker<locker_type>(_locker);
  247.             }
  248.         }
  249.     private:
  250.         point               _pointer;
  251.         counter_point       _counter;
  252.         locker_point        _locker;
  253.     }; // class smart_ptr end.
  254. }; // namespace rhl_tools end.
  255. #endif // file end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值