/**********************************************************************
 * 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 "sllconfig.h"

namespace LC::Util
{
	namespace detail
	{
		template<template<typename, typename> class PairType, typename BaseIt, typename Key, typename Value>
		struct Iterator
		{
			using iterator_category = std::bidirectional_iterator_tag;
			using difference_type = std::ptrdiff_t;

			using value_type = PairType<Key, Value>;
			using reference = value_type;
			using pointer = value_type*;

			BaseIt BaseIt_;

			bool operator== (const Iterator& other) const { return BaseIt_ == other.BaseIt_; }
			bool operator!= (const Iterator& other) const { return BaseIt_ != other.BaseIt_; }

			reference operator* () { return { BaseIt_.key (), BaseIt_.value () }; }

			Iterator& operator++ ()
			{
				++BaseIt_;
				return *this;
			}

			Iterator operator++ (int) & { return { BaseIt_++ }; }

			Iterator& operator-- ()
			{
				--BaseIt_;
				return *this;
			}

			Iterator operator-- (int) & { return { BaseIt_-- }; }
		};

		template<typename Iter, typename BaseIter, typename Assoc>
		struct Range
		{
			Assoc Assoc_;

			BaseIter Begin_ = Assoc_.begin ();
			BaseIter End_ = Assoc_.end ();

			auto begin () const { return Iter { Begin_ }; }
			auto end () const { return Iter { End_ }; }
		};
	}

	/** @brief Converts an Qt's associative sequence \em assoc to an
	 * STL-like iteratable range.
	 *
	 * This function takes an associative container \em assoc (one of
	 * Qt's containers like QHash and QMap) and returns a range with
	 * <code>value_type</code> equal to <code>PairType<K, V></code>.
	 *
	 * This way, both the key and the value of each pair in the \em assoc
	 * can be accessed in a range-for loop, for example.
	 *
	 * Example usage:
	 *	\code
		QMap<QString, int> someMap;
		for (const auto& pair : Util::Stlize (someMap))
			qDebug () << pair.first		// outputs a QString key
					<< pair.second;		// outputs an integer value corresponding to the key
		\endcode
	 *
	 * All kinds of accesses are supported: elements of a non-const
	 * container may be modified via the iterators in the returned range.
	 *
	 * @param[in] assoc The Qt's associative container to iterate over.
	 * @return A range with iterators providing access to both the key
	 * and the value via its <code>value_type</code>.
	 *
	 * @tparam PairType The type of the pairs that should be used in the
	 * resulting range's iterators' <code>value_type</code>.
	 * @tparam Assoc The type of the source Qt associative container.
	 */
	template<template<typename K, typename V> class PairType = std::pair, typename Assoc>
	auto Stlize (Assoc&& assoc)
	{
		using BaseIt = decltype (assoc.begin ());
		using Iterator = detail::Iterator<
				PairType,
				BaseIt,
				decltype (BaseIt {}.key ()),
				decltype (BaseIt {}.value ())
			>;
		return detail::Range<Iterator, BaseIt, Assoc> { std::forward<Assoc> (assoc) };
	}

	template<template<typename K, typename V> class PairType = std::pair, typename Assoc>
	auto StlizeCopy (Assoc&& assoc)
	{
		using BaseIt = decltype (assoc.begin ());
		using Iterator = detail::Iterator<
				PairType,
				BaseIt,
				std::decay_t<decltype (BaseIt {}.key ())>,
				std::decay_t<decltype (BaseIt {}.value ())>
			>;
		return detail::Range<Iterator, BaseIt, Assoc> { std::forward<Assoc> (assoc) };
	}
}
