Add Float32List support to the Linux standard message codec
This commit is contained in:
@@ -63,8 +63,8 @@ class CustomEncodableValue {
|
||||
~CustomEncodableValue() = default;
|
||||
|
||||
// Allow implicit conversion to std::any to allow direct use of any_cast.
|
||||
operator std::any&() { return value_; }
|
||||
operator const std::any&() const { return value_; }
|
||||
operator std::any &() { return value_; }
|
||||
operator const std::any &() const { return value_; }
|
||||
|
||||
#if defined(FLUTTER_ENABLE_RTTI) && FLUTTER_ENABLE_RTTI
|
||||
// Passthrough to std::any's type().
|
||||
|
||||
@@ -26,6 +26,7 @@ static constexpr int kValueInt64List = 10;
|
||||
static constexpr int kValueFloat64List = 11;
|
||||
static constexpr int kValueList = 12;
|
||||
static constexpr int kValueMap = 13;
|
||||
static constexpr int kValueFloat32List = 14;
|
||||
|
||||
struct _FlStandardMessageCodec {
|
||||
FlMessageCodec parent_instance;
|
||||
@@ -295,6 +296,30 @@ static FlValue* read_int64_list_value(FlStandardMessageCodec* self,
|
||||
return value;
|
||||
}
|
||||
|
||||
// Reads a 32 bit floating point number list from @buffer in standard codec
|
||||
// format. Returns a new #FlValue of type #FL_VALUE_TYPE_FLOAT32_LIST if
|
||||
// successful or %NULL on error.
|
||||
static FlValue* read_float32_list_value(FlStandardMessageCodec* self,
|
||||
GBytes* buffer,
|
||||
size_t* offset,
|
||||
GError** error) {
|
||||
uint32_t length;
|
||||
if (!fl_standard_message_codec_read_size(self, buffer, offset, &length,
|
||||
error)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!read_align(buffer, offset, 4, error)) {
|
||||
return nullptr;
|
||||
}
|
||||
if (!check_size(buffer, *offset, sizeof(float) * length, error)) {
|
||||
return nullptr;
|
||||
}
|
||||
FlValue* value = fl_value_new_float32_list(
|
||||
reinterpret_cast<const float*>(get_data(buffer, offset)), length);
|
||||
*offset += sizeof(float) * length;
|
||||
return value;
|
||||
}
|
||||
|
||||
// Reads a floating point number list from @buffer in standard codec format.
|
||||
// Returns a new #FlValue of type #FL_VALUE_TYPE_FLOAT_LIST if successful or
|
||||
// %NULL on error.
|
||||
@@ -547,6 +572,17 @@ gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* self,
|
||||
sizeof(int64_t) * length);
|
||||
return TRUE;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT32_LIST: {
|
||||
write_uint8(buffer, kValueFloat32List);
|
||||
size_t length = fl_value_get_length(value);
|
||||
fl_standard_message_codec_write_size(self, buffer, length);
|
||||
write_align(buffer, 4);
|
||||
g_byte_array_append(
|
||||
buffer,
|
||||
reinterpret_cast<const uint8_t*>(fl_value_get_float32_list(value)),
|
||||
sizeof(float) * length);
|
||||
return TRUE;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT_LIST: {
|
||||
write_uint8(buffer, kValueFloat64List);
|
||||
size_t length = fl_value_get_length(value);
|
||||
@@ -620,6 +656,8 @@ FlValue* fl_standard_message_codec_read_value(FlStandardMessageCodec* self,
|
||||
value = read_int32_list_value(self, buffer, offset, error);
|
||||
} else if (type == kValueInt64List) {
|
||||
value = read_int64_list_value(self, buffer, offset, error);
|
||||
} else if (type == kValueFloat32List) {
|
||||
value = read_float32_list_value(self, buffer, offset, error);
|
||||
} else if (type == kValueFloat64List) {
|
||||
value = read_float64_list_value(self, buffer, offset, error);
|
||||
} else if (type == kValueList) {
|
||||
|
||||
@@ -526,6 +526,58 @@ TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData2) {
|
||||
FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, EncodeFloat32ListEmpty) {
|
||||
g_autoptr(FlValue) value = fl_value_new_float32_list(nullptr, 0);
|
||||
g_autofree gchar* hex_string = encode_message(value);
|
||||
EXPECT_STREQ(hex_string, "0e000000");
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, EncodeFloat32List) {
|
||||
float data[] = {0.0f, -0.5f, 0.25f, -0.125f, 0.00625f};
|
||||
g_autoptr(FlValue) value = fl_value_new_float32_list(data, 5);
|
||||
g_autofree gchar* hex_string = encode_message(value);
|
||||
EXPECT_STREQ(hex_string, "0e05000000000000000000bf0000803e000000becdcccc3b");
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeFloat32ListEmpty) {
|
||||
g_autoptr(FlValue) value = decode_message("0e000000");
|
||||
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT32_LIST);
|
||||
EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeFloat32List) {
|
||||
g_autoptr(FlValue) value =
|
||||
decode_message("0e05000000000000000000bf0000803e000000becdcccc3b");
|
||||
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT32_LIST);
|
||||
const float* data = fl_value_get_float32_list(value);
|
||||
EXPECT_FLOAT_EQ(data[0], 0.0f);
|
||||
EXPECT_FLOAT_EQ(data[1], -0.5f);
|
||||
EXPECT_FLOAT_EQ(data[2], 0.25f);
|
||||
EXPECT_FLOAT_EQ(data[3], -0.125f);
|
||||
EXPECT_FLOAT_EQ(data[4], 0.00625f);
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeFloat32ListNoData) {
|
||||
decode_error_value("0e", FL_MESSAGE_CODEC_ERROR,
|
||||
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeFloat32ListLengthNoData) {
|
||||
decode_error_value("0e050000", FL_MESSAGE_CODEC_ERROR,
|
||||
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData1) {
|
||||
decode_error_value("0e05000000", FL_MESSAGE_CODEC_ERROR,
|
||||
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData2) {
|
||||
decode_error_value("0e05000000000000000000bf0000803e000000becdcccc",
|
||||
FL_MESSAGE_CODEC_ERROR,
|
||||
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
|
||||
g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
|
||||
g_autofree gchar* hex_string = encode_message(value);
|
||||
@@ -1006,7 +1058,7 @@ TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
|
||||
}
|
||||
|
||||
TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
|
||||
decode_error_value("0e", FL_MESSAGE_CODEC_ERROR,
|
||||
decode_error_value("0f", FL_MESSAGE_CODEC_ERROR,
|
||||
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,12 @@ typedef struct {
|
||||
size_t values_length;
|
||||
} FlValueInt64List;
|
||||
|
||||
typedef struct {
|
||||
FlValue parent;
|
||||
float* values;
|
||||
size_t values_length;
|
||||
} FlValueFloat32List;
|
||||
|
||||
typedef struct {
|
||||
FlValue parent;
|
||||
double* values;
|
||||
@@ -178,6 +184,18 @@ static void value_to_string(FlValue* value, GString* buffer) {
|
||||
g_string_append(buffer, "]");
|
||||
return;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT32_LIST: {
|
||||
g_string_append(buffer, "[");
|
||||
const float* values = fl_value_get_float32_list(value);
|
||||
for (size_t i = 0; i < fl_value_get_length(value); i++) {
|
||||
if (i != 0) {
|
||||
g_string_append(buffer, ", ");
|
||||
}
|
||||
float_to_string(values[i], buffer);
|
||||
}
|
||||
g_string_append(buffer, "]");
|
||||
return;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT_LIST: {
|
||||
g_string_append(buffer, "[");
|
||||
const double* values = fl_value_get_float_list(value);
|
||||
@@ -297,6 +315,16 @@ G_MODULE_EXPORT FlValue* fl_value_new_int64_list(const int64_t* data,
|
||||
return reinterpret_cast<FlValue*>(self);
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT FlValue* fl_value_new_float32_list(const float* data,
|
||||
size_t data_length) {
|
||||
FlValueFloat32List* self = reinterpret_cast<FlValueFloat32List*>(
|
||||
fl_value_new(FL_VALUE_TYPE_FLOAT32_LIST, sizeof(FlValueFloat32List)));
|
||||
self->values_length = data_length;
|
||||
self->values = static_cast<float*>(g_malloc(sizeof(float) * data_length));
|
||||
memcpy(self->values, data, sizeof(float) * data_length);
|
||||
return reinterpret_cast<FlValue*>(self);
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT FlValue* fl_value_new_float_list(const double* data,
|
||||
size_t data_length) {
|
||||
FlValueFloatList* self = reinterpret_cast<FlValueFloatList*>(
|
||||
@@ -367,6 +395,11 @@ G_MODULE_EXPORT void fl_value_unref(FlValue* self) {
|
||||
g_free(v->values);
|
||||
break;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT32_LIST: {
|
||||
FlValueFloat32List* v = reinterpret_cast<FlValueFloat32List*>(self);
|
||||
g_free(v->values);
|
||||
break;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT_LIST: {
|
||||
FlValueFloatList* v = reinterpret_cast<FlValueFloatList*>(self);
|
||||
g_free(v->values);
|
||||
@@ -458,6 +491,19 @@ G_MODULE_EXPORT bool fl_value_equal(FlValue* a, FlValue* b) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT32_LIST: {
|
||||
if (fl_value_get_length(a) != fl_value_get_length(b)) {
|
||||
return false;
|
||||
}
|
||||
const float* values_a = fl_value_get_float32_list(a);
|
||||
const float* values_b = fl_value_get_float32_list(b);
|
||||
for (size_t i = 0; i < fl_value_get_length(a); i++) {
|
||||
if (values_a[i] != values_b[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT_LIST: {
|
||||
if (fl_value_get_length(a) != fl_value_get_length(b)) {
|
||||
return false;
|
||||
@@ -621,6 +667,13 @@ G_MODULE_EXPORT const int64_t* fl_value_get_int64_list(FlValue* self) {
|
||||
return v->values;
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT const float* fl_value_get_float32_list(FlValue* self) {
|
||||
g_return_val_if_fail(self != nullptr, nullptr);
|
||||
g_return_val_if_fail(self->type == FL_VALUE_TYPE_FLOAT32_LIST, nullptr);
|
||||
FlValueFloat32List* v = reinterpret_cast<FlValueFloat32List*>(self);
|
||||
return v->values;
|
||||
}
|
||||
|
||||
G_MODULE_EXPORT const double* fl_value_get_float_list(FlValue* self) {
|
||||
g_return_val_if_fail(self != nullptr, nullptr);
|
||||
g_return_val_if_fail(self->type == FL_VALUE_TYPE_FLOAT_LIST, nullptr);
|
||||
@@ -633,6 +686,7 @@ G_MODULE_EXPORT size_t fl_value_get_length(FlValue* self) {
|
||||
g_return_val_if_fail(self->type == FL_VALUE_TYPE_UINT8_LIST ||
|
||||
self->type == FL_VALUE_TYPE_INT32_LIST ||
|
||||
self->type == FL_VALUE_TYPE_INT64_LIST ||
|
||||
self->type == FL_VALUE_TYPE_FLOAT32_LIST ||
|
||||
self->type == FL_VALUE_TYPE_FLOAT_LIST ||
|
||||
self->type == FL_VALUE_TYPE_LIST ||
|
||||
self->type == FL_VALUE_TYPE_MAP,
|
||||
@@ -651,6 +705,10 @@ G_MODULE_EXPORT size_t fl_value_get_length(FlValue* self) {
|
||||
FlValueInt64List* v = reinterpret_cast<FlValueInt64List*>(self);
|
||||
return v->values_length;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT32_LIST: {
|
||||
FlValueFloat32List* v = reinterpret_cast<FlValueFloat32List*>(self);
|
||||
return v->values_length;
|
||||
}
|
||||
case FL_VALUE_TYPE_FLOAT_LIST: {
|
||||
FlValueFloatList* v = reinterpret_cast<FlValueFloatList*>(self);
|
||||
return v->values_length;
|
||||
|
||||
@@ -30,6 +30,7 @@ G_BEGIN_DECLS
|
||||
* - #FL_VALUE_TYPE_UINT8_LIST: Uint8List
|
||||
* - #FL_VALUE_TYPE_INT32_LIST: Int32List
|
||||
* - #FL_VALUE_TYPE_INT64_LIST: Int64List
|
||||
* - #FL_VALUE_TYPE_FLOAT32_LIST: Float32List
|
||||
* - #FL_VALUE_TYPE_FLOAT_LIST: Float64List
|
||||
* - #FL_VALUE_TYPE_LIST: List<dynamic>
|
||||
* - #FL_VALUE_TYPE_MAP: Map<dynamic>
|
||||
@@ -51,6 +52,7 @@ typedef struct _FlValue FlValue;
|
||||
* @FL_VALUE_TYPE_FLOAT_LIST: An ordered list of floating point numbers.
|
||||
* @FL_VALUE_TYPE_LIST: An ordered list of #FlValue objects.
|
||||
* @FL_VALUE_TYPE_MAP: A map of #FlValue objects keyed by #FlValue object.
|
||||
* @FL_VALUE_TYPE_FLOAT32_LIST: An ordered list of 32bit floating point numbers.
|
||||
*
|
||||
* Types of #FlValue.
|
||||
*/
|
||||
@@ -66,6 +68,7 @@ typedef enum {
|
||||
FL_VALUE_TYPE_FLOAT_LIST,
|
||||
FL_VALUE_TYPE_LIST,
|
||||
FL_VALUE_TYPE_MAP,
|
||||
FL_VALUE_TYPE_FLOAT32_LIST,
|
||||
} FlValueType;
|
||||
|
||||
/**
|
||||
@@ -181,6 +184,18 @@ FlValue* fl_value_new_int32_list(const int32_t* value, size_t value_length);
|
||||
*/
|
||||
FlValue* fl_value_new_int64_list(const int64_t* value, size_t value_length);
|
||||
|
||||
/**
|
||||
* fl_value_new_float32_list:
|
||||
* @value: an array of floating point numbers.
|
||||
* @value_length: number of elements in @value.
|
||||
*
|
||||
* Creates an ordered list containing 32 bit floating point numbers.
|
||||
* The equivalent Dart type is a Float32List.
|
||||
*
|
||||
* Returns: a new #FlValue.
|
||||
*/
|
||||
FlValue* fl_value_new_float32_list(const float* value, size_t value_length);
|
||||
|
||||
/**
|
||||
* fl_value_new_float_list:
|
||||
* @value: an array of floating point numbers.
|
||||
@@ -435,7 +450,8 @@ const gchar* fl_value_get_string(FlValue* value);
|
||||
* fl_value_get_length:
|
||||
* @value: an #FlValue of type #FL_VALUE_TYPE_UINT8_LIST,
|
||||
* #FL_VALUE_TYPE_INT32_LIST, #FL_VALUE_TYPE_INT64_LIST,
|
||||
* #FL_VALUE_TYPE_FLOAT_LIST, #FL_VALUE_TYPE_LIST or #FL_VALUE_TYPE_MAP.
|
||||
* #FL_VALUE_TYPE_FLOAT32_LIST, #FL_VALUE_TYPE_FLOAT_LIST, #FL_VALUE_TYPE_LIST
|
||||
* or #FL_VALUE_TYPE_MAP.
|
||||
*
|
||||
* Gets the number of elements @value contains. This is only valid for list
|
||||
* and map types. Calling this with other types is a programming error.
|
||||
@@ -480,6 +496,18 @@ const int32_t* fl_value_get_int32_list(FlValue* value);
|
||||
*/
|
||||
const int64_t* fl_value_get_int64_list(FlValue* value);
|
||||
|
||||
/**
|
||||
* fl_value_get_float32_list:
|
||||
* @value: an #FlValue of type #FL_VALUE_TYPE_FLOAT32_LIST.
|
||||
*
|
||||
* Gets the array of floating point numbers @value contains. The data
|
||||
* contains fl_get_length() elements. Calling this with an #FlValue that is
|
||||
* not of type #FL_VALUE_TYPE_FLOAT32_LIST is a programming error.
|
||||
*
|
||||
* Returns: an array of floating point numbers.
|
||||
*/
|
||||
const float* fl_value_get_float32_list(FlValue* value);
|
||||
|
||||
/**
|
||||
* fl_value_get_float_list:
|
||||
* @value: an #FlValue of type #FL_VALUE_TYPE_FLOAT_LIST.
|
||||
|
||||
Reference in New Issue
Block a user