{- |
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 'GI.GLib.Structs.MappedFile.MappedFile' represents a file mapping created with
'GI.GLib.Structs.MappedFile.mappedFileNew'. It has only private members and should
not be accessed directly.
-}

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

module GI.GLib.Structs.MappedFile
    (

-- * Exported types
    MappedFile(..)                          ,
    noMappedFile                            ,


 -- * Methods
-- ** free #method:free#

#if ENABLE_OVERLOADING
    MappedFileFreeMethodInfo                ,
#endif
    mappedFileFree                          ,


-- ** getBytes #method:getBytes#

#if ENABLE_OVERLOADING
    MappedFileGetBytesMethodInfo            ,
#endif
    mappedFileGetBytes                      ,


-- ** getContents #method:getContents#

#if ENABLE_OVERLOADING
    MappedFileGetContentsMethodInfo         ,
#endif
    mappedFileGetContents                   ,


-- ** getLength #method:getLength#

#if ENABLE_OVERLOADING
    MappedFileGetLengthMethodInfo           ,
#endif
    mappedFileGetLength                     ,


-- ** new #method:new#

    mappedFileNew                           ,


-- ** newFromFd #method:newFromFd#

    mappedFileNewFromFd                     ,


-- ** ref #method:ref#

#if ENABLE_OVERLOADING
    MappedFileRefMethodInfo                 ,
#endif
    mappedFileRef                           ,


-- ** unref #method:unref#

#if ENABLE_OVERLOADING
    MappedFileUnrefMethodInfo               ,
#endif
    mappedFileUnref                         ,




    ) 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.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.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP

import {-# SOURCE #-} qualified GI.GLib.Structs.Bytes as GLib.Bytes

-- | Memory-managed wrapper type.
newtype MappedFile = MappedFile (ManagedPtr MappedFile)
foreign import ccall "g_mapped_file_get_type" c_g_mapped_file_get_type ::
    IO GType

instance BoxedObject MappedFile where
    boxedType _ = c_g_mapped_file_get_type

-- | A convenience alias for `Nothing` :: `Maybe` `MappedFile`.
noMappedFile :: Maybe MappedFile
noMappedFile = Nothing


#if ENABLE_OVERLOADING
instance O.HasAttributeList MappedFile
type instance O.AttributeList MappedFile = MappedFileAttributeList
type MappedFileAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method MappedFile::new
-- method type : Constructor
-- Args : [Arg {argCName = "filename", argType = TBasicType TFileName, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The path of the file to load, in the GLib\n    filename encoding", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "writable", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "whether the mapping should be writable", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "MappedFile"}))
-- throws : True
-- Skip return : False

foreign import ccall "g_mapped_file_new" g_mapped_file_new ::
    CString ->                              -- filename : TBasicType TFileName
    CInt ->                                 -- writable : TBasicType TBoolean
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr MappedFile)

{- |
Maps a file into memory. On UNIX, this is using the @/mmap()/@ function.

If /@writable@/ is 'True', the mapped buffer may be modified, otherwise
it is an error to modify the mapped buffer. Modifications to the buffer
are not visible to other processes mapping the same file, and are not
written back to the file.

Note that modifications of the underlying file might affect the contents
of the 'GI.GLib.Structs.MappedFile.MappedFile'. Therefore, mapping should only be used if the file
will not be modified, or if all modifications of the file are done
atomically (e.g. using 'GI.GLib.Functions.fileSetContents').

If /@filename@/ is the name of an empty, regular file, the function
will successfully return an empty 'GI.GLib.Structs.MappedFile.MappedFile'. In other cases of
size 0 (e.g. device files such as \/dev\/null), /@error@/ will be set
to the 'GI.GLib.Enums.FileError' value @/G_FILE_ERROR_INVAL/@.

/Since: 2.8/
-}
mappedFileNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    [Char]
    {- ^ /@filename@/: The path of the file to load, in the GLib
    filename encoding -}
    -> Bool
    {- ^ /@writable@/: whether the mapping should be writable -}
    -> m MappedFile
    {- ^ __Returns:__ a newly allocated 'GI.GLib.Structs.MappedFile.MappedFile' which must be unref\'d
   with 'GI.GLib.Structs.MappedFile.mappedFileUnref', or 'Nothing' if the mapping failed. /(Can throw 'Data.GI.Base.GError.GError')/ -}
mappedFileNew filename writable = liftIO $ do
    filename' <- stringToCString filename
    let writable' = (fromIntegral . fromEnum) writable
    onException (do
        result <- propagateGError $ g_mapped_file_new filename' writable'
        checkUnexpectedReturnNULL "mappedFileNew" result
        result' <- (wrapBoxed MappedFile) result
        freeMem filename'
        return result'
     ) (do
        freeMem filename'
     )

#if ENABLE_OVERLOADING
#endif

-- method MappedFile::new_from_fd
-- method type : Constructor
-- Args : [Arg {argCName = "fd", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "The file descriptor of the file to load", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "writable", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "whether the mapping should be writable", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "MappedFile"}))
-- throws : True
-- Skip return : False

foreign import ccall "g_mapped_file_new_from_fd" g_mapped_file_new_from_fd ::
    Int32 ->                                -- fd : TBasicType TInt
    CInt ->                                 -- writable : TBasicType TBoolean
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr MappedFile)

