@@ -338,6 +338,28 @@ 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+ if (customTypes.isNotEmpty) {
346+ indent.newln ();
347+ addDocumentationComments (
348+ indent,
349+ < String > [
350+ 'Custom type ID constants:' ,
351+ '' ,
352+ 'Constants used to identify custom types in the codec.' ,
353+ 'They are used in the codec to encode and decode custom types.' ,
354+ 'They may be used in custom object creation functions to identify the type.' ,
355+ ],
356+ _docCommentSpec);
357+ }
358+
359+ for (final EnumeratedType customType in customTypes) {
360+ final String customTypeId = _getCustomTypeId (module, customType);
361+ indent.writeln ('extern const int $customTypeId ;' );
362+ }
341363 }
342364
343365 @override
@@ -1019,18 +1041,26 @@ class GObjectSourceGenerator
10191041 _writeDefineType (indent, module, _codecBaseName,
10201042 parentType: 'fl_standard_message_codec_get_type()' );
10211043
1044+ indent.newln ();
1045+ for (final EnumeratedType customType in customTypes) {
1046+ final String customTypeId = _getCustomTypeId (module, customType);
1047+ indent.writeln ('const int $customTypeId = ${customType .enumeration };' );
1048+ }
1049+
10221050 for (final EnumeratedType customType in customTypes) {
10231051 final String customTypeName = _getClassName (module, customType.name);
10241052 final String snakeCustomTypeName =
10251053 _snakeCaseFromCamelCase (customTypeName);
1054+ final String customTypeId = _getCustomTypeId (module, customType);
1055+
10261056 indent.newln ();
10271057 final String valueType = customType.type == CustomTypes .customClass
10281058 ? '$customTypeName *'
10291059 : 'FlValue*' ;
10301060 indent.writeScoped (
10311061 'static gboolean ${codecMethodPrefix }_write_$snakeCustomTypeName ($_standardCodecName * codec, GByteArray* buffer, $valueType value, GError** error) {' ,
10321062 '}' , () {
1033- indent.writeln ('uint8_t type = ${ customType . enumeration } ;' );
1063+ indent.writeln ('uint8_t type = $customTypeId ;' );
10341064 indent.writeln ('g_byte_array_append(buffer, &type, sizeof(uint8_t));' );
10351065 if (customType.type == CustomTypes .customClass) {
10361066 indent.writeln (
@@ -1053,7 +1083,8 @@ class GObjectSourceGenerator
10531083 indent.writeScoped ('switch (fl_value_get_custom_type(value)) {' , '}' ,
10541084 () {
10551085 for (final EnumeratedType customType in customTypes) {
1056- indent.writeln ('case ${customType .enumeration }:' );
1086+ final String customTypeId = _getCustomTypeId (module, customType);
1087+ indent.writeln ('case $customTypeId :' );
10571088 indent.nest (1 , () {
10581089 final String customTypeName =
10591090 _getClassName (module, customType.name);
@@ -1082,6 +1113,7 @@ class GObjectSourceGenerator
10821113 final String customTypeName = _getClassName (module, customType.name);
10831114 final String snakeCustomTypeName =
10841115 _snakeCaseFromCamelCase (customTypeName);
1116+ final String customTypeId = _getCustomTypeId (module, customType);
10851117 indent.newln ();
10861118 indent.writeScoped (
10871119 'static FlValue* ${codecMethodPrefix }_read_$snakeCustomTypeName ($_standardCodecName * codec, GBytes* buffer, size_t* offset, GError** error) {' ,
@@ -1102,10 +1134,10 @@ class GObjectSourceGenerator
11021134 });
11031135 indent.newln ();
11041136 indent.writeln (
1105- 'return fl_value_new_custom_object(${ customType . enumeration } , G_OBJECT(value));' );
1137+ 'return fl_value_new_custom_object($customTypeId , G_OBJECT(value));' );
11061138 } else if (customType.type == CustomTypes .customEnum) {
11071139 indent.writeln (
1108- 'return fl_value_new_custom(${ customType . enumeration } , fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);' );
1140+ 'return fl_value_new_custom($customTypeId , fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);' );
11091141 }
11101142 });
11111143 }
@@ -1117,9 +1149,10 @@ class GObjectSourceGenerator
11171149 indent.writeScoped ('switch (type) {' , '}' , () {
11181150 for (final EnumeratedType customType in customTypes) {
11191151 final String customTypeName = _getClassName (module, customType.name);
1152+ final String customTypeId = _getCustomTypeId (module, customType);
11201153 final String snakeCustomTypeName =
11211154 _snakeCaseFromCamelCase (customTypeName);
1122- indent.writeln ('case ${ customType . enumeration } :' );
1155+ indent.writeln ('case $customTypeId :' );
11231156 indent.nest (1 , () {
11241157 indent.writeln (
11251158 'return ${codecMethodPrefix }_read_$snakeCustomTypeName (codec, buffer, offset, error);' );
@@ -1922,6 +1955,16 @@ String _getMethodPrefix(String module, String name) {
19221955 return _snakeCaseFromCamelCase (className);
19231956}
19241957
1958+ // Returns the code for the custom type id definition for [customType].
1959+ String _getCustomTypeId (String module, EnumeratedType customType) {
1960+ final String customTypeName = _getClassName (module, customType.name);
1961+
1962+ final String snakeCustomTypeName = _snakeCaseFromCamelCase (customTypeName);
1963+
1964+ final String customTypeId = '${snakeCustomTypeName }_type_id' ;
1965+ return customTypeId;
1966+ }
1967+
19251968// Returns an enumeration value in C++ form.
19261969String _getEnumValue (String module, String enumName, String memberName) {
19271970 final String snakeEnumName = _snakeCaseFromCamelCase (enumName);
@@ -2062,12 +2105,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
20622105 }
20632106}
20642107
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;
2108+ String _getCustomTypeIdFromDeclaration (
2109+ Root root, TypeDeclaration type, String module) {
2110+ return _getCustomTypeId (
2111+ module,
2112+ getEnumeratedTypes (root, excludeSealedClasses: true ).firstWhere (
2113+ (EnumeratedType t) =>
2114+ (type.isClass && t.associatedClass == type.associatedClass) ||
2115+ (type.isEnum && t.associatedEnum == type.associatedEnum)));
20712116}
20722117
20732118// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2123,15 @@ String _makeFlValue(
20782123 {String ? lengthVariableName}) {
20792124 final String value;
20802125 if (type.isClass) {
2081- final int enumeration = _getTypeEnumeration (root, type);
2082- value = 'fl_value_new_custom_object($enumeration , G_OBJECT($variableName ))' ;
2126+ final String customTypeId =
2127+ _getCustomTypeIdFromDeclaration (root, type, module);
2128+ value =
2129+ 'fl_value_new_custom_object($customTypeId , G_OBJECT($variableName ))' ;
20832130 } else if (type.isEnum) {
2084- final int enumeration = _getTypeEnumeration (root, type);
2131+ final String customTypeId =
2132+ _getCustomTypeIdFromDeclaration (root, type, module);
20852133 value =
2086- 'fl_value_new_custom($enumeration , fl_value_new_int(${type .isNullable ? '*$variableName ' : variableName }), (GDestroyNotify)fl_value_unref)' ;
2134+ 'fl_value_new_custom($customTypeId , fl_value_new_int(${type .isNullable ? '*$variableName ' : variableName }), (GDestroyNotify)fl_value_unref)' ;
20872135 } else if (_isFlValueWrappedType (type)) {
20882136 value = 'fl_value_ref($variableName )' ;
20892137 } else if (type.baseName == 'void' ) {
0 commit comments