module Data.StateRef.Instances
( IORef
, MVar
, MonadIO(..)
, STRef
, ST
, RealWorld
, ForeignPtr
#ifdef useSTM
, module Data.StateRef.Instances.STM
#endif
, module Data.StateRef.Instances.Undecidable
) where
#ifdef useSTM
import Data.StateRef.Instances.STM
#endif
import Data.StateRef.Classes
import Data.StateRef.Instances.Undecidable
import Data.IORef
import Control.Concurrent.MVar
import Control.Monad.Trans
import Control.Monad.ST
import Data.STRef
import qualified Control.Monad.ST.Lazy
import qualified Data.STRef.Lazy
import Foreign.Storable
import Foreign.ForeignPtr
instance Monad m => NewRef (IO a) m a where
newRef ro = return (return ro)
instance MonadIO m => ReadRef (IO a) m a where
readRef = liftIO
instance Monad m => NewRef (ST s a) m a where
newRef ro = return (return ro)
instance ReadRef (ST s a) (ST s) a where
readRef = id
instance MonadIO m => ReadRef (ST RealWorld a) m a where
readRef = liftIO . stToIO
instance DefaultStateRef (IORef a) IO a
instance MonadIO m => NewRef (IORef a) m a where
newRef = liftIO . newIORef
instance MonadIO m => ReadRef (IORef a) m a where
readRef = liftIO . readIORef
instance MonadIO m => WriteRef (IORef a) m a where
writeRef r = liftIO . writeIORef r
instance MonadIO m => ModifyRef (IORef a) m a where
atomicModifyRef r = liftIO . atomicModifyIORef r
modifyRef r = liftIO . modifyIORef r
instance DefaultStateRef (STRef s a) (ST s) a
instance NewRef (STRef s a) (ST s) a where
newRef = newSTRef
instance ReadRef (STRef s a) (ST s) a where
readRef = readSTRef
instance WriteRef (STRef s a) (ST s) a where
writeRef = writeSTRef
instance ModifyRef (STRef s a) (ST s) a where
instance NewRef (STRef RealWorld a) IO a where
newRef = stToIO . newRef
instance ReadRef (STRef RealWorld a) IO a where
readRef = stToIO . readRef
instance WriteRef (STRef RealWorld a) IO a where
writeRef r = stToIO . writeRef r
instance ModifyRef (STRef RealWorld a) IO a where
modifyRef r = stToIO . modifyRef r
instance DefaultStateRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a
instance NewRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
newRef = Data.STRef.Lazy.newSTRef
instance ReadRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
readRef = Data.STRef.Lazy.readSTRef
instance WriteRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
writeRef = Data.STRef.Lazy.writeSTRef
instance ModifyRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
instance MonadIO m => NewRef (MVar a) m (Maybe a) where
newRef Nothing = liftIO newEmptyMVar
newRef (Just x) = liftIO (newMVar x)
instance (Storable a, MonadIO m) => NewRef (ForeignPtr a) m a where
newRef val = liftIO $ do
ptr <- mallocForeignPtr
withForeignPtr ptr (\ptr -> poke ptr val)
return ptr
instance (Storable a, MonadIO m) => ReadRef (ForeignPtr a) m a where
readRef ptr = liftIO (withForeignPtr ptr peek)
instance (Storable a, MonadIO m) => WriteRef (ForeignPtr a) m a where
writeRef ptr val = liftIO (withForeignPtr ptr (\ptr -> poke ptr val))
instance (Storable a, MonadIO m) => ModifyRef (ForeignPtr a) m a