Changeset View
Changeset View
Standalone View
Standalone View
src/tinyformat.h
Show First 20 Lines • Show All 545 Lines • ▼ Show 20 Lines | |||||
namespace detail { | namespace detail { | ||||
// Type-opaque holder for an argument to format(), with associated actions | // Type-opaque holder for an argument to format(), with associated actions | ||||
// 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() | ||||
: m_value(nullptr), m_formatImpl(nullptr), m_toIntImpl(nullptr) {} | |||||
template <typename T> | template <typename T> | ||||
explicit 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 { | ||||
assert(m_value); | |||||
assert(m_formatImpl); | |||||
m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); | m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); | ||||
} | } | ||||
int toInt() const { return m_toIntImpl(m_value); } | int toInt() const { | ||||
assert(m_value); | |||||
assert(m_toIntImpl); | |||||
return m_toIntImpl(m_value); | |||||
} | |||||
private: | private: | ||||
template <typename T> | template <typename T> | ||||
TINYFORMAT_HIDDEN static void | TINYFORMAT_HIDDEN static void | ||||
formatImpl(std::ostream &out, const char *fmtBegin, const char *fmtEnd, | formatImpl(std::ostream &out, const char *fmtBegin, const char *fmtEnd, | ||||
int ntrunc, const void *value) { | int ntrunc, const void *value) { | ||||
formatValue(out, fmtBegin, fmtEnd, ntrunc, | formatValue(out, fmtBegin, fmtEnd, ntrunc, | ||||
*static_cast<const T *>(value)); | *static_cast<const T *>(value)); | ||||
▲ Show 20 Lines • Show All 503 Lines • Show Last 20 Lines |