module Data.Text.Glyphize.Oom where

import Control.Exception
import Foreign.Ptr (nullPtr, Ptr)

-- | Indicates that Harfbuzz has ran out of memory during a computation.
-- Should be extremely rare!
data HarfbuzzError = OutOfMemory deriving (Int -> HarfbuzzError -> ShowS
[HarfbuzzError] -> ShowS
HarfbuzzError -> String
(Int -> HarfbuzzError -> ShowS)
-> (HarfbuzzError -> String)
-> ([HarfbuzzError] -> ShowS)
-> Show HarfbuzzError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HarfbuzzError] -> ShowS
$cshowList :: [HarfbuzzError] -> ShowS
show :: HarfbuzzError -> String
$cshow :: HarfbuzzError -> String
showsPrec :: Int -> HarfbuzzError -> ShowS
$cshowsPrec :: Int -> HarfbuzzError -> ShowS
Show)
instance Exception HarfbuzzError

throwFalse :: IO Bool -> IO ()
throwFalse :: IO Bool -> IO ()
throwFalse cb :: IO Bool
cb = do
    Bool
ret <- IO Bool
cb
    if Bool
ret then () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return () else HarfbuzzError -> IO ()
forall e a. Exception e => e -> IO a
throwIO HarfbuzzError
OutOfMemory

throwNull :: IO (Ptr a) -> IO (Ptr a)
throwNull :: IO (Ptr a) -> IO (Ptr a)
throwNull cb :: IO (Ptr a)
cb = do
    Ptr a
ptr <- IO (Ptr a)
cb
    if Ptr a
ptr Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
forall a. Ptr a
nullPtr then HarfbuzzError -> IO (Ptr a)
forall e a. Exception e => e -> IO a
throwIO HarfbuzzError
OutOfMemory else Ptr a -> IO (Ptr a)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr a
ptr