/***************************************************************************//**
 *
 * \file rosa/support/type_helper.hpp
 *
 * \author David Juhasz (david.juhasz@tuwien.ac.at)
 *
 * \date 2017
 *
 * \brief Helper facilities for type-related stuff.
 *
 ******************************************************************************/

#ifndef ROSA_SUPPORT_TYPE_HELPER_HPP
#define ROSA_SUPPORT_TYPE_HELPER_HPP

#include <cstdint>
#include <type_traits>

namespace rosa {

/* ************************************************************************** *
 *                                 Printable                                  *
 * ************************************************************************** */

/// \defgroup PrintableType
///
/// A value of type `[u]int8_t` is treated as a character when being put to an
/// output stream, which can result in invisible characters being printed. To
/// avoid that, such a value needs to be casted to a wider type. It can be done
/// by using the following template to find a target type to cast our value to.
/// The template also turns enumerations into their underlying types. Moreover,
/// any reference type is turned into the referred type and any non-const type
/// is turned into their const-qualified type.
///
/// \note It is important to remove references before checking
/// const-qualification because constant references are not const-qualified
/// types.
///
/// The corresponding printable type for a type `T` can be obtained as \code
/// typename PrintableType<T>::Type
/// \endcode
///@{

/// Definition for the general case
///
/// \tparam T type to cast
/// \tparam IsReference always use the default value!
/// \tparam IsConst always use the default value!
/// \tparam IsEnum always use the default value!
template <typename T, bool IsReference = std::is_reference<T>::value,
          bool IsConst = std::is_const<T>::value,
          bool IsEnum = std::is_enum<T>::value>
struct PrintableType {
  using Type = T;
};

/// Specialization for reference types.
template <typename T, bool IsConst, bool IsEnum>
struct PrintableType<T, true, IsConst, IsEnum> {
  using Type =
      typename PrintableType<typename std::remove_reference<T>::type>::Type;
};

/// Specialization for non-reference, non-const types.
template <typename T, bool IsEnum>
struct PrintableType<T, false, false, IsEnum> {
  using Type = typename PrintableType<const T>::Type;
};

/// Specialization for non-reference, const, enum types.
template <typename T>
struct PrintableType<T, false, true, true> {
  using Type =
      typename PrintableType<typename std::underlying_type<T>::type>::Type;
};

/// Specialization for `const uint8_t`.
template <>
struct PrintableType<const uint8_t, false, true, false> {
  using Type = const unsigned int;
};

/// Specialization for `const int8_t`.
template <>
struct PrintableType<const int8_t, false, true, false> {
  using Type = const int;
};

///@}

/// Convenience template alias for using `rosa::PrintableType`.
template<typename T>
using printable_t = typename PrintableType<T>::Type;

/// Casts values to their corresponding printable types.
///
/// \param V value to cast
#define PRINTABLE(V) static_cast<printable_t<decltype(V)>>(V)

/* ************************************************************************** *
 *                                  Unsigned                                  *
 * ************************************************************************** */

/// \defgroup Unsigned
/// \brief Converts integral types to their corresponding unsigned type.
///
/// Provides the unsigned integer type corresponding to `T` if `T` is an
/// integral (except bool) or enumeration type. Keeps `T` otherwise.
///
/// The corresponding unsigned type for a type `T` can be obtained as \code
/// typename Unsigned<T>::Type
/// \endcode
///@{

/// Definition for the general case, used when `T` is not integral.
///
/// \tparam T type to convert
/// \tparam IsIntegral always use the default value!
template <typename T, bool IsIntegral = std::is_integral<T>::value>
struct Unsigned {
  using Type = T;
};

/// Specialization for the case when `T` is integral.
template <typename T>
struct Unsigned<T, true> {
  using Type = typename std::make_unsigned<T>::type;
};

///@}

/// Convenience template alias for using `rosa::Unsigned`.
template <typename T>
using unsigned_t = typename Unsigned<T>::Type;

} // End namespace rosa

#endif // ROSA_SUPPORT_TYPE_HELPER_HPP

