enum.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <bitset>
  3. #include <util/generic/strbuf.h>
  4. #include <util/stream/str.h>
  5. #include <util/string/cast.h>
  6. #include <util/string/split.h>
  7. #include <utility>
  8. class TEnumNotFoundException: public yexception {
  9. };
  10. #define EnumFromString(key, entries) EnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries))
  11. #define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size)
  12. #define FindEnumFromString(key, entries) FindEnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries))
  13. #define FindEnumFromStringWithSize(key, entries, size) FindEnumFromStringImpl(key, entries, size)
  14. #define EnumToString(key, entries) EnumToStringImpl(key, entries, Y_ARRAY_SIZE(entries))
  15. #define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size)
  16. #define PrintEnumItems(entries) PrintEnumItemsImpl(entries, Y_ARRAY_SIZE(entries))
  17. template <class K1, class K2, class V>
  18. const V* FindEnumFromStringImpl(K1 key, const std::pair<K2, V>* entries, size_t arraySize) {
  19. for (size_t i = 0; i < arraySize; i++)
  20. if (entries[i].first == key)
  21. return &entries[i].second;
  22. return nullptr;
  23. }
  24. // special version for const char*
  25. template <class V>
  26. const V* FindEnumFromStringImpl(const char* key, const std::pair<const char*, V>* entries, size_t arraySize) {
  27. for (size_t i = 0; i < arraySize; i++)
  28. if (entries[i].first && key && !strcmp(entries[i].first, key))
  29. return &entries[i].second;
  30. return nullptr;
  31. }
  32. template <class K, class V>
  33. TString PrintEnumItemsImpl(const std::pair<K, V>* entries, size_t arraySize) {
  34. TString result;
  35. TStringOutput out(result);
  36. for (size_t i = 0; i < arraySize; i++)
  37. out << (i ? ", " : "") << "'" << entries[i].first << "'";
  38. return result;
  39. }
  40. // special version for const char*
  41. template <class V>
  42. TString PrintEnumItemsImpl(const std::pair<const char*, V>* entries, size_t arraySize) {
  43. TString result;
  44. TStringOutput out(result);
  45. for (size_t i = 0; i < arraySize; i++)
  46. out << (i ? ", " : "") << "'" << (entries[i].first ? entries[i].first : "<null>") << "'";
  47. return result;
  48. }
  49. template <class K1, class K2, class V>
  50. const V* EnumFromStringImpl(K1 key, const std::pair<K2, V>* entries, size_t arraySize) {
  51. const V* res = FindEnumFromStringImpl(key, entries, arraySize);
  52. if (res)
  53. return res;
  54. ythrow TEnumNotFoundException() << "Key '" << key << "' not found in enum. Valid options are: " << PrintEnumItemsImpl(entries, arraySize) << ". ";
  55. }
  56. template <class K, class V>
  57. const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arraySize) {
  58. for (size_t i = 0; i < arraySize; i++)
  59. if (entries[i].second == value)
  60. return &entries[i].first;
  61. TEnumNotFoundException exc;
  62. exc << "Value '" << int(value) << "' not found in enum. Valid values are: ";
  63. for (size_t i = 0; i < arraySize; i++)
  64. exc << (i ? ", " : "") << int(entries[i].second);
  65. exc << ". ";
  66. ythrow exc;
  67. }
  68. ///////////////////////////////////
  69. template <class B>
  70. inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) {
  71. if (allIfEmpty) {
  72. flags.set();
  73. } else {
  74. flags.reset();
  75. }
  76. }
  77. // all set by default
  78. template <class E, size_t N, size_t B>
  79. inline void SetEnumFlags(const std::pair<const char*, E> (&str2Enum)[N], TStringBuf optSpec,
  80. std::bitset<B>& flags, bool allIfEmpty = true) {
  81. if (optSpec.empty()) {
  82. SetEnumFlagsForEmptySpec(flags, allIfEmpty);
  83. } else {
  84. flags.reset();
  85. for (const auto& it : StringSplitter(optSpec).Split(',')) {
  86. E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, N);
  87. flags.set(e);
  88. }
  89. }
  90. }
  91. template <class E, size_t B>
  92. inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf optSpec,
  93. std::bitset<B>& flags, const size_t size,
  94. bool allIfEmpty = true) {
  95. if (optSpec.empty()) {
  96. SetEnumFlagsForEmptySpec(flags, allIfEmpty);
  97. } else {
  98. flags.reset();
  99. for (const auto& it : StringSplitter(optSpec).Split(',')) {
  100. E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, size);
  101. flags.set(e);
  102. }
  103. }
  104. }
  105. // for enums generated with GENERATE_ENUM_SERIALIZATION
  106. template <class E, size_t B>
  107. inline void SetEnumFlags(TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) {
  108. if (optSpec.empty()) {
  109. SetEnumFlagsForEmptySpec(flags, allIfEmpty);
  110. } else {
  111. flags.reset();
  112. for (const auto& it : StringSplitter(optSpec).Split(',')) {
  113. E e;
  114. if (!TryFromString(it.Token(), e))
  115. ythrow yexception() << "Unknown enum value '" << it.Token() << "'";
  116. flags.set((size_t)e);
  117. }
  118. }
  119. }