{-# language CPP #-}
module Vulkan.CStruct
( ToCStruct(..)
, FromCStruct(..)
) where
import Control.Exception.Base ( bracket )
import Foreign.Marshal.Alloc ( allocaBytesAligned )
import Foreign.Marshal.Alloc ( callocBytes )
import Foreign.Marshal.Alloc ( free )
import Foreign.Ptr ( Ptr )
class ToCStruct a where
withCStruct :: a -> (Ptr a -> IO b) -> IO b
withCStruct a
x Ptr a -> IO b
f = forall a b. Int -> Int -> (Ptr a -> IO b) -> IO b
allocaBytesAligned (forall a. ToCStruct a => Int
cStructSize @a) (forall a. ToCStruct a => Int
cStructAlignment @a)
forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> forall a b. ToCStruct a => Ptr a -> a -> IO b -> IO b
pokeCStruct Ptr a
p a
x (Ptr a -> IO b
f Ptr a
p)
pokeCStruct :: Ptr a -> a -> IO b -> IO b
withZeroCStruct :: (Ptr a -> IO b) -> IO b
withZeroCStruct Ptr a -> IO b
f =
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (forall a. Int -> IO (Ptr a)
callocBytes @a (forall a. ToCStruct a => Int
cStructSize @a)) forall a. Ptr a -> IO ()
free forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> forall a b. ToCStruct a => Ptr a -> IO b -> IO b
pokeZeroCStruct Ptr a
p (Ptr a -> IO b
f Ptr a
p)
pokeZeroCStruct :: Ptr a -> IO b -> IO b
cStructSize :: Int
cStructAlignment :: Int
class FromCStruct a where
peekCStruct :: Ptr a -> IO a