module Codec.Archive.Common ( actFree
                            , actFreeCallback
                            ) where

import           Codec.Archive.Foreign
import           Control.Monad.IO.Class (MonadIO (..))
import           Foreign.Ptr

-- | Do something with an 'Archive' and then free it
actFree :: MonadIO m
        => (Ptr Archive -> m a)
        -> Ptr Archive
        -> m a
actFree :: (Ptr Archive -> m a) -> Ptr Archive -> m a
actFree fact :: Ptr Archive -> m a
fact a :: Ptr Archive
a = Ptr Archive -> m a
fact Ptr Archive
a m a -> m ArchiveResult -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* IO ArchiveResult -> m ArchiveResult
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Ptr Archive -> IO ArchiveResult
archiveFree Ptr Archive
a)

actFreeCallback :: MonadIO m
                => (Ptr Archive -> m a)
                -> (Ptr Archive, IO ()) -- ^ 'Ptr' to an 'Archive' and an 'IO' action to clean up when done
                -> m a
actFreeCallback :: (Ptr Archive -> m a) -> (Ptr Archive, IO ()) -> m a
actFreeCallback fact :: Ptr Archive -> m a
fact (a :: Ptr Archive
a, freeAct :: IO ()
freeAct) = Ptr Archive -> m a
fact Ptr Archive
a m a -> m ArchiveResult -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* IO ArchiveResult -> m ArchiveResult
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Ptr Archive -> IO ArchiveResult
archiveFree Ptr Archive
a) m a -> m () -> m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
freeAct