@@ -338,6 +338,15 @@ class GObjectHeaderGenerator
338338 indent.newln ();
339339 _writeDeclareFinalType (indent, module, _codecBaseName,
340340 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+ }
341350 }
342351
343352 @override
@@ -1023,14 +1032,19 @@ class GObjectSourceGenerator
10231032 final String customTypeName = _getClassName (module, customType.name);
10241033 final String snakeCustomTypeName =
10251034 _snakeCaseFromCamelCase (customTypeName);
1035+ final String customTypeId = _getCustomTypeId (module, customType);
1036+
1037+ indent.newln ();
1038+ indent.writeln ('const int $customTypeId = ${customType .enumeration };' );
1039+
10261040 indent.newln ();
10271041 final String valueType = customType.type == CustomTypes .customClass
10281042 ? '$customTypeName *'
10291043 : 'FlValue*' ;
10301044 indent.writeScoped (
10311045 'static gboolean ${codecMethodPrefix }_write_$snakeCustomTypeName ($_standardCodecName * codec, GByteArray* buffer, $valueType value, GError** error) {' ,
10321046 '}' , () {
1033- indent.writeln ('uint8_t type = ${ customType . enumeration } ;' );
1047+ indent.writeln ('uint8_t type = $customTypeId ;' );
10341048 indent.writeln ('g_byte_array_append(buffer, &type, sizeof(uint8_t));' );
10351049 if (customType.type == CustomTypes .customClass) {
10361050 indent.writeln (
@@ -1053,7 +1067,8 @@ class GObjectSourceGenerator
10531067 indent.writeScoped ('switch (fl_value_get_custom_type(value)) {' , '}' ,
10541068 () {
10551069 for (final EnumeratedType customType in customTypes) {
1056- indent.writeln ('case ${customType .enumeration }:' );
1070+ final String customTypeId = _getCustomTypeId (module, customType);
1071+ indent.writeln ('case $customTypeId :' );
10571072 indent.nest (1 , () {
10581073 final String customTypeName =
10591074 _getClassName (module, customType.name);
@@ -1082,6 +1097,7 @@ class GObjectSourceGenerator
10821097 final String customTypeName = _getClassName (module, customType.name);
10831098 final String snakeCustomTypeName =
10841099 _snakeCaseFromCamelCase (customTypeName);
1100+ final String customTypeId = _getCustomTypeId (module, customType);
10851101 indent.newln ();
10861102 indent.writeScoped (
10871103 'static FlValue* ${codecMethodPrefix }_read_$snakeCustomTypeName ($_standardCodecName * codec, GBytes* buffer, size_t* offset, GError** error) {' ,
@@ -1102,10 +1118,10 @@ class GObjectSourceGenerator
11021118 });
11031119 indent.newln ();
11041120 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));' );
11061122 } else if (customType.type == CustomTypes .customEnum) {
11071123 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);' );
11091125 }
11101126 });
11111127 }
@@ -1117,9 +1133,10 @@ class GObjectSourceGenerator
11171133 indent.writeScoped ('switch (type) {' , '}' , () {
11181134 for (final EnumeratedType customType in customTypes) {
11191135 final String customTypeName = _getClassName (module, customType.name);
1136+ final String customTypeId = _getCustomTypeId (module, customType);
11201137 final String snakeCustomTypeName =
11211138 _snakeCaseFromCamelCase (customTypeName);
1122- indent.writeln ('case ${ customType . enumeration } :' );
1139+ indent.writeln ('case $customTypeId :' );
11231140 indent.nest (1 , () {
11241141 indent.writeln (
11251142 'return ${codecMethodPrefix }_read_$snakeCustomTypeName (codec, buffer, offset, error);' );
@@ -1922,6 +1939,16 @@ String _getMethodPrefix(String module, String name) {
19221939 return _snakeCaseFromCamelCase (className);
19231940}
19241941
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+
19251952// Returns an enumeration value in C++ form.
19261953String _getEnumValue (String module, String enumName, String memberName) {
19271954 final String snakeEnumName = _snakeCaseFromCamelCase (enumName);
@@ -2062,12 +2089,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
20622089 }
20632090}
20642091
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)));
20712100}
20722101
20732102// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2107,15 @@ String _makeFlValue(
20782107 {String ? lengthVariableName}) {
20792108 final String value;
20802109 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 ))' ;
20832114 } else if (type.isEnum) {
2084- final int enumeration = _getTypeEnumeration (root, type);
2115+ final String customTypeId =
2116+ _getCustomTypeIdFromDeclaration (root, type, module);
20852117 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)' ;
20872119 } else if (_isFlValueWrappedType (type)) {
20882120 value = 'fl_value_ref($variableName )' ;
20892121 } else if (type.baseName == 'void' ) {
0 commit comments