X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FFormat.h;h=d6fd2ff0c3a038d69da6783415aeda89f355b328;hp=7ffc1527ba3d1528377351e2a8aaa6fbec3027e5;hb=3179d4464f4d211d18bb784a7d6c97390035e266;hpb=4ecf6e3f6d120907c0e32059a923d15cd6fcfedc diff --git a/folly/Format.h b/folly/Format.h index 7ffc1527..d6fd2ff0 100644 --- a/folly/Format.h +++ b/folly/Format.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,42 +14,34 @@ * limitations under the License. */ -#ifndef FOLLY_FORMAT_H_ +#pragma once #define FOLLY_FORMAT_H_ -#include #include #include #include -#include -#include -#include -#include -#include - -#include #include +#include #include -#include -#include #include -#include -#include +#include // Ignore shadowing warnings within this file, so includers can use -Wshadow. -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wshadow" +FOLLY_PUSH_WARNING +FOLLY_GCC_DISABLE_WARNING("-Wshadow") namespace folly { // forward declarations -template class Formatter; +template +class Formatter; template Formatter format(StringPiece fmt, Args&&... args); template Formatter vformat(StringPiece fmt, C&& container); -template class FormatValue; +template +class FormatValue; // meta-attribute to identify formatters in this sea of template weirdness namespace detail { @@ -59,14 +51,20 @@ class FormatterTag {}; /** * Formatter class. * - * Note that this class is tricky, as it keeps *references* to its arguments - * (and doesn't copy the passed-in format string). Thankfully, you can't use - * this directly, you have to use format(...) below. + * Note that this class is tricky, as it keeps *references* to its lvalue + * arguments (while it takes ownership of the temporaries), and it doesn't + * copy the passed-in format string. Thankfully, you can't use 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 BaseFormatter { @@ -81,9 +79,9 @@ class BaseFormatter { * Append to a string. */ template - typename std::enable_if::value>::type - appendTo(Str& str) const { - auto appender = [&str] (StringPiece s) { str.append(s.data(), s.size()); }; + typename std::enable_if::value>::type appendTo( + Str& str) const { + auto appender = [&str](StringPiece s) { str.append(s.data(), s.size()); }; (*this)(appender); } @@ -106,19 +104,22 @@ class BaseFormatter { } /** - * metadata to identify generated children of BaseFormatter + * Metadata to identify generated children of BaseFormatter */ typedef detail::FormatterTag IsFormatter; typedef BaseFormatter BaseType; private: - typedef std::tuple::type>...> ValueTuple; + typedef std::tuple ValueTuple; static constexpr size_t valueCount = std::tuple_size::value; + Derived const& asDerived() const { + return *static_cast(this); + } + template typename std::enable_if::type - doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const { + doFormatFrom(size_t i, FormatArg& arg, Callback& /*cb*/) const { arg.error("argument index out of range, max=", i); } @@ -126,9 +127,9 @@ class BaseFormatter { typename std::enable_if<(K < valueCount)>::type doFormatFrom(size_t i, FormatArg& arg, Callback& cb) const { if (i == K) { - static_cast(this)->template doFormatArg(arg, cb); + asDerived().template doFormatArg(arg, cb); } else { - doFormatFrom(i, arg, cb); + doFormatFrom(i, arg, cb); } } @@ -137,6 +138,43 @@ class BaseFormatter { return doFormatFrom<0>(i, arg, cb); } + template + typename std::enable_if::type getSizeArgFrom( + size_t i, + const FormatArg& arg) const { + arg.error("argument index out of range, max=", i); + } + + template + typename std::enable_if< + std::is_integral::value && !std::is_same::value, + int>::type + getValue(const FormatValue& format, const FormatArg&) const { + return static_cast(format.getValue()); + } + + template + typename std::enable_if< + !std::is_integral::value || std::is_same::value, + int>::type + getValue(const FormatValue&, const FormatArg& arg) const { + arg.error("dynamic field width argument must be integral"); + } + + template + typename std::enable_if < + K::type getSizeArgFrom(size_t i, const FormatArg& arg) + const { + if (i == K) { + return getValue(getFormatValue(), arg); + } + return getSizeArgFrom(i, arg); + } + + int getSizeArg(size_t i, const FormatArg& arg) const { + return getSizeArgFrom<0>(i, arg); + } + StringPiece str_; protected: @@ -150,31 +188,47 @@ class BaseFormatter { // for the exclusive use of format() (below). This way, you can't create // a Formatter object, but can handle references to it (for streaming, // conversion to string, etc) -- which is good, as Formatter objects are - // dangerous (they hold references, possibly to temporaries) + // dangerous (they may hold references). BaseFormatter(BaseFormatter&&) = default; BaseFormatter& operator=(BaseFormatter&&) = default; + template + using ArgType = typename std::tuple_element::type; + + template + FormatValue>::type> getFormatValue() const { + return FormatValue>::type>( + std::get(values_)); + } + ValueTuple values_; }; template -class Formatter : public BaseFormatter, - containerMode, - Args...> { +class Formatter : public BaseFormatter< + Formatter, + containerMode, + Args...> { private: explicit Formatter(StringPiece& str, Args&&... args) - : BaseFormatter, - containerMode, - Args...>(str, std::forward(args)...) {} + : BaseFormatter< + Formatter, + containerMode, + Args...>(str, std::forward(args)...) { + static_assert( + !containerMode || sizeof...(Args) == 1, + "Exactly one argument required in container mode"); + } template void doFormatArg(FormatArg& arg, Callback& cb) const { - std::get(this->values_).format(arg, cb); + this->template getFormatValue().format(arg, cb); } - friend class BaseFormatter, - containerMode, - Args...>; + friend class BaseFormatter< + Formatter, + containerMode, + Args...>; template friend Formatter format(StringPiece fmt, A&&... arg); @@ -185,10 +239,13 @@ class Formatter : public BaseFormatter, /** * Formatter objects can be written to streams. */ -template -std::ostream& operator<<(std::ostream& out, - const Formatter& formatter) { - auto writer = [&out] (StringPiece sp) { out.write(sp.data(), sp.size()); }; +template +std::ostream& operator<<( + std::ostream& out, + const Formatter& formatter) { + auto writer = [&out](StringPiece sp) { + out.write(sp.data(), std::streamsize(sp.size())); + }; formatter(writer); return out; } @@ -197,8 +254,9 @@ std::ostream& operator<<(std::ostream& out, * Formatter objects can be written to stdio FILEs. */ template -void writeTo(FILE* fp, - const BaseFormatter& formatter); +void writeTo( + FILE* fp, + const BaseFormatter& formatter); /** * Create a formatter object. @@ -209,8 +267,7 @@ void writeTo(FILE* fp, */ template Formatter format(StringPiece fmt, Args&&... args) { - return Formatter( - fmt, std::forward(args)...); + return Formatter(fmt, std::forward(args)...); } /** @@ -237,8 +294,7 @@ inline std::string sformat(StringPiece fmt, Args&&... args) { */ template Formatter vformat(StringPiece fmt, Container&& container) { - return Formatter( - fmt, std::forward(container)); + return Formatter(fmt, std::forward(container)); } /** @@ -258,20 +314,20 @@ inline std::string svformat(StringPiece fmt, Container&& container) { * format("[no_such_key"], defaulted(map, 42)) -> 42 */ namespace detail { -template struct DefaultValueWrapper { +template +struct DefaultValueWrapper { DefaultValueWrapper(const Container& container, const Value& defaultValue) - : container(container), - defaultValue(defaultValue) { - } + : container(container), defaultValue(defaultValue) {} const Container& container; const Value& defaultValue; }; -} // namespace +} // namespace template -detail::DefaultValueWrapper -defaulted(const Container& c, const Value& v) { +detail::DefaultValueWrapper defaulted( + const Container& c, + const Value& v) { return detail::DefaultValueWrapper(c, v); } @@ -320,25 +376,28 @@ void formatString(StringPiece val, FormatArg& arg, FormatCallback& cb); * field width") */ template -void formatNumber(StringPiece val, int prefixLen, FormatArg& arg, - FormatCallback& cb); - +void formatNumber( + StringPiece val, + int prefixLen, + FormatArg& arg, + FormatCallback& cb); /** * Format a Formatter object recursively. Behaves just like * formatString(fmt.str(), arg, cb); but avoids creating a temporary * string if possible. */ -template +template < + class FormatCallback, + class Derived, + bool containerMode, + class... Args> void formatFormatter( const BaseFormatter& formatter, FormatArg& arg, FormatCallback& cb); -} // namespace format_value +} // namespace format_value /* * Specialize folly::FormatValue for your type. @@ -386,8 +445,9 @@ inline std::string sformatChecked(StringPiece fmt, Args&&... args) { return formatChecked(fmt, std::forward(args)...).str(); } template -Formatter vformatChecked(StringPiece fmt, - Container&& container) { +Formatter vformatChecked( + StringPiece fmt, + Container&& container) { return vformat(fmt, std::forward(container)); } template @@ -405,10 +465,8 @@ vformatChecked(Str* out, StringPiece fmt, Container&& container) { vformatChecked(fmt, std::forward(container)).appendTo(*out); } -} // namespace folly +} // namespace folly #include -#pragma GCC diagnostic pop - -#endif /* FOLLY_FORMAT_H_ */ +FOLLY_POP_WARNING