Changeset View
Changeset View
Standalone View
Standalone View
src/tinyformat.h
Show First 20 Lines • Show All 1,016 Lines • ▼ Show 20 Lines | |||||
namespace detail { | namespace detail { | ||||
// Format list subclass with fixed storage to avoid dynamic allocation | // Format list subclass with fixed storage to avoid dynamic allocation | ||||
template <int N> class FormatListN : public FormatList { | template <int N> class FormatListN : public FormatList { | ||||
public: | public: | ||||
#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES | #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES | ||||
template <typename... Args> | template <typename... Args> | ||||
FormatListN(const Args &... args) | FormatListN(const Args &...args) | ||||
: FormatList(&m_formatterStore[0], N), m_formatterStore{ | : FormatList(&m_formatterStore[0], N), m_formatterStore{ | ||||
FormatArg(args)...} { | FormatArg(args)...} { | ||||
static_assert(sizeof...(args) == N, "Number of args must be N"); | static_assert(sizeof...(args) == N, "Number of args must be N"); | ||||
} | } | ||||
#else // C++98 version | #else // C++98 version | ||||
void init(int) {} | void init(int) {} | ||||
#define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \ | #define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \ | ||||
\ | \ | ||||
Show All 38 Lines | |||||
/// Make type-agnostic format list from list of template arguments. | /// Make type-agnostic format list from list of template arguments. | ||||
/// | /// | ||||
/// The exact return type of this function is an implementation detail and | /// The exact return type of this function is an implementation detail and | ||||
/// shouldn't be relied upon. Instead it should be stored as a FormatListRef: | /// shouldn't be relied upon. Instead it should be stored as a FormatListRef: | ||||
/// | /// | ||||
/// FormatListRef formatList = makeFormatList( /*...*/ ); | /// FormatListRef formatList = makeFormatList( /*...*/ ); | ||||
template <typename... Args> | template <typename... Args> | ||||
detail::FormatListN<sizeof...(Args)> makeFormatList(const Args &... args) { | detail::FormatListN<sizeof...(Args)> makeFormatList(const Args &...args) { | ||||
return detail::FormatListN<sizeof...(args)>(args...); | return detail::FormatListN<sizeof...(args)>(args...); | ||||
} | } | ||||
#else // C++98 version | #else // C++98 version | ||||
inline detail::FormatListN<0> makeFormatList() { | inline detail::FormatListN<0> makeFormatList() { | ||||
return detail::FormatListN<0>(); | return detail::FormatListN<0>(); | ||||
} | } | ||||
Show All 14 Lines | |||||
inline void vformat(std::ostream &out, const char *fmt, FormatListRef list) { | inline void vformat(std::ostream &out, const char *fmt, FormatListRef list) { | ||||
detail::formatImpl(out, fmt, list.m_args, list.m_N); | detail::formatImpl(out, fmt, list.m_args, list.m_N); | ||||
} | } | ||||
#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES | #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES | ||||
/// Format list of arguments to the stream according to given format string. | /// Format list of arguments to the stream according to given format string. | ||||
template <typename... Args> | template <typename... Args> | ||||
void format(std::ostream &out, const char *fmt, const Args &... args) { | void format(std::ostream &out, const char *fmt, const Args &...args) { | ||||
vformat(out, fmt, makeFormatList(args...)); | vformat(out, fmt, makeFormatList(args...)); | ||||
} | } | ||||
/// Format list of arguments according to the given format string and return the | /// Format list of arguments according to the given format string and return the | ||||
/// result as a string. | /// result as a string. | ||||
template <typename... Args> | template <typename... Args> | ||||
std::string format(const char *fmt, const Args &... args) { | std::string format(const char *fmt, const Args &...args) { | ||||
std::ostringstream oss; | std::ostringstream oss; | ||||
format(oss, fmt, args...); | format(oss, fmt, args...); | ||||
return oss.str(); | return oss.str(); | ||||
} | } | ||||
/// Format list of arguments to std::cout, according to the given format string | /// Format list of arguments to std::cout, according to the given format string | ||||
template <typename... Args> void printf(const char *fmt, const Args &... args) { | template <typename... Args> void printf(const char *fmt, const Args &...args) { | ||||
format(std::cout, fmt, args...); | format(std::cout, fmt, args...); | ||||
} | } | ||||
template <typename... Args> | template <typename... Args> | ||||
void printfln(const char *fmt, const Args &... args) { | void printfln(const char *fmt, const Args &...args) { | ||||
format(std::cout, fmt, args...); | format(std::cout, fmt, args...); | ||||
std::cout << '\n'; | std::cout << '\n'; | ||||
} | } | ||||
#else // C++98 version | #else // C++98 version | ||||
inline void format(std::ostream &out, const char *fmt) { | inline void format(std::ostream &out, const char *fmt) { | ||||
vformat(out, fmt, makeFormatList()); | vformat(out, fmt, makeFormatList()); | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS) | TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS) | ||||
#undef TINYFORMAT_MAKE_FORMAT_FUNCS | #undef TINYFORMAT_MAKE_FORMAT_FUNCS | ||||
#endif | #endif | ||||
// Added for Bitcoin | // Added for Bitcoin | ||||
template <typename... Args> | template <typename... Args> | ||||
std::string format(const std::string &fmt, const Args &... args) { | std::string format(const std::string &fmt, const Args &...args) { | ||||
std::ostringstream oss; | std::ostringstream oss; | ||||
format(oss, fmt.c_str(), args...); | format(oss, fmt.c_str(), args...); | ||||
return oss.str(); | return oss.str(); | ||||
} | } | ||||
} // namespace tinyformat | } // namespace tinyformat | ||||
// Added for Bitcoin: | // Added for Bitcoin: | ||||
/** | /** | ||||
* Format arguments and return the string or write to given std::ostream (see | * Format arguments and return the string or write to given std::ostream (see | ||||
* tinyformat::format doc for details) | * tinyformat::format doc for details) | ||||
*/ | */ | ||||
#define strprintf tfm::format | #define strprintf tfm::format | ||||
#endif // TINYFORMAT_H_INCLUDED | #endif // TINYFORMAT_H_INCLUDED |