{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- Represents a file descriptor, which events to poll for, and which events
-- occurred.

#if (MIN_VERSION_haskell_gi_overloading(1,0,0) && !defined(__HADDOCK_VERSION__))
#define ENABLE_OVERLOADING
#endif

module GI.GLib.Structs.PollFD
    (

-- * Exported types
    PollFD(..)                              ,
    newZeroPollFD                           ,
    noPollFD                                ,


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolvePollFDMethod                     ,
#endif




 -- * Properties
-- ** events #attr:events#
-- | a bitwise combination from t'GI.GLib.Flags.IOCondition', specifying which
--     events should be polled for. Typically for reading from a file
--     descriptor you would use 'GI.GLib.Flags.IOConditionIn' | 'GI.GLib.Flags.IOConditionHup' | 'GI.GLib.Flags.IOConditionErr', and
--     for writing you would use 'GI.GLib.Flags.IOConditionOut' | 'GI.GLib.Flags.IOConditionErr'.

    getPollFDEvents                         ,
#if defined(ENABLE_OVERLOADING)
    pollFD_events                           ,
#endif
    setPollFDEvents                         ,


-- ** fd #attr:fd#
-- | the file descriptor to poll (or a HANDLE on Win32)

    getPollFDFd                             ,
#if defined(ENABLE_OVERLOADING)
    pollFD_fd                               ,
#endif
    setPollFDFd                             ,


-- ** revents #attr:revents#
-- | a bitwise combination of flags from t'GI.GLib.Flags.IOCondition', returned
--     from the @/poll()/@ function to indicate which events occurred.

    getPollFDRevents                        ,
#if defined(ENABLE_OVERLOADING)
    pollFD_revents                          ,
#endif
    setPollFDRevents                        ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.GI.Base.Signals as B.Signals
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL


-- | Memory-managed wrapper type.
newtype PollFD = PollFD (ManagedPtr PollFD)
    deriving (Eq)
foreign import ccall "g_pollfd_get_type" c_g_pollfd_get_type ::
    IO GType

instance BoxedObject PollFD where
    boxedType _ = c_g_pollfd_get_type

-- | Convert 'PollFD' to and from 'Data.GI.Base.GValue.GValue' with 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue PollFD where
    toGValue o = do
        gtype <- c_g_pollfd_get_type
        B.ManagedPtr.withManagedPtr o (B.GValue.buildGValue gtype B.GValue.set_boxed)

    fromGValue gv = do
        ptr <- B.GValue.get_boxed gv :: IO (Ptr PollFD)
        B.ManagedPtr.newBoxed PollFD ptr



-- | Construct a `PollFD` struct initialized to zero.
newZeroPollFD :: MonadIO m => m PollFD
newZeroPollFD = liftIO $ callocBoxedBytes 8 >>= wrapBoxed PollFD

instance tag ~ 'AttrSet => Constructible PollFD tag where
    new _ attrs = do
        o <- newZeroPollFD
        GI.Attributes.set o attrs
        return o


-- | A convenience alias for `Nothing` :: `Maybe` `PollFD`.
noPollFD :: Maybe PollFD
noPollFD = Nothing

-- | Get the value of the “@fd@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' pollFD #fd
-- @
getPollFDFd :: MonadIO m => PollFD -> m Int32
getPollFDFd s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO Int32
    return val

-- | Set the value of the “@fd@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' pollFD [ #fd 'Data.GI.Base.Attributes.:=' value ]
-- @
setPollFDFd :: MonadIO m => PollFD -> Int32 -> m ()
setPollFDFd s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: Int32)

#if defined(ENABLE_OVERLOADING)
data PollFDFdFieldInfo
instance AttrInfo PollFDFdFieldInfo where
    type AttrBaseTypeConstraint PollFDFdFieldInfo = (~) PollFD
    type AttrAllowedOps PollFDFdFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PollFDFdFieldInfo = (~) Int32
    type AttrTransferTypeConstraint PollFDFdFieldInfo = (~)Int32
    type AttrTransferType PollFDFdFieldInfo = Int32
    type AttrGetType PollFDFdFieldInfo = Int32
    type AttrLabel PollFDFdFieldInfo = "fd"
    type AttrOrigin PollFDFdFieldInfo = PollFD
    attrGet = getPollFDFd
    attrSet = setPollFDFd
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v

pollFD_fd :: AttrLabelProxy "fd"
pollFD_fd = AttrLabelProxy

#endif


-- | Get the value of the “@events@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' pollFD #events
-- @
getPollFDEvents :: MonadIO m => PollFD -> m Word16
getPollFDEvents s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 4) :: IO Word16
    return val

