protected virtual void AddMethodBody(DefFunction f, int count) { string preCall = GetMethodPreNativeCall(f, count); string nativeCall = GetMethodNativeCall(f, count); string postCall = GetMethodPostNativeCall(f, count); if (!String.IsNullOrEmpty(preCall)) { _sb.AppendLine(preCall); } if (f.IsVoid) { _sb.AppendLine(nativeCall + ";"); if (!String.IsNullOrEmpty(postCall)) { _sb.AppendLine(postCall); } } else { if (String.IsNullOrEmpty(postCall)) { _sb.AppendLine("return " + nativeCall + ";"); } else { _sb.AppendLine(GetCLRTypeName(f) + " retres = " + nativeCall + ";"); _sb.AppendLine(postCall); _sb.AppendLine("return retres;"); } } }
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; } } AddEventMethodForListener(cls, adderFunc); } foreach (DefClass cls in _listenersByListenerSetter) { DefFunction setterFunc = null; foreach (DefFunction func in _t.PublicMethods) { if (func.IsListenerSetter && func.Parameters[0].Type == cls) { setterFunc = func; break; } } AddEventMethodForListener(cls, setterFunc); } }
protected override void AddMethod(DefFunction f) { if (f.IsVirtual) { base.AddMethod(f); } }
protected override void AddInterfaceMethod(DefFunction f) { _sb.DecreaseIndent(); _sb.AppendLine(GetProtectionString(f.ProtectionType) + ":"); _sb.IncreaseIndent(); base.AddInterfaceMethod(f); }
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 AddDisposerBody() { if (_t.HasAttribute <CustomDisposingAttribute>()) { string text = _t.GetAttribute <CustomDisposingAttribute>().Text; _sb.AppendLine(text); } foreach (DefClass cls in _listeners) { DefFunction removerFunc = null; foreach (DefFunction func in _t.PublicMethods) { if (func.IsListenerRemover && func.Parameters[0].Type == cls) { removerFunc = func; break; } } if (removerFunc == null) { throw new Exception("Unexpected"); } string native = "_native"; _sb.AppendLine(String.Format("if ({0} != 0)\n{{\n\tif (" + native + " != 0) " + GetNativeInvokationTarget(removerFunc) + "({0});\n\tdelete {0}; {0} = 0;\n}}", NameToPrivate(cls.Name))); } }
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 AddPublicConstructor(DefFunction function) { if (function == null) { AddPublicConstructorOverload(function, 0); } else { int defcount = 0; if (!function.HasAttribute <NoDefaultParamOverloadsAttribute>()) { foreach (DefParam param in function.Parameters) { if (param.DefaultValue != null) { defcount++; } } } bool hideParams = function.HasAttribute <HideParamsWithDefaultValuesAttribute>(); // The overloads (because of default values) for (int dc = 0; dc <= defcount; dc++) { if (dc < defcount && function.HasAttribute <HideParamsWithDefaultValuesAttribute>()) { continue; } AddPublicConstructorOverload(function, function.Parameters.Count - dc); } } }
protected override bool DeclareAsOverride(DefFunction f) { if (f.ProtectionType == ProtectionType.Public) { return(true); } else { return(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 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"); }
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 override void AddOverridableFunction(DefFunction f) { _sb.AppendIndent(""); if (f.IsVirtual) { _sb.Append("virtual "); } _sb.Append(f.NativeTypeName + " " + f.Name + "("); AddNativeMethodParams(f); _sb.Append(" ) "); if (f.IsConstFunctionCall) { _sb.Append("const "); } _sb.Append("override;\n"); }
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 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 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 virtual void AddPublicConstructor(DefFunction function) { string className = (_t.IsInterface) ? _t.Name : _t.CLRName; if (function == null) { _sb.AppendLine(className + "();"); } else { int defcount = 0; if (!function.HasAttribute <NoDefaultParamOverloadsAttribute>()) { foreach (DefParam param in function.Parameters) { if (param.DefaultValue != null) { defcount++; } } } bool hideParams = function.HasAttribute <HideParamsWithDefaultValuesAttribute>(); // The overloads (because of default values) for (int dc = 0; dc <= defcount; dc++) { if (dc < defcount && hideParams) { continue; } _sb.AppendIndent(className); AddMethodParameters(function, function.Parameters.Count - dc); _sb.Append(";\n"); } } }
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 void AddComments(DefFunction f) { //TODO }
protected override void AddProperty(DefProperty p) { //TODO comments for properties //AddComments(p); string ptype = GetCLRTypeName(p); _sb.AppendFormatIndent("property {0} {1}\n{{\n", ptype, p.Name); if (p.CanRead) { DefFunction f = p.GetterFunction; bool methodIsVirtual = DeclareAsVirtual(f); if (p.GetterFunction.ProtectionType == ProtectionType.Public || (AllowProtectedMembers && p.GetterFunction.ProtectionType == ProtectionType.Protected)) { _sb.AppendLine(GetProtectionString(p.GetterFunction.ProtectionType) + ":"); if (AllowMethodIndexAttributes && f.IsVirtual && !f.IsAbstract) { _sb.Append("\t"); AddMethodIndexAttribute(f); } _sb.AppendIndent("\t"); if (p.GetterFunction.IsStatic) { _sb.Append("static "); } if (methodIsVirtual) { _sb.Append("virtual "); } _sb.Append(ptype + " get()"); if (DeclareAsOverride(p.GetterFunction)) { _sb.Append(" override"); } else if (f.IsAbstract && AllowSubclassing) { _sb.Append(" abstract"); } _sb.Append(";\n"); } } if (p.CanWrite) { DefFunction f = p.SetterFunction; bool methodIsVirtual = DeclareAsVirtual(f); if (p.SetterFunction.ProtectionType == ProtectionType.Public || (AllowProtectedMembers && p.SetterFunction.ProtectionType == ProtectionType.Protected)) { _sb.AppendLine(GetProtectionString(p.SetterFunction.ProtectionType) + ":"); if (AllowMethodIndexAttributes && f.IsVirtual && !f.IsAbstract) { _sb.Append("\t"); AddMethodIndexAttribute(f); } _sb.AppendIndent("\t"); if (p.SetterFunction.IsStatic) { _sb.Append("static "); } if (methodIsVirtual) { _sb.Append("virtual "); } _sb.Append("void set(" + ptype + " " + p.SetterFunction.Parameters[0].Name + ")"); if (DeclareAsOverride(p.SetterFunction)) { _sb.Append(" override"); } else if (f.IsAbstract && AllowSubclassing) { _sb.Append(" abstract"); } _sb.Append(";\n"); } } _sb.AppendLine("}"); }
protected void AddMethodParameters(DefFunction f) { AddMethodParameters(f, f.Parameters.Count); }
protected virtual void AddMethodIndexAttribute(DefFunction f) { _sb.AppendLine("[Implementation::MethodIndex( " + _methodIndices[f] + " )]"); }
protected override void AddMethod(DefFunction f) { if (f.HasAttribute <CustomIncDeclarationAttribute>()) { string txt = f.GetAttribute <CustomIncDeclarationAttribute>().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); // The main method AddComments(f); if (AllowMethodIndexAttributes && f.IsVirtual && !f.IsAbstract) { AddMethodIndexAttribute(f); } _sb.AppendIndent(""); if (f.IsStatic) { _sb.Append("static "); } if (methodIsVirtual) { _sb.Append("virtual "); } _sb.Append(GetCLRTypeName(f) + " " + f.CLRName); AddMethodParameters(f, f.Parameters.Count); if (DeclareAsOverride(f)) { _sb.Append(" override"); } else if (f.IsAbstract && AllowSubclassing) { _sb.Append(" abstract"); } _sb.Append(";\n"); if (AllowMethodOverloads) { // The overloads (because of default values) for (int dc = 1; dc <= defcount; dc++) { if (dc < defcount && f.HasAttribute <HideParamsWithDefaultValuesAttribute>()) { continue; } AddComments(f); _sb.AppendIndent(""); if (f.IsStatic) { _sb.Append("static "); } _sb.Append(GetCLRTypeName(f) + " " + f.CLRName); AddMethodParameters(f, f.Parameters.Count - dc); _sb.Append(";\n"); } } }
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(""); } }