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

📄 smart_ptr.hpp

📁 比STL和BOOST更强大的智能指针库!!! 私藏很久的老底与大家一起分享了.
💻 HPP
📖 第 1 页 / 共 5 页
字号:
	{
		m_ownership_policy.destructor_lock_policy(m_type);
		CHECKING_POLICY::before_release(m_type);
		m_ownership_policy.release(m_type, m_clone_fct, m_ownership_policy);
		CHECKING_POLICY::after_release(m_type);
		m_type = NULL;
	}
#if !defined(_MSC_VER) || (_MSC_VER > 1200)
	/*! @brief  Constructor for smart pointer derived type smart_ptr\<DerivedT\> */
	template<class CompatibleDerivedT,	class OWNERSHIP_POLICY_T,	class ALLOCATOR_POLICY_T,	class CHECKING_POLICY_T,	class COMPARISON_SEMANTIC_POLICY_T,	class STREAM_OPERATOR_SEMANTIC_POLICY_T,	class ARITHMETIC_SEMANTIC_POLICY_T>
		smart_ptr(const smart_ptr<CompatibleDerivedT, OWNERSHIP_POLICY_T, ALLOCATOR_POLICY_T, CHECKING_POLICY_T, COMPARISON_SEMANTIC_POLICY_T, STREAM_OPERATOR_SEMANTIC_POLICY_T, ARITHMETIC_SEMANTIC_POLICY_T>& Src):smart_ptr_base<T,ALLOCATOR_POLICY>(NULL, NULL)
	{//No clean way to do this, so just clone it
		Src.lock();
		Src.make_clone(m_type, m_clone_fct);
		Src.unlock();
	}
	//@}
	/*! @name Operators */
	//@{
	template<class CompatibleDerivedT,	class OWNERSHIP_POLICY_T,	class ALLOCATOR_POLICY_T,	class CHECKING_POLICY_T,	class COMPARISON_SEMANTIC_POLICY_T, class STREAM_OPERATOR_SEMANTIC_POLICY_T,	class ARITHMETIC_SEMANTIC_POLICY_T>
		smart_ptr& operator=(const smart_ptr<CompatibleDerivedT, OWNERSHIP_POLICY_T, ALLOCATOR_POLICY_T, CHECKING_POLICY_T, COMPARISON_SEMANTIC_POLICY_T, STREAM_OPERATOR_SEMANTIC_POLICY_T, ARITHMETIC_SEMANTIC_POLICY_T>& Src)
	{//No clean way to do this, so just clone it
		if (m_type != Src.c_ptr())
		{
			m_ownership_policy.assignment_lock_policy(m_type);
			CHECKING_POLICY::before_release(m_type);
			m_ownership_policy.release(m_type, m_clone_fct, m_ownership_policy);
			CHECKING_POLICY::after_release(m_type);
			Src.lock();
			Src.make_clone(m_type, m_clone_fct);
			Src.unlock();
		}
		return *this;
	}
#else
	//For VC++6.0 the smart_ptr is only interchangeable through assignment operator
	template<class CompatiblePointerTypeForVC60>
	smart_ptr& operator=(const CompatiblePointerTypeForVC60& Src)
	{//No clean way to do this, so just clone it
		m_ownership_policy.assignment_lock_policy(m_type);
		CHECKING_POLICY::before_release(m_type);
		m_ownership_policy.release(m_type, m_clone_fct, m_ownership_policy);
		if (m_type) m_ownership_policy.assignment_unlock_policy(m_type);
		CHECKING_POLICY::after_release(m_type);
		Src.lock();
		Src.make_clone(m_type, m_clone_fct);
		Src.unlock();
		return *this;
	}
#endif //_MSC_VER noteq 1200
	/*! @brief Assignment operator */
	smart_ptr& operator=(smart_ptr& Src)
	{
		if (m_type != Src.m_type)
		{
			Src.lock();
			m_ownership_policy.assign(m_type, m_clone_fct, Src.m_type, Src.m_clone_fct, Src.m_ownership_policy, m_ownership_policy);
			Src.unlock();
		}
		return *this;
	}
	bool operator! () const{return c_ptr() == 0;}
