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) { 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(); }
//, bool inProtectedTypesProxy) //public void IncAddEnum(DefEnum enm, IndentStringBuilder sb) //{ // IncAddEnum(enm, sb, false); //} public void IncAddEnum(DefEnum enm, IndentStringBuilder sb) { if (enm.Name[0] == '@') return; if (enm.HasAttribute<FlagsEnumAttribute>()) { sb.AppendLine("[Flags]"); } sb.AppendIndent(""); if (!enm.IsNested) sb.Append("public "); else sb.Append(Producer.GetProtectionString(enm.ProtectionType) + ": "); //if (inProtectedTypesProxy) //sb.Append("enum " + enm.Name + "\n"); //else sb.Append("enum class " + enm.CLRName + "\n"); sb.AppendLine("{"); sb.IncreaseIndent(); for (int i=0; i < enm.CLREnumValues.Length; i++) { string value = enm.NativeEnumValues[i]; sb.AppendIndent(""); //if (inProtectedTypesProxy) //{ // value = enm.ParentFullNativeName + "::" + enm.CLREnumValues[i]; // sb.Append("PUBLIC_"); //} sb.Append(enm.CLREnumValues[i] + " = " + value); if (i < enm.CLREnumValues.Length - 1) sb.Append(","); sb.Append("\n"); } sb.DecreaseIndent(); sb.AppendLine("};\n"); }
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", ""); }
public string CreateIncludeCodeForIncludeFile(string include) { UsedTypes.Clear(); PreClassProducers.Clear(); PostClassProducers.Clear(); IndentStringBuilder sbTypes = new IndentStringBuilder(); foreach (DefType t in IncludeFiles[include]) { IncAddType(t, sbTypes); } foreach (ClassProducer producer in PostClassProducers) { producer.Add(); } foreach (ClassProducer producer in PreClassProducers) { producer.AddFirst(); } IndentStringBuilder sb = new IndentStringBuilder(); sb.AppendLine("#pragma once\n"); IncAddIncludeFiles(include, UsedTypes, sb); sb.AppendFormat("namespace {0}\n{{\n", ManagedNamespace); sb.IncreaseIndent(); sb.AppendLine(sbTypes.ToString()); sb.DecreaseIndent(); sb.AppendLine("}"); return sb.ToString().Replace("\r",""); }
public string CreateCppCodeForIncludeFile(string include, out bool hasContent) { UsedTypes.Clear(); PreClassProducers.Clear(); PostClassProducers.Clear(); IndentStringBuilder contentsb = new IndentStringBuilder(); foreach (DefType t in IncludeFiles[include]) { CppAddType(t, contentsb); } foreach (ClassProducer producer in PostClassProducers) { producer.Add(); } foreach (ClassProducer producer in PreClassProducers) { producer.AddFirst(); } IndentStringBuilder sb = new IndentStringBuilder(); hasContent = false; CppAddIncludeFiles(include, UsedTypes, sb); sb.AppendFormat("namespace {0}\n{{\n", ManagedNamespace); sb.IncreaseIndent(); string txt = contentsb.ToString(); if (txt != "") { hasContent = true; sb.AppendLine(txt); } sb.DecreaseIndent(); sb.AppendLine("}"); return sb.ToString().Replace("\r", ""); }
private void IncAddSharedPtrType(DefType type, IndentStringBuilder sb) { if (!type.Name.EndsWith("Ptr")) throw new Exception("SharedPtr class that doesn't have a name ending to 'Ptr'"); string basename = null; if (type is DefClass) basename = (type as DefClass).Inherits[0]; else basename = (type as DefTypeDef).BaseTypeName; int s = basename.IndexOf("<"); int e = basename.LastIndexOf(">"); string baseClass = basename.Substring(s + 1, e - s - 1).Trim(); //string nativeClass = _nativePrefix + "::" + baseClass; string nativeClass = type.FindType<DefType>(baseClass).FullNativeName; string className = type.FullCLRName; if (className.Contains("::")) className = className.Substring(className.IndexOf("::") + 2); if (!type.IsNested) { PreDeclarations.Add("ref class " + type.Name + ";"); sb.AppendIndent("public "); } else { sb.AppendIndent(Producer.GetProtectionString(type.ProtectionType) + ": "); } sb.Append("ref class " + type.Name + " : public " + baseClass + "\n"); sb.AppendLine("{"); sb.AppendLine("internal:"); sb.IncreaseIndent(); sb.AppendLine("\t" + type.FullNativeName + "* _sharedPtr;"); sb.AppendLine(); sb.AppendLine(type.Name + "(" + type.FullNativeName + "& sharedPtr) : " + baseClass + "( sharedPtr.getPointer() )"); sb.AppendLine("{"); sb.AppendLine("\t_sharedPtr = new " + type.FullNativeName + "(sharedPtr);"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("!" + type.Name + "()"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("if (_sharedPtr != 0)"); sb.AppendLine("{"); sb.AppendLine("\tdelete _sharedPtr;"); sb.AppendLine("\t_sharedPtr = 0;"); sb.AppendLine("}"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("~" + type.Name + "()"); sb.AppendLine("{"); sb.AppendLine("\tthis->!" + type.Name + "();"); sb.AppendLine("}"); sb.AppendLine(); sb.DecreaseIndent(); sb.AppendLine("public:"); sb.IncreaseIndent(); sb.AppendLine("DEFINE_MANAGED_NATIVE_CONVERSIONS_FOR_SHAREDPTR( " + className + " )"); sb.AppendLine(); if (type is DefClass) { DefClass realType = type.FindType<DefClass>(baseClass, false); if (realType != null && realType.BaseClass != null && realType.BaseClass.Name == "Resource") { // For Resource subclasses (Material etc.) allow implicit conversion of ResourcePtr (i.e ResourcePtr -> MaterialPtr) AddTypeDependancy(realType.BaseClass); sb.AppendLine("static " + type.Name + "^ FromResourcePtr( ResourcePtr^ ptr )"); sb.AppendLine("{"); sb.AppendLine("\treturn (" + type.Name + "^) ptr;"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("static operator " + type.Name + "^ ( ResourcePtr^ ptr )"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("void* castptr = dynamic_cast<" + nativeClass + "*>(ptr->_native);"); sb.AppendLine("if (castptr == 0) throw gcnew InvalidCastException(\"The underlying type of the ResourcePtr object is not of type " + baseClass + ".\");"); sb.AppendLine("return gcnew " + type.Name + "( (" + type.FullNativeName + ") *(ptr->_sharedPtr) );"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); } } //sb.AppendLine(type.Name + "() : " + baseClass + "( (" + nativeClass + "*) 0 )"); //sb.AppendLine("{"); //sb.AppendLine("\t_sharedPtr = new " + type.FullNativeName + "();"); //sb.AppendLine("}"); //sb.AppendLine(); sb.AppendLine(type.Name + "(" + baseClass + "^ obj) : " + baseClass + "( obj->_native )"); sb.AppendLine("{"); sb.AppendLine("\t_sharedPtr = new " + type.FullNativeName + "( static_cast<" + nativeClass + "*>(obj->_native) );"); sb.AppendLine("}"); sb.AppendLine(); //sb.AppendLine("void Bind(" + baseClass + "^ obj)"); //sb.AppendLine("{"); //sb.AppendLine("\t(*_sharedPtr).bind( static_cast<" + nativeClass + "*>(obj->_native) );"); //sb.AppendLine("}"); //sb.AppendLine(); sb.AppendLine("virtual bool Equals(Object^ obj) override"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine(type.Name + "^ clr = dynamic_cast<" + type.Name + "^>(obj);"); sb.AppendLine("if (clr == CLR_NULL)"); sb.AppendLine("{"); sb.AppendLine("\treturn false;"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("return (_native == clr->_native);"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine("bool Equals(" + type.Name + "^ obj)"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("if (obj == CLR_NULL)"); sb.AppendLine("{"); sb.AppendLine("\treturn false;"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("return (_native == obj->_native);"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("static bool operator == (" + type.Name + "^ val1, " + type.Name + "^ val2)"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("if ((Object^)val1 == (Object^)val2) return true;"); sb.AppendLine("if ((Object^)val1 == nullptr || (Object^)val2 == nullptr) return false;"); sb.AppendLine("return (val1->_native == val2->_native);"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("static bool operator != (" + type.Name + "^ val1, " + type.Name + "^ val2)"); sb.AppendLine("{"); sb.AppendLine("\treturn !(val1 == val2);"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("virtual int GetHashCode() override"); sb.AppendLine("{"); sb.AppendLine("\treturn reinterpret_cast<int>( _native );"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("property IntPtr NativePtr"); sb.AppendLine("{"); sb.AppendLine("\tIntPtr get() { return (IntPtr)_sharedPtr; }"); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("property bool Unique"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("bool get()"); sb.AppendLine("{"); sb.AppendLine("\treturn (*_sharedPtr).unique();"); sb.AppendLine("}"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("property int UseCount"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("int get()"); sb.AppendLine("{"); sb.AppendLine("\treturn (*_sharedPtr).useCount();"); sb.AppendLine("}"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); //sb.AppendLine("void SetNull()"); //sb.AppendLine("{"); //sb.AppendLine("\t(*_sharedPtr).setNull();"); //sb.AppendLine("\t_native = 0;"); //sb.AppendLine("}"); //sb.AppendLine(); sb.AppendLine("property bool IsNull"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine("bool get()"); sb.AppendLine("{"); sb.AppendLine("\treturn (*_sharedPtr).isNull();"); sb.AppendLine("}"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.AppendLine(); sb.AppendLine("property " + baseClass + "^ Target"); sb.AppendLine("{"); sb.IncreaseIndent(); sb.AppendLine(baseClass + "^ get()"); sb.AppendLine("{"); sb.AppendLine("\treturn static_cast<" + nativeClass + "*>(_native);"); sb.AppendLine("}"); sb.DecreaseIndent(); sb.AppendLine("}"); sb.DecreaseIndent(); sb.AppendLine("};\n\n"); }