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

📄 clone_ptr.hpp

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

/*
// clone_ptr class by David Maisonave (Axter)
// Copyright (C) 2005
// David Maisonave (Axter) (609-345-1007) (www.axter.com)
//
// 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 and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  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.
Description:
The clone_ptr class is a smart pointer class that can be used
with an STL container to create a container of smart pointers.
The main purpose of the clone_ptr, is to make it easier to create
a container of abstract based objects.
The clone_ptr does not share it's pointer, nor does it move it's pointer,
and moreover, it's based on the idea of strict pointer ownership logic.
The main difference between clone_ptr and other similar smart pointers
is that the clone_ptr has a clone interface, which is used to create
a copy of the derived object.  The clone interface is used in
the clone_ptr copy constructor and in the assignment operator.

The clone_ptr can also be used with sorted containers (std::map, std::set).
When used with sorted containers, the base class must have an operator<() function. (See example code.)

****** For more detailed description and example usage see following links: *******
Example Program:
http://code.axter.com/clone_ptr_demo.zip
Detail description:
http://axter.com/lib/

See example program for example usage.

*/
// @cond INCLUDE_ALL_OBJS_

#include "value_semantic_ptr.h"
#include "pointer_semantic_ptr.h"

typedef void clone_smart_ptr_default_allocate;

template<class T, class ALLOCATE_POLICY = clone_smart_ptr_default_allocate >
class clone_ptr
{
	typedef T * ( *clone_fct_Type ) ( T *, bool);
	clone_fct_Type m_clone_fct;
	T* m_type;
public:
	//clone_ptr will only clone type that is pass to the constructor
	template<typename T_obj>
		clone_ptr(T_obj* type):m_type(type), m_clone_fct(get_clone_funct(type, (ALLOCATE_POLICY*)0)){}
	//Destructor
	~clone_ptr()throw(){m_type=m_clone_fct(m_type, false);}
	//Copy constructor
	clone_ptr(const clone_ptr& Src):m_type(Src.m_clone_fct(Src.m_type, true)), m_clone_fct(Src.m_clone_fct){}
	//Assignment operator
	clone_ptr& operator=(const clone_ptr& Src){return assign(Src);}
	template<class CompatibleT>
		clone_ptr& operator=(const clone_ptr<CompatibleT>& Src){return assign(Src);}
	T* operator->() const{return m_type;}
	T& operator*() const{return *m_type;}
	const T* const c_ptr()const{return  m_type;}
	const T& c_ref()const{return  *m_type;}
	T* get_ptr(){return m_type;}
	//Other Misc methods
	void swap(clone_ptr<T> & other)throw(){std::swap(m_type, other.m_type);std::swap(m_func_ptr_interface, other.m_func_ptr_interface);}
private:
	template<class CompatibleSmartPtr>
		clone_ptr& assign(CompatibleSmartPtr& Src)
	{
		if (m_type != Src.m_type)
		{
			m_clone_fct(m_type, false);
			m_type = Src.m_clone_fct(Src.m_type, true);
			m_clone_fct = Src.m_clone_fct;
		}
		return *this;
	}
	template <typename D>
	clone_fct_Type get_clone_funct(D* , clone_smart_ptr_default_allocate*){return  construct_and_destruct_default_allocator<T, D>;}
	template <typename D, class FUNC_ALLOC_POLICY>
	clone_fct_Type get_clone_funct(D* , FUNC_ALLOC_POLICY*){return  construct_and_destruct<T,D, FUNC_ALLOC_POLICY>;}
	// The clone function: (Thanks to Kai-Uwe Bux)
	template < typename T, typename DT> static T *  construct_and_destruct_default_allocator(T *  ptr, bool bConstruct) {
		if (bConstruct){return new DT(*static_cast<DT*>(ptr));}
		delete ptr;
		return NULL;
	} 
	template < typename T, typename D, class FUNC_ALLOC_POLICY> T *  construct_and_destruct(T *  ptr, bool bConstruct) {
		D * Obj = static_cast<D*>( ptr );
		if (bConstruct)
		{
			D* tmp_ptr = NULL;
			FUNC_ALLOC_POLICY::allocate(tmp_ptr);
			FUNC_ALLOC_POLICY::construct(tmp_ptr, *(Obj));
			return tmp_ptr;
		}
		FUNC_ALLOC_POLICY::destroy(Obj);
		FUNC_ALLOC_POLICY::deallocate(Obj);
		return NULL;
	} 
};
// @endcond 

#endif //!clone_ptr_H_HEADER_GUARD_

⌨️ 快捷键说明

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