scimpl_defs.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include <library/cpp/json/json_reader.h>
  3. #include <library/cpp/json/json_value.h>
  4. #include <util/system/types.h>
  5. #include <util/memory/pool.h>
  6. #include <util/generic/deque.h>
  7. #include <util/generic/hash.h>
  8. #include <util/generic/ptr.h>
  9. #include <util/generic/strbuf.h>
  10. #include <util/generic/string.h>
  11. #include <util/generic/vector.h>
  12. #include <functional>
  13. #include <util/string/vector.h>
  14. #include <util/string/type.h>
  15. namespace NSc {
  16. namespace NDefinitions {
  17. const size_t POOL_BLOCK_SIZE = 4000; // leave 96 bytes for overhead
  18. struct TPool: public TAtomicRefCount<TPool> {
  19. TMemoryPool Pool;
  20. TPool(size_t blsz = POOL_BLOCK_SIZE, TMemoryPool::IGrowPolicy* grow = TMemoryPool::TExpGrow::Instance())
  21. : Pool(blsz, grow)
  22. {
  23. }
  24. TMemoryPool* Get() {
  25. return &Pool;
  26. }
  27. TStringBuf AppendBuf(const TStringBuf& strb) {
  28. return Pool.AppendCString(strb);
  29. }
  30. template <typename T>
  31. T* NewWithPool() {
  32. return new (Pool.Allocate<T>()) T(&Pool);
  33. }
  34. };
  35. }
  36. using TStringBufs = TVector<TStringBuf>;
  37. class TSchemeException : public yexception {
  38. };
  39. class TSchemeParseException : public TSchemeException {
  40. public:
  41. size_t Offset = 0;
  42. TString Reason;
  43. public:
  44. TSchemeParseException() = default;
  45. TSchemeParseException(size_t off, const TString& reason)
  46. : Offset(off)
  47. , Reason(reason)
  48. {
  49. }
  50. };
  51. struct TJsonOpts: public NJson::TJsonReaderConfig {
  52. enum EJsonOpts {
  53. JO_DEFAULT = 0, // just dump json, used to be default, actually
  54. JO_SORT_KEYS = 1, // sort dict keys to make output more predictable
  55. JO_SKIP_UNSAFE = 2, // skip nonunicode data to make external json parsers happy
  56. // will skip nonunicode dict keys and replace nonunicode values with nulls
  57. JO_FORMAT = 8, // format json
  58. JO_PARSER_STRICT_JSON = 16, // strict standard json
  59. JO_PARSER_STRICT_UTF8 = 32, // strict utf8
  60. JO_PARSER_DISALLOW_COMMENTS = 64,
  61. JO_PARSER_DISALLOW_DUPLICATE_KEYS = 128,
  62. JO_PRETTY = JO_FORMAT | JO_SORT_KEYS, // pretty print json
  63. JO_SAFE = JO_SKIP_UNSAFE | JO_SORT_KEYS, // ensure standard parser-safe json
  64. JO_PARSER_STRICT_WITH_COMMENTS = JO_PARSER_STRICT_JSON | JO_PARSER_STRICT_UTF8,
  65. JO_PARSER_STRICT = JO_PARSER_STRICT_JSON | JO_PARSER_STRICT_UTF8 | JO_PARSER_DISALLOW_COMMENTS,
  66. };
  67. public:
  68. TJsonOpts(int opts = JO_SORT_KEYS)
  69. : Opts(opts)
  70. , SortKeys(opts & JO_SORT_KEYS)
  71. , FormatJson(opts & JO_FORMAT)
  72. , StringPolicy((opts & JO_SKIP_UNSAFE) ? StringPolicySafe : StringPolicyUnsafe)
  73. {
  74. AllowComments = !(opts & JO_PARSER_DISALLOW_COMMENTS);
  75. RelaxedJson = !(opts & JO_PARSER_STRICT_JSON);
  76. DontValidateUtf8 = !(opts & JO_PARSER_STRICT_UTF8);
  77. }
  78. public:
  79. bool RelaxedJson = false;
  80. int Opts = 0;
  81. bool SortKeys = true;
  82. bool FormatJson = false;
  83. // return true to proceed with output, false to skip, optionally modify value
  84. std::function<bool(double&)> NumberPolicy = NumberPolicySafe;
  85. std::function<bool(TStringBuf&)> StringPolicy = StringPolicyUnsafe;
  86. public:
  87. static bool NumberPolicySafe(double&); // skip if nan or inf
  88. static bool NumberPolicyUnsafe(double&) {
  89. return true;
  90. }
  91. static bool StringPolicySafe(TStringBuf&); // skip if not utf8
  92. static bool StringPolicyUnsafe(TStringBuf&) {
  93. return true;
  94. }
  95. };
  96. struct TProtoOpts {
  97. // Serialization throws on unknown enum value if not set, else use default value
  98. bool UnknownEnumValueIsDefault = false;
  99. // Serialization throws on type mismatch if not set, else leaves protobuf empty
  100. bool SkipTypeMismatch = false;
  101. };
  102. namespace NImpl {
  103. class TKeySortContext;
  104. class TSelfLoopContext;
  105. class TSelfOverrideContext;
  106. }
  107. }
  108. namespace google {
  109. namespace protobuf {
  110. class Message;
  111. class FieldDescriptor;
  112. }
  113. }