1 {-# OPTIONS -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/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Marshalling support: Handling of common error conditions
14 -----------------------------------------------------------------------------
16 module Foreign.Marshal.Error (
18 -- throw an exception on specific return values
20 throwIf, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO a
21 throwIf_, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
22 throwIfNeg, -- :: (Ord a, Num a)
23 -- => (a -> String) -> IO a -> IO a
24 throwIfNeg_, -- :: (Ord a, Num a)
25 -- => (a -> String) -> IO a -> IO ()
26 throwIfNull, -- :: String -> IO (Ptr a) -> IO (Ptr a)
28 -- discard return value
35 #ifdef __GLASGOW_HASKELL__
44 -- guard an IO operation and throw an exception if the result meets the given
47 -- * the second argument computes an error message from the result of the IO
50 throwIf :: (a -> Bool) -> (a -> String) -> IO a -> IO a
51 throwIf pred msgfct act =
54 (if pred res then ioError . userError . msgfct else return) res
56 -- like `throwIf', but discarding the result
58 throwIf_ :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
59 throwIf_ pred msgfct act = void $ throwIf pred msgfct act
61 -- guards against negative result values
63 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
64 throwIfNeg = throwIf (< 0)
66 -- like `throwIfNeg', but discarding the result
68 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
69 throwIfNeg_ = throwIf_ (< 0)
71 -- guards against null pointers
73 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
74 throwIfNull = throwIf (== nullPtr) . const
76 -- discard the return value of an IO action
79 void act = act >> return ()