skiff.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. class TUncheckedSkiffParser
  37. {
  38. public:
  39. explicit TUncheckedSkiffParser(IZeroCopyInput* stream);
  40. TUncheckedSkiffParser(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyInput* stream);
  41. i8 ParseInt8();
  42. i16 ParseInt16();
  43. i32 ParseInt32();
  44. i64 ParseInt64();
  45. ui8 ParseUint8();
  46. ui16 ParseUint16();
  47. ui32 ParseUint32();
  48. ui64 ParseUint64();
  49. TInt128 ParseInt128();
  50. TUint128 ParseUint128();
  51. double ParseDouble();
  52. bool ParseBoolean();
  53. TStringBuf ParseString32();
  54. TStringBuf ParseYson32();
  55. ui8 ParseVariant8Tag();
  56. ui16 ParseVariant16Tag();
  57. bool HasMoreData();
  58. void ValidateFinished();
  59. ui64 GetReadBytesCount() const;
  60. private:
  61. const void* GetData(size_t size);
  62. const void* GetDataViaBuffer(size_t size);
  63. size_t RemainingBytes() const;
  64. void Advance(size_t size);
  65. void RefillBuffer();
  66. template <typename T>
  67. T ParseSimple();
  68. private:
  69. IZeroCopyInput* const Underlying_;
  70. TBuffer Buffer_;
  71. ui64 ReadBytesCount_ = 0;
  72. char* Position_ = nullptr;
  73. char* End_ = nullptr;
  74. bool Exhausted_ = false;
  75. };
  76. ////////////////////////////////////////////////////////////////////////////////
  77. class TCheckedSkiffParser
  78. {
  79. public:
  80. TCheckedSkiffParser(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyInput* stream);
  81. ~TCheckedSkiffParser();
  82. i8 ParseInt8();
  83. i16 ParseInt16();
  84. i32 ParseInt32();
  85. i64 ParseInt64();
  86. ui8 ParseUint8();
  87. ui16 ParseUint16();
  88. ui32 ParseUint32();
  89. ui64 ParseUint64();
  90. TInt128 ParseInt128();
  91. TUint128 ParseUint128();
  92. double ParseDouble();
  93. bool ParseBoolean();
  94. TStringBuf ParseString32();
  95. TStringBuf ParseYson32();
  96. ui8 ParseVariant8Tag();
  97. ui16 ParseVariant16Tag();
  98. bool HasMoreData();
  99. void ValidateFinished();
  100. ui64 GetReadBytesCount() const;
  101. private:
  102. TUncheckedSkiffParser Parser_;
  103. std::unique_ptr<TSkiffValidator> Validator_;
  104. };
  105. ////////////////////////////////////////////////////////////////////
  106. class TUncheckedSkiffWriter
  107. {
  108. public:
  109. explicit TUncheckedSkiffWriter(IZeroCopyOutput* underlying);
  110. explicit TUncheckedSkiffWriter(IOutputStream* underlying);
  111. TUncheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyOutput* underlying);
  112. TUncheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IOutputStream* underlying);
  113. ~TUncheckedSkiffWriter();
  114. void WriteDouble(double value);
  115. void WriteBoolean(bool value);
  116. void WriteInt8(i8 value);
  117. void WriteInt16(i16 value);
  118. void WriteInt32(i32 value);
  119. void WriteInt64(i64 value);
  120. void WriteUint8(ui8 value);
  121. void WriteUint16(ui16 value);
  122. void WriteUint32(ui32 value);
  123. void WriteUint64(ui64 value);
  124. void WriteInt128(TInt128 value);
  125. void WriteUint128(TUint128 value);
  126. void WriteString32(TStringBuf value);
  127. void WriteYson32(TStringBuf value);
  128. void WriteVariant8Tag(ui8 tag);
  129. void WriteVariant16Tag(ui16 tag);
  130. void Flush();
  131. void Finish();
  132. private:
  133. template <typename T>
  134. void WriteSimple(T data);
  135. private:
  136. THolder<TBufferedOutput> BufferedOutput_;
  137. TZeroCopyOutputStreamWriter Underlying_;
  138. };
  139. ////////////////////////////////////////////////////////////////////////////////
  140. class TCheckedSkiffWriter
  141. {
  142. public:
  143. TCheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IZeroCopyOutput* underlying);
  144. TCheckedSkiffWriter(const std::shared_ptr<TSkiffSchema>& schema, IOutputStream* underlying);
  145. ~TCheckedSkiffWriter();
  146. void WriteInt8(i8 value);
  147. void WriteInt16(i16 value);
  148. void WriteInt32(i32 value);
  149. void WriteInt64(i64 value);
  150. void WriteUint8(ui8 value);
  151. void WriteUint16(ui16 value);
  152. void WriteUint32(ui32 value);
  153. void WriteUint64(ui64 value);
  154. void WriteDouble(double value);
  155. void WriteBoolean(bool value);
  156. void WriteInt128(TInt128 value);
  157. void WriteUint128(TUint128 value);
  158. void WriteString32(TStringBuf value);
  159. void WriteYson32(TStringBuf value);
  160. void WriteVariant8Tag(ui8 tag);
  161. void WriteVariant16Tag(ui16 tag);
  162. void Flush();
  163. void Finish();
  164. private:
  165. TUncheckedSkiffWriter Writer_;
  166. std::unique_ptr<TSkiffValidator> Validator_;
  167. };
  168. ////////////////////////////////////////////////////////////////////////////////
  169. template <EWireType wireType>
  170. class TUnderlyingIntegerType {
  171. private:
  172. TUnderlyingIntegerType() = default;
  173. static constexpr auto F();
  174. public:
  175. using TValue = decltype(TUnderlyingIntegerType::F());
  176. };
  177. ////////////////////////////////////////////////////////////////////////////////
  178. } // namespace NSkiff
  179. #define SKIFF_H
  180. #include "skiff-inl.h"
  181. #undef SKIFF_H