<commit_before>#ifndef STINGRAYKIT_HOLDER_H
#define STINGRAYKIT_HOLDER_H

// Copyright (c) 2011 - 2017, GS Group, https://github.com/GSGroup
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
// provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#include <stingraykit/function/bind.h>
#include <stingraykit/log/Logger.h>
#include <stingraykit/shared_ptr.h>

namespace stingray
{

	template < typename NativeType >
	class ScopedHolder
	{
		STINGRAYKIT_NONCOPYABLE(ScopedHolder);

	public:
		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()					{ STINGRAYKIT_TRY_NO_MESSAGE(Cleanup()); }

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

		void Clear()
		{ Cleanup(); }

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

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


	template <>
	class ScopedHolder<void>
	{
		STINGRAYKIT_NONCOPYABLE(ScopedHolder);

	public:
		typedef function<void()>	CleanupFuncType;

	private:
		CleanupFuncType	_cleanupFunc;
		bool			_valid;

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

		~ScopedHolder()			{ STINGRAYKIT_TRY_NO_MESSAGE(Cleanup()); }

		bool Valid() const		{ return _valid; }
		void Clear()			{ Cleanup(); _valid = false; }
		void Set()				{ Cleanup(); _valid = true; }

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


	template < typename NativeType >
	class SharedHolder
	{
		typedef ScopedHolder<NativeType> Impl;
		STINGRAYKIT_DECLARE_PTR(Impl);
		typedef function<void(NativeType)>		CleanupFuncType;

	private:
		ImplPtr	_impl;

	public:
		SharedHolder()
		{ }
<commit_msg>Holder: use stingray's exception, cleanup headers<commit_after>#ifndef STINGRAYKIT_HOLDER_H
#define STINGRAYKIT_HOLDER_H

// Copyright (c) 2011 - 2017, GS Group, https://github.com/GSGroup
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
// provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#include <stingraykit/log/Logger.h>

namespace stingray
{

	template < typename NativeType >
	class ScopedHolder
	{
		STINGRAYKIT_NONCOPYABLE(ScopedHolder);

	public:
		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()					{ STINGRAYKIT_TRY_NO_MESSAGE(Cleanup()); }

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

		void Clear()
		{ Cleanup(); }

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

	private:
		void Check() const { STINGRAYKIT_CHECK(_valid, LogicException("ScopedHolder is not valid!")); }
		void Cleanup()
		{
			if (_valid)
				_cleanupFunc(_handle);
			_valid = false;
		}
	};


	template <>
	class ScopedHolder<void>
	{
		STINGRAYKIT_NONCOPYABLE(ScopedHolder);

	public:
		typedef function<void()>	CleanupFuncType;

	private:
		CleanupFuncType	_cleanupFunc;
		bool			_valid;

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

		~ScopedHolder()			{ STINGRAYKIT_TRY_NO_MESSAGE(Cleanup()); }

		bool Valid() const		{ return _valid; }
		void Clear()			{ Cleanup(); _valid = false; }
		void Set()				{ Cleanup(); _valid = true; }

	private:
		void Check() const		{ STINGRAYKIT_CHECK(_valid, LogicException("ScopedHolder is not valid!")); }
		void Cleanup() const	{ if (_valid) _cleanupFunc(); }
	};


	template < typename NativeType >
	class SharedHolder
	{
		typedef ScopedHolder<NativeType> Impl;
		STINGRAYKIT_DECLARE_PTR(Impl);
		typedef function<void(NativeType)>		CleanupFuncType;

	private:
		ImplPtr	_impl;

	public:
		SharedHolder()
		{ }

{'fraction_non_alphanumeric': 0.0978928156628505, 'fraction_numerical': 0.002654720424755268, 'mean_word_length': 4.415992812219227, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '2808595', 'n_tokens_mistral': 2200, 'n_tokens_neox': 1909, 'n_words': 565}