forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow extending the C++ standard codec (flutter#20317)
Standard*Codec allows for extensions to support arbitrary types; this had not previously been implemented for the C++ version. Overview of changes: - EncodableValue's std::variant type now allows for a new CustomEncodableValue, which is a thin wrapper around std::any, to store arbitrary extension types. - ByteBufferStream* has been split into an interface class and the buffer-based implementation, with the former now part of the public API surface to be used in standard codec extensions. - They also gained utility methods for some common data types to simplify writing extensions. - StandardCodecSerializer is now part of the public API surface, and is subclassable. - StandardCodecSerializer's ReadValue has been split into ReadValue and ReadValueOfType to match the structure used when subclassing on the Dart side, for easier porting of custom extensions across languages. - Standard*Codec now optionally accepts a non-default serializer in GetInstance, providing a shared instance using that serializer. Fixes flutter/flutter#31174
- Loading branch information
1 parent
8ed1964
commit b28b1df
Showing
15 changed files
with
682 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
shell/platform/common/cpp/client_wrapper/include/flutter/byte_streams.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BYTE_STREAMS_H_ | ||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BYTE_STREAMS_H_ | ||
|
||
// Interfaces for interacting with a stream of bytes, for use in codecs. | ||
|
||
namespace flutter { | ||
|
||
// An interface for a class that reads from a byte stream. | ||
class ByteStreamReader { | ||
public: | ||
// Reads and returns the next byte from the stream. | ||
virtual uint8_t ReadByte() = 0; | ||
|
||
// Reads the next |length| bytes from the stream into |buffer|. The caller | ||
// is responsible for ensuring that |buffer| is large enough. | ||
virtual void ReadBytes(uint8_t* buffer, size_t length) = 0; | ||
|
||
// Advances the read cursor to the next multiple of |alignment| relative to | ||
// the start of the stream, unless it is already aligned. | ||
virtual void ReadAlignment(uint8_t alignment) = 0; | ||
|
||
// Reads and returns the next 32-bit integer from the stream. | ||
int32_t ReadInt32() { | ||
int32_t value = 0; | ||
ReadBytes(reinterpret_cast<uint8_t*>(&value), 4); | ||
return value; | ||
} | ||
|
||
// Reads and returns the next 64-bit integer from the stream. | ||
int64_t ReadInt64() { | ||
int64_t value = 0; | ||
ReadBytes(reinterpret_cast<uint8_t*>(&value), 8); | ||
return value; | ||
} | ||
|
||
// Reads and returns the next 64-bit floating point number from the stream. | ||
double ReadDouble() { | ||
double value = 0; | ||
ReadBytes(reinterpret_cast<uint8_t*>(&value), 8); | ||
return value; | ||
} | ||
}; | ||
|
||
// An interface for a class that writes to a byte stream. | ||
class ByteStreamWriter { | ||
public: | ||
// Writes |byte| to the stream. | ||
virtual void WriteByte(uint8_t byte) = 0; | ||
|
||
// Writes the next |length| bytes from |bytes| to the stream | ||
virtual void WriteBytes(const uint8_t* bytes, size_t length) = 0; | ||
|
||
// Writes 0s until the next multiple of |alignment| relative to the start | ||
// of the stream, unless the write positition is already aligned. | ||
virtual void WriteAlignment(uint8_t alignment) = 0; | ||
|
||
// Writes the given 32-bit int to the stream. | ||
void WriteInt32(int32_t value) { | ||
WriteBytes(reinterpret_cast<const uint8_t*>(&value), 4); | ||
} | ||
|
||
// Writes the given 64-bit int to the stream. | ||
void WriteInt64(int64_t value) { | ||
WriteBytes(reinterpret_cast<const uint8_t*>(&value), 8); | ||
} | ||
|
||
// Writes the given 36-bit double to the stream. | ||
void WriteDouble(double value) { | ||
WriteBytes(reinterpret_cast<const uint8_t*>(&value), 8); | ||
} | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BYTE_STREAMS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.