From: Duncan C. <dun...@us...> - 2005-02-27 19:42:15
|
Update of /cvsroot/gtk2hs/gtk2hs/glib/System/Glib In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32363/glib/System/Glib Modified Files: GObject.chs.pp Log Message: GObject.chs.pp: change mkDestructor into mkFunPtrDestructor which is a tad more abstract (hiding the use of IORefs). General.chs: change use of mkDestructor to mkFunPtrDestructor and no longer export mkDestructor. EntryCompletion.chs.pp, TreeSelection.chs.pp, TreeView.chs.pp: change use of mkDestructor to mkFunPtrDestructor. Signal.chs-boot2: Following the same pattern for GClosureNotify as for GDestroyNotify above define a local copy of mkFunPtrDestructor for use in signal connection functions. HookGenerator.hs: change use of mkDestructor to mkFunPtrDestructor following the same pattern as before. This reduces code duplication a bit (saves about 350 lines of code). Index: GObject.chs.pp =================================================================== RCS file: /cvsroot/gtk2hs/gtk2hs/glib/System/Glib/GObject.chs.pp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- GObject.chs.pp 8 Jan 2005 17:46:16 -0000 1.1 +++ GObject.chs.pp 27 Feb 2005 19:42:06 -0000 1.2 @@ -35,8 +35,9 @@ objectRef, objectUnref, makeNewGObject, + DestroyNotify, + mkFunPtrDestructor, GWeakNotify, - mkDestructor, objectWeakref, objectWeakunref ) where @@ -84,14 +85,9 @@ objectUnref :: Ptr a -> FinalizerPtr a objectUnref _ = object_unref' -#elif __GLASGOW_HASKELL__>=504 - -foreign import ccall unsafe "g_object_unref" - objectUnref :: Ptr a -> IO () - #else -foreign import ccall "g_object_unref" unsafe +foreign import ccall unsafe "g_object_unref" objectUnref :: Ptr a -> IO () #endif @@ -110,6 +106,25 @@ obj <- newForeignPtr objPtr (objectUnref objPtr) return $ constr obj +{#pointer GDestroyNotify as DestroyNotify#} + +foreign import ccall "wrapper" mkDestroyNotifyPtr :: IO () -> IO DestroyNotify + +-- | Many methods in classes derived from GObject take a callback function and +-- a destructor function which is called to free that callback function when +-- it is no longer required. This function constructs a DestroyNotify function +-- pointer which when called from C land will free the given Haskell function +-- pointer (and itself). +mkFunPtrDestructor :: FunPtr a -> IO DestroyNotify +mkFunPtrDestructor hPtr = do + dRef <- newIORef nullFunPtr + dPtr <- mkDestroyNotifyPtr $ do + freeHaskellFunPtr hPtr + dPtr <- readIORef dRef + freeHaskellFunPtr dPtr + writeIORef dRef dPtr + return dPtr + {#pointer GWeakNotify#} foreign import ccall "wrapper" mkDestructor :: IO () -> IO GWeakNotify |