{- |
Maps a file into memory. On UNIX, this is using the @/mmap()/@ function.

If /@writable@/ is 'True', the mapped buffer may be modified, otherwise
it is an error to modify the mapped buffer. Modifications to the buffer
are not visible to other processes mapping the same file, and are not
written back to the file.

Note that modifications of the underlying file might affect the contents
of the 'GI.GLib.Structs.MappedFile.MappedFile'. Therefore, mapping should only be used if the file
will not be modified, or if all modifications of the file are done
atomically (e.g. using 'GI.GLib.Functions.fileSetContents').

/Since: 2.32/
-}
mappedFileNewFromFd ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Int32
    {- ^ /@fd@/: The file descriptor of the file to load -}
    -> Bool
    {- ^ /@writable@/: whether the mapping should be writable -}
    -> m MappedFile
    {- ^ __Returns:__ a newly allocated 'GI.GLib.Structs.MappedFile.MappedFile' which must be unref\'d
   with 'GI.GLib.Structs.MappedFile.mappedFileUnref', or 'Nothing' if the mapping failed. /(Can throw 'Data.GI.Base.GError.GError')/ -}
mappedFileNewFromFd fd writable = liftIO $ do
    let writable' = (fromIntegral . fromEnum) writable
    onException (do
        result <- propagateGError $ g_mapped_file_new_from_fd fd writable'
        checkUnexpectedReturnNULL "mappedFileNewFromFd" result
        result' <- (wrapBoxed MappedFile) result
        return result'
     ) (do
        return ()
     )

#if ENABLE_OVERLOADING
#endif

