enum.h 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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. TEnumNames* result = new TEnumNames(names.size());
  11. size_t index = 0;
  12. for (const auto& p: names) {
  13. Y_ABORT_UNLESS(static_cast<size_t>(p.first) == index);
  14. (*result)[index++] = &p.second;
  15. }
  16. return result;
  17. }
  18. template <typename TEnum>
  19. const TEnumNames& EnumNamesVec() {
  20. static const TEnumNames* EnumNames = BuildEnumNames<TEnum>();
  21. return *EnumNames;
  22. }
  23. }
  24. template <typename TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
  25. inline const TString& NameOf(TEnum val) noexcept {
  26. return *NPrivate::EnumNamesVec<TEnum>()[static_cast<size_t>(val)];
  27. }
  28. }