dispatch_methods.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include "ordered_pairs.h"
  3. #include <util/generic/strbuf.h>
  4. #include <util/stream/fwd.h>
  5. namespace NEnumSerializationRuntime {
  6. // compile-time conversion method selection
  7. constexpr size_t LINEAR_SEARCH_KEYS_SIZE_THRESHOLD = 6;
  8. constexpr size_t LINEAR_SEARCH_VALUES_SIZE_THRESHOLD = 2;
  9. template <class TNameBufs, typename EEnum>
  10. inline void DispatchOutFn(IOutputStream& os, EEnum n) {
  11. constexpr auto order = TNameBufs::NamesOrder;
  12. if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::DirectMapping) {
  13. return TNameBufs::OutDirect(&os, n, TNameBufs::EnumInitializationData);
  14. } else if constexpr (std::size(TNameBufs::EnumInitializationData.NamesInitializer) <= LINEAR_SEARCH_KEYS_SIZE_THRESHOLD) {
  15. return TNameBufs::OutFullScan(&os, n, TNameBufs::EnumInitializationData);
  16. } else if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::Ascending) {
  17. return TNameBufs::OutSorted(&os, n, TNameBufs::EnumInitializationData);
  18. } else {
  19. const TNameBufs& names = TNameBufs::Instance();
  20. return names.Out(&os, n);
  21. }
  22. }
  23. template <class TNameBufs, typename EEnum>
  24. inline TStringBuf DispatchToStringBufFn(EEnum n) {
  25. constexpr auto order = TNameBufs::NamesOrder;
  26. if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::DirectMapping) {
  27. return TNameBufs::ToStringBufDirect(n, TNameBufs::EnumInitializationData);
  28. } else if constexpr (std::size(TNameBufs::EnumInitializationData.NamesInitializer) <= LINEAR_SEARCH_KEYS_SIZE_THRESHOLD) {
  29. return TNameBufs::ToStringBufFullScan(n, TNameBufs::EnumInitializationData);
  30. } else if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::Ascending) {
  31. return TNameBufs::ToStringBufSorted(n, TNameBufs::EnumInitializationData);
  32. } else {
  33. const TNameBufs& names = TNameBufs::Instance();
  34. return names.ToStringBuf(n);
  35. }
  36. }
  37. template <class TNameBufs, typename EEnum>
  38. inline EEnum DispatchFromStringImplFn(const char* data, size_t len) {
  39. const TStringBuf name{data, len};
  40. constexpr auto order = TNameBufs::ValuesOrder;
  41. static_assert(order >= ::NEnumSerializationRuntime::ESortOrder::Ascending, "enum_parser produced unsorted output"); // comment this line to use run-time sort for temporary workaround
  42. if constexpr (std::size(TNameBufs::EnumInitializationData.ValuesInitializer) <= LINEAR_SEARCH_VALUES_SIZE_THRESHOLD) {
  43. return TNameBufs::FromStringFullScan(name, TNameBufs::EnumInitializationData);
  44. } else if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::Ascending) {
  45. return TNameBufs::FromStringSorted(name, TNameBufs::EnumInitializationData);
  46. } else {
  47. const TNameBufs& names = TNameBufs::Instance();
  48. return names.FromString(name);
  49. }
  50. }
  51. template <class TNameBufs, typename EEnum>
  52. inline bool DispatchTryFromStringImplFn(const char* data, size_t len, EEnum& result) {
  53. const TStringBuf name{data, len};
  54. constexpr auto order = TNameBufs::ValuesOrder;
  55. if constexpr (std::size(TNameBufs::EnumInitializationData.ValuesInitializer) <= LINEAR_SEARCH_VALUES_SIZE_THRESHOLD) {
  56. return TNameBufs::TryFromStringFullScan(name, result, TNameBufs::EnumInitializationData);
  57. } else if constexpr (order >= ::NEnumSerializationRuntime::ESortOrder::Ascending) {
  58. return TNameBufs::TryFromStringSorted(name, result, TNameBufs::EnumInitializationData);
  59. } else {
  60. const TNameBufs& names = TNameBufs::Instance();
  61. return names.FromString(name, result);
  62. }
  63. }
  64. }