Пример #1
0
        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");
        }
Пример #2
0
        public bool TypeIsWrappable(DefType type)
        {
            if (type.Name.StartsWith("DLL_"))
            {
                //It's DLL function pointers of OgrePlatformManager.h
                return false;
            }

            // Get explicit type or a new type if type has ReplaceBy attribute
            type = (type.IsNested) ? type.ParentClass.FindType<DefType>(type.Name) : type.NameSpace.FindType<DefType>(type.Name);

            if (type.HasAttribute<CustomIncClassDefinitionAttribute>())
                return true;

            if (type.IsIgnored)
                return false;

            if (type.HasAttribute<WrapTypeAttribute>())
                return true;

            if (type.IsSharedPtr)
            {
                type.Attributes.Add(new WrapTypeAttribute(WrapTypes.SharedPtr));
                return true;
            }
            else if (type is DefClass)
            {
                DefClass cls = type as DefClass;
                if (cls.HasAttribute<CLRObjectAttribute>(true))
                {
                    if (cls.HasAttribute<OverridableAttribute>(true))
                        cls.Attributes.Add(new WrapTypeAttribute(WrapTypes.Overridable));
                    else
                        cls.Attributes.Add(new WrapTypeAttribute(WrapTypes.NonOverridable));
                    return true;
                }

                if (cls.IsSingleton)
                {
                    cls.Attributes.Add(new WrapTypeAttribute(WrapTypes.Singleton));
                    return true;
                }

                return false;
            }
            else if (type is DefTypeDef)
            {
                if (type.IsSTLContainer)
                {
                    foreach (ITypeMember m in (type as DefTypeDef).TypeMembers)
                    {
                        DefType mt = m.Type;
                        if (!mt.IsValueType && !mt.IsPureManagedClass
                            && !TypeIsWrappable(mt))
                            return false;
                    }

                    return true;
                }
                else if (type is DefIterator)
                {
                    if (TypeIsWrappable((type as DefIterator).TypeMembers[0].Type))
                    {
                        if ((type as DefIterator).IsConstIterator)
                        {
                            try
                            {
                                DefType notconst = type.FindType<DefType>(type.Name.Substring("Const".Length), true);
                                return false;
                            }
                            catch
                            {
                                return true;
                            }
                        }
                        else
                            return true;
                    }
                    else
                        return false;
                }
                else if ((type as DefTypeDef).BaseType is DefInternal
                         || (type as DefTypeDef).BaseType.HasAttribute<ValueTypeAttribute>())
                    return true;
                else
                    return false;
            }
            else
                return false;
        }