@@ -338,6 +338,16 @@ class GObjectHeaderGenerator
338
338
indent.newln ();
339
339
_writeDeclareFinalType (indent, module, _codecBaseName,
340
340
parentClassName: _standardCodecName);
341
+
342
+ final Iterable <EnumeratedType > customTypes =
343
+ getEnumeratedTypes (root, excludeSealedClasses: true );
344
+
345
+ for (final EnumeratedType customType in customTypes) {
346
+ final String customTypeId = _getCustomTypeId (module, customType);
347
+ indent.newln ();
348
+ indent
349
+ .writeln ('const uint8_t $customTypeId = ${customType .enumeration };' );
350
+ }
341
351
}
342
352
343
353
@override
@@ -1023,14 +1033,15 @@ class GObjectSourceGenerator
1023
1033
final String customTypeName = _getClassName (module, customType.name);
1024
1034
final String snakeCustomTypeName =
1025
1035
_snakeCaseFromCamelCase (customTypeName);
1036
+ final String customTypeId = _getCustomTypeId (module, customType);
1026
1037
indent.newln ();
1027
1038
final String valueType = customType.type == CustomTypes .customClass
1028
1039
? '$customTypeName *'
1029
1040
: 'FlValue*' ;
1030
1041
indent.writeScoped (
1031
1042
'static gboolean ${codecMethodPrefix }_write_$snakeCustomTypeName ($_standardCodecName * codec, GByteArray* buffer, $valueType value, GError** error) {' ,
1032
1043
'}' , () {
1033
- indent.writeln ('uint8_t type = ${ customType . enumeration } ;' );
1044
+ indent.writeln ('uint8_t type = $customTypeId ;' );
1034
1045
indent.writeln ('g_byte_array_append(buffer, &type, sizeof(uint8_t));' );
1035
1046
if (customType.type == CustomTypes .customClass) {
1036
1047
indent.writeln (
@@ -1053,7 +1064,8 @@ class GObjectSourceGenerator
1053
1064
indent.writeScoped ('switch (fl_value_get_custom_type(value)) {' , '}' ,
1054
1065
() {
1055
1066
for (final EnumeratedType customType in customTypes) {
1056
- indent.writeln ('case ${customType .enumeration }:' );
1067
+ final String customTypeId = _getCustomTypeId (module, customType);
1068
+ indent.writeln ('case $customTypeId :' );
1057
1069
indent.nest (1 , () {
1058
1070
final String customTypeName =
1059
1071
_getClassName (module, customType.name);
@@ -1082,6 +1094,7 @@ class GObjectSourceGenerator
1082
1094
final String customTypeName = _getClassName (module, customType.name);
1083
1095
final String snakeCustomTypeName =
1084
1096
_snakeCaseFromCamelCase (customTypeName);
1097
+ final String customTypeId = _getCustomTypeId (module, customType);
1085
1098
indent.newln ();
1086
1099
indent.writeScoped (
1087
1100
'static FlValue* ${codecMethodPrefix }_read_$snakeCustomTypeName ($_standardCodecName * codec, GBytes* buffer, size_t* offset, GError** error) {' ,
@@ -1102,10 +1115,10 @@ class GObjectSourceGenerator
1102
1115
});
1103
1116
indent.newln ();
1104
1117
indent.writeln (
1105
- 'return fl_value_new_custom_object(${ customType . enumeration } , G_OBJECT(value));' );
1118
+ 'return fl_value_new_custom_object($customTypeId , G_OBJECT(value));' );
1106
1119
} else if (customType.type == CustomTypes .customEnum) {
1107
1120
indent.writeln (
1108
- 'return fl_value_new_custom(${ customType . enumeration } , fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);' );
1121
+ 'return fl_value_new_custom($customTypeId , fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);' );
1109
1122
}
1110
1123
});
1111
1124
}
@@ -1117,9 +1130,10 @@ class GObjectSourceGenerator
1117
1130
indent.writeScoped ('switch (type) {' , '}' , () {
1118
1131
for (final EnumeratedType customType in customTypes) {
1119
1132
final String customTypeName = _getClassName (module, customType.name);
1133
+ final String customTypeId = _getCustomTypeId (module, customType);
1120
1134
final String snakeCustomTypeName =
1121
1135
_snakeCaseFromCamelCase (customTypeName);
1122
- indent.writeln ('case ${ customType . enumeration } :' );
1136
+ indent.writeln ('case $customTypeId :' );
1123
1137
indent.nest (1 , () {
1124
1138
indent.writeln (
1125
1139
'return ${codecMethodPrefix }_read_$snakeCustomTypeName (codec, buffer, offset, error);' );
@@ -1885,6 +1899,11 @@ String _getClassName(String module, String name) {
1885
1899
return '$module $name ' ;
1886
1900
}
1887
1901
1902
+ // Returns the GObject enum name for [name].
1903
+ String _getEnumName (String module, String name) {
1904
+ return '${module }_$name ' ;
1905
+ }
1906
+
1888
1907
// Returns the name to use for a class field with [name].
1889
1908
String _getFieldName (String name) {
1890
1909
final List <String > reservedNames = < String > ['type' ];
@@ -1922,6 +1941,19 @@ String _getMethodPrefix(String module, String name) {
1922
1941
return _snakeCaseFromCamelCase (className);
1923
1942
}
1924
1943
1944
+ // Returns the code for the custom type id definition for [customType].
1945
+ String _getCustomTypeId (String module, EnumeratedType customType) {
1946
+ final String customTypeName = switch (customType.type) {
1947
+ CustomTypes .customClass => _getClassName (module, customType.name),
1948
+ CustomTypes .customEnum => _getEnumName (module, customType.name),
1949
+ };
1950
+
1951
+ final String snakeCustomTypeName = _snakeCaseFromCamelCase (customTypeName);
1952
+
1953
+ final String customTypeId = '${snakeCustomTypeName }_type_id' ;
1954
+ return customTypeId;
1955
+ }
1956
+
1925
1957
// Returns an enumeration value in C++ form.
1926
1958
String _getEnumValue (String module, String enumName, String memberName) {
1927
1959
final String snakeEnumName = _snakeCaseFromCamelCase (enumName);
@@ -2062,12 +2094,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
2062
2094
}
2063
2095
}
2064
2096
2065
- int _getTypeEnumeration (Root root, TypeDeclaration type) {
2066
- return getEnumeratedTypes (root, excludeSealedClasses: true )
2067
- .firstWhere ((EnumeratedType t) =>
2068
- (type.isClass && t.associatedClass == type.associatedClass) ||
2069
- (type.isEnum && t.associatedEnum == type.associatedEnum))
2070
- .enumeration;
2097
+ String _getCustomTypeIdFromDeclaration (
2098
+ Root root, TypeDeclaration type, String module) {
2099
+ return _getCustomTypeId (
2100
+ module,
2101
+ getEnumeratedTypes (root, excludeSealedClasses: true ).firstWhere (
2102
+ (EnumeratedType t) =>
2103
+ (type.isClass && t.associatedClass == type.associatedClass) ||
2104
+ (type.isEnum && t.associatedEnum == type.associatedEnum)));
2071
2105
}
2072
2106
2073
2107
// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2112,15 @@ String _makeFlValue(
2078
2112
{String ? lengthVariableName}) {
2079
2113
final String value;
2080
2114
if (type.isClass) {
2081
- final int enumeration = _getTypeEnumeration (root, type);
2082
- value = 'fl_value_new_custom_object($enumeration , G_OBJECT($variableName ))' ;
2115
+ final String customTypeId =
2116
+ _getCustomTypeIdFromDeclaration (root, type, module);
2117
+ value =
2118
+ 'fl_value_new_custom_object($customTypeId , G_OBJECT($variableName ))' ;
2083
2119
} else if (type.isEnum) {
2084
- final int enumeration = _getTypeEnumeration (root, type);
2120
+ final String customTypeId =
2121
+ _getCustomTypeIdFromDeclaration (root, type, module);
2085
2122
value =
2086
- 'fl_value_new_custom($enumeration , fl_value_new_int(${type .isNullable ? '*$variableName ' : variableName }), (GDestroyNotify)fl_value_unref)' ;
2123
+ 'fl_value_new_custom($customTypeId , fl_value_new_int(${type .isNullable ? '*$variableName ' : variableName }), (GDestroyNotify)fl_value_unref)' ;
2087
2124
} else if (_isFlValueWrappedType (type)) {
2088
2125
value = 'fl_value_ref($variableName )' ;
2089
2126
} else if (type.baseName == 'void' ) {
0 commit comments