@@ -338,6 +338,28 @@ 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
+ 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
+ }
341
363
}
342
364
343
365
@override
@@ -1019,18 +1041,26 @@ class GObjectSourceGenerator
1019
1041
_writeDefineType (indent, module, _codecBaseName,
1020
1042
parentType: 'fl_standard_message_codec_get_type()' );
1021
1043
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
+
1022
1050
for (final EnumeratedType customType in customTypes) {
1023
1051
final String customTypeName = _getClassName (module, customType.name);
1024
1052
final String snakeCustomTypeName =
1025
1053
_snakeCaseFromCamelCase (customTypeName);
1054
+ final String customTypeId = _getCustomTypeId (module, customType);
1055
+
1026
1056
indent.newln ();
1027
1057
final String valueType = customType.type == CustomTypes .customClass
1028
1058
? '$customTypeName *'
1029
1059
: 'FlValue*' ;
1030
1060
indent.writeScoped (
1031
1061
'static gboolean ${codecMethodPrefix }_write_$snakeCustomTypeName ($_standardCodecName * codec, GByteArray* buffer, $valueType value, GError** error) {' ,
1032
1062
'}' , () {
1033
- indent.writeln ('uint8_t type = ${ customType . enumeration } ;' );
1063
+ indent.writeln ('uint8_t type = $customTypeId ;' );
1034
1064
indent.writeln ('g_byte_array_append(buffer, &type, sizeof(uint8_t));' );
1035
1065
if (customType.type == CustomTypes .customClass) {
1036
1066
indent.writeln (
@@ -1053,7 +1083,8 @@ class GObjectSourceGenerator
1053
1083
indent.writeScoped ('switch (fl_value_get_custom_type(value)) {' , '}' ,
1054
1084
() {
1055
1085
for (final EnumeratedType customType in customTypes) {
1056
- indent.writeln ('case ${customType .enumeration }:' );
1086
+ final String customTypeId = _getCustomTypeId (module, customType);
1087
+ indent.writeln ('case $customTypeId :' );
1057
1088
indent.nest (1 , () {
1058
1089
final String customTypeName =
1059
1090
_getClassName (module, customType.name);
@@ -1082,6 +1113,7 @@ class GObjectSourceGenerator
1082
1113
final String customTypeName = _getClassName (module, customType.name);
1083
1114
final String snakeCustomTypeName =
1084
1115
_snakeCaseFromCamelCase (customTypeName);
1116
+ final String customTypeId = _getCustomTypeId (module, customType);
1085
1117
indent.newln ();
1086
1118
indent.writeScoped (
1087
1119
'static FlValue* ${codecMethodPrefix }_read_$snakeCustomTypeName ($_standardCodecName * codec, GBytes* buffer, size_t* offset, GError** error) {' ,
@@ -1102,10 +1134,10 @@ class GObjectSourceGenerator
1102
1134
});
1103
1135
indent.newln ();
1104
1136
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));' );
1106
1138
} else if (customType.type == CustomTypes .customEnum) {
1107
1139
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);' );
1109
1141
}
1110
1142
});
1111
1143
}
@@ -1117,9 +1149,10 @@ class GObjectSourceGenerator
1117
1149
indent.writeScoped ('switch (type) {' , '}' , () {
1118
1150
for (final EnumeratedType customType in customTypes) {
1119
1151
final String customTypeName = _getClassName (module, customType.name);
1152
+ final String customTypeId = _getCustomTypeId (module, customType);
1120
1153
final String snakeCustomTypeName =
1121
1154
_snakeCaseFromCamelCase (customTypeName);
1122
- indent.writeln ('case ${ customType . enumeration } :' );
1155
+ indent.writeln ('case $customTypeId :' );
1123
1156
indent.nest (1 , () {
1124
1157
indent.writeln (
1125
1158
'return ${codecMethodPrefix }_read_$snakeCustomTypeName (codec, buffer, offset, error);' );
@@ -1922,6 +1955,16 @@ String _getMethodPrefix(String module, String name) {
1922
1955
return _snakeCaseFromCamelCase (className);
1923
1956
}
1924
1957
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
+
1925
1968
// Returns an enumeration value in C++ form.
1926
1969
String _getEnumValue (String module, String enumName, String memberName) {
1927
1970
final String snakeEnumName = _snakeCaseFromCamelCase (enumName);
@@ -2062,12 +2105,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
2062
2105
}
2063
2106
}
2064
2107
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)));
2071
2116
}
2072
2117
2073
2118
// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2123,15 @@ String _makeFlValue(
2078
2123
{String ? lengthVariableName}) {
2079
2124
final String value;
2080
2125
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 ))' ;
2083
2130
} else if (type.isEnum) {
2084
- final int enumeration = _getTypeEnumeration (root, type);
2131
+ final String customTypeId =
2132
+ _getCustomTypeIdFromDeclaration (root, type, module);
2085
2133
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)' ;
2087
2135
} else if (_isFlValueWrappedType (type)) {
2088
2136
value = 'fl_value_ref($variableName )' ;
2089
2137
} else if (type.baseName == 'void' ) {
0 commit comments