X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFormat.h;h=8986c4cac847e077a26341027770b549688ade6b;hb=c2e28878d8f88359599da03080fbbe71dac2e80f;hp=02e5d1a893083e63f6864e9e52397963b7643173;hpb=59ac348b103fc91c7c39fe126b84bb45dbaec7b4;p=folly.git diff --git a/folly/Format.h b/folly/Format.h index 02e5d1a8..8986c4ca 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,7 +14,7 @@ * limitations under the License. */ -#ifndef FOLLY_FORMAT_H_ +#pragma once #define FOLLY_FORMAT_H_ #include @@ -22,10 +22,10 @@ #include #include +#include #include -#include #include -#include +#include // Ignore shadowing warnings within this file, so includers can use -Wshadow. #pragma GCC diagnostic push @@ -34,12 +34,14 @@ 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 { @@ -54,9 +56,14 @@ class FormatterTag {}; * 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 { @@ -71,9 +78,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); } @@ -102,13 +109,17 @@ class BaseFormatter { typedef BaseFormatter BaseType; private: - typedef std::tuple::type>...> ValueTuple; + typedef std::tuple::type>...> + 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); } @@ -116,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); } } @@ -127,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(std::get(values_), arg); + } + return getSizeArgFrom(i, arg); + } + + int getSizeArg(size_t i, const FormatArg& arg) const { + return getSizeArgFrom<0>(i, arg); + } + StringPiece str_; protected: @@ -148,23 +196,30 @@ class BaseFormatter { }; 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); } - friend class BaseFormatter, - containerMode, - Args...>; + friend class BaseFormatter< + Formatter, + containerMode, + Args...>; template friend Formatter format(StringPiece fmt, A&&... arg); @@ -175,10 +230,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; } @@ -187,8 +245,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. @@ -199,8 +258,7 @@ void writeTo(FILE* fp, */ template Formatter format(StringPiece fmt, Args&&... args) { - return Formatter( - fmt, std::forward(args)...); + return Formatter(fmt, std::forward(args)...); } /** @@ -227,8 +285,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)); } /** @@ -248,20 +305,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); } @@ -310,25 +367,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. @@ -376,8 +436,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 @@ -395,10 +456,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_ */