packed.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include <library/cpp/streams/zc_memory_input/zc_memory_input.h>
  3. #include <util/stream/output.h>
  4. #include <util/ysaveload.h>
  5. #include "longs.h"
  6. struct Stream_traits {
  7. template <typename T>
  8. static T get(IInputStream& in) {
  9. T x;
  10. ::Load(&in, x);
  11. return x;
  12. }
  13. static ui8 get_8(IInputStream& in) {
  14. return get<ui8>(in);
  15. }
  16. static ui16 get_16(IInputStream& in) {
  17. return get<ui16>(in);
  18. }
  19. static ui32 get_32(IInputStream& in) {
  20. return get<ui32>(in);
  21. }
  22. static void put_8(ui8 x, IOutputStream& out) {
  23. ::Save(&out, x);
  24. }
  25. static void put_16(ui16 x, IOutputStream& out) {
  26. ::Save(&out, x);
  27. }
  28. static void put_32(ui32 x, IOutputStream& out) {
  29. ::Save(&out, x);
  30. }
  31. static int is_good(IInputStream& /*in*/) {
  32. return 1;
  33. }
  34. static int is_good(IOutputStream& /*out*/) {
  35. return 1;
  36. }
  37. };
  38. struct TZCMemoryInput_traits {
  39. template <typename T>
  40. static T get(TZCMemoryInput& in) {
  41. T x;
  42. in.ReadPOD(x);
  43. return x;
  44. }
  45. static ui8 Y_FORCE_INLINE get_8(TZCMemoryInput& in) {
  46. return get<ui8>(in);
  47. }
  48. static ui16 Y_FORCE_INLINE get_16(TZCMemoryInput& in) {
  49. return get<ui16>(in);
  50. }
  51. static ui32 Y_FORCE_INLINE get_32(TZCMemoryInput& in) {
  52. return get<ui32>(in);
  53. }
  54. static int Y_FORCE_INLINE is_good(TZCMemoryInput&) {
  55. return 1;
  56. }
  57. };
  58. void Y_FORCE_INLINE PackUI32(IOutputStream& out, ui32 v) {
  59. char buf[sizeof(ui32)];
  60. char* bufPtr = buf;
  61. size_t size;
  62. PACK_28(v, bufPtr, mem_traits, size);
  63. out.Write(buf, size);
  64. }
  65. template <class TStream>
  66. struct TInputStream2Traits {
  67. typedef Stream_traits TTraits;
  68. };
  69. template <>
  70. struct TInputStream2Traits<TZCMemoryInput> {
  71. typedef TZCMemoryInput_traits TTraits;
  72. };
  73. template <class TStream>
  74. void Y_FORCE_INLINE UnPackUI32(TStream& in, ui32& v) {
  75. size_t size;
  76. UNPACK_28(v, in, TInputStream2Traits<TStream>::TTraits, size);
  77. (void)size;
  78. }
  79. template <class TStream>
  80. ui32 Y_FORCE_INLINE UnPackUI32(TStream& in) {
  81. ui32 res;
  82. UnPackUI32(in, res);
  83. return res;
  84. }