Advertisement
Guest User

Untitled

a guest
Dec 29th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #pragma once
  2. #include "algorithm.h"
  3. #include "byte-array.h"
  4. #include "common.h"
  5. #include "containers.h"
  6. #include "input-stream.h"
  7. #include "optional.h"
  8. #include "output-stream.h"
  9. #include "string.h"
  10. #include "value.h"
  11.  
  12. namespace fn {
  13. FN_UNINDENT
  14. namespace mpack {
  15.  
  16.     //
  17.     //
  18.     //
  19.  
  20.     class ValueSerializer
  21.     {
  22.     public:
  23.         // -1 means unsupported object (will be serialized as NIL);
  24.         // Negative types are reserved for future use in MessagePack Specification
  25.         virtual Int8 extTypeForValue(const Value & value) = 0;
  26.  
  27.         virtual void serializeValue(const Value & value, OutputStream & stream) = 0;
  28.  
  29.         virtual SizeType serializedLengthForValue(const Value & value)
  30.         {
  31.             CountingOutputStream stream;
  32.             serializeValue(value, stream);
  33.             return as<SizeType>(stream.currentOffset());
  34.         }
  35.  
  36.         virtual ~ValueSerializer() {}
  37.     };
  38.  
  39.     class FN_EXPORT Serializer : private ValueSerializer
  40.     {
  41.     public:
  42.         Serializer(OutputStream & stream)
  43.             : Serializer(stream, *this)
  44.         {
  45.         }
  46.  
  47.         Serializer(OutputStream & stream, ValueSerializer & valueSerializer)
  48.             : _writer(stream)
  49.             , _valueSerializer(valueSerializer)
  50.         {
  51.         }
  52.  
  53.         void serialize(const Value & value);
  54.  
  55.         virtual ~Serializer() override {}
  56.  
  57.     protected:
  58.         Int8 extTypeForValue(const Value &) override { return -1; }
  59.         void serializeValue(const Value &, OutputStream &) override {}
  60.  
  61.     private:
  62.         void writeInt(Int64 i);
  63.         void writeUInt(UInt64 i);
  64.         void writeDouble(double f);
  65.         void writeString(StringView s);
  66.         void writeByteArray(const ByteArray & b);
  67.         void writeBoxed(const Value & value);
  68.         void writeArray(const Value::Vector & array);
  69.         void writeMap(const Value::Map & map);
  70.         void writeCustom(const Value & value);
  71.  
  72.         StreamWriter _writer;
  73.         ValueSerializer & _valueSerializer;
  74.     };
  75.  
  76.     //
  77.     //
  78.     //
  79.  
  80.     class ValueDeserializer
  81.     {
  82.     public:
  83.         virtual Value deserializeValue(int objectType, SizeType size, InputStream & stream) = 0;
  84.  
  85.         virtual ~ValueDeserializer() {}
  86.     };
  87.  
  88.     class FN_EXPORT Deserializer : private ValueDeserializer
  89.     {
  90.     public:
  91.         Deserializer(InputStream & stream, ValueDeserializer & valueDeserializer)
  92.             : _reader(stream)
  93.             , _valueDeserializer(valueDeserializer)
  94.         {
  95.         }
  96.  
  97.         Deserializer(InputStream & stream)
  98.             : Deserializer(stream, *this)
  99.         {
  100.         }
  101.  
  102.         Optional<Value> deserialize();
  103.  
  104.     protected:
  105.         Value deserializeValue(int, SizeType, InputStream &) override { return Value(); }
  106.  
  107.     private:
  108.         ByteArray readByteArray(SizeType size);
  109.         Value readArray(SizeType size);
  110.         Value readMap(SizeType size);
  111.         Value readObject(SizeType size);
  112.  
  113.         StreamReader _reader;
  114.         ValueDeserializer & _valueDeserializer;
  115.     };
  116.  
  117.     //
  118.     // Inlines
  119.     //
  120.  
  121. } // namespace mpack
  122. } // namespace fn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement