Example #1
0
        static IntPtr NewObject(uint sizeofClosure, GISharp.GObject.Object @object)
        {
            if (@object == null)
            {
                throw new ArgumentNullException(nameof(@object));
            }
            var ret = g_closure_new_object(sizeofClosure, @object.Handle);

            return(ret);
        }
Example #2
0
 static void UnmanagedNetworkChanged(IntPtr monitorPtr, bool available)
 {
     try {
         var monitor = (INetworkMonitor)Object.GetInstance(monitorPtr, Transfer.None);
         monitor.OnNetworkChanged(available);
     }
     catch (Exception ex) {
         ex.LogUnhandledException();
     }
 }
Example #3
0
 static void UnmanagedCanReachAsyncFinish(IntPtr monitorPtr, IntPtr result, ref IntPtr errorPtr)
 {
     try {
         var monitor = (INetworkMonitor)Object.GetInstance(monitorPtr, Transfer.None);
         monitor.CanReachFinish(result);
     } catch (GErrorException ex) {
         GMarshal.PropagateError(errorPtr, ex.Error);
     }
     catch (Exception ex) {
         // FIXME: convert managed exception to GError
         ex.LogUnhandledException();
     }
 }
Example #4
0
 static void UnmanagedCanReachAsync(IntPtr monitorPtr, IntPtr connectablePtr, IntPtr cancellablePtr, Action <IntPtr, IntPtr, IntPtr> callback, IntPtr userData)
 {
     try {
         var             monitor         = (INetworkMonitor)Object.GetInstance(monitorPtr, Transfer.None);
         Action <IntPtr> managedCallback = (result) => {
             callback(monitorPtr, result, userData);
         };
         monitor.CanReachAsync(connectablePtr, cancellablePtr, managedCallback);
     }
     catch (Exception ex) {
         ex.LogUnhandledException();
     }
 }
Example #5
0
        public static Object New(GType objectType, params object[] parameters)
        {
            IntPtr errorPtr;
            var    ret_ = g_initable_newv(objectType, 0, IntPtr.Zero, IntPtr.Zero, out errorPtr);

            if (errorPtr != IntPtr.Zero)
            {
                var error = Opaque.GetInstance <Error> (errorPtr, Transfer.Full);
                throw new GErrorException(error);
            }
            var ret = Object.GetInstance(ret_, Transfer.Full);

            return(ret);
        }
Example #6
0
 static bool UnmanagedInit(IntPtr initablePtr, IntPtr cancellablePtr, ref IntPtr errorPtr)
 {
     try {
         var initable = (IInitable)Object.GetInstance(initablePtr, Transfer.None);
         var ret      = initable.Init(cancellablePtr);
         return(ret);
     }
     catch (GErrorException ex) {
         GMarshal.PropagateError(errorPtr, ex.Error);
     }
     catch (Exception ex) {
         // FIXME: we should convert managed exception to GError
         ex.LogUnhandledException();
     }
     return(false);
 }
Example #7
0
 static bool UnmanagedCanReach(IntPtr monitorPtr, IntPtr connectablePtr, IntPtr cancellablePtr, ref IntPtr errorPtr)
 {
     try {
         var monitor = (INetworkMonitor)Object.GetInstance(monitorPtr, Transfer.None);
         var ret     = monitor.CanReach(connectablePtr, cancellablePtr);
         return(ret);
     }
     catch (GErrorException ex) {
         GMarshal.PropagateError(errorPtr, ex.Error);
     }
     catch (Exception ex) {
         // FIXME: convert managed exception to GError
         ex.LogUnhandledException();
     }
     return(false);
 }
Example #8
0
        public void TestStopEmission()
        {
            bool stopEmission  = false;
            int  handler1Count = 0;
            int  handler2Count = 0;

            using (var pspec = new ParamSpecBoolean("test-param", "test-param", "test-param",
                                                    false, ParamFlags.Readwrite | ParamFlags.StaticStrings))
                using (var obj = new Object()) {
                    var id = Signal.TryLookup("notify", GType.Object);
                    Assume.That(id, Is.Not.EqualTo(0));

                    Object.NotifyEventHandler handler1 = (s, e) => {
                        handler1Count++;
                        if (stopEmission)
                        {
                            obj.StopEmission(id);
                        }
                    };

                    Object.NotifyEventHandler handler2 = (s, e) => handler2Count++;

                    obj.Notify += handler1;
                    obj.Notify += handler2;

                    // make sure our callbacks are working
                    obj.Emit(id, 0, pspec);
                    Assume.That(handler1Count, Is.EqualTo(1));
                    Assume.That(handler2Count, Is.EqualTo(1));

                    // now try to stop the emission
                    stopEmission = true;
                    obj.Emit(id, 0, pspec);

                    Assert.That(handler1Count, Is.EqualTo(2));
                    Assert.That(handler2Count, Is.EqualTo(1));
                }

            Utility.AssertNoGLibLog();
        }
Example #9
0
        public void TestToggleRef()
        {
            WeakReference weakRef = null;

            new Action(() => weakRef = new WeakReference(new Object())).Invoke();

            // first make sure our testing method is sane and Object is really GC'ed
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assume.That(weakRef.IsAlive, Is.False);

            // Now for the actual test.

            IntPtr handle = IntPtr.Zero;

            new Action(() => {
                var o   = new Object();
                weakRef = new WeakReference(o);
                // Simulate unmanaged code taking a reference. This should trigger
                // the toggle reference which prevents the object from being GC'ed
                handle = o.Handle;
                g_object_ref(handle);
            }).Invoke();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assert.That(weakRef.IsAlive, Is.True);

            // Simulates unmanaged code releasing the last reference. This should
            // free the GCHandle.
            g_object_unref(handle);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assert.That(weakRef.IsAlive, Is.False);

            Utility.AssertNoGLibLog();
        }
Example #10
0
        public void TestReferences()
        {
            using (var o1 = new Object()) {
                var handle = o1.Handle;

                // getting an object that already exists should return that object
                var o2 = Object.GetInstance(handle, Transfer.None);
                try {
                    Assert.That(ReferenceEquals(o1, o2), Is.True);

                    // Simulate unmanaged code taking a reference so that the handle is
                    // not freed when o1 is disposed.
                    g_object_ref(handle);

                    // After an object is disposed we should get a new object rather
                    // than the disposed object.
                    o1.Dispose();

                    // Normally, we would not dispose an object if there is a possibility
                    // that it could be used again because it will loose any state that
                    // is stored in the managed object. Instead, a GCHandle will keep
                    // the object alive as long as unmanaged code has a reference to the
                    // object.

                    // Transfer.All means the new object takes ownership of the reference
                    // from the manual call to g_object_ref(), so we don't need to call
                    // g_object_unref() manually.
                    o2 = Object.GetInstance(handle, Transfer.Full);
                    Assert.That(ReferenceEquals(o1, o2), Is.False);
                }
                finally {
                    o2.Dispose();
                }
            }

            Utility.AssertNoGLibLog();
        }
Example #11
0
 /// <summary>
 /// A variant of g_closure_new_simple() which stores @object in the
 /// @data field of the closure and calls g_object_watch_closure() on
 /// @object and the created closure. This function is mainly useful
 /// when implementing new types of closures.
 /// </summary>
 /// <param name="object">
 /// a #GObject pointer to store in the @data field of the newly
 ///  allocated #GClosure
 /// </param>
 /// <returns>
 /// a newly allocated #GClosure
 /// </returns>
 public Closure(Func <object[], object> callback, GISharp.GObject.Object @object)
     : this(NewObject((uint)Marshal.SizeOf <ManagedClosure> (), @object), Transfer.None)
 {
     SetCallback(callback, ManagedClosureFuncCallback);
 }