public static void generateDefaultFactory(IndentWriter writer, IXMLElement klass) throws IOException { SharedWriter.generateFileLocation(writer, klass); if (klass == null) { throw new RuntimeException("missing class for factory"); } String longname = XMLUtil.nameOf(klass) + "Factory" + s_factoryCounter++; String klassName = XMLUtil.nameOf(klass); writer.write("DECLARE_DEFAULT_OBJECT_FACTORY(" + longname + ", " + klassName + ");\n"); // Generate all appropriate registration information SchemaWriter.addFactory("REGISTER_OBJECT_FACTORY(id," + longname + ", " + klassName + ");\n"); }
public static void generateFactory(IndentWriter writer, IXMLElement constructor) throws IOException { IXMLElement klass = constructor.getParent(); SharedWriter.generateFileLocation(writer, klass); if (klass == null) throw new RuntimeException("missing class for factory"); String longname = XMLUtil.nameOf(klass) + "Factory" + s_factoryCounter++; writer.write("class " + longname + ": public ObjectFactory {\n"); writer.write("public:\n"); writer.indent(); writer.write(longname + "(const LabelStr& name): ObjectFactory(name){}\n"); writer.unindent(); writer.write("private:\n"); writer.indent(); writer.write("ObjectId createInstance(const PlanDatabaseId& planDb,\n"); writer.write(" const LabelStr& objectType, \n"); writer.write(" const LabelStr& objectName,\n"); writer.write( " const std::vector<const AbstractDomain*>& arguments) const {\n"); writer.indent(); Vector constructorAssignments = constructor.getChildrenNamed("arg"); String klassName = XMLUtil.nameOf(klass); String constructorArguments = ""; String comma = ""; // Do some type checking - at least the size must match! writer.write("check_error(arguments.size() == " + constructorAssignments.size() + ");\n"); // Process arguments, defining local variables and giving them initial values for (int i = 0; i < constructorAssignments.size(); i++) { IXMLElement element = (IXMLElement) constructorAssignments.elementAt(i); String target = XMLUtil.getAttribute(element, "name"); String type = XMLUtil.getAttribute(element, "type"); writer.write("check_error(AbstractDomain::canBeCompared(*arguments[" + i + "], \n"); writer.write( " planDb->getConstraintEngine()->getCESchema()->baseDomain(\"" + type + "\")), \n"); writer.write( " \"Cannot convert \" + arguments[" + i + "]->getTypeName().toString() + \" to " + type + "\");\n"); writer.write("check_error(arguments[" + i + "]->isSingleton());\n"); String localVarType = makeArgumentType( type); // Some conversion may be required for Id's or strings or enumerations // Declare local variable for current argument writer.write( localVarType + " " + target + "((" + localVarType + ")arguments[" + i + "]->getSingletonValue());\n\n"); constructorArguments = constructorArguments + comma + target; comma = ", "; } // Now do the declaration and allocation of the instance writer.write( klassName + "Id instance = (new " + klassName + "(planDb, objectType, objectName))->getId();\n"); writer.write("instance->constructor(" + constructorArguments + ");\n"); writer.write("instance->handleDefaults();\n"); writer.write("return instance;\n"); writer.unindent(); writer.write("}\n"); writer.unindent(); writer.write("};\n"); // Generate all appropriate registration informationLabelStr Vector factoryNames = makeFactoryNames(constructor); for (int i = 0; i < factoryNames.size(); i++) { String factoryName = (String) factoryNames.elementAt(i); SchemaWriter.addFactory( "REGISTER_OBJECT_FACTORY(id," + longname + ", " + factoryName + ");\n"); } }