module Eve.Internal.States
( States
, HasStates(..)
, HasEvents
, stateLens
) where
import Control.Lens
import Data.Map
import Unsafe.Coerce
import Data.Maybe
import Data.Typeable
import Data.Default
data StateWrapper =
forall s. (Typeable s, Show s) =>
StateWrapper s
instance Show StateWrapper where
show (StateWrapper s) = show s
type States = Map TypeRep StateWrapper
class HasStates s where
states :: Lens' s States
class (Typeable s, HasStates s) =>
HasEvents s
stateLens
:: forall a e.
(Show a, Typeable a, Default a, HasStates e)
=> Lens' e a
stateLens = lens getter setter
where
getter s =
fromMaybe def $ s ^. states . at (typeRep (Proxy :: Proxy a)) . mapping coerce
setter s new =
set (states . at (typeRep (Proxy :: Proxy a)) . mapping coerce) (Just new) s
coerce = iso (\(StateWrapper x) -> unsafeCoerce x) StateWrapper