writeDataClass method
void
writeDataClass(
- GObjectOptions generatorOptions,
- Root root,
- Indent indent,
- Class classDefinition, {
- required String dartPackageName,
override
Writes a single data class to indent
.
Implementation
@override
void writeDataClass(
GObjectOptions generatorOptions,
Root root,
Indent indent,
Class classDefinition, {
required String dartPackageName,
}) {
final String module = _getModule(generatorOptions, dartPackageName);
final String snakeModule = _snakeCaseFromCamelCase(module);
final String className = _getClassName(module, classDefinition.name);
final String snakeClassName = _snakeCaseFromCamelCase(classDefinition.name);
final String methodPrefix = _getMethodPrefix(module, classDefinition.name);
final String testMacro = '${snakeModule}_IS_$snakeClassName'.toUpperCase();
indent.newln();
_writeObjectStruct(indent, module, classDefinition.name, () {
for (final NamedType field in classDefinition.fields) {
final String fieldName = _getFieldName(field.name);
final String fieldType = _getType(module, field.type, isOutput: true);
indent.writeln('$fieldType $fieldName;');
if (_isNumericListType(field.type)) {
indent.writeln('size_t ${fieldName}_length;');
}
}
});
indent.newln();
_writeDefineType(indent, module, classDefinition.name);
indent.newln();
_writeDispose(indent, module, classDefinition.name, () {
bool haveSelf = false;
for (final NamedType field in classDefinition.fields) {
final String fieldName = _getFieldName(field.name);
final String? clear = _getClearFunction(field.type, 'self->$fieldName');
if (clear != null) {
if (!haveSelf) {
_writeCastSelf(indent, module, classDefinition.name, 'object');
haveSelf = true;
}
indent.writeln('$clear;');
}
}
});
indent.newln();
_writeInit(indent, module, classDefinition.name, () {});
indent.newln();
_writeClassInit(indent, module, classDefinition.name, () {});
final List<String> constructorArgs = <String>[];
for (final NamedType field in classDefinition.fields) {
final String fieldName = _getFieldName(field.name);
constructorArgs.add('${_getType(module, field.type)} $fieldName');
if (_isNumericListType(field.type)) {
constructorArgs.add('size_t ${fieldName}_length');
}
}
indent.newln();
indent.writeScoped(
"$className* ${methodPrefix}_new(${constructorArgs.join(', ')}) {", '}',
() {
_writeObjectNew(indent, module, classDefinition.name);
for (final NamedType field in classDefinition.fields) {
final String fieldName = _getFieldName(field.name);
final String value = _referenceValue(module, field.type, fieldName,
lengthVariableName: '${fieldName}_length');
if (_isNullablePrimitiveType(field.type)) {
final String primitiveType =
_getType(module, field.type, primitive: true);
indent.writeScoped('if ($value != nullptr) {', '}', () {
indent.writeln(
'self->$fieldName = static_cast<$primitiveType*>(malloc(sizeof($primitiveType)));');
indent.writeln('*self->$fieldName = *$value;');
});
indent.writeScoped('else {', '}', () {
indent.writeln('self->$fieldName = nullptr;');
});
} else if (field.type.isNullable) {
indent.writeScoped('if ($fieldName != nullptr) {', '}', () {
indent.writeln('self->$fieldName = $value;');
if (_isNumericListType(field.type)) {
indent
.writeln('self->${fieldName}_length = ${fieldName}_length;');
}
});
indent.writeScoped('else {', '}', () {
indent.writeln('self->$fieldName = nullptr;');
if (_isNumericListType(field.type)) {
indent.writeln('self->${fieldName}_length = 0;');
}
});
} else {
indent.writeln('self->$fieldName = $value;');
if (_isNumericListType(field.type)) {
indent.writeln('self->${fieldName}_length = ${fieldName}_length;');
}
}
}
indent.writeln('return self;');
});
for (final NamedType field in classDefinition.fields) {
final String fieldName = _getFieldName(field.name);
final String returnType = _getType(module, field.type);
indent.newln();
final List<String> getterArgs = <String>[
'$className* self',
if (_isNumericListType(field.type)) 'size_t* length'
];
indent.writeScoped(
'$returnType ${methodPrefix}_get_$fieldName(${getterArgs.join(', ')}) {',
'}', () {
indent.writeln(
'g_return_val_if_fail($testMacro(self), ${_getDefaultValue(module, field.type)});');
if (_isNumericListType(field.type)) {
indent.writeln('*length = self->${fieldName}_length;');
}
indent.writeln('return self->$fieldName;');
});
}
indent.newln();
indent.writeScoped(
'static FlValue* ${methodPrefix}_to_list($className* self) {', '}', () {
indent.writeln('FlValue* values = fl_value_new_list();');
for (final NamedType field in classDefinition.fields) {
final String fieldName = _getFieldName(field.name);
indent.writeln(
'fl_value_append_take(values, ${_makeFlValue(root, module, field.type, 'self->$fieldName', lengthVariableName: 'self->${fieldName}_length')});');
}
indent.writeln('return values;');
});
indent.newln();
indent.writeScoped(
'static $className* ${methodPrefix}_new_from_list(FlValue* values) {',
'}', () {
final List<String> args = <String>[];
for (int i = 0; i < classDefinition.fields.length; i++) {
final NamedType field = classDefinition.fields[i];
final String fieldName = _getFieldName(field.name);
final String fieldType = _getType(module, field.type);
final String fieldValue = _fromFlValue(module, field.type, 'value$i');
indent
.writeln('FlValue* value$i = fl_value_get_list_value(values, $i);');
args.add(fieldName);
if (_isNullablePrimitiveType(field.type)) {
indent.writeln('$fieldType $fieldName = nullptr;');
indent.writeln(
'${_getType(module, field.type, isOutput: true, primitive: true)} ${fieldName}_value;');
indent.writeScoped(
'if (fl_value_get_type(value$i) != FL_VALUE_TYPE_NULL) {', '}',
() {
indent.writeln('${fieldName}_value = $fieldValue;');
indent.writeln('$fieldName = &${fieldName}_value;');
});
} else if (field.type.isNullable) {
indent.writeln('$fieldType $fieldName = nullptr;');
if (_isNumericListType(field.type)) {
indent.writeln('size_t ${fieldName}_length = 0;');
args.add('${fieldName}_length');
}
indent.writeScoped(
'if (fl_value_get_type(value$i) != FL_VALUE_TYPE_NULL) {', '}',
() {
indent.writeln('$fieldName = $fieldValue;');
if (_isNumericListType(field.type)) {
indent.writeln(
'${fieldName}_length = fl_value_get_length(value$i);');
}
});
} else {
indent.writeln('$fieldType $fieldName = $fieldValue;');
if (_isNumericListType(field.type)) {
indent.writeln(
'size_t ${fieldName}_length = fl_value_get_length(value$i);');
args.add('${fieldName}_length');
}
}
}
indent.writeln('return ${methodPrefix}_new(${args.join(', ')});');
});
}