private static CodeMemberProperty GenerateElement(GenerationContext context, @class cls, element element) { String value; @class typeCls = context.Classes.FirstOrDefault(c => String.Equals(c.name, element.type)); if (typeCls != null) { // Make sure that we use the correct plural value = typeCls.plural ?? typeCls.name + "s"; } else { // Use the default name value = element.type; } String elementName = NamingHelper.GenerateDotNetName(String.Empty, value); String selector = NamingHelper.GenerateObjCName(value); // Define various references CodeTypeReference typeReference = new CodeTypeReference("SBElementArray"); CodeThisReferenceExpression thisReferenceExpression = new CodeThisReferenceExpression(); CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression("ObjectiveCRuntime"); // Define the property CodeMemberProperty memberProperty = new CodeMemberProperty(); memberProperty.Attributes = MemberAttributes.Public; memberProperty.Name = elementName; memberProperty.Type = typeReference; // Generate the getter CodeMethodReferenceExpression methodReferenceExpression = new CodeMethodReferenceExpression(typeReferenceExpression, "SendMessage"); methodReferenceExpression.TypeArguments.Add(typeReference); CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(methodReferenceExpression, thisReferenceExpression, new CodePrimitiveExpression(selector)); CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(invokeExpression); memberProperty.GetStatements.Add(returnStatement); return(memberProperty); }
/// <summary> /// Converts the type. /// </summary> public String ConvertType(String type, bool anyType) { String result = type; // Search for a known class or enumeration @class typeCls = (from cls in this.Classes where (cls.id != null && String.Equals(cls.id, type)) || (cls.id == null && String.Equals(cls.name, type)) select cls).FirstOrDefault(); enumeration typeEnumeration = (from enm in this.Enumerations where (enm.id != null && String.Equals(enm.id, type)) || (enm.id == null && String.Equals(enm.name, type)) select enm).FirstOrDefault(); if (typeCls != null) { result = NamingHelper.GenerateDotNetName(this.Prefix, typeCls.name); return(result); } else if (typeEnumeration != null) { result = NamingHelper.GenerateDotNetName(this.Prefix, typeEnumeration.name); return(result); } if (anyType) { // Search for a common type switch (type) { case "void": result = "void"; break; case "boolean": result = "Boolean"; break; case "integer": case "unsigned integer": result = "NSInteger"; break; case "double integer": result = "Int64"; break; case "real": result = "double"; break; case "property": result = "IntPtr"; break; case "list": result = "NSArray"; break; case "color": case "RGB color": result = "NSColor"; break; case "data": case "tdta": case "picture": result = "NSData"; break; case "date": result = "NSDate"; break; case "record": result = "NSDictionary"; break; case "type": result = "NSNumber"; break; case "point": result = "NSPoint"; break; case "text": case "version": result = "NSString"; break; case "rectangle": result = "NSRect"; break; case "alias": case "file": result = "NSURL"; break; case "specifier": case "location specifier": result = "SBObject"; break; default: result = "Id"; break; } } return(result); }
private static CodeMemberMethod GenerateCommand(GenerationContext context, @class cls, command command) { bool isApplication = GenerationContext.IsApplicationClass(cls); String returnType = context.ConvertType(GenerationContext.GetType(command.result) ?? "void", true); bool hasReturnType = returnType != "void"; bool useDirectParameter = isApplication && command.directparameter != null && command.directparameter.type != "specifier"; String methodName = NamingHelper.GenerateDotNetMethodsName(command); // Define various references CodeTypeReference typeReference = new CodeTypeReference(returnType); CodeThisReferenceExpression thisReferenceExpression = new CodeThisReferenceExpression(); CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression("ObjectiveCRuntime"); CodeMethodReferenceExpression methodReferenceExpression = new CodeMethodReferenceExpression(typeReferenceExpression, "SendMessage"); if (hasReturnType) { methodReferenceExpression.TypeArguments.Add(typeReference); } // Define the method CodeMemberMethod memberMethod = new CodeMemberMethod(); memberMethod.Attributes = MemberAttributes.Public; memberMethod.Name = methodName; if (hasReturnType) { memberMethod.ReturnType = typeReference; } // Gather all the expressions needed for the invocation. List <CodeExpression> expressions = new List <CodeExpression>(); expressions.Add(thisReferenceExpression); expressions.Add(new CodePrimitiveExpression(NamingHelper.GenerateObjCSelector(command, isApplication))); // If the command is for the application and the direct parameter is not an object specifier, add it to the signature if (useDirectParameter) { CodeParameterDeclarationExpression parameterDeclarationExpression = new CodeParameterDeclarationExpression(); parameterDeclarationExpression.Type = new CodeTypeReference(context.ConvertType(GenerationContext.GetType(command.directparameter), true)); parameterDeclarationExpression.Name = "x"; memberMethod.Parameters.Add(parameterDeclarationExpression); expressions.Add(new CodeVariableReferenceExpression(parameterDeclarationExpression.Name)); } // Add all the parameters if (command.parameter != null) { foreach (parameter parameter in command.parameter) { CodeParameterDeclarationExpression parameterDeclarationExpression = new CodeParameterDeclarationExpression(); parameterDeclarationExpression.Type = new CodeTypeReference(context.ConvertType(GenerationContext.GetType(parameter), true)); parameterDeclarationExpression.Name = GenerationContext.ConvertParameterName(NamingHelper.GenerateObjCName(parameter.name)); memberMethod.Parameters.Add(parameterDeclarationExpression); expressions.Add(new CodeVariableReferenceExpression(parameterDeclarationExpression.Name)); } } // Generate the runtime invocation CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(methodReferenceExpression, expressions.ToArray()); CodeStatement expressionStatement; if (hasReturnType) { expressionStatement = new CodeMethodReturnStatement(invokeExpression); } else { expressionStatement = new CodeExpressionStatement(invokeExpression); } memberMethod.Statements.Add(expressionStatement); return(memberMethod); }