⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sync_ptr.hpp

📁 比STL和BOOST更强大的智能指针库!!! 私藏很久的老底与大家一起分享了.
💻 HPP
字号:
// sync_ptr.h 

#ifndef SYNC_PTR_H_BY_DAVID_MAISONAVE_HEADER_GUARD_
#define SYNC_PTR_H_BY_DAVID_MAISONAVE_HEADER_GUARD_

#include <memory>
#include "sync_ctrl.h"  //download at http://code.axter.com/sync_ctrl.h

/*! 
@class sync_ptr (Synchronized Smart Pointer)
@author David Maisonave (Axter) (609-345-1007) (http://axter.com)\n
Copyright (C) 2005\n
@date    06-Feb-2006\n
@brief sync_ptr is a portable synchronized smart pointer.
@section Description
sync_ptr is a thread safe pointer.
sync_ref_ptr is a reference counting (shared_ptr) derived version of sync_ptr
sync_ptr and sync_ref_ptr  can be used with Windows or UNIX/Linux via,
WIN32 API's or POSIX functions.
They can turn any object into a thread safe object.
sync_ptr destructor will automatically destroy the object,
so the object can be create via new on sync_ptr constructor.
This insures no other process has ownership of the object.
@section  example_usage Example Usage
sync_ptr<vector<int> > MyThreadSafeVectorInt(new vector<int>, new sync_ctrl_default);\n
MyThreadSafeVectorInt->push_back(123);\n
cout << MyThreadSafeVectorInt->at(0) << endl;\n
\n
//An instance of RefLockPtr can be create to lock the object for the duration of a function.\n
//Example Usage\n
sync_ptr<string> MyThreadSafeString(new string);\n
\n
string SomeFunction()\n
{\n
	sync_ptr<string>::RefLockPtr MyLockedString = MyThreadSafeString.get_locked_obj();\n
	MyLockedString->operator=("Hello World");\n
	return MyLockedString->substr(6);\n
}\n
\n
//The destructor for RefLockPtr automatically unlocks the object, so an explicit unlock call\n
//is not required.\n
\n
@subsection subsection_License License
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies.  
David Maisonave (Axter) makes no representations
about the suitability of this software for any purpose.
It is provided "as is" without express or implied warranty.
*/
template<class T, class SYNC_T = sync_ctrl_default, class AX_TYPE = std::allocator<T>, class AX_SYNC_CTRL = std::allocator<SYNC_T> >
class sync_ptr
{
protected:
	sync_ptr(T* type, SYNC_T *sync_ctrl_, int* RefCount_)
		:m_type(type), m_sync_ctrl(sync_ctrl_), m_RefCount(RefCount_)
	{
		m_sync_ctrl->lock();
		++(*m_RefCount);
		m_sync_ctrl->unlock();
	}
public:
	sync_ptr(T* type, SYNC_T *sync_ctrl_ = new SYNC_T)
		:m_type(type), m_sync_ctrl(sync_ctrl_), m_RefCount(new int(1)){}
	virtual ~sync_ptr()
	{
		m_sync_ctrl->lock();
		if (m_RefCount) --(*m_RefCount);
		if (!m_RefCount || !(*m_RefCount))
		{
			AX_TYPE alloc;
			alloc.destroy(m_type);
			alloc.deallocate(m_type, 1);
			m_type=NULL;
			if (m_RefCount) {delete m_RefCount;m_RefCount=NULL;}
		}
		m_sync_ctrl->unlock();
		if (!m_RefCount)
		{
			AX_SYNC_CTRL alloc;
			alloc.destroy(m_sync_ctrl);
			alloc.deallocate(m_sync_ctrl, 1);
		}
	}
	/*! @class RefLockPtr
	@brief RefLockPtr is a helper class used by sync_ptr to perform an
	automatic lock when operator->() is called.
	*/
	class RefLockPtr
	{
		struct RefData{
			int m_RefCount;
			T* m_type;
			SYNC_T *m_sync_ctrl;
			bool *m_trylock_stat;
		};
		public:
			friend class sync_ptr<T, SYNC_T, AX_TYPE, AX_SYNC_CTRL>;
			RefLockPtr(const RefLockPtr& Src):m_RefData(Src.m_RefData)
			{
				++m_RefData->m_RefCount;
			}
			~RefLockPtr()
			{
				--m_RefData->m_RefCount;
				if (!m_RefData->m_RefCount)
				{
					if (!m_RefData->m_trylock_stat || (*m_RefData->m_trylock_stat)) 
					{
						m_RefData->m_sync_ctrl->unlock();
					}
					delete m_RefData;
					m_RefData=NULL;
				}
			}
			T* operator->() const{return get_ptr();}
			T& operator*() const{return *get_ptr();}
			bool islocked()const{
				if (!m_RefData->m_trylock_stat || (*m_RefData->m_trylock_stat)) return true;
				return false;
			}
		protected:
			RefLockPtr(T* type, SYNC_T *sync_ctrl_, bool *trylock_stat = NULL)
				:m_RefData(new RefData)
			{
				m_RefData->m_RefCount = 1;
				m_RefData->m_type = type;
				m_RefData->m_sync_ctrl = sync_ctrl_;
				m_RefData->m_trylock_stat = trylock_stat;
				if(m_RefData->m_trylock_stat)
				{
					*m_RefData->m_trylock_stat = m_RefData->m_sync_ctrl->trylock();
				}
				else m_RefData->m_sync_ctrl->lock();
			}
			T* get_ptr()const
			{
				if (!m_RefData->m_trylock_stat || (*m_RefData->m_trylock_stat)) return m_RefData->m_type;
				return NULL;
			}
			RefLockPtr& operator=(const RefLockPtr&);
			RefData *m_RefData;
	};
	RefLockPtr operator->(){return RefLockPtr(m_type, m_sync_ctrl);}
	RefLockPtr operator*(){return RefLockPtr(m_type, m_sync_ctrl);}
	//T& operator*() {return (*RefLockPtr(m_type, m_sync_ctrl));}
	RefLockPtr get_locked_obj(bool *trylock_stat = NULL){return RefLockPtr(m_type, m_sync_ctrl, trylock_stat);}
	protected:
		sync_ptr& operator=(const sync_ptr&);
		sync_ptr(const sync_ptr& Src);
		bool operator==(const sync_ptr&);
		bool operator!=(const sync_ptr&);
		// Data members
		T* m_type;
		SYNC_T *m_sync_ctrl;
		int *m_RefCount;
};

/*! @class sync_ref_ptr
	@brief sync_ref_ptr is a synchronized reference counting smart pointer.
*/
template<class T, class SYNC_T = sync_ctrl_default, class AX_TYPE = std::allocator<T>, class AX_SYNC_CTRL = std::allocator<SYNC_T> >
class sync_ref_ptr : public sync_ptr<T, SYNC_T, AX_TYPE, AX_SYNC_CTRL>
{
public:
	sync_ref_ptr(const sync_ref_ptr& Src)
		:sync_ptr<T, SYNC_T, AX_TYPE, AX_SYNC_CTRL>(Src.m_type, Src.m_sync_ctrl, Src.m_RefCount){}
	sync_ref_ptr(T* type, SYNC_T *sync_ctrl_ = new SYNC_T)
		:sync_ptr<T, SYNC_T, AX_TYPE, AX_SYNC_CTRL>(type, sync_ctrl_){}
private:
	sync_ref_ptr& operator=(const sync_ref_ptr&);
	bool operator==(const sync_ref_ptr&);
	bool operator!=(const sync_ref_ptr&);
};

#endif //!SYNC_PTR_H_BY_DAVID_MAISONAVE_HEADER_GUARD_


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -