{-# 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 x :: a
x f :: Ptr a -> IO b
f = Int -> Int -> (Ptr a -> IO b) -> IO b
forall a b. Int -> Int -> (Ptr a -> IO b) -> IO b
allocaBytesAligned (ToCStruct a => Int
forall a. ToCStruct a => Int
cStructSize @a) (ToCStruct a => Int
forall a. ToCStruct a => Int
cStructAlignment @a)
((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \p :: Ptr a
p -> Ptr a -> a -> IO b -> IO b
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 f :: Ptr a -> IO b
f =
IO (Ptr a) -> (Ptr a -> IO ()) -> (Ptr a -> IO b) -> IO b
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (Int -> IO (Ptr a)
forall a. Int -> IO (Ptr a)
callocBytes @a (ToCStruct a => Int
forall a. ToCStruct a => Int
cStructSize @a)) Ptr a -> IO ()
forall a. Ptr a -> IO ()
free ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \p :: Ptr a
p -> Ptr a -> IO b -> IO b
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