- #ifndef __smart_ptr_h__
- #define __smart_ptr_h__
- //
- // @author: jasonshg@gmail.com
- //
- // @date: 2008.08.05
- //
- // @compiler: ms vc7.1 / dev cpp5.2
- //
- // @description:
- // 智能指针,采用引用计数管理生存周期,支持拷贝操作,可用于stl容器中.
- // 并在模拟指针语法时.封装了线程锁的操作.
- //
- #ifndef USE_INNER_GUARD
- #define USE_INNER_GUARD // 是否在operator->时采用inner_guard保护.
- #endif // macro USE_INNER_GUARD end.
- #include <iostream>
- namespace rhl_tools
- {
- #ifdef _WINDOWS_
- class default_locker // ms windows os thread lock
- {
- public:
- typedef default_locker* point;
- public:
- default_locker()
- {
- ::InitializeCriticalSection(&ion_lock);
- }
- ~default_locker()
- {
- ::DeleteCriticalSection(&ion_lock);
- }
- public:
- inline void lock()
- {
- ::EnterCriticalSection(&ion_lock);
- }
- inline void unlock()
- {
- ::LeaveCriticalSection(&ion_lock);
- }
- private:
- ::CRITICAL_SECTION section_lock;
- }; // class default_locker end.
- #else // macro _WINDOWS_ else.
- class default_locker // null lock
- {
- public:
- typedef default_locker* point;
- public:
- default_locker()
- {}
- ~default_locker()
- {}
- public:
- inline void lock() const
- {}
- inline void unlock() const
- {}
- private:
- }; // class default_locker end.
- #endif // macro _WINDOWS_ end.
- }; // namespace rhl_tools end.
- namespace rhl_tools
- {
- template<class point> // 删除某个指针.(采用模板的方式,可随需要特化出自己的版本.)
- inline void delete_pointer(point& p)
- {
- delete p;
- p = NULL;
- }
- namespace private_detail
- {
- const std::size_t min_ref_count = 1; // 最小引用计数,小于此计数者将被销毁.
- template<class count_type>
- inline void construct_counter(typename count_type::point& p)
- {
- try
- {
- p = new count_type(min_ref_count);
- }
- catch(...)
- {
- throw std::bad_alloc("counter construct failed.");
- }
- }
- template<class count_type>
- inline void destroy_counter(typename count_type::point& p)
- {
- delete p;
- p = NULL;
- }
- template<class lock_type>
- inline void construct_locker(typename lock_type::point& p)
- {
- try
- {
- p = new lock_type;
- }
- catch(...)
- {
- throw std::bad_alloc("locker construct failed.");
- }
- }
- template<class lock_type>
- inline void destroy_locker(typename lock_type::point& p)
- {
- delete p;
- p = NULL;
- }
- template<class T>
- class default_counter // 默认的引用计数器.
- {
- public:
- typedef default_counter* point;
- typedef T count_t;
- public:
- default_counter(count_t default_count)
- : _count(default_count)
- {}
- ~default_counter()
- {}
- public:
- inline count_t get_count() const
- {
- return _count;
- }
- inline count_t increase()
- {
- return ++_count;
- }
- inline count_t decrease()
- {
- return --_count;
- }
- private:
- count_t _count;
- }; // class default_counter end.
- }; // namespace private_detail end.
- template<class obj_type, class lock_policy = default_locker>
- class smart_ptr
- {
- public:
- typedef smart_ptr<obj_type, lock_policy> self_type;
- typedef obj_type* point;
- typedef obj_type& reference;
- typedef std::size_t count_t;
- typedef private_detail::default_counter<count_t> counter_type;
- typedef typename counter_type::point counter_point;
- typedef lock_policy locker_type;
- typedef typename locker_type::point locker_point;
- typedef point (self_type::* ptr_to_bool)(void);
- private:
- class inner_guard
- {
- public:
- inner_guard(self_type& self)
- : _refer_self(self)
- {
- _refer_self._locker->lock();
- }
- ~inner_guard()
- {
- _refer_self._locker->unlock();
- }
- public:
- inline point operator ->()
- {
- return _refer_self._pointer;
- }
- private:
- self_type& _refer_self;
- }; // class inner_guard end.
- public:
- explicit smart_ptr(point p = NULL) // 失败则会抛出bad_exception异常.
- : _pointer(p), _counter(NULL), _locker(NULL)
- {
- if(p == NULL)
- return;
- private_detail::construct_counter<counter_type>(_counter);
- private_detail::construct_locker<locker_type>(_locker);
- }
- ~smart_ptr()
- {
- this->check_delete();
- }
- inline smart_ptr(const self_type& other)
- : _pointer(NULL), _counter(NULL), _locker(NULL)
- {
- *this = other; // 直接采用operator=操作.
- }
- smart_ptr& operator =(const self_type& other)
- {
- if(_pointer == other._pointer)
- return *this;
- this->check_delete(); // 赋值时需检查原有的指针是否应删除.
- _pointer = other._pointer;
- _counter = other._counter;
- _locker = other._locker;
- inner_guard guard(*this); // 放置守卫,保证引用计数在多线程环境下正常运转.
- _counter->increase();
- return *this;
- }
- inline operator ptr_to_bool() const // 判断指针是否有效.(借鉴了boost::shared_ptr的处理方式.)
- {
- return _pointer == NULL ? NULL : &self_type::get_point;
- }
- public:
- inline point get_point() // 获得原指针
- {
- return _pointer;
- }
- inline count_t use_count() const // 查询当前引用计数
- {
- return _counter->get_count();
- }
- #ifdef USE_INNER_GUARD
- inner_guard operator ->()
- {
- return inner_guard(*this); // 奇技淫巧,何足道哉.
- }
- #else
- inline point operator ->()
- {
- return _pointer;
- }
- #endif // macro USE_INNER_LOCK end.
- reference operator *()
- {
- return *_pointer;
- }
- private:
- void check_delete()
- {
- if(_pointer == NULL || _counter == NULL || _locker == NULL)
- return;
- count_t ref_count = 0;
- {inner_guard guard(*this); // 放置守卫,保证引用计数在多线程环境下正常运转.
- ref_count = _counter->decrease();
- }
- if(ref_count < private_detail::min_ref_count)
- {
- delete_pointer<point>(_pointer);
- private_detail::destroy_counter<counter_type>(_counter);
- private_detail::destroy_locker<locker_type>(_locker);
- }
- }
- private:
- point _pointer;
- counter_point _counter;
- locker_point _locker;
- }; // class smart_ptr end.
- }; // namespace rhl_tools end.
- #endif // file end.
贴上前些天无聊写的一份智能指针代码: )
最新推荐文章于 2024-07-25 20:45:37 发布