/*!
 * \file    integralconstant.h
 * \brief   Template meta-programming integral constant
 *
 * Copyright (C) 2018-2019 Christos Choutouridis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __utl_meta_integralconstant_h__
#define __utl_meta_integralconstant_h__

#include <utl/core/impl.h>
#include <type_traits>
#include <utility>

/*!
 * \ingroup meta
 * \defgroup integral
 * integral constant support header
 */
//! @{

namespace utl {
namespace meta {

   /*!
    * Empty type
    * utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
    * Each time someone applies ::type to it, he gets back nil_. This way we can prevent
    * a lot of compilation errors in a wrong meta:: handling.
    */
   struct nil_ {
      using type = nil_;
   };

   //! Type alias for \p Tp::type. Used to evaluate/extract return type of metafunctions
   template <typename Tp>
   using eval = typename Tp::type;

   //! integral_
   //! Integral Constant is a holder class for a compile-time value of an integral type.
   //! Every Integral Constant is also a null-ary Metafunction, returning itself.
   //! An integral constant object is implicitly convertible to the corresponding
   //! run-time value of the wrapped integral type
   //! @{
   template <typename Tp, Tp v>
   using integral_ = std::integral_constant<Tp, v>;
   //! @}

   //! Wrappers for basic types
   //! @{

   //! bool_ type: integral constant wrapper for bool
   template<bool v>
   using bool_ = integral_<bool, v>;

   using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
   using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.

   //! int8_ type: integral constant wrapper for \c int8_t
   template<int8_t v>
   using int8_ = integral_<int8_t, v>;
   //! uint8_ type: integral constant wrapper for \c uint8_t
   template<uint8_t v>
   using uint8_ = integral_<uint8_t, v>;

   //! int16_ type: integral constant wrapper for \c int16_t
   template<int16_t v>
   using int16_ = integral_<int16_t, v>;
   //! uint16_ type: integral constant wrapper for \c uint16_t
   template<uint16_t v>
   using uint16_ = integral_<uint16_t, v>;

   //! int32_ type: integral constant wrapper for \c int32_t
   template<int32_t v>
   using int32_ = integral_<int32_t, v>;
   //! uint32_ type: integral constant wrapper for \c uint32_t
   template<uint32_t v>
   using uint32_ = integral_<uint32_t, v>;

   //! char_ type: integral constant wrapper for \c char
   template<char v>
   using char_ = integral_<char, v>;

   //! int_ type: integral constant wrapper for \c int
   template<int v>
   using int_ = integral_<int, v>;

   //! long_ type: integral constant wrapper for \c long
   template<long v>
   using long_ = integral_<long, v>;

   //! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
   template<index_t v>
   using index_ = integral_<index_t, v>;

   //! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
   template<size_t v>
   using size_ = integral_<size_t, v>;

   //! Computes the size of the type \p Tp.
   //! Complexity \f$ O(1) \f$.
   template <typename Tp>
   using sizeof_ = size_<sizeof(Tp)>;

   //! Computes the alignment required for any instance of the type \p Tp.
   //! Complexity \f$ O(1) \f$.
   template <typename Tp>
   using alignof_ = size_<alignof(Tp)>;
   //! @}

   //! The last position we can express for indexing
   using Npos = size_<index_t(-1)>;

   //! integer sequence
   //! @{
   template< class Tp, Tp... Ints >
   using integer_sequence = std::integer_sequence<Tp, Ints...>;

   template<typename Tp, Tp Num>
   using make_integer_sequence  = std::make_integer_sequence<Tp, Num>;

   //! Alias template index_sequence
   template<index_t... Idx>
   using index_sequence = integer_sequence<index_t, Idx...>;

   //! Alias template make_index_sequence
   template<index_t Num>
   using make_index_sequence = make_integer_sequence <index_t, Num>;

   //! Alias template index_sequence_for
   template<typename... Types>
   using index_sequence_for = make_index_sequence<sizeof...(Types)>;

   //! @}
}}

//!@}

#endif /* __utl_meta_integralconstant_h__ */
