-- |
-- Module     : Simulation.Aivika.Ref.Base
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines a light-weight and more fast version of an updatable reference
-- that depends on the event queue but that doesn't supply with the signal notification.
--
module Simulation.Aivika.Ref.Base
       (Ref,
        newRef,
        readRef,
        writeRef,
        modifyRef) where

import Data.IORef
import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Event

-- | The 'Ref' type represents a mutable variable similar to the 'IORef' variable 
-- but only dependent on the event queue, which allows synchronizing the reference
-- with the model explicitly through the 'Event' monad.
newtype Ref a = 
  Ref { Ref a -> IORef a
refValue :: IORef a }

-- | Create a new reference.
newRef :: a -> Simulation (Ref a)
newRef :: a -> Simulation (Ref a)
newRef a
a =
  do IORef a
x <- IO (IORef a) -> Simulation (IORef a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef a) -> Simulation (IORef a))
-> IO (IORef a) -> Simulation (IORef a)
forall a b. (a -> b) -> a -> b
$ a -> IO (IORef a)
forall a. a -> IO (IORef a)
newIORef a
a
     Ref a -> Simulation (Ref a)
forall (m :: * -> *) a. Monad m => a -> m a
return Ref :: forall a. IORef a -> Ref a
Ref { refValue :: IORef a
refValue = IORef a
x }
     
-- | Read the value of a reference.
readRef :: Ref a -> Event a
readRef :: Ref a -> Event a
readRef Ref a
r = (Point -> IO a) -> Event a
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO a) -> Event a) -> (Point -> IO a) -> Event a
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef a -> IO a
forall a. IORef a -> IO a
readIORef (Ref a -> IORef a
forall a. Ref a -> IORef a
refValue Ref a
r)

-- | Write a new value into the reference.
writeRef :: Ref a -> a -> Event ()
writeRef :: Ref a -> a -> Event ()
writeRef Ref a
r a
a = (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p -> 
  a
a a -> IO () -> IO ()
`seq` IORef a -> a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Ref a -> IORef a
forall a. Ref a -> IORef a
refValue Ref a
r) a
a

-- | Mutate the contents of the reference.
modifyRef :: Ref a -> (a -> a) -> Event ()
modifyRef :: Ref a -> (a -> a) -> Event ()
modifyRef Ref a
r a -> a
f = (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p -> 
  do a
a <- IORef a -> IO a
forall a. IORef a -> IO a
readIORef (Ref a -> IORef a
forall a. Ref a -> IORef a
refValue Ref a
r)
     let b :: a
b = a -> a
f a
a
     a
b a -> IO () -> IO ()
`seq` IORef a -> a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Ref a -> IORef a
forall a. Ref a -> IORef a
refValue Ref a
r) a
b