diff options
author | akhropov <akhropov@yandex-team.ru> | 2022-02-10 16:46:32 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:32 +0300 |
commit | 298c6da79f1d8f35089a67f463f0b541bec36d9b (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/draft/enum.h | |
parent | 00afc96e9c0298054b7386fa7fb9e3cc3d67b974 (diff) | |
download | ydb-298c6da79f1d8f35089a67f463f0b541bec36d9b.tar.gz |
Restoring authorship annotation for <akhropov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/draft/enum.h')
-rw-r--r-- | util/draft/enum.h | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/util/draft/enum.h b/util/draft/enum.h index 2939099b5e..18002b7df2 100644 --- a/util/draft/enum.h +++ b/util/draft/enum.h @@ -1,16 +1,16 @@ #pragma once - + #include <bitset> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <util/stream/str.h> -#include <util/string/cast.h> +#include <util/string/cast.h> #include <util/string/split.h> #include <utility> - + class TEnumNotFoundException: public yexception { -}; - +}; + #define EnumFromString(key, entries) EnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) #define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size) #define FindEnumFromString(key, entries) FindEnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) @@ -18,24 +18,24 @@ class TEnumNotFoundException: public yexception { #define EnumToString(key, entries) EnumToStringImpl(key, entries, Y_ARRAY_SIZE(entries)) #define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size) #define PrintEnumItems(entries) PrintEnumItemsImpl(entries, Y_ARRAY_SIZE(entries)) - + template <class K1, class K2, class V> const V* FindEnumFromStringImpl(K1 key, const std::pair<K2, V>* entries, size_t arraySize) { - for (size_t i = 0; i < arraySize; i++) - if (entries[i].first == key) - return &entries[i].second; + for (size_t i = 0; i < arraySize; i++) + if (entries[i].first == key) + return &entries[i].second; return nullptr; -} - +} + // special version for const char* template <class V> const V* FindEnumFromStringImpl(const char* key, const std::pair<const char*, V>* entries, size_t arraySize) { - for (size_t i = 0; i < arraySize; i++) - if (entries[i].first && key && !strcmp(entries[i].first, key)) - return &entries[i].second; + for (size_t i = 0; i < arraySize; i++) + if (entries[i].first && key && !strcmp(entries[i].first, key)) + return &entries[i].second; return nullptr; -} - +} + template <class K, class V> TString PrintEnumItemsImpl(const std::pair<K, V>* entries, size_t arraySize) { TString result; @@ -57,18 +57,18 @@ TString PrintEnumItemsImpl(const std::pair<const char*, V>* entries, size_t arra template <class K1, class K2, class V> const V* EnumFromStringImpl(K1 key, const std::pair<K2, V>* entries, size_t arraySize) { - const V* res = FindEnumFromStringImpl(key, entries, arraySize); - if (res) - return res; + const V* res = FindEnumFromStringImpl(key, entries, arraySize); + if (res) + return res; ythrow TEnumNotFoundException() << "Key '" << key << "' not found in enum. Valid options are: " << PrintEnumItemsImpl(entries, arraySize) << ". "; -} - +} + template <class K, class V> const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arraySize) { - for (size_t i = 0; i < arraySize; i++) - if (entries[i].second == value) - return &entries[i].first; + for (size_t i = 0; i < arraySize; i++) + if (entries[i].second == value) + return &entries[i].first; TEnumNotFoundException exc; exc << "Value '" << int(value) << "' not found in enum. Valid values are: "; @@ -76,10 +76,10 @@ const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arrayS exc << (i ? ", " : "") << int(entries[i].second); exc << ". "; ythrow exc; -} - -/////////////////////////////////// - +} + +/////////////////////////////////// + template <class B> inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { if (allIfEmpty) { @@ -89,18 +89,18 @@ inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { } } -// all set by default +// all set by default template <class E, size_t N, size_t B> inline void SetEnumFlags(const std::pair<const char*, E> (&str2Enum)[N], TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) { - if (optSpec.empty()) { + if (optSpec.empty()) { SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { flags.reset(); for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, N); flags.set(e); - } + } } } @@ -110,14 +110,14 @@ inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf o bool allIfEmpty = true) { if (optSpec.empty()) { SetEnumFlagsForEmptySpec(flags, allIfEmpty); - } else { + } else { flags.reset(); for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, size); flags.set(e); - } - } -} + } + } +} // for enums generated with GENERATE_ENUM_SERIALIZATION template <class E, size_t B> @@ -128,7 +128,7 @@ inline void SetEnumFlags(TStringBuf optSpec, std::bitset<B>& flags, bool allIfEm flags.reset(); for (const auto& it : StringSplitter(optSpec).Split(',')) { E e; - if (!TryFromString(it.Token(), e)) + if (!TryFromString(it.Token(), e)) ythrow yexception() << "Unknown enum value '" << it.Token() << "'"; flags.set((size_t)e); } |