Files

362 lines
11 KiB
C++

/**
* @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 T, typename F>
typename CxxYZ::enable_if<!Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, const F &format, const fmt::ArgList &argList) const
{
fmt::BasicContainerWriter<String> 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 T, typename F>
typename CxxYZ::enable_if<Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, const F &format, const fmt::ArgList &argList) const
{
str.append(fmt::format(format, argList));
}
public:
template<typename F>
void operator()(String &str, const F &format, const fmt::ArgList &argList) const
{
Write(str, format, argList);
}
#else
private:
template<typename T, typename F, typename... ARGS>
typename CxxYZ::enable_if<!Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, F &&format, ARGS &&...args) const
{
fmt::BasicContainerWriter<String> writer(str);
writer.write(CxxYZ::forward<F>(format), CxxYZ::forward<ARGS>(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 T, typename F, typename... ARGS>
typename CxxYZ::enable_if<Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, F &&format, ARGS &&...args) const
{
str.append(fmt::format(format, CxxYZ::forward<ARGS>(args)...));
}
public:
template<typename F, typename... ARGS>
void operator()(String &str, F &&format, ARGS &&...args) const
{
Write(str, CxxYZ::forward<F>(format), CxxYZ::forward<ARGS>(args)...);
}
#endif
};
// ---------------------------------------------------------------------------------------------------------------------------------
class PrintfSyntax
{
public:
#if (defined ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION)
private:
template<typename T, typename F>
typename CxxYZ::enable_if<!Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, F &format, const fmt::ArgList &argList) const
{
fmt::BasicContainerWriter<String> writer(str);
fmt::PrintfFormatter<typename T::value_type>(argList, writer).format(CxxYZ::forward<F>(format));
}
template<typename T, typename F>
typename CxxYZ::enable_if<Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, F &format, const fmt::ArgList &argList) const
{
str.append(fmt::sprintf(format, argList));
}
public:
template<typename F>
void operator()(String &str, const F &format, const fmt::ArgList &argList) const
{
Write(str, CxxYZ::forward<F>(format), argList);
}
#else
private:
template<typename T, typename F, typename... ARGS>
typename CxxYZ::enable_if<!Boost::is_same<typename CxxYZ::decay<T>::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<sizeof...(ARGS)>;
typename ArgArray::Type array
{
ArgArray::template make<fmt::BasicFormatter<String::value_type>>(args)...
};
const auto argList(fmt::ArgList(fmt::internal::make_type(CxxYZ::forward<ARGS>(args)...), array));
fmt::BasicContainerWriter<String> writer(str);
fmt::PrintfFormatter<typename T::value_type>(argList, writer).format(CxxYZ::forward<F>(format));
}
template<typename T, typename F, typename... ARGS>
typename CxxYZ::enable_if<Boost::is_same<typename CxxYZ::decay<T>::type, std::string>::value>::type
Write(T &str, F &&format, ARGS &&...args) const
{
str.append(fmt::sprintf(CxxYZ::forward<F>(format), CxxYZ::forward<ARGS>(args)...));
}
public:
// vstring compatible fmt::sprintf(). Note there seems to be no variadic template version of printf formatters.
template<typename F, typename... ARGS>
void operator()(String &str, F &&format, ARGS &&...args) const
{
Write(str, CxxYZ::forward<F>(format), CxxYZ::forward<ARGS>(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<typename SYNTAX>
class FormatterBase
{
public:
#if (defined ACTIV_FMT_VARIADIC_TEMPLATE_EMULATION)
template<typename T>
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<fmt::BasicFormatter<String::value_type> >(f##n)
#define ACTIV_FMT_CALL_OPERATOR(z, n, data) \
template<ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_FMT_TEMPLATE_PARAM, )> \
const String &operator()( \
ACTIV_BOOST_PP_REPEAT_FROM_TO(0, ACTIV_BOOST_PP_INC(n), ACTIV_FMT_SIGNATURE_PARAM, )) \
{ \
typedef fmt::internal::ArgArray<ACTIV_BOOST_PP_INC(n)> 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<typename T>
FormatterBase(T &&formatString) :
m_formatString(std::forward<T>(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<typename... ARGS>
const String &operator()(ARGS &&...args)
{
SYNTAX()(m_output, m_formatString, std::forward<ARGS>(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<typename F, typename... ARGS>
inline String Format(F &&format, ARGS &&...args)
{
String str;
Detail::FormatSyntax()(str, std::forward<F>(format), std::forward<ARGS>(args)...);
return str;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief vstring compatible fmt::sprintf(). See http://fmtlib.net/latest/api.html
*
* @param format
* @param args...
*
* @return The formatted string.
*/
template<typename F, typename... ARGS>
inline String Sprintf(F &&format, ARGS &&...args)
{
String str;
Detail::PrintfSyntax()(str, std::forward<F>(format), std::forward<ARGS>(args)...);
return str;
}
#endif
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
//! fmt-syntax formatter.
typedef Detail::FormatterBase<Detail::FormatSyntax> Formatter;
//! printf-syntax formatter.
typedef Detail::FormatterBase<Detail::PrintfSyntax> PrintfFormatter;
} // namespace Fmt
} // namespace Activ
#endif // (!defined ACTIV_MISC_FMT_H)