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

The GIConv struct wraps an @/iconv()/@ conversion descriptor. It contains
private data and should only be accessed using the following functions.
-}

module GI.GLib.Structs.IConv
    ( 

-- * Exported types
    IConv(..)                               ,
    noIConv                                 ,


 -- * Methods
-- ** close #method:close#
#ifdef ENABLE_OVERLOADING
    IConvCloseMethodInfo                    ,
#endif
    iConvClose                              ,




    ) 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.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
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


newtype IConv = IConv (ManagedPtr IConv)
-- XXX Wrapping a foreign struct/union with no known destructor or size, leak?
instance WrappedPtr IConv where
    wrappedPtrCalloc = return nullPtr
    wrappedPtrCopy = return
    wrappedPtrFree = Nothing

noIConv :: Maybe IConv
noIConv = Nothing


#ifdef ENABLE_OVERLOADING
instance O.HasAttributeList IConv
type instance O.AttributeList IConv = IConvAttributeList
type IConvAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method IConv::close
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "converter", argType = TInterface (Name {namespace = "GLib", name = "IConv"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a conversion descriptor from g_iconv_open()", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TInt)
-- throws : False
-- Skip return : False

foreign import ccall "g_iconv_close" g_iconv_close :: 
    Ptr IConv ->                            -- converter : TInterface (Name {namespace = "GLib", name = "IConv"})
    IO Int32

{- |
Same as the standard UNIX routine @/iconv_close()/@, but
may be implemented via libiconv on UNIX flavors that lack
a native implementation. Should be called to clean up
the conversion descriptor from @/g_iconv_open()/@ when
you are done converting things.

GLib provides 'GI.GLib.Functions.convert' and 'GI.GLib.Functions.localeToUtf8' which are likely
more convenient than the raw iconv wrappers.
-}
iConvClose ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    IConv
    {- ^ /@converter@/: a conversion descriptor from @/g_iconv_open()/@ -}
    -> m Int32
    {- ^ __Returns:__ -1 on error, 0 on success -}
iConvClose converter = liftIO $ do
    converter' <- unsafeManagedPtrGetPtr converter
    result <- g_iconv_close converter'
    touchManagedPtr converter
    return result

#ifdef ENABLE_OVERLOADING
data IConvCloseMethodInfo
instance (signature ~ (m Int32), MonadIO m) => O.MethodInfo IConvCloseMethodInfo IConv signature where
    overloadedMethod _ = iConvClose

#endif

#ifdef ENABLE_OVERLOADING
type family ResolveIConvMethod (t :: Symbol) (o :: *) :: * where
    ResolveIConvMethod "close" o = IConvCloseMethodInfo
    ResolveIConvMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveIConvMethod t IConv, O.MethodInfo info IConv p) => O.IsLabelProxy t (IConv -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveIConvMethod t IConv, O.MethodInfo info IConv p) => O.IsLabel t (IConv -> 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

#endif