enum_runtime.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include "dispatch_methods.h"
  3. #include "ordered_pairs.h"
  4. #include <util/generic/array_ref.h>
  5. #include <util/generic/strbuf.h>
  6. #include <util/generic/string.h>
  7. #include <util/generic/vector.h>
  8. #include <util/generic/serialized_enum.h>
  9. #include <util/stream/fwd.h>
  10. #include <utility>
  11. namespace NEnumSerializationRuntime {
  12. /// Stores all information about enumeration except its real type
  13. template <typename TEnumRepresentationType>
  14. class TEnumDescriptionBase {
  15. public:
  16. using TRepresentationType = TEnumRepresentationType;
  17. using TEnumStringPair = ::NEnumSerializationRuntime::TEnumStringPair<TRepresentationType>;
  18. /// Refers initialization data stored in constexpr-friendly format
  19. struct TInitializationData {
  20. const TArrayRef<const TEnumStringPair> NamesInitializer;
  21. const TArrayRef<const TEnumStringPair> ValuesInitializer;
  22. const TArrayRef<const TStringBuf> CppNamesInitializer;
  23. const TStringBuf CppNamesPrefix;
  24. const TStringBuf ClassName;
  25. };
  26. public:
  27. TEnumDescriptionBase(const TInitializationData& enumInitData);
  28. ~TEnumDescriptionBase();
  29. const TString& ToString(TRepresentationType key) const;
  30. TStringBuf ToStringBuf(TRepresentationType key) const;
  31. static TStringBuf ToStringBufFullScan(const TRepresentationType key, const TInitializationData& enumInitData);
  32. static TStringBuf ToStringBufSorted(const TRepresentationType key, const TInitializationData& enumInitData);
  33. static TStringBuf ToStringBufDirect(const TRepresentationType key, const TInitializationData& enumInitData);
  34. std::pair<bool, TRepresentationType> TryFromString(const TStringBuf name) const;
  35. static std::pair<bool, TRepresentationType> TryFromStringFullScan(const TStringBuf name, const TInitializationData& enumInitData);
  36. static std::pair<bool, TRepresentationType> TryFromStringSorted(const TStringBuf name, const TInitializationData& enumInitData);
  37. TRepresentationType FromString(const TStringBuf name) const;
  38. static TRepresentationType FromStringFullScan(const TStringBuf name, const TInitializationData& enumInitData);
  39. static TRepresentationType FromStringSorted(const TStringBuf name, const TInitializationData& enumInitData);
  40. void Out(IOutputStream* os, const TRepresentationType key) const;
  41. static void OutFullScan(IOutputStream* os, const TRepresentationType key, const TInitializationData& enumInitData);
  42. static void OutSorted(IOutputStream* os, const TRepresentationType key, const TInitializationData& enumInitData);
  43. static void OutDirect(IOutputStream* os, const TRepresentationType key, const TInitializationData& enumInitData);
  44. const TString& AllEnumNames() const noexcept {
  45. return AllNames;
  46. }
  47. const TVector<TString>& AllEnumCppNames() const noexcept {
  48. return AllCppNames;
  49. }
  50. const TMap<TRepresentationType, TString>& TypelessEnumNames() const noexcept {
  51. return Names;
  52. }
  53. const TVector<TRepresentationType>& TypelessEnumValues() const noexcept {
  54. return AllValues;
  55. }
  56. private:
  57. TMap<TRepresentationType, TString> Names;
  58. TMap<TStringBuf, TRepresentationType> Values;
  59. TString AllNames;
  60. TVector<TString> AllCppNames;
  61. TString ClassName;
  62. TVector<TRepresentationType> AllValues;
  63. };
  64. /// Wraps TEnumDescriptionBase and performs on-demand casts
  65. template <typename EEnum, typename TEnumRepresentationType = typename NDetail::TSelectEnumRepresentationType<EEnum>::TType>
  66. class TEnumDescription: public NDetail::TMappedViewBase<EEnum, TEnumRepresentationType>, private TEnumDescriptionBase<TEnumRepresentationType> {
  67. public:
  68. using TBase = TEnumDescriptionBase<TEnumRepresentationType>;
  69. using TCast = NDetail::TMappedViewBase<EEnum, TEnumRepresentationType>;
  70. using TBase::AllEnumCppNames;
  71. using TBase::AllEnumNames;
  72. using typename TBase::TEnumStringPair;
  73. using typename TBase::TRepresentationType;
  74. using typename TBase::TInitializationData;
  75. private:
  76. static bool MapFindResult(std::pair<bool, TEnumRepresentationType> findResult, EEnum& ret) {
  77. if (findResult.first) {
  78. ret = TCast::CastFromRepresentationType(findResult.second);
  79. return true;
  80. }
  81. return false;
  82. }
  83. public:
  84. using TBase::TBase;
  85. // ToString
  86. // Return reference to singleton preallocated string
  87. const TString& ToString(const EEnum key) const {
  88. return TBase::ToString(TCast::CastToRepresentationType(key));
  89. }
  90. // ToStringBuf
  91. TStringBuf ToStringBuf(EEnum key) const {
  92. return TBase::ToStringBuf(TCast::CastToRepresentationType(key));
  93. }
  94. static TStringBuf ToStringBufFullScan(const EEnum key, const TInitializationData& enumInitData) {
  95. return TBase::ToStringBufFullScan(TCast::CastToRepresentationType(key), enumInitData);
  96. }
  97. static TStringBuf ToStringBufSorted(const EEnum key, const TInitializationData& enumInitData) {
  98. return TBase::ToStringBufSorted(TCast::CastToRepresentationType(key), enumInitData);
  99. }
  100. static TStringBuf ToStringBufDirect(const EEnum key, const TInitializationData& enumInitData) {
  101. return TBase::ToStringBufDirect(TCast::CastToRepresentationType(key), enumInitData);
  102. }
  103. // TryFromString-like functons
  104. // Return false for unknown enumeration names
  105. bool FromString(const TStringBuf name, EEnum& ret) const {
  106. return MapFindResult(TBase::TryFromString(name), ret);
  107. }
  108. static bool TryFromStringFullScan(const TStringBuf name, EEnum& ret, const TInitializationData& enumInitData) {
  109. return MapFindResult(TBase::TryFromStringFullScan(name, enumInitData), ret);
  110. }
  111. static bool TryFromStringSorted(const TStringBuf name, EEnum& ret, const TInitializationData& enumInitData) {
  112. return MapFindResult(TBase::TryFromStringSorted(name, enumInitData), ret);
  113. }
  114. // FromString
  115. // Throw exception for unknown enumeration names
  116. EEnum FromString(const TStringBuf name) const {
  117. return TCast::CastFromRepresentationType(TBase::FromString(name));
  118. }
  119. static EEnum FromStringFullScan(const TStringBuf name, const TInitializationData& enumInitData) {
  120. return TCast::CastFromRepresentationType(TBase::FromStringFullScan(name, enumInitData));
  121. }
  122. static EEnum FromStringSorted(const TStringBuf name, const TInitializationData& enumInitData) {
  123. return TCast::CastFromRepresentationType(TBase::FromStringSorted(name, enumInitData));
  124. }
  125. // Inspection
  126. TMappedDictView<EEnum, TString> EnumNames() const noexcept {
  127. return {TBase::TypelessEnumNames()};
  128. }
  129. TMappedArrayView<EEnum> AllEnumValues() const noexcept {
  130. return {TBase::TypelessEnumValues()};
  131. }
  132. // Out
  133. void Out(IOutputStream* os, const EEnum key) const {
  134. TBase::Out(os, TCast::CastToRepresentationType(key));
  135. }
  136. static void OutFullScan(IOutputStream* os, const EEnum key, const TInitializationData& enumInitData) {
  137. TBase::OutFullScan(os, TCast::CastToRepresentationType(key), enumInitData);
  138. }
  139. static void OutSorted(IOutputStream* os, const EEnum key, const TInitializationData& enumInitData) {
  140. TBase::OutSorted(os, TCast::CastToRepresentationType(key), enumInitData);
  141. }
  142. static void OutDirect(IOutputStream* os, const EEnum key, const TInitializationData& enumInitData) {
  143. TBase::OutDirect(os, TCast::CastToRepresentationType(key), enumInitData);
  144. }
  145. static constexpr TEnumStringPair EnumStringPair(const EEnum key, const TStringBuf name) noexcept {
  146. return {TCast::CastToRepresentationType(key), name};
  147. }
  148. };
  149. }