X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FFormat.h;h=2c5f2fc3c32342f931f82339bb173c15c21621b1;hp=d6fd2ff0c3a038d69da6783415aeda89f355b328;hb=24d6b776bc7f5608d7e553f361eb79f2dcf6d7f7;hpb=2734e379f411f7d9757f9b8d13c020249a6c2dd6 diff --git a/folly/Format.h b/folly/Format.h index d6fd2ff0..2c5f2fc3 100644 --- a/folly/Format.h +++ b/folly/Format.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2012-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,9 +18,11 @@ #define FOLLY_FORMAT_H_ #include +#include #include #include +#include #include #include #include @@ -46,7 +48,7 @@ class FormatValue; // meta-attribute to identify formatters in this sea of template weirdness namespace detail { class FormatterTag {}; -}; +} // namespace detail /** * Formatter class. @@ -306,6 +308,33 @@ inline std::string svformat(StringPiece fmt, Container&& container) { return vformat(fmt, std::forward(container)).str(); } +/** + * Exception class thrown when a format key is not found in the given + * associative container keyed by strings. We inherit std::out_of_range for + * compatibility with callers that expect exception to be thrown directly + * by std::map or std::unordered_map. + * + * Having the key be at the end of the message string, we can access it by + * simply adding its offset to what(). Not storing separate std::string key + * makes the exception type small and noexcept-copyable like std::out_of_range, + * and therefore able to fit in-situ in exception_wrapper. + */ +class FOLLY_EXPORT FormatKeyNotFoundException : public std::out_of_range { + public: + explicit FormatKeyNotFoundException(StringPiece key); + + char const* key() const noexcept { + return what() + kMessagePrefix.size(); + } + + private: + static constexpr StringPiece const kMessagePrefix = "format key not found: "; +}; + +namespace detail { +[[noreturn]] void throwFormatKeyNotFoundException(StringPiece key); +} // namespace detail + /** * Wrap a sequence or associative container so that out-of-range lookups * return a default value rather than throwing an exception. @@ -322,7 +351,7 @@ struct DefaultValueWrapper { const Container& container; const Value& defaultValue; }; -} // namespace +} // namespace detail template detail::DefaultValueWrapper defaulted( @@ -432,7 +461,7 @@ struct IsFormatter< typename std::enable_if< std::is_same::value>:: type> : public std::true_type {}; -} // folly::detail +} // namespace detail // Deprecated API. formatChecked() et. al. now behave identically to their // non-Checked counterparts.