Esempio n. 1
0
        public void TestParamSpecObject()
        {
            var param = TestParamSpec(GType.Object, (name, nick, blurb, flags) =>
                                      new ParamSpecObject(name, nick, blurb, GType.TypeOf <GISharp.GObject.Object> (), flags));

            Assert.That(param.GetGType().Name, Is.EqualTo("GParamObject"));

            Utility.AssertNoGLibLog();
        }
Esempio n. 2
0
        public void TestParamSpecBoxed()
        {
            var gtype = GType.TypeOf <object> ();

            Assume.That(gtype.IsA(GType.Boxed));

            var param = TestParamSpec(gtype, (name, nick, blurb, flags) =>
                                      new ParamSpecBoxed(name, nick, blurb, gtype, flags));

            Assert.That(param.GetGType().Name, Is.EqualTo("GParamBoxed"));

            Utility.AssertNoGLibLog();
        }
Esempio n. 3
0
        public void TestParamSpecParam()
        {
            var gtype = GType.TypeOf <ParamSpecBoolean> ();

            Assume.That(gtype.IsA(GType.Param));

            var param = TestParamSpec(gtype, (name, nick, blurb, flags) =>
                                      new ParamSpecParam(name, nick, blurb, gtype, flags));

            Assert.That(param.GetGType().Name, Is.EqualTo("GParamParam"));

            Utility.AssertNoGLibLog();
        }
Esempio n. 4
0
        public void TestParamSpecFlags()
        {
            const TestFlags defaultValue = TestFlags.Two;
            var             gtype        = GType.TypeOf <TestFlags> ();

            Assume.That(gtype.IsA(GType.Flags));

            var param = TestParamSpec(gtype, (name, nick, blurb, flags) =>
                                      new ParamSpecFlags(name, nick, blurb, gtype, defaultValue, flags));

            Assert.That(param.FlagsType, Is.EqualTo(typeof(TestFlags)));
            Assert.That(param.DefaultValue, Is.EqualTo(defaultValue));
            Assert.That(param.GetGType().Name, Is.EqualTo("GParamFlags"));

            Utility.AssertNoGLibLog();
        }
Esempio n. 5
0
        /// <summary>
        /// Gets a managed proxy for a an unmanged GParamSpec.
        /// </summary>
        /// <param name="handle">
        /// The pointer to the unmanaged instance
        /// </param>
        /// <param name="ownership">
        /// Indicates if we already have a reference to the unmanged instance
        /// or not.
        /// </param>
        /// <returns>
        /// A managed proxy instance
        /// </returns>
        /// <remarks>
        /// This method tries to get an existing managed proxy instance by
        /// looking for a GC handle attached to the unmanaged instance (using
        /// QData). If one is found, it returns the existing managed instance,
        /// otherwise a new instance is created.
        /// </remarks>
        public static new T GetInstance <T>(IntPtr handle, Transfer ownership) where T : ParamSpec
        {
            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            // see if the unmanaged object has a managed GC handle
            var ptr = g_param_spec_get_qdata(handle, managedProxyGCHandleQuark);

            if (ptr != IntPtr.Zero)
            {
                var gcHandle = (GCHandle)ptr;
                if (gcHandle.IsAllocated)
                {
                    // the GC handle looks good, so we should have the managed
                    // proxy for the unmanged object here
                    var target = (ParamSpec)gcHandle.Target;
                    // make sure the managed object has not been disposed
                    if (target.handle == handle)
                    {
                        // release the extra reference, if there is one
                        if (ownership != Transfer.None)
                        {
                            g_param_spec_unref(handle);
                        }
                        // return the existing managed proxy
                        return((T)(object)target);
                    }
                }
            }

            // if we get here, that means that there wasn't a viable existing
            // proxy, so we need to create a new managed instance

            // get the exact type of the object
            ptr = Marshal.ReadIntPtr(handle);
            var gtype = Marshal.PtrToStructure <GType>(ptr);
            var type  = GType.TypeOf(gtype);

            return((T)Activator.CreateInstance(type, handle, ownership));
        }
Esempio n. 6
0
        /// <summary>
        /// Invokes the closure, i.e. executes the callback represented by this closure.
        /// </summary>
        /// <param name="paramValues">
        /// an array of #GValues holding the arguments on which to
        /// invoke the callback of this closure
        /// </param>
        /// <returns>The return value of the closure invocation</returns>
        public T Invoke <T>(params object[] paramValues)
        {
            AssertNotDisposed();
            if (paramValues == null)
            {
                throw new ArgumentNullException(nameof(paramValues));
            }

            var returnValue = new Value(GType.TypeOf <T>());
            var values      = paramValues.Select(p => new Value(p?.GetType(), p)).ToArray();

            g_closure_invoke(handle, ref returnValue, (uint)values.Length, values, IntPtr.Zero);
            foreach (var v in values)
            {
                v.Unset();
            }

            var ret = returnValue.Get();

            returnValue.Unset();

            return((T)ret);
        }
Esempio n. 7
0
        public void TestTypeOfUnregisteredManagedType()
        {
            // This tests that if a GType is received from unmanged code
            // that has never been initialized in managed code.

            const string testName = "TestTypeOfUnregisteredManagedType";

            // first we register the type in unmanged code and make sure that
            // it throws an exception because there is no matching type in
            // managed code.

            var gtype = GetDummyGType();

            // TODO: Need more specific type of exception
            Assert.That(() => GType.TypeOf(gtype), Throws.Exception);

            // then we dynamically generate a matching managed type

            var asmBuilder = AssemblyBuilder.DefineDynamicAssembly(
                new AssemblyName(testName + "_assembly"),
                AssemblyBuilderAccess.Run);
            var modBuilder  = asmBuilder.DefineDynamicModule(testName + "_module");
            var typeBuilder = modBuilder.DefineType(testName + "_type",
                                                    TypeAttributes.Public);
            var gtypeAttribute = typeof(GTypeAttribute);

            typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                               gtypeAttribute.GetConstructors().Single(),
                                               new object [] { dummyTypeName },
                                               new [] {
                gtypeAttribute.GetProperty("IsProxyForUnmanagedType"),
            },
                                               new object [] {
                true,     // IsProxyForUnmanagedType
            }));
            var gtypeStructAttribute = typeof(GTypeStructAttribute);

            typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(
                                               gtypeStructAttribute.GetConstructors().Single(),
                                               new object[] { typeof(ObjectClass) },
                                               new FieldInfo[0], new object[0]));
            var getTypeMethod = typeBuilder.DefineMethod("getGType",
                                                         MethodAttributes.Private | MethodAttributes.Static);

            getTypeMethod.SetReturnType(typeof(GType));
            var getDummyGType = GetType().GetMethod(nameof(GetDummyGType));
            var generator     = getTypeMethod.GetILGenerator();

            generator.Emit(OpCodes.Call, getDummyGType);
            generator.Emit(OpCodes.Ret);

            var expectedType = typeBuilder.CreateType();

            // and try the test again

            var actualType = GType.TypeOf(gtype);

            Assert.That(actualType, Is.EqualTo(expectedType));

            Utility.AssertNoGLibLog();
        }