@@ -338,6 +338,15 @@ 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.writeln ('extern const int $customTypeId ;' );
349
+ }
341
350
}
342
351
343
352
@override
@@ -1023,14 +1032,19 @@ class GObjectSourceGenerator
1023
1032
final String customTypeName = _getClassName (module, customType.name);
1024
1033
final String snakeCustomTypeName =
1025
1034
_snakeCaseFromCamelCase (customTypeName);
1035
+ final String customTypeId = _getCustomTypeId (module, customType);
1036
+
1037
+ indent.newln ();
1038
+ indent.writeln ('const int $customTypeId = ${customType .enumeration };' );
1039
+
1026
1040
indent.newln ();
1027
1041
final String valueType = customType.type == CustomTypes .customClass
1028
1042
? '$customTypeName *'
1029
1043
: 'FlValue*' ;
1030
1044
indent.writeScoped (
1031
1045
'static gboolean ${codecMethodPrefix }_write_$snakeCustomTypeName ($_standardCodecName * codec, GByteArray* buffer, $valueType value, GError** error) {' ,
1032
1046
'}' , () {
1033
- indent.writeln ('uint8_t type = ${ customType . enumeration } ;' );
1047
+ indent.writeln ('uint8_t type = $customTypeId ;' );
1034
1048
indent.writeln ('g_byte_array_append(buffer, &type, sizeof(uint8_t));' );
1035
1049
if (customType.type == CustomTypes .customClass) {
1036
1050
indent.writeln (
@@ -1053,7 +1067,8 @@ class GObjectSourceGenerator
1053
1067
indent.writeScoped ('switch (fl_value_get_custom_type(value)) {' , '}' ,
1054
1068
() {
1055
1069
for (final EnumeratedType customType in customTypes) {
1056
- indent.writeln ('case ${customType .enumeration }:' );
1070
+ final String customTypeId = _getCustomTypeId (module, customType);
1071
+ indent.writeln ('case $customTypeId :' );
1057
1072
indent.nest (1 , () {
1058
1073
final String customTypeName =
1059
1074
_getClassName (module, customType.name);
@@ -1082,6 +1097,7 @@ class GObjectSourceGenerator
1082
1097
final String customTypeName = _getClassName (module, customType.name);
1083
1098
final String snakeCustomTypeName =
1084
1099
_snakeCaseFromCamelCase (customTypeName);
1100
+ final String customTypeId = _getCustomTypeId (module, customType);
1085
1101
indent.newln ();
1086
1102
indent.writeScoped (
1087
1103
'static FlValue* ${codecMethodPrefix }_read_$snakeCustomTypeName ($_standardCodecName * codec, GBytes* buffer, size_t* offset, GError** error) {' ,
@@ -1102,10 +1118,10 @@ class GObjectSourceGenerator
1102
1118
});
1103
1119
indent.newln ();
1104
1120
indent.writeln (
1105
- 'return fl_value_new_custom_object(${ customType . enumeration } , G_OBJECT(value));' );
1121
+ 'return fl_value_new_custom_object($customTypeId , G_OBJECT(value));' );
1106
1122
} else if (customType.type == CustomTypes .customEnum) {
1107
1123
indent.writeln (
1108
- 'return fl_value_new_custom(${ customType . enumeration } , fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);' );
1124
+ 'return fl_value_new_custom($customTypeId , fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);' );
1109
1125
}
1110
1126
});
1111
1127
}
@@ -1117,9 +1133,10 @@ class GObjectSourceGenerator
1117
1133
indent.writeScoped ('switch (type) {' , '}' , () {
1118
1134
for (final EnumeratedType customType in customTypes) {
1119
1135
final String customTypeName = _getClassName (module, customType.name);
1136
+ final String customTypeId = _getCustomTypeId (module, customType);
1120
1137
final String snakeCustomTypeName =
1121
1138
_snakeCaseFromCamelCase (customTypeName);
1122
- indent.writeln ('case ${ customType . enumeration } :' );
1139
+ indent.writeln ('case $customTypeId :' );
1123
1140
indent.nest (1 , () {
1124
1141
indent.writeln (
1125
1142
'return ${codecMethodPrefix }_read_$snakeCustomTypeName (codec, buffer, offset, error);' );
@@ -1922,6 +1939,16 @@ String _getMethodPrefix(String module, String name) {
1922
1939
return _snakeCaseFromCamelCase (className);
1923
1940
}
1924
1941
1942
+ // Returns the code for the custom type id definition for [customType].
1943
+ String _getCustomTypeId (String module, EnumeratedType customType) {
1944
+ final String customTypeName = _getClassName (module, customType.name);
1945
+
1946
+ final String snakeCustomTypeName = _snakeCaseFromCamelCase (customTypeName);
1947
+
1948
+ final String customTypeId = '${snakeCustomTypeName }_type_id' ;
1949
+ return customTypeId;
1950
+ }
1951
+
1925
1952
// Returns an enumeration value in C++ form.
1926
1953
String _getEnumValue (String module, String enumName, String memberName) {
1927
1954
final String snakeEnumName = _snakeCaseFromCamelCase (enumName);
@@ -2062,12 +2089,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
2062
2089
}
2063
2090
}
2064
2091
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;
2092
+ String _getCustomTypeIdFromDeclaration (
2093
+ Root root, TypeDeclaration type, String module) {
2094
+ return _getCustomTypeId (
2095
+ module,
2096
+ getEnumeratedTypes (root, excludeSealedClasses: true ).firstWhere (
2097
+ (EnumeratedType t) =>
2098
+ (type.isClass && t.associatedClass == type.associatedClass) ||
2099
+ (type.isEnum && t.associatedEnum == type.associatedEnum)));
2071
2100
}
2072
2101
2073
2102
// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2107,15 @@ String _makeFlValue(
2078
2107
{String ? lengthVariableName}) {
2079
2108
final String value;
2080
2109
if (type.isClass) {
2081
- final int enumeration = _getTypeEnumeration (root, type);
2082
- value = 'fl_value_new_custom_object($enumeration , G_OBJECT($variableName ))' ;
2110
+ final String customTypeId =
2111
+ _getCustomTypeIdFromDeclaration (root, type, module);
2112
+ value =
2113
+ 'fl_value_new_custom_object($customTypeId , G_OBJECT($variableName ))' ;
2083
2114
} else if (type.isEnum) {
2084
- final int enumeration = _getTypeEnumeration (root, type);
2115
+ final String customTypeId =
2116
+ _getCustomTypeIdFromDeclaration (root, type, module);
2085
2117
value =
2086
- 'fl_value_new_custom($enumeration , fl_value_new_int(${type .isNullable ? '*$variableName ' : variableName }), (GDestroyNotify)fl_value_unref)' ;
2118
+ 'fl_value_new_custom($customTypeId , fl_value_new_int(${type .isNullable ? '*$variableName ' : variableName }), (GDestroyNotify)fl_value_unref)' ;
2087
2119
} else if (_isFlValueWrappedType (type)) {
2088
2120
value = 'fl_value_ref($variableName )' ;
2089
2121
} else if (type.baseName == 'void' ) {
0 commit comments