/*******************************************************************************
 *
 * File:     type_helper.hpp
 *
 * Contents: Helper facilities for type-related stuff.
 *
 * Copyright 2017
 *
 * Author: David Juhasz (david.juhasz@tuwien.ac.at)
 *
 ******************************************************************************/

#ifndef ROSA_SUPPORT_TYPE_HELPER_HPP
#define ROSA_SUPPORT_TYPE_HELPER_HPP

#include <cstdint>
#include <type_traits>

namespace rosa {

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

// 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.
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;
};

template <typename T, bool IsConst, bool IsEnum>
struct PrintableType<T, true, IsConst, IsEnum> {
  using Type =
      typename PrintableType<typename std::remove_reference<T>::type>::Type;
};

template <typename T, bool IsEnum>
struct PrintableType<T, false, false, IsEnum> {
  using Type = typename PrintableType<const T>::Type;
};

template <typename T>
struct PrintableType<T, false, true, true> {
  using Type =
      typename PrintableType<typename std::underlying_type<T>::type>::Type;
};

template <>
struct PrintableType<const uint8_t, false, true, false> {
  using Type = const unsigned int;
};

template <>
struct PrintableType<const int8_t, false, true, false> {
  using Type = const int;
};

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

// Helper preprocessor macro to cast values to their printable types.
#define PRINTABLE(V) static_cast<printable_t<decltype(V)>>(V)

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

// Provides the unsigned integer type corresponding to T if T is an integral
// (except bool) or enumeration type. Keeps T otherwise.
template <typename T, bool IsIntegral = std::is_integral<T>::value>
struct Unsigned {
  using Type = T;
};

template <typename T>
struct Unsigned<T, true> {
  using Type = typename std::make_unsigned<T>::type;
};

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

} // End namespace rosa

#endif // ROSA_SUPPORT_TYPE_HELPER_HPP

