Attr.hs.template 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. {-# LANGUAGE DataKinds #-}
  2. {-# LANGUAGE RecordWildCards #-}
  3. module $module_path$.Attr where
  4. import Ivory.Language
  5. import Ivory.Tower
  6. data AttrWriter a =
  7. AttrWriter
  8. { aw_chan :: ChanInput a
  9. , aw_name :: String
  10. }
  11. data AttrReader a =
  12. AttrReader
  13. { ar_chan :: ChanOutput a
  14. , ar_name :: String
  15. , ar_ival :: Init a
  16. }
  17. data Attr a =
  18. Attr
  19. { attr_writer :: AttrWriter a
  20. , attr_reader :: AttrReader a
  21. }
  22. attrReaderState :: (IvoryArea a, IvoryZero a)
  23. => AttrReader a -> Monitor e (Ref Global a)
  24. attrReaderState ar@AttrReader{..} = do
  25. s <- stateInit ar_name ar_ival
  26. attrReaderHandler ar \$ do
  27. callback \$ \\v -> refCopy s v
  28. return s
  29. attrReaderHandler :: (IvoryArea a, IvoryZero a)
  30. => AttrReader a -> Handler a e () -> Monitor e ()
  31. attrReaderHandler AttrReader{..} k =
  32. handler ar_chan (ar_name ++ "_update") k
  33. attrWriterEmitter :: (IvoryArea a, IvoryZero a)
  34. => AttrWriter a -> Handler b e (Emitter a)
  35. attrWriterEmitter AttrWriter{..} = emitter aw_chan 1
  36. towerAttr :: (IvoryArea a) => String -> Init a -> Tower e (Attr a)
  37. towerAttr n i = do
  38. c <- channel
  39. return Attr
  40. { attr_writer = AttrWriter
  41. { aw_chan = fst c
  42. , aw_name = n
  43. }
  44. , attr_reader = AttrReader
  45. { ar_chan = snd c
  46. , ar_name = n
  47. , ar_ival = i
  48. }
  49. }
  50. class AttrNamed p where
  51. attrName :: (IvoryArea a) => p a -> String
  52. instance AttrNamed AttrReader where
  53. attrName = ar_name
  54. instance AttrNamed AttrWriter where
  55. attrName = aw_name
  56. instance AttrNamed Attr where
  57. attrName = attrName . attr_reader
  58. class AttrReadable p where
  59. attrState :: (IvoryArea a, IvoryZero a) => p a -> Monitor e (Ref Global a)
  60. attrHandler :: (IvoryArea a, IvoryZero a) => p a -> Handler a e () -> Monitor e ()
  61. instance AttrReadable AttrReader where
  62. attrState = attrReaderState
  63. attrHandler = attrReaderHandler
  64. instance AttrReadable Attr where
  65. attrState = attrReaderState . attr_reader
  66. attrHandler p k = attrReaderHandler (attr_reader p) k
  67. class AttrWritable p where
  68. attrEmitter :: (IvoryArea a, IvoryZero a) => p a -> Handler b e (Emitter a)
  69. instance AttrWritable AttrWriter where
  70. attrEmitter = attrWriterEmitter
  71. instance AttrWritable Attr where
  72. attrEmitter = attrWriterEmitter . attr_writer
  73. readableAttrServer :: (IvoryArea a, IvoryZero a)
  74. => Attr a
  75. -> ChanOutput (Stored IBool)
  76. -> Tower e (ChanOutput a)
  77. readableAttrServer p get = do
  78. val <- channel
  79. monitor (named "Server") \$ do
  80. s <- attrState p
  81. handler get (named "Get") \$ do
  82. e <- emitter (fst val) 1
  83. callback \$ const \$ emit e (constRef s)
  84. return (snd val)
  85. where
  86. named n = attrName p ++ n
  87. writableAttrServer :: (IvoryArea a, IvoryZero a)
  88. => Attr a
  89. -> ChanOutput a
  90. -> Tower e ()
  91. writableAttrServer p set = do
  92. monitor (named "Server") \$ do
  93. handler set (named "Set") \$ do
  94. e <- attrEmitter p
  95. callback \$ \\v -> emit e v
  96. where
  97. named n = attrName p ++ n
  98. readwritableAttrServer :: (IvoryArea a, IvoryZero a)
  99. => Attr a
  100. -> ChanOutput (Stored IBool)
  101. -> ChanOutput a
  102. -> Tower e (ChanOutput a)
  103. readwritableAttrServer p get set = do
  104. val <- channel
  105. monitor (named "Server") \$ do
  106. s <- attrState p
  107. handler set (named "Set") \$ do
  108. e <- attrEmitter p
  109. callback \$ \\v -> emit e v
  110. handler get (named "Get") \$ do
  111. e <- emitter (fst val) 1
  112. callback \$ const \$ emit e (constRef s)
  113. return (snd val)
  114. where
  115. named n = attrName p ++ n