module Foreign.Hoppy.Generator.Spec.ClassFeature (
ClassFeature (..),
classAddFeatures,
) where
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid (mempty)
#endif
import Foreign.Hoppy.Generator.Spec.Base
import Foreign.Hoppy.Generator.Types
data ClassFeature =
Assignable
| Comparable
| Copyable
| Equatable
deriving (Eq, Show)
featureContents :: ClassFeature -> Class -> ([ClassEntity], Reqs)
featureContents feature cls = case feature of
Assignable -> assignableContents cls
Comparable -> comparableContents cls
Copyable -> copyableContents cls
Equatable -> equatableContents cls
assignableContents :: Class -> ([ClassEntity], Reqs)
assignableContents cls =
([ mkMethod OpAssign [refT $ constT $ objT cls] $ refT $ objT cls
],
mempty)
comparableContents :: Class -> ([ClassEntity], Reqs)
comparableContents cls =
([ mkConstMethod OpLt [refT $ constT $ objT cls] boolT
, mkConstMethod OpLe [refT $ constT $ objT cls] boolT
, mkConstMethod OpGt [refT $ constT $ objT cls] boolT
, mkConstMethod OpGe [refT $ constT $ objT cls] boolT
],
mempty)
copyableContents :: Class -> ([ClassEntity], Reqs)
copyableContents cls =
([ mkCtor "newCopy" [objT cls]
],
mempty)
equatableContents :: Class -> ([ClassEntity], Reqs)
equatableContents cls =
([ mkConstMethod OpEq [objT cls] boolT
, mkConstMethod OpNe [objT cls] boolT
],
mempty)
classAddFeatures :: [ClassFeature] -> Class -> Class
classAddFeatures features cls =
foldr (\feature cls' ->
let (entities, reqs) = featureContents feature cls'
in addReqs reqs $
classAddEntities entities cls')
cls
features