<commit_before>// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Basic smart pointer for scalar and array types
// Note: this class is not thread safe
// evilrix 2009

namespace devtools {

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This functor will delete a pointer to a scalar object
template <typename ptrT>
struct delete_scalar_policy
{
	void operator()(ptrT * & px) { delete px; px = 0; }
};

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This functor will delete a pointer to an array of object
template <typename ptrT>
struct delete_array_policy
{
	void operator()(ptrT * & px) { delete [] px; px = 0; }
};

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This is the smart pointer template, which takes pointer type and
// a destruct policy, which it uses to destruct object(s) pointed to
// when the reference counter for the object becomes zero.
template <
	typename ptrT,
	template <
		typename ptrT
		> class destruct_policy
	>
class smart_ptr
{
private:
	typedef delete_scalar_policy<ptrT> delete_policy_;
	typedef void (smart_ptr::*safe_bool_t)();
	typedef int refcnt_t;

public:
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// Make a nice typedef for the pointer type
	typedef ptrT ptr_type;
	
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// dc_tor, c_tor and cc_tor
	smart_ptr() : px_(0), pn_(0) {}
	
	explicit smart_ptr(ptr_type * px) :
			px_(px), pn_(0) { pn_ = new int(1); }
	
	smart_ptr(smart_ptr const & o) : 
			px_(o.px_), pn_(o.pn_) { ++*pn_;}
	
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=	
	// d_tor, deletes the pointer using the destruct policy when the
	// reference counter for the object reaches zero
	~smart_ptr()
	{
		try
		{
			if(0 == --*pn_)
			{
				delete_policy_()(px_);
				delete pn_; pn_ = 0;
			}
		}
		catch(...) 
		{
			// Ignored. Prevent percolation during stack unwinding.
		}<commit_msg>Fix error in destructor where it dereferences null<commit_after>// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Basic smart pointer for scalar and array types
// Note: this class is not thread safe
// evilrix 2009

namespace devtools {

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This functor will delete a pointer to a scalar object
template <typename ptrT>
struct delete_scalar_policy
{
	void operator()(ptrT * & px) { delete px; px = 0; }
};

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This functor will delete a pointer to an array of object
template <typename ptrT>
struct delete_array_policy
{
	void operator()(ptrT * & px) { delete [] px; px = 0; }
};

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This is the smart pointer template, which takes pointer type and
// a destruct policy, which it uses to destruct object(s) pointed to
// when the reference counter for the object becomes zero.
template <
	typename ptrT,
	template <
		typename ptrT
		> class destruct_policy
	>
class smart_ptr
{
private:
	typedef delete_scalar_policy<ptrT> delete_policy_;
	typedef void (smart_ptr::*safe_bool_t)();
	typedef int refcnt_t;

public:
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// Make a nice typedef for the pointer type
	typedef ptrT ptr_type;
	
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// dc_tor, c_tor and cc_tor
	smart_ptr() : px_(0), pn_(0) {}
	
	explicit smart_ptr(ptr_type * px) :
			px_(px), pn_(0) { pn_ = new int(1); }
	
	smart_ptr(smart_ptr const & o) : 
			px_(o.px_), pn_(o.pn_) { ++*pn_;}
	
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=	
	// d_tor, deletes the pointer using the destruct policy when the
	// reference counter for the object reaches zero
	~smart_ptr()
	{
		try
		{
			if(pn_ && 0 == --*pn_)
			{
				delete_policy_()(px_);
				delete pn_;;
			}
		}
		catch(...) 
		{
			// Ignored. Prevent percolation during stack unwinding.
		}
{'fraction_non_alphanumeric': 0.31135802469135804, 'fraction_numerical': 0.005679012345679012, 'mean_word_length': 2.389958158995816, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6262183', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1324, 'n_words': 370}