module NanoVG.Internal.FFIHelpers
(withCString
,useAsCStringLen'
,useAsPtr
,zero
,null
,bitMask
) where
import Data.Bits ((.|.))
import Data.ByteString hiding (null)
import qualified Data.Set as S
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Foreign.C.String (CString)
import Foreign.C.Types
import Foreign.Ptr
import Prelude hiding (null)
withCString :: T.Text -> (CString -> IO b) -> IO b
withCString t = useAsCString (T.encodeUtf8 t)
useAsCStringLen' :: ByteString -> ((Ptr CUChar,CInt) -> IO a) -> IO a
useAsCStringLen' bs f = useAsCStringLen bs (\(ptr,len) -> f (castPtr ptr,fromIntegral len))
useAsPtr :: ByteString -> (Ptr CUChar -> IO a) -> IO a
useAsPtr bs f = useAsCStringLen' bs (f . fst)
zero :: Num a => (a -> b) -> b
zero f = f 0
null :: (Ptr a -> b) -> b
null f = f nullPtr
bitMask :: Enum a => S.Set a -> CInt
bitMask = S.fold (.|.) 0 . S.map (fromIntegral . fromEnum)