#ifdef SMART_PTR_SUPPORT_RETURN_TYPE_THROUGH_POLICY_
	// @cond INCLUDE_ALL_OBJS_
	//Return type for arithmetic operators
	typedef typename ARITHMETIC_SEMANTIC_POLICY::template return_type<smart_ptr>::type arith_retrn_type;
	// @endcond 
	//The arithmetic semantic policy might not make it through boost peer review, so for now there's only
	//a limited set for demonstration purposes.
	arith_retrn_type& operator+=(const smart_ptr& Src){return ARITHMETIC_SEMANTIC_POLICY::plus_equal_smart_ptr(*this, Src);}
	template<class T2>	arith_retrn_type& operator+=(const T2& Src){return ARITHMETIC_SEMANTIC_POLICY::plus_equal(*this, Src);}
	arith_retrn_type& operator-=(const smart_ptr& Src){return ARITHMETIC_SEMANTIC_POLICY::minus_equal_smart_ptr(*this, Src);}
	template<class T2>	arith_retrn_type& operator-=(const T2& Src){return ARITHMETIC_SEMANTIC_POLICY::minus_equal(*this, Src);}
	friend arith_retrn_type operator+(const smart_ptr& a, const smart_ptr& b){return ARITHMETIC_SEMANTIC_POLICY::plus(a, b);}
	friend arith_retrn_type operator-(const smart_ptr& a, const smart_ptr& b){return ARITHMETIC_SEMANTIC_POLICY::minus(a, b);}
	//
	// @cond INCLUDE_ALL_OBJS_
	typedef typename OWNERSHIP_POLICY::template return_type<T>::type_pointee ownership_retrn_type_pointee;
	typedef typename OWNERSHIP_POLICY::template return_type<T>::type_ref ownership_retrn_type_ref;
	// @endcond 
	//Operators that will force exclusive pointer
	inline ownership_retrn_type_pointee operator->() 
	{
		m_ownership_policy.pointee_lock_policy(m_type);
		CHECKING_POLICY::on_dereference(m_type);
		return m_ownership_policy.get_non_constant_ptr(m_type, m_clone_fct);
	}
	inline ownership_retrn_type_ref operator*() 
	{
		m_ownership_policy.pointee_lock_policy(m_type);
		CHECKING_POLICY::on_dereference(m_type);
		return *m_ownership_policy.get_non_constant_ptr(m_type, m_clone_fct);
	}
	inline ownership_retrn_type_pointee operator->()const {
		m_ownership_policy.const_pointee_lock_policy(m_type);
		CHECKING_POLICY::on_const_dereference(m_type);
		return m_type;
	}
	inline ownership_retrn_type_ref operator*() const {
		m_ownership_policy.const_pointee_lock_policy(m_type);
		CHECKING_POLICY::on_const_dereference(m_type);
		return *m_type;
	}
#else
	//Operators that will force exclusive pointer
	inline T* operator->() 
	{
		m_ownership_policy.pointee_lock_policy(m_type);
		CHECKING_POLICY::on_dereference(m_type);
		return m_ownership_policy.get_non_constant_ptr(m_type, m_clone_fct);
	}
	inline T& operator*() 
	{
		m_ownership_policy.pointee_lock_policy(m_type);
		CHECKING_POLICY::on_dereference(m_type);
		return *m_ownership_policy.get_non_constant_ptr(m_type, m_clone_fct);
	}
	inline const T* operator->()const {
		m_ownership_policy.const_pointee_lock_policy(m_type);
		CHECKING_POLICY::on_const_dereference(m_type);
		return m_type;
	}
	inline const T& operator*() const {
		m_ownership_policy.const_pointee_lock_policy(m_type);
		CHECKING_POLICY::on_const_dereference(m_type);
		return *m_type;
	}
