Flutter Engine
test_codec_extensions.cc
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 #include "flutter/shell/platform/common/cpp/client_wrapper/testing/test_codec_extensions.h"
6 
7 namespace flutter {
8 
11 
12 // static
14  static PointExtensionSerializer sInstance;
15  return sInstance;
16 }
17 
19  uint8_t type,
20  ByteStreamReader* stream) const {
21  if (type == kPointType) {
22  int32_t x = stream->ReadInt32();
23  int32_t y = stream->ReadInt32();
24  return CustomEncodableValue(Point(x, y));
25  }
26  return StandardCodecSerializer::ReadValueOfType(type, stream);
27 }
28 
30  ByteStreamWriter* stream) const {
31  auto custom_value = std::get_if<CustomEncodableValue>(&value);
32  if (!custom_value) {
34  return;
35  }
36  stream->WriteByte(kPointType);
37  const Point& point = std::any_cast<Point>(*custom_value);
38  stream->WriteInt32(point.x());
39  stream->WriteInt32(point.y());
40 }
41 
44 
45 // static
47  static SomeDataExtensionSerializer sInstance;
48  return sInstance;
49 }
50 
52  uint8_t type,
53  ByteStreamReader* stream) const {
54  if (type == kSomeDataType) {
55  size_t size = ReadSize(stream);
56  std::vector<uint8_t> data;
57  data.resize(size);
58  stream->ReadBytes(data.data(), size);
59  EncodableValue label = ReadValue(stream);
60  return CustomEncodableValue(SomeData(std::get<std::string>(label), data));
61  }
62  return StandardCodecSerializer::ReadValueOfType(type, stream);
63 }
64 
66  ByteStreamWriter* stream) const {
67  auto custom_value = std::get_if<CustomEncodableValue>(&value);
68  if (!custom_value) {
70  return;
71  }
72  stream->WriteByte(kSomeDataType);
73  const SomeData& some_data = std::any_cast<SomeData>(*custom_value);
74  size_t data_size = some_data.data().size();
75  WriteSize(data_size, stream);
76  stream->WriteBytes(some_data.data().data(), data_size);
77  WriteValue(EncodableValue(some_data.label()), stream);
78 }
79 
80 } // namespace flutter
static const PointExtensionSerializer & GetInstance()
size_t ReadSize(ByteStreamReader *stream) const
virtual void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const
virtual void ReadBytes(uint8_t *buffer, size_t length)=0
virtual void WriteByte(uint8_t byte)=0
void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const override
const std::string & label() const
const std::vector< uint8_t > & data() const
void WriteValue(const EncodableValue &value, ByteStreamWriter *stream) const override
static const SomeDataExtensionSerializer & GetInstance()
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
EncodableValue ReadValue(ByteStreamReader *stream) const
EncodableValue ReadValueOfType(uint8_t type, ByteStreamReader *stream) const override
uint8_t value
virtual void WriteBytes(const uint8_t *bytes, size_t length)=0
void WriteInt32(int32_t value)
Definition: byte_streams.h:68
virtual EncodableValue ReadValueOfType(uint8_t type, ByteStreamReader *stream) const
void WriteSize(size_t size, ByteStreamWriter *stream) const
EncodableValue ReadValueOfType(uint8_t type, ByteStreamReader *stream) const override