protected virtual void AddCreator(DefFunction f) { if (f == null) AddCreatorOverload(f, 0); else { int defcount = 0; if (!f.HasAttribute<NoDefaultParamOverloadsAttribute>()) { foreach (DefParam param in f.Parameters) if (param.DefaultValue != null) defcount++; } // The overloads (because of default values) for (int dc = 0; dc <= defcount; dc++) { if (dc < defcount && f.HasAttribute<HideParamsWithDefaultValuesAttribute>()) continue; AddCreatorOverload(f, f.Parameters.Count - dc); } } }
protected virtual void AddCreator(DefFunction f) { if (f == null) _sb.AppendLine("static " + _t.CLRName + " Create();"); else { int defcount = 0; if (!f.HasAttribute<NoDefaultParamOverloadsAttribute>()) { foreach (DefParam param in f.Parameters) if (param.DefaultValue != null) defcount++; } // The overloads (because of default values) for (int dc = 0; dc <= defcount; dc++) { if (dc < defcount && f.HasAttribute<HideParamsWithDefaultValuesAttribute>()) continue; _sb.AppendIndent("static " + _t.CLRName + " Create"); AddMethodParameters(f, f.Parameters.Count - dc); _sb.Append(";\n"); } } }
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 virtual bool? CheckFunctionForGetProperty(DefFunction f) { string name = f.HasAttribute<RenameAttribute>() ? f.GetAttribute<RenameAttribute>().Name : f.Name; if (f.HasAttribute<CustomIncDeclarationAttribute>() || f.HasAttribute<CustomCppDeclarationAttribute>()) return false; if (f.TypeName == "bool" && ((name.StartsWith("is") && Char.IsUpper(name[2])) || (name.StartsWith("has") && Char.IsUpper(name[3]))) && f.Parameters.Count == 0) return true; return CheckTypeMemberForGetProperty(f); }
protected virtual bool AllowFunction(DefFunction f) { if (f.HasAttribute<IgnoreAttribute>() || IsUnhandledType(f)) return false; else { foreach (DefParam param in f.Parameters) { if (IsUnhandledType(param)) return false; } return true; } }
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) { 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 virtual void AddConstructor(DefFunction f) { }
protected override string GetNativeInvokationTarget(DefFunction f) { return "static_cast<" + ProxyName + "*>(_native)->" + f.Class.Name + "::" + f.Name; }
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 virtual void AddMethod(DefFunction func) { }
protected override void AddPublicConstructor(DefFunction f) { }
protected override void AddMethod(DefFunction f) { if (f.IsVirtual) base.AddMethod(f); }
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 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 string ReplaceCustomVariables(string txt, DefFunction func) { txt = ReplaceCustomVariables(txt); string replace; if (DeclareAsOverride(func)) replace = "override"; else if (func.IsAbstract && AllowSubclassing) replace = "abstract"; else replace = ""; txt = txt.Replace("@OVERRIDE@", replace); txt = txt.Replace("@NATIVE_INVOKATION_TARGET_FOR_FUNCTION@", GetNativeInvokationTarget(func)); return txt; }
protected virtual string GetNativeInvokationTarget(DefFunction f) { if (!f.IsStatic) { if (f.ProtectionType == ProtectionType.Public) { return GetNativeInvokationTarget(f.IsConstFunctionCall) + "->" + f.Name; } else if (f.ProtectionType == ProtectionType.Protected) { if (!f.IsVirtual) { string proxyName = NativeProtectedStaticsProxy.GetProtectedStaticsProxyName(_t); return "static_cast<" + proxyName + "*>(_native)->" + f.Name; } else throw new Exception("Unexpected"); } else throw new Exception("Unexpected"); } else { if (f.ProtectionType == ProtectionType.Public) return f.Class.FullNativeName + "::" + f.Name; else return NativeProtectedStaticsProxy.GetProtectedStaticsProxyName(f.Class) + "::" + f.Name; } }
protected virtual bool DeclareAsVirtual(DefFunction f) { return (f.IsVirtual && AllowVirtualMethods) || f.IsVirtualInterfaceMethod || (f.IsVirtual && f.BaseFunction != null && f.BaseFunction.Class.AllowVirtuals); }
protected virtual bool DeclareAsOverride(DefFunction f) { return (f.IsOverride && DeclareAsVirtual(f)) || (f.IsVirtualInterfaceMethod && !f.Class.IsInterface && !f.Class.ContainsInterfaceFunctionSignature(f.Signature, false)); }
protected bool ContainsFunction(DefFunction func, List<DefFunction> list) { foreach (DefFunction lf in list) { if (lf.Signature == func.Signature) return true; } return false; }
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 virtual void AddOverridableFunction(DefFunction f) { }
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("}"); } }
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 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("}"); }
private static bool IsCachedFunction(DefFunction f) { return (f.PassedByType == PassedByType.Reference && (f.Type.IsValueType || f.Type.IsPureManagedClass)); }
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 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"); }
protected virtual void AddInterfaceMethod(DefFunction f) { AddMethod(f); }