module Data.TCache.TMVar.Dynamic(
T.IResource(..)
,T.Resources(..)
,T.resources
,T.setCache
,T.refcache
,T.defaultCheck,T.readFileStrict
,I.IDynamic(..)
,T.Cache
,DynamicInterface (
toIDyn
,registerType
,fromIDyn
,unsafeFromIDyn
)
--,ofType
,I.Key(..)
,getTMVars,getTMVarsIO,withDResource, withDResources, withDSTMResources, getDResource, getDResources, deleteDResource, deleteDResources
,syncCache
,clearSyncCacheProc
, withResource, withResources, withSTMResources, getResource, getResources, deleteResource, deleteResources
)
where
import System.IO.Unsafe
import Data.Typeable
import qualified Data.TCache.TMVar as T
import Data.TCache.IDynamic as I
import Debug.Trace
import Control.Concurrent.STM(atomically,STM)
import Control.Concurrent.STM.TMVar
import Control.Concurrent(forkIO)
import Control.Exception(finally)
debug a b= trace b a
withDResource :: IDynamic->(Maybe IDynamic->IDynamic)->IO ()
withDResource = T.withResource
withDResources:: [IDynamic]->([Maybe IDynamic]->[IDynamic])->IO ()
withDResources = T.withResources
withDSTMResources :: [IDynamic]->([Maybe IDynamic]->T.Resources IDynamic x)->STM x
withDSTMResources = T.withSTMResources
getDResource :: IDynamic -> IO (Maybe IDynamic)
getDResource = T.getResource
getDResources :: [IDynamic] -> IO [Maybe IDynamic]
getDResources = T.getResources
getTMVars :: [IDynamic] -> STM [Maybe (TMVar IDynamic)]
getTMVars= T.getTMVars
getTMVarsIO :: [IDynamic] -> IO [TMVar IDynamic]
getTMVarsIO= T.getTMVarsIO
justGetDResources rs=do mrs <- getDResources rs
return $ map process $ zip mrs rs
where
process (Nothing, r) = error ("\""++T.keyResource r ++ "\" does not exist")
process (Just r', _) = r'
justGetDResource r= do [r']<- justGetDResources [r]
return r'
deleteDResource :: IDynamic -> IO ()
deleteDResource= T.deleteResource
deleteDResources :: [IDynamic] -> IO ()
deleteDResources= T.deleteResources
syncCache= T.syncCache (T.refcache :: T.Cache IDynamic)
clearSyncCacheProc= T.clearSyncCacheProc (T.refcache :: T.Cache IDynamic)
withResource ::(Typeable a, Typeable b, T.IResource a, T.IResource b) => a->(Maybe a->b)->IO ()
withResource r f= withResources [r] (\[mr]-> [f mr])
withResources::(Typeable a, Typeable b, T.IResource a, T.IResource b) => [a]->([Maybe a]->[b])->IO ()
withResources rs f= withDResources (map toIDyn rs) (\mrs-> f' mrs) where
f' = map toIDyn . f . map g
g Nothing= Nothing
g (Just x)= Just (fromIDyn x)
withSTMResources :: forall x.forall a.forall b.(Typeable a, Typeable b, T.IResource a, T.IResource b) => [a]
->([Maybe a]-> T.Resources b x)
-> STM x
withSTMResources rs f= withDSTMResources (map toIDyn rs) f' where
f' :: [Maybe IDynamic]-> T.Resources IDynamic x
f' = h . f . map g
g (Just x)= Just $ fromIDyn x
g Nothing = Nothing
maybeDyn ( Just x) = Just $ toIDyn x
maybeDyn Nothing = Nothing
h (T.Resources a d r)= T.Resources (map toIDyn a) (map toIDyn d) r
getResource ::(Typeable a, Typeable b, T.IResource a, T.IResource b) => a -> IO (Maybe b)
getResource x= getDResource (toIDyn x) >>= return . g where
g Nothing= Nothing
g (Just x)= Just (fromIDyn x)
getResources ::(Typeable a, Typeable b, T.IResource a, T.IResource b) => [a] -> IO [Maybe b]
getResources rs = getDResources (map toIDyn rs) >>= return . map g where
g Nothing= Nothing
g (Just x)= Just (fromIDyn x)
deleteResource ::(Typeable a, T.IResource a) => a -> IO ()
deleteResource x= deleteDResource (toIDyn x)
deleteResources ::(Typeable a, T.IResource a) => [a] -> IO ()
deleteResources xs= deleteDResources (map toIDyn xs)