-- | Set the value of the “@events@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' pollFD [ #events 'Data.GI.Base.Attributes.:=' value ]
-- @
setPollFDEvents :: MonadIO m => PollFD -> Word16 -> m ()
setPollFDEvents s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 4) (val :: Word16)

#if defined(ENABLE_OVERLOADING)
data PollFDEventsFieldInfo
instance AttrInfo PollFDEventsFieldInfo where
    type AttrBaseTypeConstraint PollFDEventsFieldInfo = (~) PollFD
    type AttrAllowedOps PollFDEventsFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PollFDEventsFieldInfo = (~) Word16
    type AttrTransferTypeConstraint PollFDEventsFieldInfo = (~)Word16
    type AttrTransferType PollFDEventsFieldInfo = Word16
    type AttrGetType PollFDEventsFieldInfo = Word16
    type AttrLabel PollFDEventsFieldInfo = "events"
    type AttrOrigin PollFDEventsFieldInfo = PollFD
    attrGet = getPollFDEvents
    attrSet = setPollFDEvents
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v

pollFD_events :: AttrLabelProxy "events"
pollFD_events = AttrLabelProxy

#endif


-- | Get the value of the “@revents@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' pollFD #revents
-- @
getPollFDRevents :: MonadIO m => PollFD -> m Word16
getPollFDRevents s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 6) :: IO Word16
    return val

-- | Set the value of the “@revents@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' pollFD [ #revents 'Data.GI.Base.Attributes.:=' value ]
-- @
setPollFDRevents :: MonadIO m => PollFD -> Word16 -> m ()
setPollFDRevents s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 6) (val :: Word16)

#if defined(ENABLE_OVERLOADING)
data PollFDReventsFieldInfo
instance AttrInfo PollFDReventsFieldInfo where
    type AttrBaseTypeConstraint PollFDReventsFieldInfo = (~) PollFD
    type AttrAllowedOps PollFDReventsFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PollFDReventsFieldInfo = (~) Word16
    type AttrTransferTypeConstraint PollFDReventsFieldInfo = (~)Word16
    type AttrTransferType PollFDReventsFieldInfo = Word16
    type AttrGetType PollFDReventsFieldInfo = Word16
    type AttrLabel PollFDReventsFieldInfo = "revents"
    type AttrOrigin PollFDReventsFieldInfo = PollFD
    attrGet = getPollFDRevents
    attrSet = setPollFDRevents
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v

pollFD_revents :: AttrLabelProxy "revents"
pollFD_revents = AttrLabelProxy

#endif



#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList PollFD
type instance O.AttributeList PollFD = PollFDAttributeList
type PollFDAttributeList = ('[ '("fd", PollFDFdFieldInfo), '("events", PollFDEventsFieldInfo), '("revents", PollFDReventsFieldInfo)] :: [(Symbol, *)])
#endif

#if defined(ENABLE_OVERLOADING)
type family ResolvePollFDMethod (t :: Symbol) (o :: *) :: * where
    ResolvePollFDMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolvePollFDMethod t PollFD, O.MethodInfo info PollFD p) => OL.IsLabel t (PollFD -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#endif