X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FFormat.h;h=b8df86fbfa1e8d02c54251011ea3b570df62e892;hp=3837d1079e9636b31d529a32d39f990e0805057d;hb=e75cf21f157eee116bd55cbffa68de02831ed775;hpb=968945c5e981c50746ba2f71293310a4536f96e1 diff --git a/folly/Format.h b/folly/Format.h index 3837d107..b8df86fb 100644 --- a/folly/Format.h +++ b/folly/Format.h @@ -18,6 +18,7 @@ #define FOLLY_FORMAT_H_ #include +#include #include #include @@ -306,6 +307,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 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.