module Data.GI.GIR.Enum
( Enumeration(..)
, EnumerationMember(..)
, parseEnum
) where
import Data.Int (Int64)
import Data.Text (Text)
import Foreign.C (CInt(..))
import Data.GI.GIR.Parser
import Data.GI.GIR.Type (parseCType)
data Enumeration = Enumeration {
enumMembers :: [EnumerationMember],
enumErrorDomain :: Maybe Text,
enumTypeInit :: Maybe Text,
enumDocumentation :: Documentation,
enumCType :: Text,
enumStorageBytes :: Int,
enumDeprecated :: Maybe DeprecationInfo }
deriving Show
data EnumerationMember = EnumerationMember {
enumMemberName :: Text,
enumMemberValue :: Int64,
enumMemberCId :: Text,
enumMemberDoc :: Documentation
} deriving Show
parseEnumMember :: Parser EnumerationMember
parseEnumMember = do
name <- getAttr "name"
value <- getAttr "value" >>= parseIntegral
cid <- getAttrWithNamespace CGIRNS "identifier"
doc <- parseDocumentation
return $ EnumerationMember {
enumMemberName = name,
enumMemberValue = value,
enumMemberCId = cid,
enumMemberDoc = doc
}
foreign import ccall "_gi_get_enum_storage_bytes" get_storage_bytes ::
Int64 -> Int64 -> CInt
extractEnumStorageBytes :: [Int64] -> Int
extractEnumStorageBytes values =
fromIntegral $ get_storage_bytes (minimum values) (maximum values)
parseEnum :: Parser (Name, Enumeration)
parseEnum = do
name <- parseName
ctype <- parseCType
doc <- parseDocumentation
deprecated <- parseDeprecation
errorDomain <- queryAttrWithNamespace GLibGIRNS "error-domain"
typeInit <- queryAttrWithNamespace GLibGIRNS "get-type"
members <- parseChildrenWithLocalName "member" parseEnumMember
return (name,
Enumeration {
enumMembers = members
, enumErrorDomain = errorDomain
, enumDocumentation = doc
, enumTypeInit = typeInit
, enumCType = ctype
, enumStorageBytes = extractEnumStorageBytes (map enumMemberValue members)
, enumDeprecated = deprecated
})