gameServer/Store.hs
changeset 3566 772a46ef8288
parent 3501 a3159a410e5c
child 3671 a94d1dc4a8d9
equal deleted inserted replaced
3565:bc3410104894 3566:772a46ef8288
    22 import Data.IORef
    22 import Data.IORef
    23 import Control.Monad
    23 import Control.Monad
    24 
    24 
    25 
    25 
    26 newtype ElemIndex = ElemIndex Int
    26 newtype ElemIndex = ElemIndex Int
    27     deriving (Eq, Show, Read)
    27     deriving (Eq, Show, Read, Ord)
    28 newtype MStore e = MStore (IORef (IntSet.IntSet, IntSet.IntSet, IOA.IOArray Int e))
    28 newtype MStore e = MStore (IORef (IntSet.IntSet, IntSet.IntSet, IOA.IOArray Int e))
    29 newtype IStore e = IStore (IntSet.IntSet, IA.Array Int e)
    29 newtype IStore e = IStore (IntSet.IntSet, IA.Array Int e)
    30 
    30 
    31 
    31 
    32 firstIndex :: ElemIndex
    32 firstIndex :: ElemIndex
   103 
   103 
   104 -- A way to use see MStore elements in pure code via IStore
   104 -- A way to use see MStore elements in pure code via IStore
   105 m2i :: MStore e -> IO (IStore e)
   105 m2i :: MStore e -> IO (IStore e)
   106 m2i (MStore ref) = do
   106 m2i (MStore ref) = do
   107     (a, _, c') <- readIORef ref 
   107     (a, _, c') <- readIORef ref 
   108     c <- IOA.unsafeFreeze c'
   108     c <- IOA.freeze c'
   109     return $ IStore (a, c)
   109     return $ IStore (a, c)
   110 
   110 
   111 
   111 
   112 withIStore :: MStore e -> (IStore e -> a) -> IO a
   112 withIStore :: MStore e -> (IStore e -> a) -> IO a
   113 withIStore m f = liftM f (m2i m)
   113 withIStore m f = liftM f (m2i m)