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 -- $Id: Error.hs,v 1.2 2001/07/03 11:37:50 simonmar Exp $
14 -- Marshalling support: Handling of common error conditions
16 -----------------------------------------------------------------------------
18 module Foreign.Marshal.Error (
20 -- throw an exception on specific return values
22 throwIf, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO a
23 throwIf_, -- :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
24 throwIfNeg, -- :: (Ord a, Num a)
25 -- => (a -> String) -> IO a -> IO a
26 throwIfNeg_, -- :: (Ord a, Num a)
27 -- => (a -> String) -> IO a -> IO ()
28 throwIfNull, -- :: String -> IO (Ptr a) -> IO (Ptr a)
30 -- discard return value
37 #ifdef __GLASGOW_HASKELL__
46 -- guard an IO operation and throw an exception if the result meets the given
49 -- * the second argument computes an error message from the result of the IO
52 throwIf :: (a -> Bool) -> (a -> String) -> IO a -> IO a
53 throwIf pred msgfct act =
56 (if pred res then ioError . userError . msgfct else return) res
58 -- like `throwIf', but discarding the result
60 throwIf_ :: (a -> Bool) -> (a -> String) -> IO a -> IO ()
61 throwIf_ pred msgfct act = void $ throwIf pred msgfct act
63 -- guards against negative result values
65 throwIfNeg :: (Ord a, Num a) => (a -> String) -> IO a -> IO a
66 throwIfNeg = throwIf (< 0)
68 -- like `throwIfNeg', but discarding the result
70 throwIfNeg_ :: (Ord a, Num a) => (a -> String) -> IO a -> IO ()
71 throwIfNeg_ = throwIf_ (< 0)
73 -- guards against null pointers
75 throwIfNull :: String -> IO (Ptr a) -> IO (Ptr a)
76 throwIfNull = throwIf (== nullPtr) . const
78 -- discard the return value of an IO action
81 void act = act >> return ()