protected void AddEventMethodForListener(DefClass cls, DefFunction listenerSetFunc) { if (listenerSetFunc == 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(listenerSetFunc) + "(" + 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 AddPublicDeclarations() { foreach (DefField field in _t.PublicFields) { if (!field.IsIgnored) { //if (CheckTypeMemberForGetProperty(field) == false) // AddMethodsForField(field); //else AddPropertyField(field); _sb.AppendLine(); } } foreach (DefProperty p in _t.GetProperties()) { if (AllowProperty(p) && (p.Function.ProtectionType == ProtectionType.Public || (AllowSubclassing && (p.Function.IsStatic || !p.Function.IsVirtual)) || (AllowProtectedMembers && p.Function.ProtectionType == ProtectionType.Protected))) { AddProperty(EnhanceProperty(p)); _sb.Append("\n"); } } foreach (DefFunction f in _t.PublicMethods) { if (f.IsOperatorOverload) { if (f.Name.EndsWith("==")) { AddPredefinedMethods(PredefinedMethods.Equals); _sb.AppendLine(); } else if (f.Name.EndsWith("=")) { AddPredefinedMethods(PredefinedMethods.CopyTo); _sb.AppendLine(); } } else if (f.IsDeclarableFunction) { AddMethod(f); _sb.Append("\n"); } } if (_t.HasAttribute <IncludePredefinedMethodAttribute>()) { AddPredefinedMethods(_t.GetAttribute <IncludePredefinedMethodAttribute>().Methods); _sb.AppendLine(); } foreach (DefClass cls in _interfaces) { if (cls == _t) { continue; } AddInterfaceImplementation(cls); } }
public string CreateIncludeCodeForOverridable(DefClass type) { UsedTypes.Clear(); PreClassProducers.Clear(); PostClassProducers.Clear(); IndentStringBuilder sbTypes = new IndentStringBuilder(); new IncSubclassingClassProducer(this, type, sbTypes, null).Add(); if (type.HasAttribute<InterfacesForOverridableAttribute>()) { List<DefClass[]> interfaces = type.GetAttribute<InterfacesForOverridableAttribute>().Interfaces; foreach (DefClass[] ifaces in interfaces) { new IncSubclassingClassProducer(this, type, sbTypes, ifaces).Add(); } } foreach (ClassProducer producer in PostClassProducers) { if (!(producer is NativeProtectedTypesProxy) && !(producer is NativeProtectedStaticsProxy)) producer.Add(); } foreach (ClassProducer producer in PreClassProducers) { if (!(producer is NativeProtectedTypesProxy) && !(producer is NativeProtectedStaticsProxy)) producer.AddFirst(); } IndentStringBuilder sb = new IndentStringBuilder(); sb.AppendLine("#pragma once\n"); sb.AppendFormat("namespace {0}\n{{\n", ManagedNamespace); sb.IncreaseIndent(); sb.AppendLine(sbTypes.ToString()); sb.DecreaseIndent(); sb.AppendLine("}"); return sb.ToString().Replace("\r", ""); }