{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson and Iñaki García Etxebarria
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- The t'GI.GLib.Unions.FloatIEEE754.FloatIEEE754' and t'GI.GLib.Unions.DoubleIEEE754.DoubleIEEE754' unions are used to access the sign,
-- mantissa and exponent of IEEE floats and doubles. These unions are defined
-- as appropriate for a given platform. IEEE floats and doubles are supported
-- (used for storage) by at least Intel, PPC and Sparc.

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

module GI.GLib.Unions.FloatIEEE754
    ( 

-- * Exported types
    FloatIEEE754(..)                        ,
    newZeroFloatIEEE754                     ,


 -- * Methods

#if defined(ENABLE_OVERLOADING)
    ResolveFloatIEEE754Method               ,
#endif



 -- * Properties


-- ** vFloat #attr:vFloat#
-- | the double value

#if defined(ENABLE_OVERLOADING)
    floatIEEE754_vFloat                     ,
#endif
    getFloatIEEE754VFloat                   ,
    setFloatIEEE754VFloat                   ,




    ) 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.BasicTypes as B.Types
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GArray as B.GArray
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GHashTable as B.GHT
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 Control.Monad.IO.Class as MIO
import qualified Data.Coerce as Coerce
import qualified Data.Text as T
import qualified Data.Kind as DK
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
import qualified GHC.Records as R
import qualified Data.Word as DW
import qualified Data.Int as DI
import qualified System.Posix.Types as SPT
import qualified Foreign.C.Types as FCT

-- Workaround for https://gitlab.haskell.org/ghc/ghc/-/issues/23392
#if MIN_VERSION_base(4,18,0)

#else

#endif

-- | Memory-managed wrapper type.
newtype FloatIEEE754 = FloatIEEE754 (SP.ManagedPtr FloatIEEE754)
    deriving (Eq)

instance SP.ManagedPtrNewtype FloatIEEE754 where
    toManagedPtr (FloatIEEE754 p) = p

instance BoxedPtr FloatIEEE754 where
    boxedPtrCopy = \p -> B.ManagedPtr.withManagedPtr p (copyBytes 4 >=> B.ManagedPtr.wrapPtr FloatIEEE754)
    boxedPtrFree = \x -> SP.withManagedPtr x SP.freeMem
instance CallocPtr FloatIEEE754 where
    boxedPtrCalloc = callocBytes 4


-- | Construct a t'FloatIEEE754' struct initialized to zero.
newZeroFloatIEEE754 :: MonadIO m => m FloatIEEE754
newZeroFloatIEEE754 = liftIO $ boxedPtrCalloc >>= wrapPtr FloatIEEE754

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


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

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

#if defined(ENABLE_OVERLOADING)
data FloatIEEE754VFloatFieldInfo
instance AttrInfo FloatIEEE754VFloatFieldInfo where
    type AttrBaseTypeConstraint FloatIEEE754VFloatFieldInfo = (~) FloatIEEE754
    type AttrAllowedOps FloatIEEE754VFloatFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint FloatIEEE754VFloatFieldInfo = (~) Float
    type AttrTransferTypeConstraint FloatIEEE754VFloatFieldInfo = (~)Float
    type AttrTransferType FloatIEEE754VFloatFieldInfo = Float
    type AttrGetType FloatIEEE754VFloatFieldInfo = Float
    type AttrLabel FloatIEEE754VFloatFieldInfo = "v_float"
    type AttrOrigin FloatIEEE754VFloatFieldInfo = FloatIEEE754
    attrGet = getFloatIEEE754VFloat
    attrSet = setFloatIEEE754VFloat
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v
    dbgAttrInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.GLib.Unions.FloatIEEE754.vFloat"
        , O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-glib-2.0.30/docs/GI-GLib-Unions-FloatIEEE754.html#g:attr:vFloat"
        })

floatIEEE754_vFloat :: AttrLabelProxy "vFloat"
floatIEEE754_vFloat = AttrLabelProxy

#endif



#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList FloatIEEE754
type instance O.AttributeList FloatIEEE754 = FloatIEEE754AttributeList
type FloatIEEE754AttributeList = ('[ '("vFloat", FloatIEEE754VFloatFieldInfo)] :: [(Symbol, DK.Type)])
#endif

#if defined(ENABLE_OVERLOADING)
type family ResolveFloatIEEE754Method (t :: Symbol) (o :: DK.Type) :: DK.Type where
    ResolveFloatIEEE754Method l o = O.MethodResolutionFailed l o

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

#if MIN_VERSION_base(4,13,0)
instance (info ~ ResolveFloatIEEE754Method t FloatIEEE754, O.OverloadedMethod info FloatIEEE754 p, R.HasField t FloatIEEE754 p) => R.HasField t FloatIEEE754 p where
    getField = O.overloadedMethod @info

#endif

instance (info ~ ResolveFloatIEEE754Method t FloatIEEE754, O.OverloadedMethodInfo info FloatIEEE754) => OL.IsLabel t (O.MethodProxy info FloatIEEE754) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.MethodProxy
#else
    fromLabel _ = O.MethodProxy
#endif

#endif


