1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.Marshal.Error
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Routines for testing return values and raising a 'userError' exception
13 -- in case of values indicating an error state.
15 -----------------------------------------------------------------------------
17 module Foreign.Marshal.Error (
18 throwIf, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO a
19 throwIf_, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
20 throwIfNeg, -- :: (Ord a, Num a)
21 -- => (a -> String) -> IO a -> IO a
22 throwIfNeg_, -- :: (Ord a, Num a)
23 -- => (a -> String) -> IO a -> IO ()
24 throwIfNull, -- :: String -> IO (Ptr a) -> IO (Ptr a)
26 -- Discard return value
33 #ifdef __GLASGOW_HASKELL__
36 import System.IO.Error
46 -- |Execute an 'IO' action, throwing a 'userError' if the predicate yields
47 -- 'True' when applied to the result returned by the 'IO' action.
48 -- If no exception is raised, return the result of the computation.
50 throwIf :: (a -> Bool) -- ^ error condition on the result of the 'IO' action
51 -> (a -> String) -- ^ computes an error message from erroneous results
53 -> IO a -- ^ the 'IO' action to be executed
55 throwIf pred msgfct act =
58 (if pred res then ioError . userError . msgfct else return) res
60 -- |Like 'throwIf', but discarding the result
62 throwIf_ :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
63 throwIf_ pred msgfct act = void $ throwIf pred msgfct act
65 -- |Guards against negative result values
67 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
68 throwIfNeg = throwIf (< 0)
70 -- |Like 'throwIfNeg', but discarding the result
72 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
73 throwIfNeg_ = throwIf_ (< 0)
75 -- |Guards against null pointers
77 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
78 throwIfNull = throwIf (== nullPtr) . const
80 -- |Discard the return value of an 'IO' action
83 void act = act >> return ()