diff options
author | dcherednik <dcherednik@ydb.tech> | 2023-03-15 12:17:10 +0300 |
---|---|---|
committer | dcherednik <dcherednik@ydb.tech> | 2023-03-15 12:17:10 +0300 |
commit | 8f966ca56ac29e6faa9d98f99d31d66bd99a2720 (patch) | |
tree | c875e439197cfb49c1f4be73645fb4cabe0f22e1 /util/stream/format.h | |
parent | 3fa1494d4d1918374b848314a33a69806e5b9bdc (diff) | |
download | ydb-8f966ca56ac29e6faa9d98f99d31d66bd99a2720.tar.gz |
TBaseNumber to std::ostream print support.
Diffstat (limited to 'util/stream/format.h')
-rw-r--r-- | util/stream/format.h | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/util/stream/format.h b/util/stream/format.h index b033208a1b..daba3b8a05 100644 --- a/util/stream/format.h +++ b/util/stream/format.h @@ -28,12 +28,28 @@ namespace NFormatPrivate { template <> struct TLog2<1>: std::integral_constant<size_t, 0> {}; - static inline void WriteChars(IOutputStream& os, char c, size_t count) { + template <typename T> + inline void StreamWrite(T& stream, const char* s, size_t size) { + stream.write(s, size); + } + + template <> + inline void StreamWrite(IOutputStream& stream, const char* s, size_t size) { + stream.Write(s, size); + } + + template <> + inline void StreamWrite(TStringStream& stream, const char* s, size_t size) { + stream.Write(s, size); + } + + template <typename T> + static inline void WriteChars(T& os, char c, size_t count) { if (count == 0) return; TTempBuf buf(count); memset(buf.Data(), c, count); - os.Write(buf.Data(), count); + StreamWrite(os, buf.Data(), count); } template <typename T> @@ -106,8 +122,8 @@ namespace NFormatPrivate { template <typename T, size_t Base> using TUnsignedBaseNumber = TBaseNumber<std::make_unsigned_t<std::remove_cv_t<T>>, Base>; - template <typename T, size_t Base> - IOutputStream& operator<<(IOutputStream& stream, const TBaseNumber<T, Base>& value) { + template <typename TStream, typename T, size_t Base> + TStream& ToStreamImpl(TStream& stream, const TBaseNumber<T, Base>& value) { char buf[8 * sizeof(T) + 1]; /* Add 1 for sign. */ TStringBuf str(buf, IntToString<Base>(value.Value, buf, sizeof(buf))); @@ -132,6 +148,16 @@ namespace NFormatPrivate { return stream; } + template <typename T, size_t Base> + IOutputStream& operator<<(IOutputStream& stream, const TBaseNumber<T, Base>& value) { + return ToStreamImpl(stream, value); + } + + template <typename T, size_t Base> + std::ostream& operator<<(std::ostream& stream, const TBaseNumber<T, Base>& value) { + return ToStreamImpl(stream, value); + } + template <typename Char, size_t Base> struct TBaseText { TBasicStringBuf<Char> Text; |