/** * @file include/ActivMiddleware/Misc/Fmt.h * * @brief Wrappers for http://fmtlib.net. */ #if (!defined ACTIV_MISC_FMT_H) #define ACTIV_MISC_FMT_H #include "ActivMiddleware/ActivMiddleware.h" #define FMT_HEADER_ONLY #include "ActivMiddleware/Misc/fmt/format.h" #include "ActivMiddleware/Misc/fmt/printf.h" #include "ActivMiddleware/Misc/fmt/container.h" #if (defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) || (defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES) //! Maximum arity of variadic emulation. #define ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION 30 #include ACTIV_BOOST_INCLUDE(preprocessor/arithmetic/inc.hpp) #include ACTIV_BOOST_INCLUDE(preprocessor/punctuation/comma_if.hpp) #include ACTIV_BOOST_INCLUDE(preprocessor/repetition/repeat_from_to.hpp) #endif //! Activ namespace. namespace Activ { //! Fmt namespace. namespace Fmt { namespace Detail { class FormatSyntax { #if (defined ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION) private: template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, const F &format, const fmt::ArgList &argList) const { fmt::BasicContainerWriter writer(str); writer.write(format, argList); // Fix an issue where formatting a trailing double will result in a null as the last char. // This will break code that tries to insert nulls into strings but I doubt anyone will be doing that. if (!str.empty() && ('\0' == str[str.size() - 1])) str.resize(str.size() - 1); } template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, const F &format, const fmt::ArgList &argList) const { str.append(fmt::format(format, argList)); } public: template void operator()(String &str, const F &format, const fmt::ArgList &argList) const { Write(str, format, argList); } #else private: template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, F &&format, ARGS &&...args) const { fmt::BasicContainerWriter writer(str); writer.write(CxxYZ::forward(format), CxxYZ::forward(args)...); // Fix an issue where formatting a trailing double will result in a null as the last char. // This will break code that tries to insert nulls into strings but I doubt anyone will be doing that. if (!str.empty() && ('\0' == str[str.size() - 1])) str.resize(str.size() - 1); } template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, F &&format, ARGS &&...args) const { str.append(fmt::format(format, CxxYZ::forward(args)...)); } public: template void operator()(String &str, F &&format, ARGS &&...args) const { Write(str, CxxYZ::forward(format), CxxYZ::forward(args)...); } #endif }; // --------------------------------------------------------------------------------------------------------------------------------- class PrintfSyntax { public: #if (defined ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION) private: template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, F &format, const fmt::ArgList &argList) const { fmt::BasicContainerWriter writer(str); fmt::PrintfFormatter(argList, writer).format(CxxYZ::forward(format)); } template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, F &format, const fmt::ArgList &argList) const { str.append(fmt::sprintf(format, argList)); } public: template void operator()(String &str, const F &format, const fmt::ArgList &argList) const { Write(str, CxxYZ::forward(format), argList); } #else private: template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, F &&format, ARGS &&...args) const { // Seems there's no variadic PrintfFormatter, so construct an ArgList. using ArgArray = fmt::internal::ArgArray; typename ArgArray::Type array { ArgArray::template make>(args)... }; const auto argList(fmt::ArgList(fmt::internal::make_type(CxxYZ::forward(args)...), array)); fmt::BasicContainerWriter writer(str); fmt::PrintfFormatter(argList, writer).format(CxxYZ::forward(format)); } template typename CxxYZ::enable_if::type, std::string>::value>::type Write(T &str, F &&format, ARGS &&...args) const { str.append(fmt::sprintf(CxxYZ::forward(format), CxxYZ::forward(args)...)); } public: // vstring compatible fmt::sprintf(). Note there seems to be no variadic template version of printf formatters. template void operator()(String &str, F &&format, ARGS &&...args) const { Write(str, CxxYZ::forward(format), CxxYZ::forward(args)...); } #endif }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief BoostFormat somewhat-alike formatter. * * Currently, fmtlib has no parse-format-string-once capability. But it's on the way: https://github.com/fmtlib/fmt/issues/618 * So this is sub-optimal for now and is just for convenience porting from BoostFormat. Hopefully once #618 is implemented * the interface won't have to change too much. * * Unlike BoostFormat % operator, this class will continue to append to the built string with successive calls to operator(). * * @tparam SYNTAX PrintfSyntax or FormatSyntax. */ template class FormatterBase { public: #if (defined ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION) template FormatterBase(const T &formatString) : m_formatString(formatString) { } #define ACTIV_FMT_TEMPLATE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) typename F##n #define ACTIV_FMT_SIGNATURE_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) const F##n &f##n #define ACTIV_FMT_PARAM(z, n, data) ACTIV_BOOST_PP_COMMA_IF(n) f##n #define ACTIV_FMT_MAKE_ARG(z, n, data) \ ACTIV_BOOST_PP_COMMA_IF(n) ArgArray::template make >(f##n) #define ACTIV_FMT_CALL_OPERATOR(z, n, data) \ template \ const String &operator()( \ ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_FMT_SIGNATURE_PARAM, )) \ { \ typedef fmt::internal::ArgArray ArgArray; \ typename ArgArray::Type array = \ { \ ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_FMT_MAKE_ARG, ) \ }; \ const fmt::ArgList argList(fmt::internal::make_type( \ ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_FMT_PARAM, )), array); \ SYNTAX()(m_output, m_formatString, argList); \ return m_output; \ } ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION, ACTIV_FMT_CALL_OPERATOR, ) #undef ACTIV_FMT_CALL_OPERATOR #undef ACTIV_FMT_MAKE_ARG #undef ACTIV_FMT_PARAM #undef ACTIV_FMT_SIGNATURE_PARAM #undef ACTIV_FMT_TEMPLATE_PARAM #else /** * @brief Constructor. * * @param formatString */ template FormatterBase(T &&formatString) : m_formatString(std::forward(formatString)) { } /** * @brief Append output. * * Note unlike BoostFormat, this does no validation that sizeof...(args) matches the number of format specifiers in * the format string provided to the constructor. * * @param args... * * @return The current built string. */ template const String &operator()(ARGS &&...args) { SYNTAX()(m_output, m_formatString, std::forward(args)...); return m_output; } #endif //! Clear down the built string. void Reset() { m_output.clear(); } //! Get the built string. const String &ToString() const { return m_output; } private: const String m_formatString; String m_output; }; } // namespace Detail // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- #if (defined ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION) /** * Note: ArgList can hold at most 15 elements. */ inline String Format(fmt::CStringRef format, const fmt::ArgList &argList) { String str; Detail::FormatSyntax()(str, format, argList); return ACTIV_BOOST_MOVE_RET(String, str); } FMT_VARIADIC(String, Format, fmt::CStringRef); // --------------------------------------------------------------------------------------------------------------------------------- /** * Note: ArgList can hold at most 15 elements. */ inline String Sprintf(fmt::CStringRef format, const fmt::ArgList &argList) { String str; Detail::PrintfSyntax()(str, format, argList); return ACTIV_BOOST_MOVE_RET(String, str); } FMT_VARIADIC(String, Sprintf, fmt::CStringRef); // --------------------------------------------------------------------------------------------------------------------------------- #else /** * @brief vstring compatible fmt::format(). See http://fmtlib.net/latest/api.html * * @param format * @param args... * * @return The formatted string. */ template inline String Format(F &&format, ARGS &&...args) { String str; Detail::FormatSyntax()(str, std::forward(format), std::forward(args)...); return str; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief vstring compatible fmt::sprintf(). See http://fmtlib.net/latest/api.html * * @param format * @param args... * * @return The formatted string. */ template inline String Sprintf(F &&format, ARGS &&...args) { String str; Detail::PrintfSyntax()(str, std::forward(format), std::forward(args)...); return str; } #endif // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- //! fmt-syntax formatter. typedef Detail::FormatterBase Formatter; //! printf-syntax formatter. typedef Detail::FormatterBase PrintfFormatter; } // namespace Fmt } // namespace Activ #endif // (!defined ACTIV_MISC_FMT_H)