Beispiel #1
0
        public virtual void InitNonPrimaryBase(ICppObject baseInDerived, ICppObject derived, Type baseType)
        {
            int offset;
            var baseTypeInfo = GetCastInfo(derived.GetType(), baseType, out offset);

            Marshal.WriteIntPtr(baseInDerived.Native.Native, baseTypeInfo.GCHandleOffset, CppInstancePtr.MakeGCHandle(baseInDerived));
        }
Beispiel #2
0
        public virtual CppInstancePtr Cast(ICppObject instance, Type targetType)
        {
            int offset;
            var baseTypeInfo = GetCastInfo(instance.GetType(), targetType, out offset);
            var result       = new CppInstancePtr(instance.Native, offset);

            if (offset > 0 && instance.Native.IsManagedAlloc && baseTypeInfo.HasVTable)
            {
                // we might need to paste the managed base-in-derived vtptr here --also inits native_vtptr
                baseTypeInfo.VTable.InitInstance(ref result);
            }

            return(result);
        }
Beispiel #3
0
        public virtual TTarget Cast <TTarget> (ICppObject instance) where TTarget : class
        {
            TTarget result;
            var     ptr = Cast(instance, typeof(TTarget));

            // Check for existing instance based on vtable ptr
            result = CppInstancePtr.ToManaged <TTarget> (ptr.Native);

            // Create a new wrapper if necessary
            if (result == null)
            {
                result = Activator.CreateInstance(typeof(TTarget), ptr) as TTarget;
            }

            return(result);
        }
Beispiel #4
0
        public virtual CppInstancePtr Cast(ICppObject instance, Type targetType)
        {
            int offset;
            var baseTypeInfo = GetCastInfo(instance.GetType(), targetType, out offset);
            var result       = new CppInstancePtr(instance.Native, offset);

            if (offset > 0 && instance.Native.IsManagedAlloc && baseTypeInfo.HasVTable)
            {
                // we might need to paste the managed base-in-derived vtptr here --also inits native_vtptr
                baseTypeInfo.VTable.InitInstanceOffset(ref result, false, 0);

                // Make sure we update the vtable location in the original instance as well
                instance.Native.SetNativeBaseVTable(baseTypeInfo.WrapperType, result.NativeVTable);
            }

            return(result);
        }