diff options
author | swarmer <swarmer@yandex-team.ru> | 2022-06-13 01:37:00 +0300 |
---|---|---|
committer | swarmer <swarmer@yandex-team.ru> | 2022-06-13 01:37:00 +0300 |
commit | e5e7d3b69b61a7855104d9eddb0b6ff02e7a2979 (patch) | |
tree | 355a99d6e7e7cdb5c269b105f15580e2de445bca | |
parent | 669e8d5ef59e6f49667af117dfee9a43efbf2441 (diff) | |
download | ydb-e5e7d3b69b61a7855104d9eddb0b6ff02e7a2979.tar.gz |
[enum_parser] generate ToStringBuf specialization
IGNIETFERRO-1350
ref:bb9bb1755b6abfc5a27639daf049b8a7cd748fe4
-rw-r--r-- | tools/enum_parser/enum_parser/main.cpp | 9 | ||||
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/dispatch_methods.h | 15 | ||||
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/enum_runtime.h | 35 | ||||
-rw-r--r-- | util/generic/serialized_enum.h | 8 |
4 files changed, 57 insertions, 10 deletions
diff --git a/tools/enum_parser/enum_parser/main.cpp b/tools/enum_parser/enum_parser/main.cpp index 4ad43d4a35..1a93a1a90b 100644 --- a/tools/enum_parser/enum_parser/main.cpp +++ b/tools/enum_parser/enum_parser/main.cpp @@ -310,6 +310,9 @@ void GenerateEnum( // outer ToString if (headerOutPtr) { (*headerOutPtr) << "const TString& ToString(" << name << ");\n"; + (*headerOutPtr) << "Y_FORCE_INLINE TStringBuf ToStringBuf(" << name << " e) {\n"; + (*headerOutPtr) << " return ::NEnumSerializationRuntime::ToStringBuf<" << name << ">(e);\n"; + (*headerOutPtr) << "}\n"; } out << "const TString& ToString(" << name << " x) {\n"; out << " const " << nsNameBufsClass << "& names = " << nsNameBufsClass << "::Instance();\n"; @@ -352,6 +355,12 @@ void GenerateEnum( // specializations for NEnumSerializationRuntime function family out << "namespace NEnumSerializationRuntime {\n"; + // template<> ToStringBuf + out << " template<>\n"; + out << " TStringBuf ToStringBuf<" << name << ">(" << name << " e) {\n"; + out << " return ::NEnumSerializationRuntime::DispatchToStringBufFn<" << nsNameBufsClass << ">(e);\n"; + out << " }\n\n"; + // template<> GetEnumAllValues out << " template<>\n"; out << " TMappedArrayView<" << name <<"> GetEnumAllValuesImpl<" << name << ">() {\n"; diff --git a/tools/enum_parser/enum_serialization_runtime/dispatch_methods.h b/tools/enum_parser/enum_serialization_runtime/dispatch_methods.h index db752ae710..fa2c8aa833 100644 --- a/tools/enum_parser/enum_serialization_runtime/dispatch_methods.h +++ b/tools/enum_parser/enum_serialization_runtime/dispatch_methods.h @@ -27,6 +27,21 @@ namespace NEnumSerializationRuntime { } template <class TNameBufs, typename EEnum> + inline TStringBuf DispatchToStringBufFn(EEnum n) { + constexpr auto order = TNameBufs::NamesOrder; + if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::DirectMapping) { + return TNameBufs::ToStringBufDirect(n, TNameBufs::EnumInitializationData); + } else if constexpr (std::size(TNameBufs::EnumInitializationData.NamesInitializer) <= LINEAR_SEARCH_KEYS_SIZE_THRESHOLD) { + return TNameBufs::ToStringBufFullScan(n, TNameBufs::EnumInitializationData); + } else if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::Ascending) { + return TNameBufs::ToStringBufSorted(n, TNameBufs::EnumInitializationData); + } else { + const TNameBufs& names = TNameBufs::Instance(); + return names.ToStringBuf(n); + } + } + + template <class TNameBufs, typename EEnum> inline EEnum DispatchFromStringImplFn(const char* data, size_t len) { const TStringBuf name{data, len}; constexpr auto order = TNameBufs::ValuesOrder; diff --git a/tools/enum_parser/enum_serialization_runtime/enum_runtime.h b/tools/enum_parser/enum_serialization_runtime/enum_runtime.h index cac142608d..7c85ed3934 100644 --- a/tools/enum_parser/enum_serialization_runtime/enum_runtime.h +++ b/tools/enum_parser/enum_serialization_runtime/enum_runtime.h @@ -102,34 +102,51 @@ namespace NEnumSerializationRuntime { public: using TBase::TBase; + // ToString + // Return reference to singleton preallocated string const TString& ToString(const EEnum key) const { return TBase::ToString(TCast::CastToRepresentationType(key)); } - bool FromString(const TStringBuf name, EEnum& ret) const { - return MapFindResult(TBase::TryFromString(name), ret); + // ToStringBuf + TStringBuf ToStringBuf(EEnum key) const { + return TBase::ToStringBuf(TCast::CastToRepresentationType(key)); } - - EEnum FromString(const TStringBuf name) const { - return TCast::CastFromRepresentationType(TBase::FromString(name)); + static TStringBuf ToStringBufFullScan(const EEnum key, const TInitializationData& enumInitData) { + return TBase::ToStringBufFullScan(TCast::CastToRepresentationType(key), enumInitData); + } + static TStringBuf ToStringBufSorted(const EEnum key, const TInitializationData& enumInitData) { + return TBase::ToStringBufSorted(TCast::CastToRepresentationType(key), enumInitData); + } + static TStringBuf ToStringBufDirect(const EEnum key, const TInitializationData& enumInitData) { + return TBase::ToStringBufDirect(TCast::CastToRepresentationType(key), enumInitData); } + // TryFromString-like functons + // Return false for unknown enumeration names + bool FromString(const TStringBuf name, EEnum& ret) const { + return MapFindResult(TBase::TryFromString(name), ret); + } static bool TryFromStringFullScan(const TStringBuf name, EEnum& ret, const TInitializationData& enumInitData) { return MapFindResult(TBase::TryFromStringFullScan(name, enumInitData), ret); } - static bool TryFromStringSorted(const TStringBuf name, EEnum& ret, const TInitializationData& enumInitData) { return MapFindResult(TBase::TryFromStringSorted(name, enumInitData), ret); } + // FromString + // Throw exception for unknown enumeration names + EEnum FromString(const TStringBuf name) const { + return TCast::CastFromRepresentationType(TBase::FromString(name)); + } static EEnum FromStringFullScan(const TStringBuf name, const TInitializationData& enumInitData) { return TCast::CastFromRepresentationType(TBase::FromStringFullScan(name, enumInitData)); } - static EEnum FromStringSorted(const TStringBuf name, const TInitializationData& enumInitData) { return TCast::CastFromRepresentationType(TBase::FromStringSorted(name, enumInitData)); } + // Inspection TMappedDictView<EEnum, TString> EnumNames() const noexcept { return {TBase::TypelessEnumNames()}; } @@ -138,18 +155,16 @@ namespace NEnumSerializationRuntime { return {TBase::TypelessEnumValues()}; } + // Out void Out(IOutputStream* os, const EEnum key) const { TBase::Out(os, TCast::CastToRepresentationType(key)); } - static void OutFullScan(IOutputStream* os, const EEnum key, const TInitializationData& enumInitData) { TBase::OutFullScan(os, TCast::CastToRepresentationType(key), enumInitData); } - static void OutSorted(IOutputStream* os, const EEnum key, const TInitializationData& enumInitData) { TBase::OutSorted(os, TCast::CastToRepresentationType(key), enumInitData); } - static void OutDirect(IOutputStream* os, const EEnum key, const TInitializationData& enumInitData) { TBase::OutDirect(os, TCast::CastToRepresentationType(key), enumInitData); } diff --git a/util/generic/serialized_enum.h b/util/generic/serialized_enum.h index 79df2bac22..8188c43eed 100644 --- a/util/generic/serialized_enum.h +++ b/util/generic/serialized_enum.h @@ -81,6 +81,14 @@ namespace NEnumSerializationRuntime { */ template <typename EnumT> const TVector<TString>& GetEnumAllCppNamesImpl(); + + /** + * Converts @c e to a string. Works like @c ToString(e) function, but returns @c TStringBuf instead of @c TString. + * Thus works slightly faster and usually avoids any dynamic memory allocation. + * @throw yexception is case of unknown enum value + */ + template <typename EnumT> + TStringBuf ToStringBuf(EnumT e); } /** |