diff options
author | ifsmirnov <ifsmirnov@yandex-team.com> | 2023-07-06 22:56:46 +0300 |
---|---|---|
committer | ifsmirnov <ifsmirnov@yandex-team.com> | 2023-07-06 22:56:46 +0300 |
commit | 3edecc646fd526f54d6cca6331130a9aafb52be4 (patch) | |
tree | 89dd719a90774a01e0bf5a123b71bc978c68c5a5 /library/cpp/yt/string/format.h | |
parent | a106ebbbe062aa4b0c17a246be1acf8da6785556 (diff) | |
download | ydb-3edecc646fd526f54d6cca6331130a9aafb52be4.tar.gz |
Introduce TLazyMultiValueFormatter
Diffstat (limited to 'library/cpp/yt/string/format.h')
-rw-r--r-- | library/cpp/yt/string/format.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/library/cpp/yt/string/format.h b/library/cpp/yt/string/format.h index 9708fe5906..8e0d5f82e5 100644 --- a/library/cpp/yt/string/format.h +++ b/library/cpp/yt/string/format.h @@ -107,6 +107,45 @@ TFormatterWrapper<TFormatter> MakeFormatterWrapper( //////////////////////////////////////////////////////////////////////////////// +template <class... TArgs> +class TLazyMultiValueFormatter; + +template <class... TArgs> +void FormatValue( + TStringBuilderBase* builder, + const TLazyMultiValueFormatter<TArgs...>& value, + TStringBuf /*format*/); + +//! A wrapper for a bunch of values that formats them lazily on demand. +/*! + * The intended use of this class is when you need to use the same formatted string + * in several places in the function (e.g. log message tags) and want both to avoid + * code duplication and premature formatting of the values until necessary. + * + * NB: lvalues are captured by reference without lifetime extension. + */ +template <class... TArgs> +class TLazyMultiValueFormatter + : private TNonCopyable +{ +public: + TLazyMultiValueFormatter(TStringBuf format, TArgs&&... args); + + friend void FormatValue<>( + TStringBuilderBase* builder, + const TLazyMultiValueFormatter& value, + TStringBuf /*format*/); + +private: + const TStringBuf Format_; + const std::tuple<TArgs...> Args_; +}; + +template <class ... Args> +auto MakeLazyMultiValueFormatter(TStringBuf format, Args&&... args); + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT #define FORMAT_INL_H_ |