Attr.hs.template 4.9 KB

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