{-# LANGUAGE DeriveDataTypeable #-}
module SDL.Raw.Error (
  -- * Error Handling
  SDLError(..),
  throwError,

  -- * Manual Error Handling
  clearError,
  getError,
  setError
) where

import Control.Exception
import Control.Monad.Catch
import Control.Monad.IO.Class
import Data.Typeable
import Foreign.C.String
import Foreign.C.Types

-- | Note: the 'CString' is only valid until the next SDL function call. If you
-- need to preserve the error message, make a copy of it.
newtype SDLError = SDLError CString
  deriving (SDLError -> SDLError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SDLError -> SDLError -> Bool
$c/= :: SDLError -> SDLError -> Bool
== :: SDLError -> SDLError -> Bool
$c== :: SDLError -> SDLError -> Bool
Eq, Int -> SDLError -> ShowS
[SDLError] -> ShowS
SDLError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SDLError] -> ShowS
$cshowList :: [SDLError] -> ShowS
show :: SDLError -> String
$cshow :: SDLError -> String
showsPrec :: Int -> SDLError -> ShowS
$cshowsPrec :: Int -> SDLError -> ShowS
Show, Typeable)

instance Exception SDLError

foreign import ccall "SDL.h SDL_ClearError" clearErrorFFI :: IO ()
foreign import ccall "SDL.h SDL_GetError" getErrorFFI :: IO CString
foreign import ccall "sdlhelper.c SDLHelper_SetError" setErrorFFI :: CString -> IO CInt

throwError :: (MonadThrow m, MonadIO m) => m ()
throwError :: forall (m :: Type -> Type). (MonadThrow m, MonadIO m) => m ()
throwError = forall (m :: Type -> Type). MonadIO m => m CString
getError forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: Type -> Type) e a.
(MonadThrow m, Exception e) =>
e -> m a
throwM forall b c a. (b -> c) -> (a -> b) -> a -> c
. CString -> SDLError
SDLError

clearError :: MonadIO m => m ()
clearError :: forall (m :: Type -> Type). MonadIO m => m ()
clearError = forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO IO ()
clearErrorFFI
{-# INLINE clearError #-}

getError :: MonadIO m => m CString
getError :: forall (m :: Type -> Type). MonadIO m => m CString
getError = forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO IO CString
getErrorFFI
{-# INLINE getError #-}

setError :: MonadIO m => CString -> m CInt
setError :: forall (m :: Type -> Type). MonadIO m => CString -> m CInt
setError CString
v1 = forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ CString -> IO CInt
setErrorFFI CString
v1
{-# INLINE setError #-}