#endif //SMART_PTR_SUPPORT_RETURN_TYPE_THROUGH_POLICY_
	//@}

	/*! @name Synchronize Methods */
	//@{
	/*! @brief lock function is used with lock policy, and can be called
		manually (directly), or automatically through scope_lock.
	*/
	void lock() const {if (m_type) m_ownership_policy.lock(m_type);}
	/*! @brief unlock function is used with lock policy, and can be called
		manually (directly), or automatically through scope_lock.
	*/
	void unlock() const {if (m_type) m_ownership_policy.unlock(m_type);}
	/*! @brief trylock is an optional synchronization function that can be
		used with the lock policy.  Use islock function to test trylock success.
		Some implementations do not support trylock and islock logic.
	*/
	void trylock() const {if (m_type) m_ownership_policy.trylock(m_type);}
	/*! @brief islock is an optional synchronization function that can be
		used with the lock policy.  The islock function should never be used
		with lock() method, and should only be used with trylock().  Some
		implementations do not support trylock and islock logic.
	*/
	void islock() const {if (m_type) m_ownership_policy.islock(m_type);}// Should only be used with trylock
	//@}


	/*! @name Misc Methods */
	//@{
	/*! @brief For added safety, call set_default_object to set default object before
		using this class as the second type in a std::map container.
	*/
	static void set_default_object(const smart_ptr& NewValue){get_or_set_default_object(&NewValue);}
	/*! @brief Use equal method to test if two pointee's are equal to each other.
		This does not test if the address of the pointers are equal to each other,
		and instead test if what the pointers are pointing to, are equal to each other.
	*/
	template<class T2>	smart_ptr& equal(const T2& Src){
		(*get_ptr()) = (Src);
		return *this;
	}
	/*! @brief The get_function_ptr method is mainly for internal usage, but it can not 
		be made private/protected because policy methods require access to this method.
	*/
	clone_fct_Type get_function_ptr()const{return m_clone_fct;}
	/*! @brief The swap method is not used by any policy, and it's implemented here
		to make this class compatible with generic coding.
	*/
	void swap(smart_ptr<T> & other)throw(){std::swap(m_ownership_policy, other.m_ownership_policy);std::swap(m_type, other.m_type);std::swap(m_clone_fct, other.m_clone_fct);}
	/*! @brief The make_clone method is used to allow other smart pointers access
		to the cloning logic.  One of the main reasons it's required, is because a
		derived T type, would not have access to the private members (m_clone_fct & m_type),
		so without make_clone, it would be difficult to clone smart_ptr's if the T type where not the same.
	*/
	template<class PT, class FPT>	void make_clone(PT*& ptr, FPT& func_ptr) const
	{
		if (m_type && m_clone_fct)
		{
			ptr = m_clone_fct(m_type, true, NULL, NULL, NULL);
			func_ptr = (FPT)m_clone_fct;
		}
		else
			ptr = NULL;
	}
	//@}


	/*! @name Common Pointer Access Methods */
	//@{
	inline T* get_ptr(){return m_ownership_policy.get_non_constant_ptr(m_type, m_clone_fct);}
	inline const T* c_ptr()const{return  m_type;}
	inline const T& c_ref()const{return  *m_type;}
	//@}

	// @cond INCLUDE_ALL_OBJS_
	typedef COMPARISON_SEMANTIC_POLICY comparison_semantic_policy_type;
	typedef T* pointer;
	typedef T& reference;
	// @endcond 
private:
#if !defined(_MSC_VER) || (_MSC_VER > 1200)
/*
Note#1:
The constructor_and_destructor_allocator_function function has three extra arguments that are not used.  These arguments are not required at all in more compliant compilers
like VC++ 7.x and GNU 3.x.  However, for none compliant (pre-standard) compilers like VC++ 6.0 and BCC55, the extra argument declaration is required
so as to be able to fully qualify the function template type.  The argument declarations are needed for these compilers, but the actual argument variables
are not needed.  Anything pass to these last three arguments is discarded.
*/
	template < typename TT, typename DT, typename AL> static TT * constructor_and_destructor_allocator_function( TT *  ptr, bool bConstruct, DT*, AL* , void*) 
	{
		if (!ptr) return NULL;
		if (bConstruct){return AL::allocate(static_cast<const DT*>(ptr));}
		AL::deallocate(ptr);
		return NULL;
	} 
	template<typename T_obj>
		inline static	clone_fct_Type get_alloc_func(T_obj*)
	{
		T * ( *tmp ) (T *, bool, T_obj*, ALLOCATOR_POLICY*, void*) = constructor_and_destructor_allocator_function<T,T_obj,ALLOCATOR_POLICY>;
		return (clone_fct_Type)tmp;
	}
#else
	template<typename T_obj>
		inline static	clone_fct_Type get_alloc_func(T_obj*)
	{
		T * ( *tmp ) (T *, bool, T_obj*, ALLOCATOR_POLICY*, void*) = constructor_and_destructor_allocator_function<T,T_obj,ALLOCATOR_POLICY>;
		if (!tmp) constructor_and_destructor_allocator_function((T*)NULL, false, (T_obj*)NULL, (ALLOCATOR_POLICY*)NULL, NULL);
		return (clone_fct_Type)tmp;
	}
#endif //_MSC_VER noteq 1200

	static smart_ptr& get_or_set_default_object(const smart_ptr* NewValue = NULL)
	{
		static smart_ptr DefaultObj(eNo);
		if (NewValue && NewValue->m_type)
			DefaultObj = *NewValue;
		return DefaultObj;
	}

