Changeset View
Changeset View
Standalone View
Standalone View
src/tinyformat.h
Show First 20 Lines • Show All 548 Lines • ▼ Show 20 Lines | namespace detail { | ||||
// on the type held as explicit function pointers. This allows FormatArg's | // on the type held as explicit function pointers. This allows FormatArg's | ||||
// for each argument to be allocated as a homogenous array inside FormatList | // for each argument to be allocated as a homogenous array inside FormatList | ||||
// whereas a naive implementation based on inheritance does not. | // whereas a naive implementation based on inheritance does not. | ||||
class FormatArg { | class FormatArg { | ||||
public: | public: | ||||
FormatArg() {} | FormatArg() {} | ||||
template <typename T> | template <typename T> | ||||
FormatArg(const T &value) | explicit FormatArg(const T &value) | ||||
: m_value(static_cast<const void *>(&value)), | : m_value(static_cast<const void *>(&value)), | ||||
m_formatImpl(&formatImpl<T>), m_toIntImpl(&toIntImpl<T>) {} | m_formatImpl(&formatImpl<T>), m_toIntImpl(&toIntImpl<T>) {} | ||||
void format(std::ostream &out, const char *fmtBegin, const char *fmtEnd, | void format(std::ostream &out, const char *fmtBegin, const char *fmtEnd, | ||||
int ntrunc) const { | int ntrunc) const { | ||||
m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); | m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 334 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) | explicit 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) \ | ||||
\ | \ | ||||
template <TINYFORMAT_ARGTYPES(n)> \ | template <TINYFORMAT_ARGTYPES(n)> \ | ||||
FormatListN(TINYFORMAT_VARARGS(n)) : FormatList(&m_formatterStore[0], n) { \ | explicit FormatListN(TINYFORMAT_VARARGS(n)) \ | ||||
: FormatList(&m_formatterStore[0], n) { \ | |||||
assert(n == N); \ | assert(n == N); \ | ||||
init(0, TINYFORMAT_PASSARGS(n)); \ | init(0, TINYFORMAT_PASSARGS(n)); \ | ||||
} \ | } \ | ||||
\ | \ | ||||
template <TINYFORMAT_ARGTYPES(n)> \ | template <TINYFORMAT_ARGTYPES(n)> \ | ||||
void init(int i, TINYFORMAT_VARARGS(n)) { \ | void init(int i, TINYFORMAT_VARARGS(n)) { \ | ||||
m_formatterStore[i] = FormatArg(v1); \ | m_formatterStore[i] = FormatArg(v1); \ | ||||
init(i + 1 TINYFORMAT_PASSARGS_TAIL(n)); \ | init(i + 1 TINYFORMAT_PASSARGS_TAIL(n)); \ | ||||
▲ Show 20 Lines • Show All 150 Lines • Show Last 20 Lines |