arcadia_enum-inl.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #ifndef ARCADIA_ENUM_INL_H_
  3. #error "Direct inclusion of this file is not allowed, include arcadia_enum.h"
  4. // For the sake of sane code completion.
  5. #include "arcadia_enum.h"
  6. #endif
  7. #include <util/system/type_name.h>
  8. namespace NYT::NDetail {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template <class T>
  11. struct TArcadiaEnumTraitsImpl
  12. {
  13. static constexpr bool IsBitEnum = false;
  14. static constexpr bool IsStringSerializableEnum = false;
  15. static TStringBuf GetTypeName()
  16. {
  17. static const auto Result = TypeName<T>();
  18. return Result;
  19. }
  20. static std::optional<TStringBuf> FindLiteralByValue(T value)
  21. {
  22. auto names = GetEnumNames<T>();
  23. auto it = names.find(value);
  24. return it == names.end() ? std::nullopt : std::make_optional(TStringBuf(it->second));
  25. }
  26. static std::optional<T> FindValueByLiteral(TStringBuf literal)
  27. {
  28. static const auto LiteralToValue = [] {
  29. THashMap<TString, T> result;
  30. for (const auto& [value, name] : GetEnumNames<T>()) {
  31. result.emplace(name, value);
  32. }
  33. return result;
  34. }();
  35. auto it = LiteralToValue.find(literal);
  36. return it == LiteralToValue.end() ? std::nullopt : std::make_optional(it->second);
  37. }
  38. };
  39. ////////////////////////////////////////////////////////////////////////////////
  40. } // namespace NYT::NDetail