aboutsummaryrefslogtreecommitdiffstats
path: root/util/draft/enum.h
diff options
context:
space:
mode:
authorMaxim Yurchuk <maxim-yurchuk@ydb.tech>2024-11-20 17:37:57 +0000
committerGitHub <noreply@github.com>2024-11-20 17:37:57 +0000
commitf76323e9b295c15751e51e3443aa47a36bee8023 (patch)
tree4113c8cad473a33e0f746966e0cf087252fa1d7a /util/draft/enum.h
parent753ecb8d410a4cb459c26f3a0082fb2d1724fe63 (diff)
parenta7b9a6afea2a9d7a7bfac4c5eb4c1a8e60adb9e6 (diff)
downloadydb-f76323e9b295c15751e51e3443aa47a36bee8023.tar.gz
Merge pull request #11788 from ydb-platform/mergelibs-241120-1113
Library import 241120-1113
Diffstat (limited to 'util/draft/enum.h')
-rw-r--r--util/draft/enum.h33
1 files changed, 22 insertions, 11 deletions
diff --git a/util/draft/enum.h b/util/draft/enum.h
index 18002b7df2..e33cfea02c 100644
--- a/util/draft/enum.h
+++ b/util/draft/enum.h
@@ -21,18 +21,22 @@ class TEnumNotFoundException: public yexception {
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)
+ 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))
+ for (size_t i = 0; i < arraySize; i++) {
+ if (entries[i].first && key && !strcmp(entries[i].first, key)) {
return &entries[i].second;
+ }
+ }
return nullptr;
}
@@ -40,8 +44,9 @@ template <class K, class V>
TString PrintEnumItemsImpl(const std::pair<K, V>* entries, size_t arraySize) {
TString result;
TStringOutput out(result);
- for (size_t i = 0; i < arraySize; i++)
+ for (size_t i = 0; i < arraySize; i++) {
out << (i ? ", " : "") << "'" << entries[i].first << "'";
+ }
return result;
}
@@ -50,30 +55,35 @@ template <class V>
TString PrintEnumItemsImpl(const std::pair<const char*, V>* entries, size_t arraySize) {
TString result;
TStringOutput out(result);
- for (size_t i = 0; i < arraySize; i++)
+ for (size_t i = 0; i < arraySize; i++) {
out << (i ? ", " : "") << "'" << (entries[i].first ? entries[i].first : "<null>") << "'";
+ }
return result;
}
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)
+ 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)
+ 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: ";
- for (size_t i = 0; i < arraySize; i++)
+ for (size_t i = 0; i < arraySize; i++) {
exc << (i ? ", " : "") << int(entries[i].second);
+ }
exc << ". ";
ythrow exc;
}
@@ -128,8 +138,9 @@ 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);
}
}