{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (inaki@blueleaf.cc)

The 'GI.GLib.Unions.FloatIEEE754.FloatIEEE754' and '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.
-}

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

module GI.GLib.Unions.FloatIEEE754
    (

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


 -- * Properties
-- ** vFloat #attr:vFloat#
{- | the double value
-}
#if 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.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.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 FloatIEEE754 = FloatIEEE754 (ManagedPtr FloatIEEE754)
instance WrappedPtr FloatIEEE754 where
    wrappedPtrCalloc = callocBytes 4
    wrappedPtrCopy = \p -> withManagedPtr p (copyBytes 4 >=> wrapPtr FloatIEEE754)
    wrappedPtrFree = Just ptr_to_g_free

-- | Construct a `FloatIEEE754` struct initialized to zero.
newZeroFloatIEEE754 :: MonadIO m => m FloatIEEE754
newZeroFloatIEEE754 = liftIO $ wrappedPtrCalloc >>= wrapPtr FloatIEEE754

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


-- | A convenience alias for `Nothing` :: `Maybe` `FloatIEEE754`.
noFloatIEEE754 :: Maybe FloatIEEE754
noFloatIEEE754 = Nothing

{- |
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 ENABLE_OVERLOADING
data FloatIEEE754VFloatFieldInfo
instance AttrInfo FloatIEEE754VFloatFieldInfo where
    type AttrAllowedOps FloatIEEE754VFloatFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint FloatIEEE754VFloatFieldInfo = (~) Float
    type AttrBaseTypeConstraint FloatIEEE754VFloatFieldInfo = (~) FloatIEEE754
    type AttrGetType FloatIEEE754VFloatFieldInfo = Float
    type AttrLabel FloatIEEE754VFloatFieldInfo = "v_float"
    type AttrOrigin FloatIEEE754VFloatFieldInfo = FloatIEEE754
    attrGet _ = getFloatIEEE754VFloat
    attrSet _ = setFloatIEEE754VFloat
    attrConstruct = undefined
    attrClear _ = undefined

floatIEEE754_vFloat :: AttrLabelProxy "vFloat"
floatIEEE754_vFloat = AttrLabelProxy

#endif



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

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

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

#endif