{-# LANGUAGE CPP #-}
module Database.PostgreSQL.LibPQ.Marshal where

import Foreign (Ptr, Storable, allocaArray, nullPtr, pokeArray)

unsafeWithArray :: Storable a => Int -> [a] -> (Ptr a -> IO b) -> IO b
unsafeWithArray :: Int -> [a] -> (Ptr a -> IO b) -> IO b
unsafeWithArray Int
len [a]
vals Ptr a -> IO b
f =
#if 0
  if len /= length vals then error "unsafeWithArray: len mismatch" else
#endif
  Int -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
len ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
      Ptr a -> [a] -> IO ()
forall a. Storable a => Ptr a -> [a] -> IO ()
pokeArray Ptr a
ptr [a]
vals
      Ptr a -> IO b
f Ptr a
ptr

-- | Like maybe with but takes an int. Usable with 'withArrayLen'.
-- In 'Nothing' case uses 0 and 'nullPtr'.
maybeWithInt :: (     a -> (Int -> Ptr b -> IO c) -> IO c)
          -> (Maybe a -> (Int -> Ptr b -> IO c) -> IO c)
maybeWithInt :: (a -> (Int -> Ptr b -> IO c) -> IO c)
-> Maybe a -> (Int -> Ptr b -> IO c) -> IO c
maybeWithInt = ((Int -> Ptr b -> IO c) -> IO c)
-> (a -> (Int -> Ptr b -> IO c) -> IO c)
-> Maybe a
-> (Int -> Ptr b -> IO c)
-> IO c
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (\Int -> Ptr b -> IO c
f -> Int -> Ptr b -> IO c
f Int
0 Ptr b
forall a. Ptr a
nullPtr)