<commit_before>#ifndef STINGRAY_TOOLKIT_HOLDER_H
#define STINGRAY_TOOLKIT_HOLDER_H


#include <stingray/toolkit/bind.h>
#include <stingray/toolkit/shared_ptr.h>


namespace stingray
{


	template < typename NativeType >
	class ScopedHolder
	{
		TOOLKIT_NONCOPYABLE(ScopedHolder);
		typedef function<void(NativeType)>		CleanupFuncType;

	private:
		NativeType		_handle;
		CleanupFuncType	_cleanupFunc;
		bool			_valid;

	public:
		ScopedHolder(const CleanupFuncType& cleanupFunc)
			: _cleanupFunc(cleanupFunc), _valid(false)
		{}

		ScopedHolder(NativeType handle, const CleanupFuncType& cleanupFunc)
			: _handle(handle), _cleanupFunc(cleanupFunc), _valid(true)
		{}

		~ScopedHolder()			{ Cleanup(); }

		bool Valid() const		{ return _valid; }
		NativeType Get() const	{ Check();					return _handle; }
		NativeType Release()	{ Check(); _valid = false;	return _handle; }

		void Clear()
		{
			Cleanup();
			_valid = false;
		}

		void Set(NativeType handle)
		{
			Cleanup();
			_handle = handle;
			_valid = true;
		}

	private:
		void Check() const { TOOLKIT_CHECK(_valid, std::logic_error("ScopedHolder is not valid!")); }
		void Cleanup() const
		{
			if (_valid)
				_cleanupFunc(_handle);
		}
	};


	template <>
	class ScopedHolder<void>
	{
		TOOLKIT_NONCOPYABLE(ScopedHolder);
		typedef function<void()>	CleanupFuncType;

	private:
		CleanupFuncType	_cleanupFunc;
		bool			_valid;

	public:
		ScopedHolder(const CleanupFuncType& cleanupFunc)
			: _cleanupFunc(cleanupFunc), _valid(false)
		{}<commit_msg>Revised handle passing in ScopedHolder to avoid copying<commit_after>#ifndef STINGRAY_TOOLKIT_HOLDER_H
#define STINGRAY_TOOLKIT_HOLDER_H


#include <stingray/toolkit/bind.h>
#include <stingray/toolkit/shared_ptr.h>


namespace stingray
{


	template < typename NativeType >
	class ScopedHolder
	{
		TOOLKIT_NONCOPYABLE(ScopedHolder);
		typedef function<void(NativeType&)>		CleanupFuncType;

	private:
		NativeType		_handle;
		CleanupFuncType	_cleanupFunc;
		bool			_valid;

	public:
		ScopedHolder(const CleanupFuncType& cleanupFunc)
			: _cleanupFunc(cleanupFunc), _valid(false)
		{}

		ScopedHolder(const NativeType& handle, const CleanupFuncType& cleanupFunc)
			: _handle(handle), _cleanupFunc(cleanupFunc), _valid(true)
		{}

		~ScopedHolder()					{ Cleanup(); }

		bool Valid() const				{ return _valid; }
		const NativeType& Get() const	{ Check();					return _handle; }
		NativeType Release()			{ Check(); _valid = false;	return _handle; }

		void Clear()
		{
			Cleanup();
			_valid = false;
		}

		void Set(const NativeType& handle)
		{
			Cleanup();
			_handle = handle;
			_valid = true;
		}

	private:
		void Check() const { TOOLKIT_CHECK(_valid, std::logic_error("ScopedHolder is not valid!")); }
		void Cleanup() const
		{
			if (_valid)
				_cleanupFunc(_handle);
		}
	};


	template <>
	class ScopedHolder<void>
	{
		TOOLKIT_NONCOPYABLE(ScopedHolder);
		typedef function<void()>	CleanupFuncType;

	private:
		CleanupFuncType	_cleanupFunc;
		bool			_valid;

	public:
		ScopedHolder(const CleanupFuncType& cleanupFunc)
			: _cleanupFunc(cleanupFunc), _valid(false)
		{}
{'fraction_non_alphanumeric': 0.10852713178294573, 'fraction_numerical': 0.0, 'mean_word_length': 4.442882249560633, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '1603668', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1080, 'n_words': 222}