212 lines
6.7 KiB
C++
212 lines
6.7 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/IntegralStringLiteral.h
|
|
*
|
|
* @brief Template string-literal from integral class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_INTEGRAL_STRING_LITERAL_H)
|
|
#define ACTIV_MISC_INTEGRAL_STRING_LITERAL_H
|
|
|
|
#include "ActivMiddleware/Misc/StringLiteral.h"
|
|
|
|
#include <type_traits>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
namespace IntegralStringLiteralDetail
|
|
{
|
|
|
|
// Get absolute value of a signed integral.
|
|
template<int64_t VALUE>
|
|
class Abs
|
|
{
|
|
public:
|
|
static constexpr uint64_t value = (VALUE < 0) ? -VALUE : VALUE;
|
|
};
|
|
|
|
template<>
|
|
class Abs<INT64_MIN>
|
|
{
|
|
public:
|
|
static constexpr uint64_t value = static_cast<uint64_t>(-(INT64_MIN + 1)) + 1;
|
|
};
|
|
|
|
static_assert(Abs<INT64_MIN>::value == UINT64_C(0x8000000000000000), "INT64_MIN conversion to absolute is broken");
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Recursive template to build vararg character list.
|
|
*
|
|
* @tparam IS_NEGATIVE
|
|
* @tparam REMAINING_ABS_VALUE /10 for each invocation.
|
|
* @tparam CHARS vararg character list built so far.
|
|
*/
|
|
template<bool IS_NEGATIVE, uint64_t REMAINING_ABS_VALUE, int... CHARS>
|
|
class DecimalIntegralStringLiteralImpl3
|
|
{
|
|
public:
|
|
using type = typename DecimalIntegralStringLiteralImpl3<IS_NEGATIVE,
|
|
REMAINING_ABS_VALUE / 10, (int('0') + (REMAINING_ABS_VALUE % 10)), CHARS...>::type;
|
|
};
|
|
|
|
template<int... CHARS>
|
|
class DecimalIntegralStringLiteralImpl3<false, 0, CHARS...>
|
|
{
|
|
public:
|
|
// Terminal case for positive. Note NUL termination so c_str() is valid.
|
|
using type = StringLiteral<CHARS..., 0>;
|
|
};
|
|
|
|
template<int... CHARS>
|
|
class DecimalIntegralStringLiteralImpl3<true, 0, CHARS...>
|
|
{
|
|
public:
|
|
// Terminal case for negative; prefix with a '-'. Note NUL termination so c_str() is valid.
|
|
using type = StringLiteral<int('-'), CHARS..., 0>;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Used to cope with simple case of provided value of 0. Pass through to DecimalIntegralStringLiteralImpl3 for all other cases.
|
|
template<bool IS_NEGATIVE, uint64_t ABS_VALUE>
|
|
class DecimalIntegralStringLiteralImpl2
|
|
{
|
|
public:
|
|
using type = typename DecimalIntegralStringLiteralImpl3<IS_NEGATIVE, ABS_VALUE>::type;
|
|
};
|
|
|
|
template<bool IS_NEGATIVE>
|
|
class DecimalIntegralStringLiteralImpl2<IS_NEGATIVE, 0>
|
|
{
|
|
public:
|
|
using type = ACTIV_MAKE_STRING_LITERAL("0");
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Used to switch on signedness and get an absolute value for negative integrals.
|
|
template<bool IS_SIGNED, typename T>
|
|
class DecimalIntegralStringLiteralImpl1;
|
|
|
|
template<typename T>
|
|
class DecimalIntegralStringLiteralImpl1<false, T>
|
|
{
|
|
public:
|
|
// Unsigned.
|
|
static_assert(sizeof(typename T::value_type) <= sizeof(uint64_t), "update for new maximum unsigned integral size");
|
|
|
|
using type = typename DecimalIntegralStringLiteralImpl2<false, T::value>::type;
|
|
};
|
|
|
|
template<typename T>
|
|
class DecimalIntegralStringLiteralImpl1<true, T>
|
|
{
|
|
public:
|
|
// Signed.
|
|
static_assert(sizeof(typename T::value_type) <= sizeof(int64_t), "update for new maximum signed integral size");
|
|
|
|
using type = typename DecimalIntegralStringLiteralImpl2<(T::value < 0), Abs<T::value>::value>::type;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<uint8_t VALUE>
|
|
class NibbleToChar
|
|
{
|
|
static_assert(VALUE <= 15, "ended up with a nibble >= 16");
|
|
|
|
public:
|
|
static constexpr int value = (VALUE < 10) ? (int('0') + VALUE) : (int('a') + VALUE - 10);
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Recursive template to build vararg character list.
|
|
*
|
|
* @tparam REMAINING_ABS_VALUE >>8 for each invocation.
|
|
* @tparam CHARS vararg character list built so far.
|
|
*/
|
|
template<uint64_t REMAINING_VALUE, int... CHARS>
|
|
class HexadecimalIntegralStringLiteralImpl2
|
|
{
|
|
static constexpr uint8_t thisByte = REMAINING_VALUE & 0xff;
|
|
static constexpr byte_t highNibble = (thisByte & 0xf0) >> 4;
|
|
static constexpr byte_t lowNibble = thisByte & 0x0f;
|
|
|
|
public:
|
|
using type = typename HexadecimalIntegralStringLiteralImpl2<(REMAINING_VALUE >> 8),
|
|
NibbleToChar<highNibble>::value, NibbleToChar<lowNibble>::value, CHARS...>::type;
|
|
};
|
|
|
|
template<int... CHARS>
|
|
class HexadecimalIntegralStringLiteralImpl2<0, CHARS...>
|
|
{
|
|
public:
|
|
// Terminal case. Note NUL termination so c_str() is valid.
|
|
using type = StringLiteral<'0', 'x', CHARS..., 0>;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Used to cope with simple case of provided value of 0. Pass through to HexadecimalIntegralStringLiteralImpl2 for all other cases.
|
|
template<uint64_t VALUE>
|
|
class HexadecimalIntegralStringLiteralImpl1
|
|
{
|
|
public:
|
|
using type = typename HexadecimalIntegralStringLiteralImpl2<VALUE>::type;
|
|
};
|
|
|
|
template<>
|
|
class HexadecimalIntegralStringLiteralImpl1<0>
|
|
{
|
|
public:
|
|
using type = ACTIV_MAKE_STRING_LITERAL("0x0");
|
|
};
|
|
|
|
} // namespace IntegralStringLiteralDetail
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Decimal output format.
|
|
class Decimal;
|
|
|
|
//! Hexadecimal output format (lowercase alpha, 0x prefixed).
|
|
class Hexadecimal;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Helper class to make a StringLiteral from an integer.
|
|
*
|
|
* @tparam OUTPUT output format. Decimal, Hexadecimal, etc.
|
|
* @tparam VALUE a std::integral_constant.
|
|
*/
|
|
template<typename OUTPUT, typename VALUE>
|
|
class IntegralStringLiteral;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename VALUE>
|
|
class IntegralStringLiteral<Decimal, VALUE> : public IntegralStringLiteralDetail::DecimalIntegralStringLiteralImpl1<
|
|
std::is_signed<typename VALUE::value_type>::value, VALUE>::type
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename VALUE>
|
|
class IntegralStringLiteral<Hexadecimal, VALUE> :
|
|
public IntegralStringLiteralDetail::HexadecimalIntegralStringLiteralImpl1<VALUE::value>::type
|
|
{
|
|
static_assert(std::is_unsigned<typename VALUE::value_type>::value, "only unsigned types supported for hexadecimal output");
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_INTEGRAL_STRING_LITERAL_H)
|