Args&&... args)
: str_(str),
values_(FormatValue<typename std::decay<Args>::type>(
- std::forward<Args>(args))...) {
- static_assert(
- !containerMode || sizeof...(Args) == 1,
- "Exactly one argument required in container mode");
-}
+ std::forward<Args>(args))...) {}
template <class Derived, bool containerMode, class... Args>
template <class Output>
arg.widthIndex == FormatArg::kNoIndex,
"cannot provide width arg index without value arg index");
int sizeArg = nextArg++;
- arg.width = getSizeArg(size_t(sizeArg), arg);
+ arg.width = asDerived().getSizeArg(size_t(sizeArg), arg);
}
argIndex = nextArg++;
arg.enforce(
arg.widthIndex != FormatArg::kNoIndex,
"cannot provide value arg index without width arg index");
- arg.width = getSizeArg(size_t(arg.widthIndex), arg);
+ arg.width = asDerived().getSizeArg(size_t(arg.widthIndex), arg);
}
try {
"folly::format: may not have both default and explicit arg indexes");
}
- doFormat(size_t(argIndex), arg, out);
+ asDerived().template doFormat(size_t(argIndex), arg, out);
}
}
* this directly, you have to use format(...) below.
*/
-/* BaseFormatter class. Currently, the only behavior that can be
- * overridden is the actual formatting of positional parameters in
+/* BaseFormatter class.
+ * Overridable behaviours:
+ * You may override the actual formatting of positional parameters in
* `doFormatArg`. The Formatter class provides the default implementation.
+ *
+ * You may also override `doFormat` and `getSizeArg`. These override points were
+ * added to permit static analysis of format strings, when it is inconvenient
+ * or impossible to instantiate a BaseFormatter with the correct storage
*/
template <class Derived, bool containerMode, class... Args>
class BaseFormatter {
ValueTuple;
static constexpr size_t valueCount = std::tuple_size<ValueTuple>::value;
+ Derived const& asDerived() const {
+ return *static_cast<const Derived*>(this);
+ }
+
template <size_t K, class Callback>
typename std::enable_if<K == valueCount>::type
doFormatFrom(size_t i, FormatArg& arg, Callback& /*cb*/) const {
typename std::enable_if<(K < valueCount)>::type
doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const {
if (i == K) {
- static_cast<const Derived*>(this)->template doFormatArg<K>(arg, cb);
+ asDerived().template doFormatArg<K>(arg, cb);
} else {
doFormatFrom<K + 1>(i, arg, cb);
}
: BaseFormatter<
Formatter<containerMode, Args...>,
containerMode,
- Args...>(str, std::forward<Args>(args)...) {}
+ Args...>(str, std::forward<Args>(args)...) {
+ static_assert(
+ !containerMode || sizeof...(Args) == 1,
+ "Exactly one argument required in container mode");
+ }
template <size_t K, class Callback>
void doFormatArg(FormatArg& arg, Callback& cb) const {