enum.h 924 B

123456789101112131415161718192021222324252627282930
  1. #pragma once
  2. #include <util/generic/serialized_enum.h>
  3. #include <util/generic/vector.h>
  4. namespace NUnifiedAgent {
  5. namespace NPrivate {
  6. using TEnumNames = TVector<const TString*>;
  7. template <typename TEnum>
  8. TEnumNames BuildEnumNames() {
  9. const auto names = GetEnumNames<TEnum>();
  10. auto result = TEnumNames(names.size());
  11. size_t index = 0;
  12. for (const auto& p: names) {
  13. Y_VERIFY(static_cast<size_t>(p.first) == index);
  14. result[index++] = &p.second;
  15. }
  16. return result;
  17. }
  18. template <typename TEnum>
  19. inline const auto EnumNames = BuildEnumNames<TEnum>();
  20. }
  21. template <typename TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
  22. inline const TString& NameOf(TEnum val) noexcept {
  23. return *NPrivate::EnumNames<TEnum>[static_cast<size_t>(val)];
  24. }
  25. }