#include #include //----------------------------------------------------------------------------- // 允许互斥访问一个指针对象 //----------------------------------------------------------------------------- template 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 C * AS (); template 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 > m_Lock; const ExclusiveHolder * m_Holder; }; public: inline LockHolder Lock () { return LockHolder (this); } inline LockHolder Lock () const { return LockHolder (this); } template void LockExec (Action action) { LockHolder holder (this); action (holder.As ()); } template void LockExec (Action action) const { LockHolder holder (this); action (holder.As ()); } }; template ExclusiveHolder ::ExclusiveHolder() { m_pObject = NULL; } template ExclusiveHolder ::~ExclusiveHolder() { LockHolder Lock(this); delete m_pObject; } template ExclusiveHolder ::ExclusiveHolder(T* object) { m_pObject = object; } template void ExclusiveHolder ::Attach(T* object) { LockHolder Lock(this); delete m_pObject; m_pObject = object; } template T* ExclusiveHolder ::Detach() { LockHolder Lock(this); T* old = m_pObject; m_pObject = NULL; Release(); return old; } template void ExclusiveHolder ::Release() { LockHolder Lock(this); delete m_pObject; m_pObject = NULL; } template ExclusiveHolder & ExclusiveHolder ::operator = (T* object) { Attach(object); return *this; } template bool ExclusiveHolder ::IsEmpty() const { LockHolder Lock(this); return (m_pObject == NULL); } template ExclusiveHolder ::LockHolder::LockHolder(const ExclusiveHolder * holder) { auto lock = new std::lock_guard < std::recursive_mutex >(holder->m_Mutex); m_Lock.reset(lock); m_Holder = holder; } template ExclusiveHolder ::LockHolder::LockHolder(LockHolder&& from) { m_Lock.swap(from.m_Lock); m_Holder = from.m_Holder; } template ExclusiveHolder ::LockHolder::~LockHolder() { } template T* ExclusiveHolder ::LockHolder::As() { return m_Holder->m_pObject; } template const T* ExclusiveHolder ::LockHolder::As() const { return m_Holder->m_pObject; } template template C* ExclusiveHolder ::LockHolder::AS() { return m_Holder->m_pObject; } template template const C* ExclusiveHolder ::LockHolder::AS() const { return m_Holder->m_pObject; }