/// <summary> /// Generates the name of the DotNet method name. /// </summary> /// <param name="command">The command.</param> /// <returns>A method name.</returns> public static String GenerateDotNetMethodsName(command command) { List<String> parts = new List<String>(); parts.Add(GenerateDotNetName(String.Empty, command.name)); if (command.parameter != null) { foreach (parameter parameter in command.parameter) { parts.Add(GenerateDotNetName(String.Empty, parameter.name)); } } return String.Join(String.Empty, parts.ToArray()); }
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; }
/// <summary> /// Generates a valid Objective-C selector. /// </summary> /// <param name="command">The command.</param> /// <param name="isApplication">if set to <c>true</c>, then the command is for an application class.</param> /// <returns>A valid Objective-C selector.</returns> public static String GenerateObjCSelector(command command, bool isApplication) { List<String> parts = new List<String>(); parts.Add(GenerateObjCName(command.name)); bool first = true; if (isApplication && command.directparameter != null && command.directparameter.type != "specifier") { parts.Add(":"); first = false; } if (command.parameter != null) { foreach (parameter parameter in command.parameter) { if (first) { first = false; parts.Add(GenerateDotNetName(String.Empty, parameter.name) + ":"); } else { parts.Add(GenerateObjCName(parameter.name) + ":"); } } } return String.Join(String.Empty, parts.ToArray()); }