Skip to content

Instantly share code, notes, and snippets.

@chpatrick
Last active August 29, 2015 14:22
Show Gist options
  • Save chpatrick/d4f666e32d9ae7f39dd3 to your computer and use it in GitHub Desktop.
Save chpatrick/d4f666e32d9ae7f39dd3 to your computer and use it in GitHub Desktop.
Foreign dependency graph
{-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies #-}
import Foreign
import System.IO.Unsafe
type family NodeType n
class Ref n where
withRef :: n -> (Ptr (NodeType n) -> IO b) -> IO b
type instance NodeType (ForeignPtr a) = a
instance Ref (ForeignPtr a) where
withRef = withForeignPtr
data Child p a = Child p (ForeignPtr a)
type instance NodeType (Child p a) = a
instance Ref p => Ref (Child p a) where
withRef (Child p fp) f
= withRef p $ \_ -> withRef fp f
unsafeWithRef :: Ref n => n -> (Ptr (NodeType n) -> IO b) -> b
unsafeWithRef r f = unsafePerformIO $ withRef r f
data CXIndexImpl
type instance NodeType Index = CXIndexImpl
newtype Index = Index (ForeignPtr CXIndexImpl)
deriving Ref
type instance NodeType TranslationUnit = CXTranslationUnitImpl
data CXTranslationUnitImpl
newtype TranslationUnit = TranslationUnit (Child Index CXTranslationUnitImpl)
deriving Ref
type instance NodeType Cursor = CXCursor
data CXCursor
newtype Cursor = Cursor (Child TranslationUnit CXCursor)
deriving Ref
type instance NodeType SourceLocation = CXSourceLocation
data CXSourceLocation
newtype SourceLocation = SourceLocation (Child TranslationUnit CXSourceLocation)
deriving Ref
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment