diff options
author | swarmer <swarmer@yandex-team.ru> | 2022-02-10 16:46:31 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:31 +0300 |
commit | 11a24635da4c4f39428b182c49a7bc35e47c9534 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /tools/enum_parser | |
parent | 317da38588b7898a99fd9168571408123350012b (diff) | |
download | ydb-11a24635da4c4f39428b182c49a7bc35e47c9534.tar.gz |
Restoring authorship annotation for <swarmer@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'tools/enum_parser')
-rw-r--r-- | tools/enum_parser/enum_parser/main.cpp | 200 | ||||
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/README.md | 4 | ||||
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp | 192 | ||||
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/enum_runtime.h | 232 | ||||
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/ya.make | 26 | ||||
-rw-r--r-- | tools/enum_parser/ya.make | 2 |
6 files changed, 328 insertions, 328 deletions
diff --git a/tools/enum_parser/enum_parser/main.cpp b/tools/enum_parser/enum_parser/main.cpp index 66bf6c88c2..0943c69c1d 100644 --- a/tools/enum_parser/enum_parser/main.cpp +++ b/tools/enum_parser/enum_parser/main.cpp @@ -10,10 +10,10 @@ #include <util/stream/mem.h> #include <util/charset/wide.h> -#include <util/string/builder.h> +#include <util/string/builder.h> #include <util/string/strip.h> #include <util/string/cast.h> -#include <util/string/join.h> +#include <util/string/join.h> #include <util/string/subst.h> #include <util/generic/map.h> #include <util/generic/string.h> @@ -27,7 +27,7 @@ void WriteHeader(const TString& headerName, IOutputStream& out, IOutputStream* headerOutPtr = nullptr) { out << "// This file was auto-generated. Do not edit!!!\n"; out << "#include " << headerName << "\n"; - out << "#include <tools/enum_parser/enum_serialization_runtime/enum_runtime.h>\n\n"; + out << "#include <tools/enum_parser/enum_serialization_runtime/enum_runtime.h>\n\n"; out << "#include <tools/enum_parser/enum_parser/stdlib_deps.h>\n\n"; out << "#include <util/generic/typetraits.h>\n"; out << "#include <util/generic/singleton.h>\n"; @@ -128,10 +128,10 @@ static inline void CloseArray(TStringStream& out) { out << "]\n"; } -static TString WrapStringBuf(const TStringBuf str) { +static TString WrapStringBuf(const TStringBuf str) { return TString::Join("TStringBuf(\"", str, "\")"); -} - +} + void GenerateEnum( const TEnumParser::TEnum& en, IOutputStream& out, @@ -197,18 +197,18 @@ void GenerateEnum( strValue = it.Aliases[0]; OutKey(jEnumItem, "str_value", strValue); } - nameInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(strValue) + ")"); - cppNamesInitializer.push_back(WrapStringBuf(it.CppName)); + nameInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(strValue) + ")"); + cppNamesInitializer.push_back(WrapStringBuf(it.CppName)); for (const auto& alias : it.Aliases) { - valueInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(alias) + ")"); + valueInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(alias) + ")"); OutItem(jAliases, alias); } FinishItems(jAliases); CloseArray(jAliases); if (!it.Aliases) { - valueInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(it.CppName) + ")"); + valueInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(it.CppName) + ")"); } OutKey(jEnumItem, "aliases", jAliases.Str(), false); @@ -221,83 +221,83 @@ void GenerateEnum( CloseArray(jItems); OutKey(jEnum, "items", jItems.Str(), false); - auto defineConstArray = [&out, payloadCache = TMap<std::pair<TString, TVector<TString>>, TString>()](const TStringBuf indent, const TStringBuf elementType, const TStringBuf name, const TVector<TString>& items) mutable { - if (items.empty()) { // ISO C++ forbids zero-size array - out << indent << "static constexpr const TArrayRef<const " << elementType << "> " << name << ";\n"; - } else { - // try to reuse one of the previous payload arrays - const auto inserted = payloadCache.emplace(std::make_pair(elementType, items), ToString(name) + "_PAYLOAD"); - const TString& payloadStorageName = inserted.first->second; - if (inserted.second) { // new array content or type - out << indent << "static constexpr const " << elementType << " " << payloadStorageName << "[" << items.size() << "]{\n"; - for (const auto& it : items) { - out << indent << " " << it << ",\n"; - } - out << indent << "};\n"; - } - out << indent << "static constexpr const TArrayRef<const " << elementType << "> " << name << "{" << payloadStorageName << "};\n"; - } - out << "\n"; - }; - - out << " class TNameBufs : public ::NEnumSerializationRuntime::TEnumDescription<" << name << "> {\n"; - out << " public:\n"; - out << " using TBase = ::NEnumSerializationRuntime::TEnumDescription<" << name << ">;\n\n"; - out << " inline TNameBufs();\n\n"; - - // Instance - out << " static inline const TNameBufs& Instance() {\n"; + auto defineConstArray = [&out, payloadCache = TMap<std::pair<TString, TVector<TString>>, TString>()](const TStringBuf indent, const TStringBuf elementType, const TStringBuf name, const TVector<TString>& items) mutable { + if (items.empty()) { // ISO C++ forbids zero-size array + out << indent << "static constexpr const TArrayRef<const " << elementType << "> " << name << ";\n"; + } else { + // try to reuse one of the previous payload arrays + const auto inserted = payloadCache.emplace(std::make_pair(elementType, items), ToString(name) + "_PAYLOAD"); + const TString& payloadStorageName = inserted.first->second; + if (inserted.second) { // new array content or type + out << indent << "static constexpr const " << elementType << " " << payloadStorageName << "[" << items.size() << "]{\n"; + for (const auto& it : items) { + out << indent << " " << it << ",\n"; + } + out << indent << "};\n"; + } + out << indent << "static constexpr const TArrayRef<const " << elementType << "> " << name << "{" << payloadStorageName << "};\n"; + } + out << "\n"; + }; + + out << " class TNameBufs : public ::NEnumSerializationRuntime::TEnumDescription<" << name << "> {\n"; + out << " public:\n"; + out << " using TBase = ::NEnumSerializationRuntime::TEnumDescription<" << name << ">;\n\n"; + out << " inline TNameBufs();\n\n"; + + // Instance + out << " static inline const TNameBufs& Instance() {\n"; out << " return *SingletonWithPriority<TNameBufs, 0>();\n"; // destroy enum serializers last, because it may be used from destructor of another global object - out << " }\n"; - out << " };\n\n"; - - // Initialization data - defineConstArray(" ", "TNameBufs::TBase::TEnumStringPair", "NAMES_INITIALIZATION_PAIRS", nameInitializerPairs); - defineConstArray(" ", "TNameBufs::TBase::TEnumStringPair", "VALUES_INITIALIZATION_PAIRS", valueInitializerPairs); - defineConstArray(" ", "TStringBuf", "CPP_NAMES_INITIALIZATION_ARRAY", cppNamesInitializer); - - out << " static constexpr const TNameBufs::TInitializationData ENUM_INITIALIZATION_DATA{\n"; - out << " NAMES_INITIALIZATION_PAIRS,\n"; - out << " VALUES_INITIALIZATION_PAIRS,\n"; - out << " CPP_NAMES_INITIALIZATION_ARRAY,\n"; - out << " " << WrapStringBuf(outerScopeStr) << ",\n"; - out << " " << WrapStringBuf(name) << "\n"; - out << " };\n\n"; - - // Constructor - out << " inline TNameBufs::TNameBufs()\n"; - out << " : TBase(ENUM_INITIALIZATION_DATA)\n"; - out << " {\n"; - out << " }\n\n"; + out << " }\n"; + out << " };\n\n"; + + // Initialization data + defineConstArray(" ", "TNameBufs::TBase::TEnumStringPair", "NAMES_INITIALIZATION_PAIRS", nameInitializerPairs); + defineConstArray(" ", "TNameBufs::TBase::TEnumStringPair", "VALUES_INITIALIZATION_PAIRS", valueInitializerPairs); + defineConstArray(" ", "TStringBuf", "CPP_NAMES_INITIALIZATION_ARRAY", cppNamesInitializer); + + out << " static constexpr const TNameBufs::TInitializationData ENUM_INITIALIZATION_DATA{\n"; + out << " NAMES_INITIALIZATION_PAIRS,\n"; + out << " VALUES_INITIALIZATION_PAIRS,\n"; + out << " CPP_NAMES_INITIALIZATION_ARRAY,\n"; + out << " " << WrapStringBuf(outerScopeStr) << ",\n"; + out << " " << WrapStringBuf(name) << "\n"; + out << " };\n\n"; + + // Constructor + out << " inline TNameBufs::TNameBufs()\n"; + out << " : TBase(ENUM_INITIALIZATION_DATA)\n"; + out << " {\n"; + out << " }\n\n"; out << "}}\n\n"; - if (headerOutPtr) { - (*headerOutPtr) << "// I/O for " << name << "\n"; - } - + if (headerOutPtr) { + (*headerOutPtr) << "// I/O for " << name << "\n"; + } + // outer ToString - if (headerOutPtr) { - (*headerOutPtr) << "const TString& ToString(" << name << ");\n"; - } + if (headerOutPtr) { + (*headerOutPtr) << "const TString& ToString(" << name << ");\n"; + } out << "const TString& ToString(" << name << " x) {\n"; out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; out << " return names.ToString(x);\n"; out << "}\n\n"; // outer FromString - if (headerOutPtr) { - (*headerOutPtr) << "bool FromString(const TString& name, " << name << "& ret);\n"; - } + if (headerOutPtr) { + (*headerOutPtr) << "bool FromString(const TString& name, " << name << "& ret);\n"; + } out << "bool FromString(const TString& name, " << name << "& ret) {\n"; out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; out << " return names.FromString(name, ret);\n"; out << "}\n\n"; // outer FromString - if (headerOutPtr) { - (*headerOutPtr) << "bool FromString(const TStringBuf& name, " << name << "& ret);\n"; - } + if (headerOutPtr) { + (*headerOutPtr) << "bool FromString(const TStringBuf& name, " << name << "& ret);\n"; + } out << "bool FromString(const TStringBuf& name, " << name << "& ret) {\n"; out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; out << " return names.FromString(name, ret);\n"; @@ -319,40 +319,40 @@ void GenerateEnum( // outer Out out << "template<>\n"; out << "void Out<" << name << ">(IOutputStream& os, TTypeTraits<" << name << ">::TFuncParam n) {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.Out(&os, n);\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.Out(&os, n);\n"; out << "}\n\n"; - // specializations for NEnumSerializationRuntime function family - out << "namespace NEnumSerializationRuntime {\n"; - // template<> GetEnumAllValues - out << " template<>\n"; - out << " TMappedArrayView<" << name <<"> GetEnumAllValuesImpl<" << name << ">() {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.AllEnumValues();\n"; - out << " }\n\n"; - - // template<> GetEnumAllNames - out << " template<>\n"; - out << " const TString& GetEnumAllNamesImpl<" << name << ">() {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.AllEnumNames();\n"; - out << " }\n\n"; + // specializations for NEnumSerializationRuntime function family + out << "namespace NEnumSerializationRuntime {\n"; + // template<> GetEnumAllValues + out << " template<>\n"; + out << " TMappedArrayView<" << name <<"> GetEnumAllValuesImpl<" << name << ">() {\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.AllEnumValues();\n"; + out << " }\n\n"; + + // template<> GetEnumAllNames + out << " template<>\n"; + out << " const TString& GetEnumAllNamesImpl<" << name << ">() {\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.AllEnumNames();\n"; + out << " }\n\n"; // template<> GetEnumNames<EnumType> - out << " template<>\n"; - out << " TMappedDictView<" << name << ", TString> GetEnumNamesImpl<" << name << ">() {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.EnumNames();\n"; - out << " }\n\n"; - - // template<> GetEnumAllCppNames, see IGNIETFERRO-534 - out << " template<>\n"; - out << " const TVector<TString>& GetEnumAllCppNamesImpl<" << name << ">() {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.AllEnumCppNames();\n"; - out << " }\n"; - + out << " template<>\n"; + out << " TMappedDictView<" << name << ", TString> GetEnumNamesImpl<" << name << ">() {\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.EnumNames();\n"; + out << " }\n\n"; + + // template<> GetEnumAllCppNames, see IGNIETFERRO-534 + out << " template<>\n"; + out << " const TVector<TString>& GetEnumAllCppNamesImpl<" << name << ">() {\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.AllEnumCppNames();\n"; + out << " }\n"; + out << "}\n\n"; if (headerOutPtr) { diff --git a/tools/enum_parser/enum_serialization_runtime/README.md b/tools/enum_parser/enum_serialization_runtime/README.md index 0f87800b00..7d2cbae4ce 100644 --- a/tools/enum_parser/enum_serialization_runtime/README.md +++ b/tools/enum_parser/enum_serialization_runtime/README.md @@ -1,2 +1,2 @@ -This library should not be used or referred directly. -Use `GENERATE_ENUM_SERIALIZATION_WITH_HEADER` and `GENERATE_ENUM_SERIALIZATION` macros instead. +This library should not be used or referred directly. +Use `GENERATE_ENUM_SERIALIZATION_WITH_HEADER` and `GENERATE_ENUM_SERIALIZATION` macros instead. diff --git a/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp b/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp index 80a38fab91..73d38b396a 100644 --- a/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp +++ b/tools/enum_parser/enum_serialization_runtime/enum_runtime.cpp @@ -1,100 +1,100 @@ -#include "enum_runtime.h" - -#include <util/generic/map.h> -#include <util/generic/yexception.h> -#include <util/stream/output.h> - -namespace NEnumSerializationRuntime { - template <typename TEnumRepresentationType> - [[noreturn]] static void ThrowUndefinedValueException(const TEnumRepresentationType key, const TStringBuf className) { - throw yexception() << "Undefined value " << key << " in " << className << ". "; - } - - template <typename TEnumRepresentationType> - const TString& TEnumDescriptionBase<TEnumRepresentationType>::ToString(TRepresentationType key) const { - const auto it = Names.find(key); - if (Y_LIKELY(it != Names.end())) { - return it->second; - } - ThrowUndefinedValueException(key, ClassName); - } - - template <typename TEnumRepresentationType> - std::pair<bool, TEnumRepresentationType> TEnumDescriptionBase<TEnumRepresentationType>::TryFromString(const TStringBuf name) const { - const auto it = Values.find(name); - if (it != Values.end()) { - return {true, it->second}; - } - return {false, TRepresentationType()}; - } - - [[noreturn]] static void ThrowUndefinedNameException(const TStringBuf name, const TStringBuf className, const TStringBuf allEnumNames) { - ythrow yexception() << "Key '" << name << "' not found in enum " << className << ". Valid options are: " << allEnumNames << ". "; - } - - template <typename TEnumRepresentationType> - auto TEnumDescriptionBase<TEnumRepresentationType>::FromString(const TStringBuf name) const -> TRepresentationType { - const auto findResult = TryFromString(name); - if (Y_LIKELY(findResult.first)) { - return findResult.second; - } - ThrowUndefinedNameException(name, ClassName, AllEnumNames()); - } - - template <typename TEnumRepresentationType> - void TEnumDescriptionBase<TEnumRepresentationType>::Out(IOutputStream* os, const TRepresentationType key) const { - (*os) << this->ToString(key); - } - - template <typename TEnumRepresentationType> - TEnumDescriptionBase<TEnumRepresentationType>::TEnumDescriptionBase(const TInitializationData& enumInitData) - : ClassName(enumInitData.ClassName) - { - const TArrayRef<const TEnumStringPair>& namesInitializer = enumInitData.NamesInitializer; - const TArrayRef<const TEnumStringPair>& valuesInitializer = enumInitData.ValuesInitializer; - const TArrayRef<const TStringBuf>& cppNamesInitializer = enumInitData.CppNamesInitializer; - - TMap<TRepresentationType, TString> mapValueToName; - TMap<TString, TRepresentationType> mapNameToValue; +#include "enum_runtime.h" + +#include <util/generic/map.h> +#include <util/generic/yexception.h> +#include <util/stream/output.h> + +namespace NEnumSerializationRuntime { + template <typename TEnumRepresentationType> + [[noreturn]] static void ThrowUndefinedValueException(const TEnumRepresentationType key, const TStringBuf className) { + throw yexception() << "Undefined value " << key << " in " << className << ". "; + } + + template <typename TEnumRepresentationType> + const TString& TEnumDescriptionBase<TEnumRepresentationType>::ToString(TRepresentationType key) const { + const auto it = Names.find(key); + if (Y_LIKELY(it != Names.end())) { + return it->second; + } + ThrowUndefinedValueException(key, ClassName); + } + + template <typename TEnumRepresentationType> + std::pair<bool, TEnumRepresentationType> TEnumDescriptionBase<TEnumRepresentationType>::TryFromString(const TStringBuf name) const { + const auto it = Values.find(name); + if (it != Values.end()) { + return {true, it->second}; + } + return {false, TRepresentationType()}; + } + + [[noreturn]] static void ThrowUndefinedNameException(const TStringBuf name, const TStringBuf className, const TStringBuf allEnumNames) { + ythrow yexception() << "Key '" << name << "' not found in enum " << className << ". Valid options are: " << allEnumNames << ". "; + } + + template <typename TEnumRepresentationType> + auto TEnumDescriptionBase<TEnumRepresentationType>::FromString(const TStringBuf name) const -> TRepresentationType { + const auto findResult = TryFromString(name); + if (Y_LIKELY(findResult.first)) { + return findResult.second; + } + ThrowUndefinedNameException(name, ClassName, AllEnumNames()); + } + + template <typename TEnumRepresentationType> + void TEnumDescriptionBase<TEnumRepresentationType>::Out(IOutputStream* os, const TRepresentationType key) const { + (*os) << this->ToString(key); + } + + template <typename TEnumRepresentationType> + TEnumDescriptionBase<TEnumRepresentationType>::TEnumDescriptionBase(const TInitializationData& enumInitData) + : ClassName(enumInitData.ClassName) + { + const TArrayRef<const TEnumStringPair>& namesInitializer = enumInitData.NamesInitializer; + const TArrayRef<const TEnumStringPair>& valuesInitializer = enumInitData.ValuesInitializer; + const TArrayRef<const TStringBuf>& cppNamesInitializer = enumInitData.CppNamesInitializer; + + TMap<TRepresentationType, TString> mapValueToName; + TMap<TString, TRepresentationType> mapNameToValue; const bool bijectiveHint = (namesInitializer.data() == valuesInitializer.data() && namesInitializer.size() == valuesInitializer.size()); - if (bijectiveHint) { - for (const TEnumStringPair& it : namesInitializer) { - TString name{it.Name}; - mapValueToName.emplace(it.Key, name); - mapNameToValue.emplace(std::move(name), it.Key); - } - } else { - for (const TEnumStringPair& it : namesInitializer) { + if (bijectiveHint) { + for (const TEnumStringPair& it : namesInitializer) { + TString name{it.Name}; + mapValueToName.emplace(it.Key, name); + mapNameToValue.emplace(std::move(name), it.Key); + } + } else { + for (const TEnumStringPair& it : namesInitializer) { mapValueToName.emplace(it.Key, TString(it.Name)); - } - for (const TEnumStringPair& it : valuesInitializer) { + } + for (const TEnumStringPair& it : valuesInitializer) { mapNameToValue.emplace(TString(it.Name), it.Key); - } - } - Names = std::move(mapValueToName); - Values = std::move(mapNameToValue); - - AllValues.reserve(Names.size()); - for (const auto& it : Names) { + } + } + Names = std::move(mapValueToName); + Values = std::move(mapNameToValue); + + AllValues.reserve(Names.size()); + for (const auto& it : Names) { if (!AllNames.empty()) { - AllNames += ", "; - } - AllNames += TString::Join('\'', it.second, '\''); - AllValues.push_back(it.first); - } - - AllCppNames.reserve(cppNamesInitializer.size()); - for (const auto& cn : cppNamesInitializer) { - AllCppNames.push_back(TString::Join(enumInitData.CppNamesPrefix, cn)); - } - } - - template <typename TEnumRepresentationType> - TEnumDescriptionBase<TEnumRepresentationType>::~TEnumDescriptionBase() = default; - - // explicit instantiation - template class TEnumDescriptionBase<int>; - template class TEnumDescriptionBase<unsigned>; - template class TEnumDescriptionBase<long long>; - template class TEnumDescriptionBase<unsigned long long>; -} + AllNames += ", "; + } + AllNames += TString::Join('\'', it.second, '\''); + AllValues.push_back(it.first); + } + + AllCppNames.reserve(cppNamesInitializer.size()); + for (const auto& cn : cppNamesInitializer) { + AllCppNames.push_back(TString::Join(enumInitData.CppNamesPrefix, cn)); + } + } + + template <typename TEnumRepresentationType> + TEnumDescriptionBase<TEnumRepresentationType>::~TEnumDescriptionBase() = default; + + // explicit instantiation + template class TEnumDescriptionBase<int>; + template class TEnumDescriptionBase<unsigned>; + template class TEnumDescriptionBase<long long>; + template class TEnumDescriptionBase<unsigned long long>; +} diff --git a/tools/enum_parser/enum_serialization_runtime/enum_runtime.h b/tools/enum_parser/enum_serialization_runtime/enum_runtime.h index 905406a9d1..f157d43109 100644 --- a/tools/enum_parser/enum_serialization_runtime/enum_runtime.h +++ b/tools/enum_parser/enum_serialization_runtime/enum_runtime.h @@ -1,116 +1,116 @@ -#pragma once - -#include <util/generic/array_ref.h> -#include <util/generic/strbuf.h> -#include <util/generic/string.h> -#include <util/generic/vector.h> -#include <util/generic/serialized_enum.h> - -#include <utility> - -class IOutputStream; - -namespace NEnumSerializationRuntime { - /// Stores all information about enumeration except its real type - template <typename TEnumRepresentationType> - class TEnumDescriptionBase { - public: - using TRepresentationType = TEnumRepresentationType; - - struct TEnumStringPair { - const TRepresentationType Key; - const TStringBuf Name; - }; - - /// Refers initialization data stored in constexpr-friendly format - struct TInitializationData { - const TArrayRef<const TEnumStringPair> NamesInitializer; - const TArrayRef<const TEnumStringPair> ValuesInitializer; - const TArrayRef<const TStringBuf> CppNamesInitializer; - const TStringBuf CppNamesPrefix; - const TStringBuf ClassName; - }; - - public: - TEnumDescriptionBase(const TInitializationData& enumInitData); - ~TEnumDescriptionBase(); - - const TString& ToString(TRepresentationType key) const; - std::pair<bool, TRepresentationType> TryFromString(const TStringBuf name) const; - TRepresentationType FromString(const TStringBuf name) const; - void Out(IOutputStream* os, const TRepresentationType key) const; - - const TString& AllEnumNames() const noexcept { - return AllNames; - } - - const TVector<TString>& AllEnumCppNames() const noexcept { - return AllCppNames; - } - - const TMap<TRepresentationType, TString>& TypelessEnumNames() const noexcept { - return Names; - } - - const TVector<TRepresentationType>& TypelessEnumValues() const noexcept { - return AllValues; - } - - private: - TMap<TRepresentationType, TString> Names; - TMap<TString, TRepresentationType> Values; - TString AllNames; - TVector<TString> AllCppNames; - TString ClassName; - TVector<TRepresentationType> AllValues; - }; - - /// Wraps TEnumDescriptionBase and performs on-demand casts - template <typename EEnum, typename TEnumRepresentationType = typename NDetail::TSelectEnumRepresentationType<EEnum>::TType> - class TEnumDescription: public NDetail::TMappedViewBase<EEnum, TEnumRepresentationType>, private TEnumDescriptionBase<TEnumRepresentationType> { - public: - using TBase = TEnumDescriptionBase<TEnumRepresentationType>; - using TCast = NDetail::TMappedViewBase<EEnum, TEnumRepresentationType>; - using TBase::AllEnumCppNames; - using TBase::AllEnumNames; - using typename TBase::TEnumStringPair; - using typename TBase::TRepresentationType; - using typename TBase::TInitializationData; - - public: - using TBase::TBase; - - const TString& ToString(const EEnum key) const { - return TBase::ToString(TCast::CastToRepresentationType(key)); - } - - bool FromString(const TStringBuf name, EEnum& ret) const { - const auto findResult = TBase::TryFromString(name); - if (findResult.first) { - ret = TCast::CastFromRepresentationType(findResult.second); - return true; - } - return false; - } - - EEnum FromString(const TStringBuf name) const { - return TCast::CastFromRepresentationType(TBase::FromString(name)); - } - - TMappedDictView<EEnum, TString> EnumNames() const noexcept { - return {TBase::TypelessEnumNames()}; - } - - TMappedArrayView<EEnum> AllEnumValues() const noexcept { - return {TBase::TypelessEnumValues()}; - } - - void Out(IOutputStream* os, const EEnum key) const { - TBase::Out(os, TCast::CastToRepresentationType(key)); - } - - static constexpr TEnumStringPair EnumStringPair(const EEnum key, const TStringBuf name) noexcept { - return {TCast::CastToRepresentationType(key), name}; - } - }; -} +#pragma once + +#include <util/generic/array_ref.h> +#include <util/generic/strbuf.h> +#include <util/generic/string.h> +#include <util/generic/vector.h> +#include <util/generic/serialized_enum.h> + +#include <utility> + +class IOutputStream; + +namespace NEnumSerializationRuntime { + /// Stores all information about enumeration except its real type + template <typename TEnumRepresentationType> + class TEnumDescriptionBase { + public: + using TRepresentationType = TEnumRepresentationType; + + struct TEnumStringPair { + const TRepresentationType Key; + const TStringBuf Name; + }; + + /// Refers initialization data stored in constexpr-friendly format + struct TInitializationData { + const TArrayRef<const TEnumStringPair> NamesInitializer; + const TArrayRef<const TEnumStringPair> ValuesInitializer; + const TArrayRef<const TStringBuf> CppNamesInitializer; + const TStringBuf CppNamesPrefix; + const TStringBuf ClassName; + }; + + public: + TEnumDescriptionBase(const TInitializationData& enumInitData); + ~TEnumDescriptionBase(); + + const TString& ToString(TRepresentationType key) const; + std::pair<bool, TRepresentationType> TryFromString(const TStringBuf name) const; + TRepresentationType FromString(const TStringBuf name) const; + void Out(IOutputStream* os, const TRepresentationType key) const; + + const TString& AllEnumNames() const noexcept { + return AllNames; + } + + const TVector<TString>& AllEnumCppNames() const noexcept { + return AllCppNames; + } + + const TMap<TRepresentationType, TString>& TypelessEnumNames() const noexcept { + return Names; + } + + const TVector<TRepresentationType>& TypelessEnumValues() const noexcept { + return AllValues; + } + + private: + TMap<TRepresentationType, TString> Names; + TMap<TString, TRepresentationType> Values; + TString AllNames; + TVector<TString> AllCppNames; + TString ClassName; + TVector<TRepresentationType> AllValues; + }; + + /// Wraps TEnumDescriptionBase and performs on-demand casts + template <typename EEnum, typename TEnumRepresentationType = typename NDetail::TSelectEnumRepresentationType<EEnum>::TType> + class TEnumDescription: public NDetail::TMappedViewBase<EEnum, TEnumRepresentationType>, private TEnumDescriptionBase<TEnumRepresentationType> { + public: + using TBase = TEnumDescriptionBase<TEnumRepresentationType>; + using TCast = NDetail::TMappedViewBase<EEnum, TEnumRepresentationType>; + using TBase::AllEnumCppNames; + using TBase::AllEnumNames; + using typename TBase::TEnumStringPair; + using typename TBase::TRepresentationType; + using typename TBase::TInitializationData; + + public: + using TBase::TBase; + + const TString& ToString(const EEnum key) const { + return TBase::ToString(TCast::CastToRepresentationType(key)); + } + + bool FromString(const TStringBuf name, EEnum& ret) const { + const auto findResult = TBase::TryFromString(name); + if (findResult.first) { + ret = TCast::CastFromRepresentationType(findResult.second); + return true; + } + return false; + } + + EEnum FromString(const TStringBuf name) const { + return TCast::CastFromRepresentationType(TBase::FromString(name)); + } + + TMappedDictView<EEnum, TString> EnumNames() const noexcept { + return {TBase::TypelessEnumNames()}; + } + + TMappedArrayView<EEnum> AllEnumValues() const noexcept { + return {TBase::TypelessEnumValues()}; + } + + void Out(IOutputStream* os, const EEnum key) const { + TBase::Out(os, TCast::CastToRepresentationType(key)); + } + + static constexpr TEnumStringPair EnumStringPair(const EEnum key, const TStringBuf name) noexcept { + return {TCast::CastToRepresentationType(key), name}; + } + }; +} diff --git a/tools/enum_parser/enum_serialization_runtime/ya.make b/tools/enum_parser/enum_serialization_runtime/ya.make index 67c971027f..c18e4aee10 100644 --- a/tools/enum_parser/enum_serialization_runtime/ya.make +++ b/tools/enum_parser/enum_serialization_runtime/ya.make @@ -1,13 +1,13 @@ -LIBRARY() - -OWNER( - g:util - swarmer -) - -SRCS( - enum_runtime.cpp -) - - -END() +LIBRARY() + +OWNER( + g:util + swarmer +) + +SRCS( + enum_runtime.cpp +) + + +END() diff --git a/tools/enum_parser/ya.make b/tools/enum_parser/ya.make index 5bf0717668..762b282b3e 100644 --- a/tools/enum_parser/ya.make +++ b/tools/enum_parser/ya.make @@ -2,5 +2,5 @@ RECURSE( parse_enum parse_enum/ut enum_parser - enum_serialization_runtime + enum_serialization_runtime ) |