-- method MappedFile::free
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "file", argType = TInterface (Name {namespace = "GLib", name = "MappedFile"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GMappedFile", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_mapped_file_free" g_mapped_file_free ::
    Ptr MappedFile ->                       -- file : TInterface (Name {namespace = "GLib", name = "MappedFile"})
    IO ()

{-# DEPRECATED mappedFileFree ["(Since version 2.22)","Use 'GI.GLib.Structs.MappedFile.mappedFileUnref' instead."] #-}
{- |
This call existed before 'GI.GLib.Structs.MappedFile.MappedFile' had refcounting and is currently
exactly the same as 'GI.GLib.Structs.MappedFile.mappedFileUnref'.

/Since: 2.8/
-}
mappedFileFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    MappedFile
    {- ^ /@file@/: a 'GI.GLib.Structs.MappedFile.MappedFile' -}
    -> m ()
mappedFileFree file = liftIO $ do
    file' <- unsafeManagedPtrGetPtr file
    g_mapped_file_free file'
    touchManagedPtr file
    return ()

#if ENABLE_OVERLOADING
data MappedFileFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo MappedFileFreeMethodInfo MappedFile signature where
    overloadedMethod _ = mappedFileFree

#endif

-- method MappedFile::get_bytes
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "file", argType = TInterface (Name {namespace = "GLib", name = "MappedFile"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GMappedFile", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "Bytes"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_mapped_file_get_bytes" g_mapped_file_get_bytes ::
    Ptr MappedFile ->                       -- file : TInterface (Name {namespace = "GLib", name = "MappedFile"})
    IO (Ptr GLib.Bytes.Bytes)

{- |
Creates a new 'GI.GLib.Structs.Bytes.Bytes' which references the data mapped from /@file@/.
The mapped contents of the file must not be modified after creating this
bytes object, because a 'GI.GLib.Structs.Bytes.Bytes' should be immutable.

/Since: 2.34/
-}
mappedFileGetBytes ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    MappedFile
    {- ^ /@file@/: a 'GI.GLib.Structs.MappedFile.MappedFile' -}
    -> m GLib.Bytes.Bytes
    {- ^ __Returns:__ A newly allocated 'GI.GLib.Structs.Bytes.Bytes' referencing data
    from /@file@/ -}
mappedFileGetBytes file = liftIO $ do
    file' <- unsafeManagedPtrGetPtr file
    result <- g_mapped_file_get_bytes file'
    checkUnexpectedReturnNULL "mappedFileGetBytes" result
    result' <- (wrapBoxed GLib.Bytes.Bytes) result
    touchManagedPtr file
    return result'

#if ENABLE_OVERLOADING
data MappedFileGetBytesMethodInfo
instance (signature ~ (m GLib.Bytes.Bytes), MonadIO m) => O.MethodInfo MappedFileGetBytesMethodInfo MappedFile signature where
    overloadedMethod _ = mappedFileGetBytes

#endif

-- method MappedFile::get_contents
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "file", argType = TInterface (Name {namespace = "GLib", name = "MappedFile"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GMappedFile", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUTF8)
-- throws : False
-- Skip return : False

foreign import ccall "g_mapped_file_get_contents" g_mapped_file_get_contents ::
    Ptr MappedFile ->                       -- file : TInterface (Name {namespace = "GLib", name = "MappedFile"})
    IO CString

{- |
Returns the contents of a 'GI.GLib.Structs.MappedFile.MappedFile'.

Note that the contents may not be zero-terminated,
even if the 'GI.GLib.Structs.MappedFile.MappedFile' is backed by a text file.

If the file is empty then 'Nothing' is returned.

/Since: 2.8/
-}
mappedFileGetContents ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    MappedFile
    {- ^ /@file@/: a 'GI.GLib.Structs.MappedFile.MappedFile' -}
    -> m T.Text
    {- ^ __Returns:__ the contents of /@file@/, or 'Nothing'. -}
mappedFileGetContents file = liftIO $ do
    file' <- unsafeManagedPtrGetPtr file
    result <- g_mapped_file_get_contents file'
    checkUnexpectedReturnNULL "mappedFileGetContents" result
    result' <- cstringToText result
    freeMem result
    touchManagedPtr file
    return result'

#if ENABLE_OVERLOADING
data MappedFileGetContentsMethodInfo
instance (signature ~ (m T.Text), MonadIO m) => O.MethodInfo MappedFileGetContentsMethodInfo MappedFile signature where
    overloadedMethod _ = mappedFileGetContents

#endif

-- method MappedFile::get_length
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "file", argType = TInterface (Name {namespace = "GLib", name = "MappedFile"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GMappedFile", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUInt64)
-- throws : False
-- Skip return : False

foreign import ccall "g_mapped_file_get_length" g_mapped_file_get_length ::
    Ptr MappedFile ->                       -- file : TInterface (Name {namespace = "GLib", name = "MappedFile"})
    IO Word64

{- |
Returns the length of the contents of a 'GI.GLib.Structs.MappedFile.MappedFile'.

/Since: 2.8/
-}
mappedFileGetLength ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    MappedFile
    {- ^ /@file@/: a 'GI.GLib.Structs.MappedFile.MappedFile' -}
    -> m Word64
    {- ^ __Returns:__ the length of the contents of /@file@/. -}
mappedFileGetLength file = liftIO $ do
    file' <- unsafeManagedPtrGetPtr file
    result <- g_mapped_file_get_length file'
    touchManagedPtr file
    return result

#if ENABLE_OVERLOADING
data MappedFileGetLengthMethodInfo
instance (signature ~ (m Word64), MonadIO m) => O.MethodInfo MappedFileGetLengthMethodInfo MappedFile signature where
    overloadedMethod _ = mappedFileGetLength

#endif

-- method MappedFile::ref
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "file", argType = TInterface (Name {namespace = "GLib", name = "MappedFile"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GMappedFile", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "MappedFile"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_mapped_file_ref" g_mapped_file_ref ::
    Ptr MappedFile ->                       -- file : TInterface (Name {namespace = "GLib", name = "MappedFile"})
    IO (Ptr MappedFile)

{- |
Increments the reference count of /@file@/ by one.  It is safe to call
this function from any thread.

/Since: 2.22/
-}
mappedFileRef ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    MappedFile
    {- ^ /@file@/: a 'GI.GLib.Structs.MappedFile.MappedFile' -}
    -> m MappedFile
    {- ^ __Returns:__ the passed in 'GI.GLib.Structs.MappedFile.MappedFile'. -}
mappedFileRef file = liftIO $ do
    file' <- unsafeManagedPtrGetPtr file
    result <- g_mapped_file_ref file'
    checkUnexpectedReturnNULL "mappedFileRef" result
    result' <- (wrapBoxed MappedFile) result
    touchManagedPtr file
    return result'

#if ENABLE_OVERLOADING
data MappedFileRefMethodInfo
instance (signature ~ (m MappedFile), MonadIO m) => O.MethodInfo MappedFileRefMethodInfo MappedFile signature where
    overloadedMethod _ = mappedFileRef

#endif

-- method MappedFile::unref
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "file", argType = TInterface (Name {namespace = "GLib", name = "MappedFile"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GMappedFile", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_mapped_file_unref" g_mapped_file_unref ::
    Ptr MappedFile ->                       -- file : TInterface (Name {namespace = "GLib", name = "MappedFile"})
    IO ()

{- |
Decrements the reference count of /@file@/ by one.  If the reference count
drops to 0, unmaps the buffer of /@file@/ and frees it.

It is safe to call this function from any thread.

Since 2.22
-}
mappedFileUnref ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    MappedFile
    {- ^ /@file@/: a 'GI.GLib.Structs.MappedFile.MappedFile' -}
    -> m ()
mappedFileUnref file = liftIO $ do
    file' <- unsafeManagedPtrGetPtr file
    g_mapped_file_unref file'
    touchManagedPtr file
    return ()

#if ENABLE_OVERLOADING
data MappedFileUnrefMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo MappedFileUnrefMethodInfo MappedFile signature where
    overloadedMethod _ = mappedFileUnref

#endif

#if ENABLE_OVERLOADING
type family ResolveMappedFileMethod (t :: Symbol) (o :: *) :: * where
    ResolveMappedFileMethod "free" o = MappedFileFreeMethodInfo
    ResolveMappedFileMethod "ref" o = MappedFileRefMethodInfo
    ResolveMappedFileMethod "unref" o = MappedFileUnrefMethodInfo
    ResolveMappedFileMethod "getBytes" o = MappedFileGetBytesMethodInfo
    ResolveMappedFileMethod "getContents" o = MappedFileGetContentsMethodInfo
    ResolveMappedFileMethod "getLength" o = MappedFileGetLengthMethodInfo
    ResolveMappedFileMethod l o = O.MethodResolutionFailed l o

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

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