protected override void AddMethodsForField(DefField field) { _sb.AppendLine(GetCLRTypeName(field) + " get_" + field.Name + "();"); DefParam param = new DefParam(field, "value"); _sb.AppendLine("void set_" + field.Name + "(" + param.Type.GetCLRParamTypeName(param) + " value);"); }
protected override void InterpretChildElement(XmlElement child) { switch (child.Name) { case "parameters": int count = 1; foreach (XmlElement param in child.ChildNodes) { DefParam p = new DefParam(param); if (p.Name == null && (p.TypeName != "void" || p.PassedByType != PassedByType.Value)) { p.Name = "param" + count; } if (p.Name != null) { p.Function = this; this.Parameters.Add(p); } count++; } break; default: throw new Exception("Unknown child of function: '" + child.Name + "'"); } }
protected virtual void AddEventInvokers() { foreach (DefClass cls in _listeners) { foreach (DefFunction f in cls.PublicMethods) { if (f.IsDeclarableFunction) { _sb.AppendIndent("virtual " + GetCLRTypeName(f) + " On" + f.CLRName); AddMethodParameters(f); _sb.Append(" = " + GetNativeDirectorReceiverInterfaceName(cls) + "::" + f.CLRName + "\n"); _sb.AppendLine("{"); _sb.AppendIndent("\t"); if (f.TypeName != "void") { _sb.Append("return "); } _sb.Append(f.CLRName + "("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" );\n"); _sb.AppendLine("}\n"); } } _sb.AppendLine(""); } }
private void AddAttributesInMember(DefMember member, XmlElement elem) { foreach (XmlAttribute attr in elem.Attributes) { if (attr.Name != "name") { AddAttributeInHolder(member, CreateAttribute(attr)); } } foreach (XmlNode child in elem.ChildNodes) { if (!(child is XmlElement)) { continue; } if (child.Name[0] == '_') { AddAttributeInHolder(member, CreateAttribute(child as XmlElement)); continue; } switch (child.Name) { case "param": if (!(member is DefFunction)) { throw new Exception("Unexpected"); } string name = (child as XmlElement).GetAttribute("name"); DefParam param = null; foreach (DefParam p in (member as DefFunction).Parameters) { if (p.Name == name) { param = p; break; } } if (param == null) { throw new Exception("Wrong param name"); } foreach (XmlAttribute attr in child.Attributes) { if (attr.Name != "name") { AddAttributeInHolder(param, CreateAttribute(attr)); } } break; default: throw new Exception("Unexpected"); } } }
protected virtual string GetMethodPostNativeCall(DefFunction f, int paramCount) { string res = String.Empty; for (int i = 0; i < paramCount; i++) { DefParam p = f.Parameters[i]; res += p.Type.GetPostCallParamConversionCleanup(p); } return(res); }
protected virtual string GetMethodPreNativeCall(DefFunction f, int paramCount) { string res = String.Empty; for (int i = 0; i < paramCount; i++) { DefParam p = f.Parameters[i]; string newname; res += p.Type.GetPreCallParamConversion(p, out newname); } return(res); }
protected virtual void AddMethodParameters(DefFunction f, int count) { _sb.Append("("); for (int i = 0; i < count; i++) { DefParam p = f.Parameters[i]; _sb.Append(" " + GetCLRParamTypeName(p) + " " + p.Name); if (i < count - 1) { _sb.Append(","); } } _sb.Append(" )"); }
protected override void AddMethod(DefFunction f) { string def = f.Definition.Replace(f.Class.FullNativeName, GetClassName()) + "("; if (def.StartsWith("virtual ")) { def = def.Substring("virtual ".Length); } _sb.AppendIndent(def); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" "); AddNativeMethodParam(param); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" )\n"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine("if (doCallFor" + f.CLRName + ")"); _sb.AppendLine("{"); _sb.IncreaseIndent(); CppNativeProxyClassProducer.AddNativeProxyMethodBody(f, "_receiver", _sb); _sb.DecreaseIndent(); _sb.AppendLine("}"); if (!f.IsVoid) { _sb.AppendLine("else"); string ret = null; if (f.HasAttribute <DefaultReturnValueAttribute>()) { ret = f.GetAttribute <DefaultReturnValueAttribute>().Name; } else { throw new Exception("Default return value not set."); } _sb.AppendLine("\treturn " + ret + ";"); } _sb.DecreaseIndent(); _sb.AppendLine("}"); }
protected override void AddConstructor(DefFunction f) { string className; if (_t.IsNested) { className = _t.ParentClass.FullCLRName + "::" + _t.Name; } else { className = _wrapper.ManagedNamespace + "::" + _t.Name; } _sb.AppendIndent(ProxyName + "( " + className + "^ managedObj"); if (f != null) { foreach (DefParam param in f.Parameters) { _sb.Append(", " + param.NativeTypeName + " " + param.Name); } } _sb.Append(" ) :\n"); if (f != null) { _sb.AppendIndent("\t" + _t.FullNativeName + "("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" ),\n"); } _sb.AppendIndent("\t_managed(managedObj)"); //foreach (DefField field in _protectedFields) //{ // _sb.Append(",\n"); // _sb.AppendIndent("\tref_" + field.Name + "(" + field.Name + ")"); //} _sb.Append("\n"); _sb.AppendLine("{"); _sb.AppendLine("}"); }
protected override void AddMethod(DefFunction f) { _sb.AppendIndent(f.Definition.Replace(f.Class.FullNativeName + "::", "") + "("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" "); AddNativeMethodParam(param); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" ) override;\n"); }
public static void AddMethodHandlersClass(DefClass type, IndentStringBuilder sb) { if (!type.HasWrapType(WrapTypes.NativeDirector)) { throw new Exception("Unexpected"); } if (type.IsNested) { sb.AppendIndent("public: "); } else { sb.AppendIndent("public "); } sb.Append("ref class " + type.Name + " abstract sealed\n"); sb.AppendLine("{"); sb.AppendLine("public:"); sb.IncreaseIndent(); foreach (DefFunction f in type.PublicMethods) { if (f.IsDeclarableFunction && f.IsVirtual) { //if (f.Parameters.Count > 0) //{ // AddEventArgsClass(f, sb); //} sb.AppendIndent("delegate static " + f.CLRTypeName + " " + f.CLRName + "Handler("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; sb.Append(" " + param.Type.GetCLRParamTypeName(param) + " " + param.Name); if (i < f.Parameters.Count - 1) { sb.Append(","); } } sb.Append(" );\n"); } } sb.DecreaseIndent(); sb.AppendLine("};"); sb.AppendLine(); }
protected virtual void AddNativeMethodParams(DefFunction f) { for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" "); _sb.Append(param.NativeTypeName); _sb.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } }
protected string AddParameterConversion(DefParam param) { string newname, expr, postcall; expr = param.Type.GetPreCallParamConversion(param, out newname); postcall = param.Type.GetPostCallParamConversionCleanup(param); if (!String.IsNullOrEmpty(postcall)) { throw new Exception("Unexpected"); } if (!String.IsNullOrEmpty(expr)) { _sb.AppendLine(expr); } return(newname); }
protected override void AddMethodsForField(DefField field) { string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), field); _sb.AppendLine(GetCLRTypeName(field) + " " + GetClassName() + "::get_" + field.Name + "()"); _sb.AppendLine("{"); _sb.AppendLine("\treturn " + managedType + ";"); _sb.AppendLine("}"); DefParam param = new DefParam(field, "value"); _sb.AppendLine("void " + GetClassName() + "::set_" + field.Name + "(" + param.Type.GetCLRParamTypeName(param) + " value)"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine(GetNativeInvokationTarget(field) + " = " + AddParameterConversion(param) + ";"); _sb.DecreaseIndent(); _sb.AppendLine("}"); }
protected virtual string GetMethodNativeCall(DefFunction f, int paramCount) { string invoke; if (f.IsStatic) { if (f.ProtectionType == ProtectionType.Protected) { string classname = NativeProtectedStaticsProxy.GetProtectedStaticsProxyName(_t); invoke = classname + "::" + f.Name + "("; } else { invoke = _t.FullNativeName + "::" + f.Name + "("; } } else { invoke = GetNativeInvokationTarget(f) + "("; } for (int i = 0; i < paramCount; i++) { DefParam p = f.Parameters[i]; string newname; p.Type.GetPreCallParamConversion(p, out newname); invoke += " " + newname; if (i < paramCount - 1) { invoke += ","; } } invoke += " )"; if (f.IsVoid) { return(invoke); } else { return(f.Type.GetNativeCallConversion(invoke, f)); } }
protected virtual void AddCreatorOverload(DefFunction f, int count) { _sb.AppendIndent(_t.FullCLRName + " " + GetClassName() + "::Create"); if (f == null) { _sb.Append("()"); } else { AddMethodParameters(f, count); } _sb.Append("\n"); _sb.AppendLine("{"); _sb.IncreaseIndent(); string preCall = null, postCall = null; if (f != null) { preCall = GetMethodPreNativeCall(f, count); postCall = GetMethodPostNativeCall(f, count); if (!String.IsNullOrEmpty(preCall)) { _sb.AppendLine(preCall); } } _sb.AppendLine(_t.CLRName + " ptr;"); _sb.AppendIndent("ptr._native = new " + _t.FullNativeName + "("); if (count > 0) { for (int i = 0; i < count; i++) { DefParam p = f.Parameters[i]; string newname; p.Type.GetPreCallParamConversion(p, out newname); _sb.Append(" " + newname); if (i < count - 1) { _sb.Append(","); } } } _sb.Append(");\n"); if (!String.IsNullOrEmpty(postCall)) { _sb.AppendLine(); _sb.AppendLine(postCall); _sb.AppendLine(); } _sb.AppendLine("return ptr;"); _sb.DecreaseIndent(); _sb.AppendLine("}"); }
protected override void AddPropertyField(DefField field) { //TODO comments for fields //AddComments(field); string ptype; if (field.IsNativeArray) { if (field.Type.HasAttribute <NativeValueContainerAttribute>() || (field.Type.IsValueType && !field.Type.HasWrapType(WrapTypes.NativePtrValueType))) { DefParam tmpParam = new DefParam(field, field.Name); switch (field.PassedByType) { case PassedByType.Value: tmpParam.PassedByType = PassedByType.Pointer; break; case PassedByType.Pointer: tmpParam.PassedByType = PassedByType.PointerPointer; break; default: throw new Exception("Unexpected"); } ptype = tmpParam.CLRTypeName; _sb.AppendIndent(""); if (field.IsStatic) { _sb.Append("static "); } _sb.AppendFormat("property {0} {1}\n", ptype, field.Name); _sb.AppendLine("{"); _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":"); _sb.AppendLine("\t" + ptype + " get();"); _sb.AppendLine("}"); } else { ptype = field.CLRTypeName; _sb.AppendIndent(""); if (field.IsStatic) { _sb.Append("static "); } _sb.AppendFormat("property {0} {1}[int]\n", ptype, field.Name); _sb.AppendLine("{"); _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":"); _sb.AppendLine("\t" + ptype + " get(int index);"); _sb.AppendLine("\tvoid set(int index, " + ptype + " value);"); _sb.AppendLine("}"); } } else if (_cachedMembers.Contains(field)) { ptype = field.CLRTypeName; _sb.AppendIndent(""); if (field.IsStatic) { _sb.Append("static "); } _sb.AppendFormat("property {0} {1}\n", ptype, field.Name); _sb.AppendLine("{"); _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":"); _sb.AppendLine("\t" + ptype + " get();"); _sb.AppendLine("}"); } else { ptype = GetCLRTypeName(field); _sb.AppendIndent(""); if (field.IsStatic) { _sb.Append("static "); } _sb.AppendFormat("property {0} {1}\n", ptype, field.Name); _sb.AppendLine("{"); _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":"); _sb.AppendLine("\t" + ptype + " get();"); if ( // SharedPtrs can be copied by value. Let all be copied by value just to be sure (field.PassedByType == PassedByType.Pointer || field.Type.IsValueType) !IsReadOnly && !field.Type.HasAttribute <ReadOnlyForFieldsAttribute>() && !field.IsConst) { _sb.AppendLine(GetProtectionString(field.ProtectionType) + ":"); _sb.AppendLine("\tvoid set(" + ptype + " value);"); } _sb.AppendLine("}"); } }
protected virtual string GetCLRParamTypeName(DefParam param) { CheckTypeForDependancy(param.Type); return(param.Type.GetCLRParamTypeName(param)); }
protected virtual void AddEventMethods() { foreach (DefClass cls in _listeners) { DefFunction adderFunc = null; foreach (DefFunction func in _t.PublicMethods) { if (func.IsListenerAdder && func.Parameters[0].Type == cls) { adderFunc = func; break; } } if (adderFunc == null) { throw new Exception("Unexpected"); } foreach (DefFunction f in cls.PublicMethods) { if (f.IsDeclarableFunction) { string handler = cls.FullCLRName + "::" + f.CLRName + "Handler^"; string privField = NameToPrivate(f.Name); string listener = NameToPrivate(cls.Name); _sb.AppendLine("event " + handler + " " + f.CLRName); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine("void add(" + handler + " hnd)"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine("if (" + privField + " == CLR_NULL)"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine("if (" + listener + " == 0)"); _sb.AppendLine("{"); _sb.AppendLine("\t" + listener + " = new " + GetNativeDirectorName(cls) + "(this);"); _sb.AppendLine("\t" + GetNativeInvokationTarget(adderFunc) + "(" + listener + ");"); _sb.AppendLine("}"); _sb.AppendLine(listener + "->doCallFor" + f.CLRName + " = true;"); _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.AppendLine(privField + " += hnd;"); if (cls.HasAttribute <StopDelegationForReturnAttribute>()) { _sb.AppendLine(privField + "Delegates = " + privField + "->GetInvocationList();"); } _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.AppendLine("void remove(" + handler + " hnd)"); _sb.AppendLine("{"); _sb.AppendLine("\t" + privField + " -= hnd;"); _sb.AppendLine("\tif (" + privField + " == CLR_NULL) " + listener + "->doCallFor" + f.CLRName + " = false;"); if (cls.HasAttribute <StopDelegationForReturnAttribute>()) { _sb.AppendLine("\tif (" + privField + " == CLR_NULL) " + privField + "Delegates = nullptr; else " + privField + "Delegates = " + privField + "->GetInvocationList();"); } _sb.AppendLine("}"); _sb.DecreaseIndent(); _sb.AppendLine("private:"); _sb.IncreaseIndent(); _sb.AppendIndent(GetCLRTypeName(f) + " raise"); AddMethodParameters(f); _sb.Append("\n"); _sb.AppendLine("{"); if (cls.HasAttribute <StopDelegationForReturnAttribute>()) { _sb.IncreaseIndent(); _sb.AppendLine("if (" + privField + ")"); _sb.AppendLine("{"); _sb.IncreaseIndent(); string list = privField + "Delegates"; string stopret = cls.GetAttribute <StopDelegationForReturnAttribute>().Return; _sb.AppendLine(f.Type.FullCLRName + " mp_return;"); _sb.AppendLine("for (int i=0; i < " + list + "->Length; i++)"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendIndent("mp_return = " + "static_cast<" + handler + ">(" + list + "[i])("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" );\n"); _sb.AppendLine("if (mp_return == " + stopret + ") break;"); _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.AppendLine("return mp_return;"); _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.DecreaseIndent(); } else { _sb.AppendLine("\tif (" + privField + ")"); _sb.AppendIndent("\t\t"); if (f.TypeName != "void") { _sb.Append("return "); } _sb.Append(privField + "->Invoke("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" );\n"); } _sb.AppendLine("}"); _sb.DecreaseIndent(); _sb.AppendLine("}\n"); } } _sb.AppendLine(""); } }
protected virtual void AddPublicConstructorOverload(DefFunction f, int count) { _sb.AppendIndent(GetClassName() + "::" + _t.CLRName); if (f == null) { _sb.Append("()"); } else { AddMethodParameters(f, count); } string nativeType = GetTopClass(_t).FullNativeName; if (GetTopBaseClassName() == "Wrapper") { nativeType = "CLRObject"; } if (GetBaseClassName() != null) { _sb.Append(" : " + GetBaseClassName() + "((" + nativeType + "*) 0)"); } _sb.Append("\n"); _sb.AppendLine("{"); _sb.IncreaseIndent(); if (!_t.IsInterface) { _sb.AppendLine("_createdByCLR = true;"); } string preCall = null, postCall = null; if (f != null) { preCall = GetMethodPreNativeCall(f, count); postCall = GetMethodPostNativeCall(f, count); if (!String.IsNullOrEmpty(preCall)) { _sb.AppendLine(preCall); } } _sb.AppendIndent("_native = new " + _t.FullNativeName + "("); if (count > 0) { for (int i = 0; i < count; i++) { DefParam p = f.Parameters[i]; string newname; p.Type.GetPreCallParamConversion(p, out newname); _sb.Append(" " + newname); if (i < count - 1) { _sb.Append(","); } } } _sb.Append(");\n"); if (!String.IsNullOrEmpty(postCall)) { _sb.AppendLine(); _sb.AppendLine(postCall); _sb.AppendLine(); } AddConstructorBody(); _sb.DecreaseIndent(); _sb.AppendLine("}"); }
protected virtual void AddNativeMethodParam(DefParam param) { _sb.Append(param.NativeTypeName + " " + param.Name); }
protected override void AddMethod(DefFunction f) { if (f.HasAttribute <CustomCppDeclarationAttribute>()) { if (f.IsAbstract && AllowSubclassing) { return; } else { string txt = f.GetAttribute <CustomCppDeclarationAttribute>().DeclarationText; txt = ReplaceCustomVariables(txt, f); _sb.AppendLine(txt); _sb.AppendLine(); return; } } int defcount = 0; if (!f.HasAttribute <NoDefaultParamOverloadsAttribute>()) { foreach (DefParam param in f.Parameters) { if (param.DefaultValue != null) { defcount++; } } } bool methodIsVirtual = DeclareAsVirtual(f); for (int dc = 0; dc <= defcount; dc++) { if (dc == 0 && f.IsAbstract && AllowSubclassing) { //It's abstract, no body definition continue; } if (!AllowMethodOverloads && dc > 0) { continue; } if (dc < defcount && f.HasAttribute <HideParamsWithDefaultValuesAttribute>()) { continue; } _sb.AppendIndent(GetCLRTypeName(f) + " " + GetClassName() + "::" + f.CLRName); AddMethodParameters(f, f.Parameters.Count - dc); _sb.Append("\n"); _sb.AppendLine("{"); _sb.IncreaseIndent(); bool isVirtualOverload = dc > 0 && methodIsVirtual && AllowVirtualMethods; if (isVirtualOverload) { // Overloads (because of default values) // main method is virtual, call it with CLR default values if _isOverriden=true, // else do a normal native call _sb.AppendLine("if (_isOverriden)"); _sb.AppendLine("{"); _sb.IncreaseIndent(); bool hasPostConversions = false; for (int i = f.Parameters.Count - dc; i < f.Parameters.Count; i++) { DefParam p = f.Parameters[i]; if (!String.IsNullOrEmpty(p.CLRDefaultValuePreConversion)) { _sb.AppendLine(p.CLRDefaultValuePreConversion); } if (!String.IsNullOrEmpty(p.CLRDefaultValuePostConversion)) { hasPostConversions = true; } string n1, n2, n3; DefType dependancy; p.Type.GetDefaultParamValueConversion(p, out n1, out n2, out n3, out dependancy); if (dependancy != null) { AddTypeDependancy(dependancy); } } _sb.AppendIndent(""); if (!f.IsVoid) { if (hasPostConversions) { _sb.Append(GetCLRTypeName(f) + " mp_return = "); } else { _sb.Append("return "); } } _sb.Append(f.CLRName + "("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam p = f.Parameters[i]; _sb.Append(" "); if (i < f.Parameters.Count - dc) { _sb.Append(p.Name); } else { _sb.Append(p.CLRDefaultValue); } if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" );\n"); for (int i = f.Parameters.Count - dc; i < f.Parameters.Count; i++) { DefParam p = f.Parameters[i]; if (!String.IsNullOrEmpty(p.CLRDefaultValuePostConversion)) { _sb.AppendLine(p.CLRDefaultValuePostConversion); } } if (!f.IsVoid && hasPostConversions) { _sb.AppendLine("return mp_return;"); } _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.AppendLine("else"); _sb.AppendLine("{"); _sb.IncreaseIndent(); } AddMethodBody(f, f.Parameters.Count - dc); if (isVirtualOverload) { _sb.DecreaseIndent(); _sb.AppendLine("}"); } _sb.DecreaseIndent(); _sb.AppendLine("}"); } }
public static void AddNativeProxyMethodBody(DefFunction f, string managedTarget, IndentStringBuilder sb) { string managedCall; string fullPostConv = null; if (f.IsGetProperty) { sb.AppendLine(f.CLRTypeName + " mp_return = " + managedTarget + "->" + f.CLRName + ";"); managedCall = "mp_return"; } else if (f.IsSetProperty) { DefParam param = f.Parameters[0]; managedCall = managedTarget + "->" + f.CLRName + " = " + param.Type.GetNativeCallConversion(param.Name, param); } else { string pre, post, conv; foreach (DefParam param in f.Parameters) { param.Type.GetNativeParamConversion(param, out pre, out conv, out post); if (!String.IsNullOrEmpty(pre)) { sb.AppendLine(pre); } if (!String.IsNullOrEmpty(post)) { fullPostConv += post + "\n"; } } bool explicitCast = f.HasAttribute <ExplicitCastingForParamsAttribute>(); if (!f.IsVoid) { sb.AppendIndent(f.CLRTypeName + " mp_return = " + managedTarget + "->" + f.CLRName + "("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; param.Type.GetNativeParamConversion(param, out pre, out conv, out post); sb.Append(" "); if (explicitCast) { sb.Append("(" + param.CLRTypeName + ")"); } sb.Append(conv); if (i < f.Parameters.Count - 1) { sb.Append(","); } } sb.Append(" );\n"); managedCall = "mp_return"; if (!String.IsNullOrEmpty(fullPostConv)) { sb.AppendLine(fullPostConv); } } else { managedCall = managedTarget + "->" + f.CLRName + "("; for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; param.Type.GetNativeParamConversion(param, out pre, out conv, out post); managedCall += " "; if (explicitCast) { managedCall += "(" + param.CLRTypeName + ")"; } managedCall += conv; if (i < f.Parameters.Count - 1) { managedCall += ","; } } managedCall += " )"; } } if (!f.IsVoid) { if (f.Type is IDefString) { sb.AppendLine("SET_NATIVE_STRING( Mogre::Implementation::cachedReturnString, " + managedCall + " )"); sb.AppendLine("return Mogre::Implementation::cachedReturnString;"); } else { string returnExpr; string newname, expr, postcall; DefParam param = new DefParam(f, managedCall); expr = f.Type.GetPreCallParamConversion(param, out newname); postcall = f.Type.GetPostCallParamConversionCleanup(param); if (!String.IsNullOrEmpty(expr)) { sb.AppendLine(expr); if (String.IsNullOrEmpty(postcall)) { returnExpr = newname; } else { throw new Exception("Unexpected"); } } else { returnExpr = newname; } if (IsCachedFunction(f)) { sb.AppendLine("STATIC_ASSERT( sizeof(" + f.Type.FullNativeName + ") <= CACHED_RETURN_SIZE )"); sb.AppendLine("memcpy( Mogre::Implementation::cachedReturn, &" + returnExpr + ", sizeof(" + f.Type.FullNativeName + ") );"); sb.AppendLine("return *reinterpret_cast<" + f.Type.FullNativeName + "*>(Mogre::Implementation::cachedReturn);"); } else { sb.AppendLine("return " + returnExpr + ";"); } } } else { sb.AppendLine(managedCall + ";"); if (!String.IsNullOrEmpty(fullPostConv)) { sb.AppendLine(fullPostConv); } } }
protected override void AddPublicConstructor(DefFunction f) { _sb.AppendIndent(GetClassName() + "::" + _t.Name); if (f == null) { _sb.Append("()"); } else { AddMethodParameters(f); } _sb.Append(" : " + GetBaseClassName() + "( (CLRObject*)0 )"); _sb.Append("\n"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine("_createdByCLR = true;"); _sb.AppendLine("Type^ thisType = this->GetType();"); if (!IsAbstractClass && !_t.IsInterface) { _sb.AppendLine("_isOverriden = (thisType != " + _t.CLRName + "::typeid);"); } else { _sb.AppendLine("_isOverriden = true; //it's abstract or interface so it must be overriden"); } int count = 0; string preCall = null, postCall = null; if (f != null) { count = f.Parameters.Count; preCall = GetMethodPreNativeCall(f, count); postCall = GetMethodPostNativeCall(f, count); if (!String.IsNullOrEmpty(preCall)) { _sb.AppendLine(preCall); } } if (!IsAbstractClass && !_t.IsInterface) { _sb.AppendLine("if (_isOverriden)"); _sb.AppendLine("{"); _sb.IncreaseIndent(); } string proxyName = NativeProxyClassProducer.GetProxyName(_t); _sb.AppendIndent(proxyName + "* proxy = new " + proxyName + "(this"); if (count > 0) { _sb.Append(","); for (int i = 0; i < count; i++) { DefParam p = f.Parameters[i]; string newname; p.Type.GetPreCallParamConversion(p, out newname); _sb.Append(" " + newname); if (i < count - 1) { _sb.Append(","); } } } _sb.Append(");\n"); _sb.AppendLine("proxy->_overriden = Implementation::SubclassingManager::Instance->GetOverridenMethodsArrayPointer(thisType, " + _t.Name + "::typeid, " + _methodIndicesCount + ");"); _sb.AppendLine("_native = proxy;"); if (!IsAbstractClass && !_t.IsInterface) { _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.AppendLine("else"); _sb.AppendIndent("\t_native = new " + _t.FullNativeName + "("); if (count > 0) { _sb.Append(","); for (int i = 0; i < count; i++) { DefParam p = f.Parameters[i]; string newname; p.Type.GetPreCallParamConversion(p, out newname); _sb.Append(" " + newname); if (i < count - 1) { _sb.Append(","); } } } _sb.Append(");\n"); } if (!String.IsNullOrEmpty(postCall)) { _sb.AppendLine(); _sb.AppendLine(postCall); _sb.AppendLine(); } _sb.AppendLine(); AddConstructorBody(); _sb.DecreaseIndent(); _sb.AppendLine("}"); }
protected override void AddOverridableFunction(DefFunction f) { _wrapper.CppCheckTypeForDependancy(f.Type); foreach (DefParam param in f.Parameters) { _wrapper.CppCheckTypeForDependancy(param.Type); } _sb.AppendIndent(""); _sb.Append(f.NativeTypeName + " " + ProxyName + "::" + f.Name + "("); AddNativeMethodParams(f); _sb.Append(" )"); if (f.IsConstFunctionCall) { _sb.Append(" const"); } _sb.Append("\n"); _sb.AppendLine("{"); _sb.IncreaseIndent(); if (!f.IsAbstract) { _sb.AppendLine("if (_overriden[ " + _methodIndices[f] + " ])"); _sb.AppendLine("{"); _sb.IncreaseIndent(); } if (f.HasAttribute <CustomNativeProxyDeclarationAttribute>()) { string txt = f.GetAttribute <CustomNativeProxyDeclarationAttribute>().DeclarationText; txt = ReplaceCustomVariables(txt, f).Replace("@MANAGED@", "_managed"); _sb.AppendLine(txt); } else { AddNativeProxyMethodBody(f, "_managed", _sb); } if (!f.IsAbstract) { _sb.DecreaseIndent(); _sb.AppendLine("}"); _sb.AppendLine("else"); _sb.AppendIndent("\t"); if (!f.IsVoid) { _sb.Append("return "); } _sb.Append(f.Class.Name + "::" + f.Name + "("); for (int i = 0; i < f.Parameters.Count; i++) { DefParam param = f.Parameters[i]; _sb.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _sb.Append(","); } } _sb.Append(" );\n"); } _sb.DecreaseIndent(); _sb.AppendLine("}"); }
protected override void AddPropertyField(DefField field) { string ptype = GetCLRTypeName(field); string pname = GetClassName() + "::" + field.Name; if (field.IsNativeArray) { if (field.Type.HasAttribute <NativeValueContainerAttribute>() || (field.Type.IsValueType && !field.Type.HasWrapType(WrapTypes.NativePtrValueType))) { DefParam tmpParam = new DefParam(field, field.Name + "_array"); switch (field.PassedByType) { case PassedByType.Value: tmpParam.PassedByType = PassedByType.Pointer; break; case PassedByType.Pointer: tmpParam.PassedByType = PassedByType.PointerPointer; break; default: throw new Exception("Unexpected"); } ptype = GetCLRTypeName(tmpParam); string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), tmpParam); _sb.AppendLine(ptype + " " + pname + "::get()"); _sb.AppendLine("{"); _sb.AppendLine("\treturn " + managedType + ";"); _sb.AppendLine("}"); } else { string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field) + "[index]", field); _sb.AppendLine(ptype + " " + pname + "::get(int index)"); _sb.AppendLine("{"); _sb.AppendLine("\tif (index < 0 || index >= " + field.ArraySize + ") throw gcnew IndexOutOfRangeException();"); _sb.AppendLine("\treturn " + managedType + ";"); _sb.AppendLine("}"); _sb.AppendLine("void " + pname + "::set(int index, " + ptype + " value )"); _sb.AppendLine("{"); _sb.IncreaseIndent(); _sb.AppendLine("if (index < 0 || index >= " + field.ArraySize + ") throw gcnew IndexOutOfRangeException();"); string param = AddParameterConversion(new DefParam(field, "value")); _sb.AppendLine(GetNativeInvokationTarget(field) + "[index] = " + param + ";"); _sb.DecreaseIndent(); _sb.AppendLine("}"); } } else if (_cachedMembers.Contains(field)) { string managedType; if (field.Type.IsSTLContainer) { managedType = GetNativeInvokationTarget(field); } else { managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), field); } string priv = NameToPrivate(field); _sb.AppendLine(ptype + " " + pname + "::get()"); _sb.AppendLine("{"); if (!field.IsStatic) { _sb.AppendLine("\treturn ( CLR_NULL == " + priv + " ) ? (" + priv + " = " + managedType + ") : " + priv + ";"); } else { _sb.AppendLine("\treturn " + priv + ";"); } _sb.AppendLine("}"); } else { string managedType = field.Type.GetNativeCallConversion(GetNativeInvokationTarget(field), field); _sb.AppendLine(ptype + " " + pname + "::get()"); _sb.AppendLine("{"); _sb.AppendLine("\treturn " + managedType + ";"); _sb.AppendLine("}"); if ( // SharedPtrs can be copied by value. Let all be copied by value just to be sure (field.PassedByType == PassedByType.Pointer || field.Type.IsValueType) !IsReadOnly && !field.Type.HasAttribute <ReadOnlyForFieldsAttribute>() && !field.IsConst) { _sb.AppendLine("void " + pname + "::set( " + ptype + " value )"); _sb.AppendLine("{"); _sb.IncreaseIndent(); string param = AddParameterConversion(new DefParam(field, "value")); _sb.AppendLine(GetNativeInvokationTarget(field) + " = " + param + ";"); _sb.DecreaseIndent(); _sb.AppendLine("}"); } } }