generateObjectManager method
void
generateObjectManager()
Implementation
void generateObjectManager() {
output.writeLn('class \$ObjectManager {');
for (var type in typeMap.allTypes.values) {
if (type.canBeSingleton()) {
String getterName = type.varName;
output.writeLn('${type.creatorName}? _$getterName;');
output.writeLn('${type.creatorName} get $getterName {');
output.writeLn('if(_$getterName == null){');
output.writeLn('_$getterName = ${type.generateCreator()};');
output.writeLn('}');
output.writeLn('return _$getterName as ${type.creatorName};');
output.writeLn('}');
}
}
for (var typeInfo in typeMap.subtypeInstanes.values) {
output.writeLn(
'Map<String, ${typeInfo.uniqueName}>? _instancesOf${typeInfo.flatName};',
);
output.writeLn(
'Map<String, ${typeInfo.uniqueName}> get instancesOf${typeInfo.flatName} {',
);
output.writeLn(
'if (_instancesOf${typeInfo.flatName} != null) return _instancesOf${typeInfo.flatName}!;',
);
output.writeLn('return _instancesOf${typeInfo.flatName} = {');
typeMap.getNonAbstractSubtypes(typeInfo).forEach((subtypeInfo) {
if (subtypeInfo.allRequiredFields().isEmpty) {
output.writeLn('${subtypeInfo.classCodeAsReference}: ');
output.writeLn(subtypeInfo.varName);
//output.writeMany(subtypeInfo.generateCreator());
output.writeLn(',');
} else {
output.writeLn('//${subtypeInfo.fullName} requires a param');
}
});
output.writeLn('};');
output.writeLn('}');
}
typeMap.subtypeFactories.forEach((key, subtypeFactoryInfo) {
output.writeLn('${subtypeFactoryInfo.returnType.uniqueName} $key {');
//output.writeLn('switch(className){');
for (var type in typeMap.getNonAbstractSubtypes(
subtypeFactoryInfo.returnType,
)) {
//TODO compare parameters by types and names?
//type.allRequiredFields().map((f) => f.name).join(',');
if (type.allRequiredFields().length ==
subtypeFactoryInfo.arguments.length - 1) {
output.writeLn('if (className == ${type.classCodeAsReference})');
output.writeLn('return ${type.generateCreator()};');
//output.writeLn('case ${type.classCodeAsReference}:');
//output.writeLn('return ' + type.generateCreator() + ';');
}
}
//output.writeLn('}');
output.writeLn('throw new UnknownTypeException(className);');
output.writeLn('}');
});
for (var typeInfo in typeMap.subtypesOf.values) {
output.writeLn(
'SubtypesOf<${typeInfo.uniqueName}> subtypesOf${typeInfo.flatName} = new \$SubtypesOf${typeInfo.flatName}();',
);
}
output.writeLn('final List<String> s = const [');
output.writeLn(typeMap.allSymbols.map((e) => '"$e"').join(','));
output.writeLn('];');
output.writeLn('}');
output.writeLn('\$ObjectManager \$om = new \$ObjectManager();');
}