/** * @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 //! Activ namespace. namespace Activ { namespace IntegralStringLiteralDetail { // Get absolute value of a signed integral. template class Abs { public: static constexpr uint64_t value = (VALUE < 0) ? -VALUE : VALUE; }; template<> class Abs { public: static constexpr uint64_t value = static_cast(-(INT64_MIN + 1)) + 1; }; static_assert(Abs::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 class DecimalIntegralStringLiteralImpl3 { public: using type = typename DecimalIntegralStringLiteralImpl3::type; }; template class DecimalIntegralStringLiteralImpl3 { public: // Terminal case for positive. Note NUL termination so c_str() is valid. using type = StringLiteral; }; template class DecimalIntegralStringLiteralImpl3 { public: // Terminal case for negative; prefix with a '-'. Note NUL termination so c_str() is valid. using type = StringLiteral; }; // --------------------------------------------------------------------------------------------------------------------------------- // Used to cope with simple case of provided value of 0. Pass through to DecimalIntegralStringLiteralImpl3 for all other cases. template class DecimalIntegralStringLiteralImpl2 { public: using type = typename DecimalIntegralStringLiteralImpl3::type; }; template class DecimalIntegralStringLiteralImpl2 { public: using type = ACTIV_MAKE_STRING_LITERAL("0"); }; // --------------------------------------------------------------------------------------------------------------------------------- // Used to switch on signedness and get an absolute value for negative integrals. template class DecimalIntegralStringLiteralImpl1; template class DecimalIntegralStringLiteralImpl1 { public: // Unsigned. static_assert(sizeof(typename T::value_type) <= sizeof(uint64_t), "update for new maximum unsigned integral size"); using type = typename DecimalIntegralStringLiteralImpl2::type; }; template class DecimalIntegralStringLiteralImpl1 { 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::value>::type; }; // --------------------------------------------------------------------------------------------------------------------------------- template 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 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::value, NibbleToChar::value, CHARS...>::type; }; template 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 class HexadecimalIntegralStringLiteralImpl1 { public: using type = typename HexadecimalIntegralStringLiteralImpl2::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 class IntegralStringLiteral; // --------------------------------------------------------------------------------------------------------------------------------- template class IntegralStringLiteral : public IntegralStringLiteralDetail::DecimalIntegralStringLiteralImpl1< std::is_signed::value, VALUE>::type { }; // --------------------------------------------------------------------------------------------------------------------------------- template class IntegralStringLiteral : public IntegralStringLiteralDetail::HexadecimalIntegralStringLiteralImpl1::type { static_assert(std::is_unsigned::value, "only unsigned types supported for hexadecimal output"); }; } // namespace Activ #endif // (!defined ACTIV_MISC_INTEGRAL_STRING_LITERAL_H)