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.Types
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 HasRef m => NewRef (Ref m a) m a where
newReference = newRef
instance ReadRef (Ref m a) m a where
readReference (Ref sr) = readReference sr
instance WriteRef (Ref m a) m a where
writeReference (Ref sr) = writeReference sr
instance ModifyRef (Ref m a) m a where
atomicModifyReference (Ref sr) = atomicModifyReference sr
modifyReference (Ref sr) = modifyReference sr
instance Monad m => NewRef (IO a) m a where
newReference ro = return (return ro)
instance MonadIO m => ReadRef (IO a) m a where
readReference = liftIO
instance Monad m => NewRef (ST s a) m a where
newReference ro = return (return ro)
instance ReadRef (ST s a) (ST s) a where
readReference = id
instance MonadIO m => ReadRef (ST RealWorld a) m a where
readReference = liftIO . stToIO
instance HasRef IO where
newRef x = do
sr <- newIORef x
return (Ref sr)
instance MonadIO m => NewRef (IORef a) m a where
newReference = liftIO . newIORef
instance MonadIO m => ReadRef (IORef a) m a where
readReference = liftIO . readIORef
instance MonadIO m => WriteRef (IORef a) m a where
writeReference r = liftIO . writeIORef r
instance MonadIO m => ModifyRef (IORef a) m a where
atomicModifyReference r = liftIO . atomicModifyIORef r
modifyReference r = liftIO . modifyIORef r
instance HasRef (ST s) where
newRef x = do
sr <- newSTRef x
return (Ref sr)
instance NewRef (STRef s a) (ST s) a where
newReference = newSTRef
instance ReadRef (STRef s a) (ST s) a where
readReference = readSTRef
instance WriteRef (STRef s a) (ST s) a where
writeReference = writeSTRef
instance ModifyRef (STRef s a) (ST s) a where
atomicModifyReference = defaultAtomicModifyReference
modifyReference = defaultModifyReference
instance NewRef (STRef RealWorld a) IO a where
newReference = stToIO . newReference
instance ReadRef (STRef RealWorld a) IO a where
readReference = stToIO . readReference
instance WriteRef (STRef RealWorld a) IO a where
writeReference r = stToIO . writeReference r
instance ModifyRef (STRef RealWorld a) IO a where
modifyReference r = stToIO . modifyReference r
atomicModifyReference r = stToIO . atomicModifyReference r
instance HasRef (Control.Monad.ST.Lazy.ST s) where
newRef x = do
sr <- Data.STRef.Lazy.newSTRef x
return (Ref sr)
instance NewRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
newReference = Data.STRef.Lazy.newSTRef
instance ReadRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
readReference = Data.STRef.Lazy.readSTRef
instance WriteRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
writeReference = Data.STRef.Lazy.writeSTRef
instance ModifyRef (STRef s a) (Control.Monad.ST.Lazy.ST s) a where
atomicModifyReference = defaultAtomicModifyReference
modifyReference = defaultModifyReference
instance MonadIO m => NewRef (MVar a) m (Maybe a) where
newReference Nothing = liftIO newEmptyMVar
newReference (Just x) = liftIO (newMVar x)
instance (Storable a, MonadIO m) => NewRef (ForeignPtr a) m a where
newReference val = liftIO $ do
ptr <- mallocForeignPtr
withForeignPtr ptr (\ptr -> poke ptr val)
return ptr
instance (Storable a, MonadIO m) => ReadRef (ForeignPtr a) m a where
readReference ptr = liftIO (withForeignPtr ptr peek)
instance (Storable a, MonadIO m) => WriteRef (ForeignPtr a) m a where
writeReference ptr val = liftIO (withForeignPtr ptr (\ptr -> poke ptr val))
instance (Storable a, MonadIO m) => ModifyRef (ForeignPtr a) m a where
atomicModifyReference = defaultAtomicModifyReference
modifyReference = defaultModifyReference