module Data.Flags
( Flags(..)
, (.+.), (.-.), (.*.)
, BoundedFlags(..)
, allBut
, containsAll, (.<=.), (.>=.)
, containsSome, (.~.)
, containsNone, (./~.)
) where
import Data.Bits ()
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Foreign.Ptr (IntPtr, WordPtr)
import Foreign.C.Types (CChar, CSChar, CUChar, CShort, CUShort, CInt, CUInt,
CLong, CULong, CLLong, CULLong)
import Data.Flags.Base
import Data.Flags.TH
infixl 7 .*.
infixl 6 .+.
infixl 5 .-.
infix 4 .<=., .>=., `containsAll`, .~., `containsSome`, ./~., `containsNone`
(.+.) ∷ Flags α ⇒ α → α → α
(.+.) = andFlags
(.-.) ∷ Flags α ⇒ α → α → α
(.-.) = butFlags
(.*.) ∷ Flags α ⇒ α → α → α
(.*.) = commonFlags
allBut ∷ BoundedFlags α ⇒ α → α
allBut = (allFlags .-.)
containsAll ∷ Flags α ⇒ α → α → Bool
containsAll flags subflags = flags .*. subflags == subflags
(.>=.) ∷ Flags α ⇒ α → α → Bool
(.>=.) = containsAll
(.<=.) ∷ Flags α ⇒ α → α → Bool
(.<=.) = flip containsAll
containsSome ∷ Flags α ⇒ α → α → Bool
containsSome flags subflags = flags .*. subflags /= noFlags
(.~.) ∷ Flags α ⇒ α → α → Bool
(.~.) = containsSome
containsNone ∷ Flags α ⇒ α → α → Bool
containsNone flags subflags = flags .*. subflags == noFlags
(./~.) ∷ Flags α ⇒ α → α → Bool
(./~.) = containsNone
$(dataBitsAsFlags ''Integer)
$(dataBitsAsBoundedFlags ''Int)
$(dataBitsAsBoundedFlags ''Int8)
$(dataBitsAsBoundedFlags ''Int16)
$(dataBitsAsBoundedFlags ''Int32)
$(dataBitsAsBoundedFlags ''Int64)
$(dataBitsAsBoundedFlags ''Word)
$(dataBitsAsBoundedFlags ''Word8)
$(dataBitsAsBoundedFlags ''Word16)
$(dataBitsAsBoundedFlags ''Word32)
$(dataBitsAsBoundedFlags ''Word64)
$(dataBitsAsBoundedFlags ''IntPtr)
$(dataBitsAsBoundedFlags ''WordPtr)
$(dataBitsAsBoundedFlags ''CChar)
$(dataBitsAsBoundedFlags ''CSChar)
$(dataBitsAsBoundedFlags ''CUChar)
$(dataBitsAsBoundedFlags ''CShort)
$(dataBitsAsBoundedFlags ''CUShort)
$(dataBitsAsBoundedFlags ''CInt)
$(dataBitsAsBoundedFlags ''CUInt)
$(dataBitsAsBoundedFlags ''CLong)
$(dataBitsAsBoundedFlags ''CULong)
$(dataBitsAsBoundedFlags ''CLLong)
$(dataBitsAsBoundedFlags ''CULLong)