• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_ENCODABLE_VALUE_SERIALIZER_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_ENCODABLE_VALUE_SERIALIZER_H_
7 
8 #include "byte_stream_wrappers.h"
9 #include "include/flutter/encodable_value.h"
10 
11 namespace flutter {
12 
13 // Encapsulates the logic for encoding/decoding EncodableValues to/from the
14 // standard codec binary representation.
15 class StandardCodecSerializer {
16  public:
17   StandardCodecSerializer();
18   ~StandardCodecSerializer();
19 
20   // Prevent copying.
21   StandardCodecSerializer(StandardCodecSerializer const&) = delete;
22   StandardCodecSerializer& operator=(StandardCodecSerializer const&) = delete;
23 
24   // Reads and returns the next value from |stream|.
25   EncodableValue ReadValue(ByteBufferStreamReader* stream) const;
26 
27   // Writes the encoding of |value| to |stream|.
28   void WriteValue(const EncodableValue& value,
29                   ByteBufferStreamWriter* stream) const;
30 
31  protected:
32   // Reads the variable-length size from the current position in |stream|.
33   size_t ReadSize(ByteBufferStreamReader* stream) const;
34 
35   // Writes the variable-length size encoding to |stream|.
36   void WriteSize(size_t size, ByteBufferStreamWriter* stream) const;
37 
38   // Reads a fixed-type list whose values are of type T from the current
39   // position in |stream|, and returns it as the corresponding EncodableValue.
40   // |T| must correspond to one of the support list value types of
41   // EncodableValue.
42   template <typename T>
43   EncodableValue ReadVector(ByteBufferStreamReader* stream) const;
44 
45   // Writes |vector| to |stream| as a fixed-type list. |T| must correspond to
46   // one of the support list value types of EncodableValue.
47   template <typename T>
48   void WriteVector(const std::vector<T> vector,
49                    ByteBufferStreamWriter* stream) const;
50 };
51 
52 }  // namespace flutter
53 
54 #endif  // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_ENCODABLE_VALUE_SERIALIZER_H_
55