/// <summary> /// Generate the native code for the given value object. /// </summary> /// <param name="obj">The given object.</param> /// <param name="stream">The file stream for generating the codes.</param> /// <param name="indent">The indent string when generating the line of codes.</param> /// <param name="typename">The native type of the variable.</param> /// <param name="var">The variable for the given object when generating the codes.</param> /// <param name="caller">The caller for the method or the agent.</param> /// <returns>Returns the string generated value.</returns> public static string GenerateCode(object obj, StreamWriter stream, string indent, string typename, string var, string caller) { string retStr = string.Empty; if (obj != null) { Type type = obj.GetType(); if (obj is Behaviac.Design.MethodDef) { Behaviac.Design.MethodDef method = obj as Behaviac.Design.MethodDef; retStr = MethodCppExporter.GenerateCode(method, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.MethodDef.Param) { Behaviac.Design.MethodDef.Param param = obj as Behaviac.Design.MethodDef.Param; retStr = ParameterCppExporter.GenerateCode(param, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.ParInfo) { Behaviac.Design.ParInfo par = obj as Behaviac.Design.ParInfo; retStr = ParInfoCppExporter.GenerateCode(par, false, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.PropertyDef) { Behaviac.Design.PropertyDef property = obj as Behaviac.Design.PropertyDef; retStr = PropertyCppExporter.GenerateCode(property, null, false, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.VariableDef) { Behaviac.Design.VariableDef variable = obj as Behaviac.Design.VariableDef; retStr = VariableCppExporter.GenerateCode(variable, false, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.RightValueDef) { Behaviac.Design.RightValueDef rightValue = obj as Behaviac.Design.RightValueDef; retStr = RightValueCppExporter.GenerateCode(rightValue, stream, indent, typename, var, caller); } // Array type else if (Plugin.IsArrayType(type)) { retStr = var; if (!string.IsNullOrEmpty(typename)) { stream.WriteLine("{0}{1} {2};", indent, DataCppExporter.GetBasicGeneratedNativeType(typename), var); } else { typename = DataCppExporter.GetGeneratedNativeType(type); } int startIndex = typename.IndexOf('<'); int endIndex = typename.LastIndexOf('>'); string itemType = typename.Substring(startIndex + 1, endIndex - startIndex - 1); ArrayCppExporter.GenerateCode(obj, stream, indent, itemType, var); } // Struct type else if (Plugin.IsCustomClassType(type)) { retStr = var; if (!string.IsNullOrEmpty(typename)) { if (typename.EndsWith("*")) { stream.WriteLine("{0}{1} {2} = NULL;", indent, DataCppExporter.GetBasicGeneratedNativeType(typename), var); } else { stream.WriteLine("{0}{1} {2};", indent, DataCppExporter.GetBasicGeneratedNativeType(typename), var); } } StructCppExporter.GenerateCode(obj, stream, indent, var, null, ""); } // Other types else { retStr = obj.ToString(); if (Plugin.IsStringType(type)) // string { retStr = string.Format("\"{0}\"", retStr); if (typename.StartsWith("behaviac::wstring")) { retStr = string.Format("StringUtils::MBSToWCS({0})", retStr); } else { retStr = string.Format("(char*)({0})", retStr); } } else if (Plugin.IsBooleanType(type)) // bool { retStr = retStr.ToLowerInvariant(); } else if (Plugin.IsEnumType(type)) // enum { retStr = EnumCppExporter.GeneratedCode(obj); } else if (Plugin.IsFloatType(type)) // float { if (retStr.Contains(".") && !retStr.EndsWith("f") && !retStr.EndsWith("F")) { retStr = retStr + "f"; } } if (!string.IsNullOrEmpty(var)) { if (string.IsNullOrEmpty(typename)) { stream.WriteLine("{0}{1} = {2};", indent, var, retStr); } else { typename = DataCppExporter.GetGeneratedNativeType(typename); stream.WriteLine("{0}{1} {2} = {3};", indent, typename, var, retStr); } } } } return(retStr); }