#if !defined(_MSC_VER) || (_MSC_VER > 1200)
	template<class T1> friend bool operator==(const smart_ptr & a, const T1 & b){return T1::comparison_semantic_policy_type::is_equal(a,b);}
	template<class T1> friend bool operator!=(const smart_ptr & a, const T1 & b){return !T1::comparison_semantic_policy_type::is_equal(a,b);}
	template<class T1> friend bool operator> (const smart_ptr & a, const T1 & b){return T1::comparison_semantic_policy_type::greater_than(a,b);}
	template<class T1> friend bool operator< (const smart_ptr & a, const T1 & b){return T1::comparison_semantic_policy_type::less_than(a,b);}
	template<class T1> friend bool operator>=(const smart_ptr & a, const T1 & b){return T1::comparison_semantic_policy_type::greater_than_or_equal(a,b);}
	template<class T1> friend bool operator<=(const smart_ptr & a, const T1 & b){return T1::comparison_semantic_policy_type::less_than_or_equal(a,b);}
#else //Can not support exchangeable comparison in VC++ 6.0 
	friend inline bool operator==(const smart_ptr & a, const smart_ptr & b){return COMPARISON_SEMANTIC_POLICY::is_equal(a,b);}
	friend inline bool operator!=(const smart_ptr & a, const smart_ptr & b){return !COMPARISON_SEMANTIC_POLICY::is_equal(a,b);}
	friend inline bool operator< (const smart_ptr & a, const smart_ptr & b){return COMPARISON_SEMANTIC_POLICY::less_than(a,b);}
	friend inline bool operator> (const smart_ptr & a, const smart_ptr & b){return COMPARISON_SEMANTIC_POLICY::greater_than(a,b);}
	friend inline bool operator<=(const smart_ptr & a, const smart_ptr & b){return COMPARISON_SEMANTIC_POLICY::less_than_or_equal(a,b);}
	friend inline bool operator>=(const smart_ptr & a, const smart_ptr & b){return COMPARISON_SEMANTIC_POLICY::greater_than_or_equal(a,b);}
#endif //not defined(_MSC_VER) || (_MSC_VER > 1200)

#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
// Resolve the ambiguity between our op!= and the one in rel_ops
	template<class T> friend bool operator!=(const smart_ptr<T> & a, const smart_ptr<T> & b){return !COMPARISON_SEMANTIC_POLICY::is_equal(a,b);}
#endif

	template<class T1> T1& input_stream(T1 &is){return STREAM_OP_SEMANTIC_POLICY::input_stream(is, *this);}
	template<class T1> T1& output_stream(T1 &io) const {return STREAM_OP_SEMANTIC_POLICY::output_stream(io, *this);}

	template<class TS> inline friend TS& operator >>(TS &is,smart_ptr &obj){return obj.input_stream(is);}
	template<class TS> inline friend TS& operator <<(TS &io,const smart_ptr &obj){return obj.output_stream(io);}

};

// @cond INCLUDE_ALL_OBJS_

//The following smart_ptr_type, allows typedef declarations for certain types
//Example:
//typedef smart_ptr_type<copy_on_write_policy<ref_link_policy, intrusive_lock_policy> > MyCowSyncSmartPtr;
//MyCowSyncSmartPtr::to<Shape>::type pShape1;
template< 
//Available OWNERSHIP_POLICY policies:				shared_ptr_policy<ref_link_policy>, copy_on_write_policy<ref_link_policy>, deep_copy_policy, shared_ptr_policy<ref_count_policy>, copy_on_write_policy<ref_count_policy>, shared_ptr_policy<ref_intrusive_policy>, copy_on_write_policy<ref_intrusive_policy>
class OWNERSHIP_POLICY = ownership_default_policy , 
	//Available ALLOCATOR_POLICY policies:				allocator_default_policy, clone_function_allocator_policy, clone_static_function_allocator_policy
class ALLOCATOR_POLICY =  allocator_default_policy,
	//Available CHECKING_POLICY policies:				no_checking_policy, boost_assert_checking_policy
class CHECKING_POLICY =  checking_default_policy,
	//Available COMPARISON_SEMANTIC_POLICY policies:	value_comparsion_semantic_policy, pointer_comparsion_semantic_policy, no_comparsion_semantic_policy 
class COMPARISON_SEMANTIC_POLICY = comparison_default_policy,
	//Available STREAM_OP_SEMANTIC_POLICY policies:		value_stream_operator_semantic_policy, no_stream_operator_semantic_policy  (Not supported on VC++6.0)
class STREAM_OP_SEMANTIC_POLICY = stream_default_policy,
	//Available ARITHMETIC_SEMANTIC_POLICY policies:	no_arithmetic_semantic_policy, value_arithmetic_semantic_policy  (Not support for non-compliant compilers like VC++6.0 and BCC55)
class ARITHMETIC_SEMANTIC_POLICY = arithmetic_default_policy>
struct smart_ptr_type
{//Future version of this class will have str

⌨️ 快捷键说明

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