Flutter Engine
standard_codec_serializer.h
Go to the documentation of this file.
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_INCLUDE_FLUTTER_STANDARD_CODEC_SERIALIZER_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_CODEC_SERIALIZER_H_
7 
8 #include "byte_streams.h"
9 #include "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 //
16 // This can be subclassed to extend the standard codec with support for new
17 // types.
19  public:
20  virtual ~StandardCodecSerializer();
21 
22  // Returns the shared serializer instance.
23  static const StandardCodecSerializer& GetInstance();
24 
25  // Prevent copying.
28 
29  // Reads and returns the next value from |stream|.
31 
32  // Writes the encoding of |value| to |stream|, including the initial type
33  // discrimination byte.
34  //
35  // Can be overridden by a subclass to extend the codec.
36  virtual void WriteValue(const EncodableValue& value,
37  ByteStreamWriter* stream) const;
38 
39  protected:
40  // Codecs require long-lived serializers, so clients should always use
41  // GetInstance().
43 
44  // Reads and returns the next value from |stream|, whose discrimination byte
45  // was |type|.
46  //
47  // The discrimination byte will already have been read from the stream when
48  // this is called.
49  //
50  // Can be overridden by a subclass to extend the codec.
51  virtual EncodableValue ReadValueOfType(uint8_t type,
52  ByteStreamReader* stream) const;
53 
54  // Reads the variable-length size from the current position in |stream|.
55  size_t ReadSize(ByteStreamReader* stream) const;
56 
57  // Writes the variable-length size encoding to |stream|.
58  void WriteSize(size_t size, ByteStreamWriter* stream) const;
59 
60  private:
61  // Reads a fixed-type list whose values are of type T from the current
62  // position in |stream|, and returns it as the corresponding EncodableValue.
63  // |T| must correspond to one of the supported list value types of
64  // EncodableValue.
65  template <typename T>
66  EncodableValue ReadVector(ByteStreamReader* stream) const;
67 
68  // Writes |vector| to |stream| as a fixed-type list. |T| must correspond to
69  // one of the supported list value types of EncodableValue.
70  template <typename T>
71  void WriteVector(const std::vector<T> vector, ByteStreamWriter* stream) const;
72 };
73 
74 } // namespace flutter
75 
76 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_CODEC_SERIALIZER_H_
size_t ReadSize(ByteStreamReader *stream) const
virtual void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
EncodableValue ReadValue(ByteStreamReader *stream) const
static const StandardCodecSerializer & GetInstance()
uint8_t value
StandardCodecSerializer & operator=(StandardCodecSerializer const &)=delete
virtual EncodableValue ReadValueOfType(uint8_t type, ByteStreamReader *stream) const
void WriteSize(size_t size, ByteStreamWriter *stream) const