skiff.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #pragma once
  2. #include "public.h"
  3. #include "zerocopy_output_writer.h"
  4. #include <util/generic/buffer.h>
  5. #include <util/generic/yexception.h>
  6. #include <util/stream/input.h>
  7. #include <util/stream/output.h>
  8. namespace NSkiff {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. class TSkiffException
  11. : public yexception
  12. { };
  13. ////////////////////////////////////////////////////////////////////////////////
  14. template <typename T>
  15. constexpr T EndOfSequenceTag()
  16. {
  17. static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value, "T must be unsigned integer");
  18. return T(-1);
  19. }
  20. ////////////////////////////////////////////////////////////////////////////////
  21. struct TInt128
  22. {
  23. ui64 Low = 0;
  24. i64 High = 0;
  25. };
  26. struct TUint128
  27. {
  28. ui64 Low = 0;
  29. ui64 High = 0;
  30. };
  31. bool operator==(TInt128 lhs, TInt128 rhs);
  32. bool operator!=(TInt128 lhs, TInt128 rhs);
  33. bool operator==(TUint128 lhs, TUint128 rhs);
  34. bool operator!=(TUint128 lhs, TUint128 rhs);
  35. ////////////////////////////////////////////////////////////////////////////////
  36. struct TInt256
  37. {
  38. std::array<ui64, 4> Parts;
  39. };
  40. struct TUint256
  41. {
  42. std::array<ui64, 4> Parts;
  43. };
  44. // Operator != is synthesized since C++ 20.
  45. bool operator==(const TInt256& lhs, const TInt256& rhs);
  46. bool operator==(const TUint256& lhs, const TUint256& rhs);
  47. ////////////////////////////////////////////////////////////////////////////////
  48. class TUncheckedSkiffParser
  49. {
  50. public:
  51. explicit TUncheckedSkiffParser(IZeroCopyInput* stream);
  52. TUncheckedSkiffParser(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyInput* stream);
  53. i8 ParseInt8();
  54. i16 ParseInt16();
  55. i32 ParseInt32();
  56. i64 ParseInt64();
  57. ui8 ParseUint8();
  58. ui16 ParseUint16();
  59. ui32 ParseUint32();
  60. ui64 ParseUint64();
  61. TInt128 ParseInt128();
  62. TUint128 ParseUint128();
  63. TInt256 ParseInt256();
  64. TUint256 ParseUint256();
  65. double ParseDouble();
  66. bool ParseBoolean();
  67. TStringBuf ParseString32();
  68. TStringBuf ParseYson32();
  69. ui8 ParseVariant8Tag();
  70. ui16 ParseVariant16Tag();
  71. bool HasMoreData();
  72. void ValidateFinished();
  73. ui64 GetReadBytesCount() const;
  74. private:
  75. const void* GetData(size_t size);
  76. const void* GetDataViaBuffer(size_t size);
  77. size_t RemainingBytes() const;
  78. void Advance(size_t size);
  79. void RefillBuffer();
  80. template <typename T>
  81. T ParseSimple();
  82. private:
  83. IZeroCopyInput* const Underlying_;
  84. TBuffer Buffer_;
  85. ui64 ReadBytesCount_ = 0;
  86. char* Position_ = nullptr;
  87. char* End_ = nullptr;
  88. bool Exhausted_ = false;
  89. };
  90. ////////////////////////////////////////////////////////////////////////////////
  91. class TCheckedSkiffParser
  92. {
  93. public:
  94. TCheckedSkiffParser(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyInput* stream);
  95. ~TCheckedSkiffParser();
  96. i8 ParseInt8();
  97. i16 ParseInt16();
  98. i32 ParseInt32();
  99. i64 ParseInt64();
  100. ui8 ParseUint8();
  101. ui16 ParseUint16();
  102. ui32 ParseUint32();
  103. ui64 ParseUint64();
  104. TInt128 ParseInt128();
  105. TUint128 ParseUint128();
  106. TInt256 ParseInt256();
  107. TUint256 ParseUint256();
  108. double ParseDouble();
  109. bool ParseBoolean();
  110. TStringBuf ParseString32();
  111. TStringBuf ParseYson32();
  112. ui8 ParseVariant8Tag();
  113. ui16 ParseVariant16Tag();
  114. bool HasMoreData();
  115. void ValidateFinished();
  116. ui64 GetReadBytesCount() const;
  117. private:
  118. TUncheckedSkiffParser Parser_;
  119. std::unique_ptr<TSkiffValidator> Validator_;
  120. };
  121. ////////////////////////////////////////////////////////////////////
  122. class TUncheckedSkiffWriter
  123. {
  124. public:
  125. explicit TUncheckedSkiffWriter(IZeroCopyOutput* underlying);
  126. explicit TUncheckedSkiffWriter(IOutputStream* underlying);
  127. TUncheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyOutput* underlying);
  128. TUncheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IOutputStream* underlying);
  129. ~TUncheckedSkiffWriter();
  130. void WriteDouble(double value);
  131. void WriteBoolean(bool value);
  132. void WriteInt8(i8 value);
  133. void WriteInt16(i16 value);
  134. void WriteInt32(i32 value);
  135. void WriteInt64(i64 value);
  136. void WriteUint8(ui8 value);
  137. void WriteUint16(ui16 value);
  138. void WriteUint32(ui32 value);
  139. void WriteUint64(ui64 value);
  140. void WriteInt128(TInt128 value);
  141. void WriteUint128(TUint128 value);
  142. void WriteInt256(const TInt256& value);
  143. void WriteUint256(const TUint256& value);
  144. void WriteString32(TStringBuf value);
  145. void WriteYson32(TStringBuf value);
  146. void WriteVariant8Tag(ui8 tag);
  147. void WriteVariant16Tag(ui16 tag);
  148. void Flush();
  149. void Finish();
  150. private:
  151. template <typename T>
  152. void WriteSimple(T data);
  153. private:
  154. THolder<TBufferedOutput> BufferedOutput_;
  155. TZeroCopyOutputStreamWriter Underlying_;
  156. };
  157. ////////////////////////////////////////////////////////////////////////////////
  158. class TCheckedSkiffWriter
  159. {
  160. public:
  161. TCheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyOutput* underlying);
  162. TCheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IOutputStream* underlying);
  163. ~TCheckedSkiffWriter();
  164. void WriteInt8(i8 value);
  165. void WriteInt16(i16 value);
  166. void WriteInt32(i32 value);
  167. void WriteInt64(i64 value);
  168. void WriteUint8(ui8 value);
  169. void WriteUint16(ui16 value);
  170. void WriteUint32(ui32 value);
  171. void WriteUint64(ui64 value);
  172. void WriteDouble(double value);
  173. void WriteBoolean(bool value);
  174. void WriteInt128(TInt128 value);
  175. void WriteUint128(TUint128 value);
  176. void WriteInt256(TInt256 value);
  177. void WriteUint256(TUint256 value);
  178. void WriteString32(TStringBuf value);
  179. void WriteYson32(TStringBuf value);
  180. void WriteVariant8Tag(ui8 tag);
  181. void WriteVariant16Tag(ui16 tag);
  182. void Flush();
  183. void Finish();
  184. private:
  185. TUncheckedSkiffWriter Writer_;
  186. std::unique_ptr<TSkiffValidator> Validator_;
  187. };
  188. ////////////////////////////////////////////////////////////////////////////////
  189. template <EWireType wireType>
  190. class TUnderlyingIntegerType {
  191. private:
  192. TUnderlyingIntegerType() = default;
  193. static constexpr auto F();
  194. public:
  195. using TValue = decltype(TUnderlyingIntegerType::F());
  196. };
  197. ////////////////////////////////////////////////////////////////////////////////
  198. } // namespace NSkiff
  199. #define SKIFF_H
  200. #include "skiff-inl.h"
  201. #undef SKIFF_H