/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#pragma once

#include <memory>
#include <functional>
#include <stdexcept>

namespace LC
{
namespace Util
{
	namespace detail
	{
		using DefaultScopeGuardDeleter = std::function<void ()>;

		class [[nodiscard]] SharedScopeGuard
		{
			std::shared_ptr<void> Guard_;
		public:
			template<typename F>
			SharedScopeGuard (const F& f)
			: Guard_ { nullptr, [f] (void*) { f (); } }
			{
			}

			SharedScopeGuard () = delete;

			SharedScopeGuard (const SharedScopeGuard&) = default;
			SharedScopeGuard (SharedScopeGuard&&) = default;

			SharedScopeGuard& operator= (const SharedScopeGuard&) = default;
			SharedScopeGuard& operator= (SharedScopeGuard&&) = default;
		};

		template<typename F>
		class [[nodiscard]] ScopeGuard
		{
			F F_;
			bool Perform_ = true;
		public:
			ScopeGuard () noexcept
			: F_ {}
			, Perform_ { false }
			{
			}

			ScopeGuard (const F& f) noexcept
			: F_ { f }
			{
			}

			ScopeGuard (const ScopeGuard&) = delete;
			ScopeGuard& operator= (const ScopeGuard&) = delete;

			ScopeGuard& operator= (ScopeGuard&& other)
			{
				if (Perform_)
					F_ ();

				F_ = other.F_;
				Perform_ = other.Perform_;
				other.Perform_ = false;
				return *this;
			}

			ScopeGuard (ScopeGuard&& other) noexcept
			: F_ { other.F_ }
			, Perform_ { other.Perform_ }
			{
				other.Perform_ = false;
			}

			~ScopeGuard ()
			{
				if (Perform_)
					F_ ();
			}

			void Dismiss () noexcept
			{
				Perform_ = false;
			}

			ScopeGuard<DefaultScopeGuardDeleter> EraseType ()
			{
				Dismiss ();
				return ScopeGuard<DefaultScopeGuardDeleter> { F_ };
			}

			operator ScopeGuard<DefaultScopeGuardDeleter> ()
			{
				return EraseType ();
			}

			SharedScopeGuard Shared ()
			{
				if (!Perform_)
					throw std::logic_error { "this scope guard has already been converted to a shared one" };

				Perform_ = false;
				return { F_ };
			}
		};
	}

	using DefaultScopeGuard = detail::ScopeGuard<detail::DefaultScopeGuardDeleter>;

	/** @brief Returns an object performing passed function on scope exit.
	 *
	 * The returned object performs the passed function \em f upon
	 * destruction (and, thus, on scope exit).
	 *
	 * The object is not copyable and not movable, and otherwise is
	 * implementation-defined.
	 *
	 * Typical usage:
	 * \code{.cpp}
		QSettings settings { "OrgNameName", "AppName" };
		settings.beginGroup ();
		const auto guard = Util::MakeScopeGuard ([&settings] { settings.endGroup (); });
		// ...
	   \endcode
	 *
	 * @param[in] f The function to execute on scope exit. Should be a
	 * callable without any arguments.
	 * @return An object executing \em f on destruction.
	 */
	template<typename F>
	[[nodiscard]] detail::ScopeGuard<F> MakeScopeGuard (const F& f)
	{
		return { f };
	}
}
}
