123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include <memory>
- #include <mutex>
- //-----------------------------------------------------------------------------
- // 允许互斥访问一个指针对象
- //-----------------------------------------------------------------------------
- template <typename T> class ExclusiveHolder
- {
- protected:
- T * m_pObject;
- public:
- ExclusiveHolder ();
- explicit ExclusiveHolder (T * object);
- virtual ~ExclusiveHolder ();
- private:
- // 禁止拷贝构造函数
- ExclusiveHolder (const ExclusiveHolder & h) = delete;
- // 禁止复制
- ExclusiveHolder & operator = (const ExclusiveHolder & h) = delete;
- public:
- void Attach (T * object);
- T * Detach ();
- ExclusiveHolder & operator = (T * object);
- bool IsEmpty () const;
- void Release ();
- protected:
- mutable std::recursive_mutex m_Mutex;
- public:
- class LockHolder
- {
- public:
- LockHolder (const ExclusiveHolder * holder);
- ~LockHolder ();
- LockHolder (LockHolder && from);
- private:
- // 可以对同一个 ExclusiveHolder 对象反复调用 Lock, 但是 LockHolder 还是不要拷贝构造了吧
- // 禁止拷贝构造函数
- LockHolder (const LockHolder & h) = delete;
- // 禁止复制
- LockHolder & operator = (const LockHolder & h) = delete;
- public:
- T * As ();
- const T * As () const;
- template <typename C> C * AS ();
- template <typename C> const C * AS () const;
- T * operator -> ()
- {
- return As ();
- }
- const T * operator -> () const
- {
- return As ();
- }
- T & operator * ()
- {
- return *As ();
- }
- const T & operator * () const
- {
- return *As ();
- }
- protected:
- std::unique_ptr <std::lock_guard < std::recursive_mutex > > m_Lock;
- const ExclusiveHolder * m_Holder;
- };
- public:
- inline LockHolder Lock ()
- {
- return LockHolder (this);
- }
- inline LockHolder Lock () const
- {
- return LockHolder (this);
- }
- template <class Action> void LockExec (Action action)
- {
- LockHolder holder (this);
- action (holder.As ());
- }
- template <class Action> void LockExec (Action action) const
- {
- LockHolder holder (this);
- action (holder.As ());
- }
- };
- template <typename T>
- ExclusiveHolder <T>::ExclusiveHolder()
- {
- m_pObject = NULL;
- }
- template <typename T>
- ExclusiveHolder <T>::~ExclusiveHolder()
- {
- LockHolder Lock(this);
- delete m_pObject;
- }
- template <typename T>
- ExclusiveHolder <T>::ExclusiveHolder(T* object)
- {
- m_pObject = object;
- }
- template <typename T>
- void ExclusiveHolder <T>::Attach(T* object)
- {
- LockHolder Lock(this);
- delete m_pObject;
- m_pObject = object;
- }
- template <typename T>
- T* ExclusiveHolder <T>::Detach()
- {
- LockHolder Lock(this);
- T* old = m_pObject;
- m_pObject = NULL;
- Release();
- return old;
- }
- template <typename T>
- void ExclusiveHolder <T>::Release()
- {
- LockHolder Lock(this);
- delete m_pObject;
- m_pObject = NULL;
- }
- template <typename T>
- ExclusiveHolder <T>& ExclusiveHolder <T>::operator = (T* object)
- {
- Attach(object);
- return *this;
- }
- template <typename T>
- bool ExclusiveHolder <T>::IsEmpty() const
- {
- LockHolder Lock(this);
- return (m_pObject == NULL);
- }
- template <typename T>
- ExclusiveHolder <T>::LockHolder::LockHolder(const ExclusiveHolder <T>* holder)
- {
- auto lock = new std::lock_guard < std::recursive_mutex >(holder->m_Mutex);
- m_Lock.reset(lock);
- m_Holder = holder;
- }
- template <typename T>
- ExclusiveHolder <T>::LockHolder::LockHolder(LockHolder&& from)
- {
- m_Lock.swap(from.m_Lock);
- m_Holder = from.m_Holder;
- }
- template <typename T>
- ExclusiveHolder <T>::LockHolder::~LockHolder()
- {
- }
- template <typename T>
- T* ExclusiveHolder <T>::LockHolder::As()
- {
- return m_Holder->m_pObject;
- }
- template <typename T>
- const T* ExclusiveHolder <T>::LockHolder::As() const
- {
- return m_Holder->m_pObject;
- }
- template <typename T>
- template <typename C> C* ExclusiveHolder <T>::LockHolder::AS()
- {
- return m_Holder->m_pObject;
- }
- template <typename T>
- template <typename C> const C* ExclusiveHolder <T>::LockHolder::AS() const
- {
- return m_Holder->m_pObject;
- }
|