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

A data buffer, generally used to represent a chunk of a
#SoupMessageBody.

@data is a #char because that's generally convenient; in some
situations you may need to cast it to #guchar or another type.
-}

module GI.Soup.Structs.Buffer
    ( 

-- * Exported types
    Buffer(..)                              ,
    noBuffer                                ,


 -- * Methods
-- ** bufferCopy
    bufferCopy                              ,


-- ** bufferFree
    bufferFree                              ,


-- ** bufferGetAsBytes
    bufferGetAsBytes                        ,


-- ** bufferGetData
    bufferGetData                           ,


-- ** bufferGetOwner
    bufferGetOwner                          ,


-- ** bufferNew
    bufferNew                               ,


-- ** bufferNewSubbuffer
    bufferNewSubbuffer                      ,


-- ** bufferNewWithOwner
    bufferNewWithOwner                      ,




 -- * Properties
-- ** Data
    bufferReadData                          ,


-- ** Length
    bufferReadLength                        ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.Soup.Types
import GI.Soup.Callbacks
import qualified GI.GLib as GLib

newtype Buffer = Buffer (ForeignPtr Buffer)
foreign import ccall "soup_buffer_get_type" c_soup_buffer_get_type :: 
    IO GType

instance BoxedObject Buffer where
    boxedType _ = c_soup_buffer_get_type

noBuffer :: Maybe Buffer
noBuffer = Nothing

bufferReadData :: Buffer -> IO (Ptr ())
bufferReadData s = withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO (Ptr ())
    return val

bufferReadLength :: Buffer -> IO Word64
bufferReadLength s = withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO Word64
    return val

-- method Buffer::new
-- method type : Constructor
-- Args : [Arg {argName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything},Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : [Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- hInArgs : [Arg {argName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- returnType : TInterface "Soup" "Buffer"
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_new_take" soup_buffer_new_take :: 
    Ptr Word8 ->                            -- data : TCArray False (-1) 1 (TBasicType TUInt8)
    Word64 ->                               -- length : TBasicType TUInt64
    IO (Ptr Buffer)


bufferNew ::
    (MonadIO m) =>
    ByteString ->                           -- data
    m Buffer
bufferNew data_ = liftIO $ do
    let length_ = fromIntegral $ B.length data_
    data_' <- packByteString data_
    result <- soup_buffer_new_take data_' length_
    checkUnexpectedReturnNULL "soup_buffer_new_take" result
    result' <- (wrapBoxed Buffer) result
    return result'

-- method Buffer::new_with_owner
-- method type : Constructor
-- Args : [Arg {argName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "owner", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "owner_dnotify", argType = TInterface "GLib" "DestroyNotify", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : [Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- hInArgs : [Arg {argName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "owner", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "owner_dnotify", argType = TInterface "GLib" "DestroyNotify", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Soup" "Buffer"
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_new_with_owner" soup_buffer_new_with_owner :: 
    Ptr Word8 ->                            -- data : TCArray False (-1) 1 (TBasicType TUInt8)
    Word64 ->                               -- length : TBasicType TUInt64
    Ptr () ->                               -- owner : TBasicType TVoid
    FunPtr GLib.DestroyNotifyC ->           -- owner_dnotify : TInterface "GLib" "DestroyNotify"
    IO (Ptr Buffer)


bufferNewWithOwner ::
    (MonadIO m) =>
    ByteString ->                           -- data
    Ptr () ->                               -- owner
    Maybe (GLib.DestroyNotify) ->           -- owner_dnotify
    m Buffer
bufferNewWithOwner data_ owner owner_dnotify = liftIO $ do
    let length_ = fromIntegral $ B.length data_
    data_' <- packByteString data_
    ptrowner_dnotify <- callocMem :: IO (Ptr (FunPtr GLib.DestroyNotifyC))
    maybeOwner_dnotify <- case owner_dnotify of
        Nothing -> return (castPtrToFunPtr nullPtr)
        Just jOwner_dnotify -> do
            jOwner_dnotify' <- GLib.mkDestroyNotify (GLib.destroyNotifyWrapper (Just ptrowner_dnotify) jOwner_dnotify)
            poke ptrowner_dnotify jOwner_dnotify'
            return jOwner_dnotify'
    result <- soup_buffer_new_with_owner data_' length_ owner maybeOwner_dnotify
    checkUnexpectedReturnNULL "soup_buffer_new_with_owner" result
    result' <- (wrapBoxed Buffer) result
    freeMem data_'
    return result'

-- method Buffer::copy
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Soup" "Buffer"
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_copy" soup_buffer_copy :: 
    Ptr Buffer ->                           -- _obj : TInterface "Soup" "Buffer"
    IO (Ptr Buffer)


bufferCopy ::
    (MonadIO m) =>
    Buffer ->                               -- _obj
    m Buffer
bufferCopy _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- soup_buffer_copy _obj'
    checkUnexpectedReturnNULL "soup_buffer_copy" result
    result' <- (wrapBoxed Buffer) result
    touchManagedPtr _obj
    return result'

-- method Buffer::free
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_free" soup_buffer_free :: 
    Ptr Buffer ->                           -- _obj : TInterface "Soup" "Buffer"
    IO ()


bufferFree ::
    (MonadIO m) =>
    Buffer ->                               -- _obj
    m ()
bufferFree _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    soup_buffer_free _obj'
    touchManagedPtr _obj
    return ()

-- method Buffer::get_as_bytes
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "GLib" "Bytes"
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_get_as_bytes" soup_buffer_get_as_bytes :: 
    Ptr Buffer ->                           -- _obj : TInterface "Soup" "Buffer"
    IO (Ptr GLib.Bytes)


bufferGetAsBytes ::
    (MonadIO m) =>
    Buffer ->                               -- _obj
    m GLib.Bytes
bufferGetAsBytes _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- soup_buffer_get_as_bytes _obj'
    checkUnexpectedReturnNULL "soup_buffer_get_as_bytes" result
    result' <- (wrapBoxed GLib.Bytes) result
    touchManagedPtr _obj
    return result'

-- method Buffer::get_data
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "data", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- Lengths : [Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_get_data" soup_buffer_get_data :: 
    Ptr Buffer ->                           -- _obj : TInterface "Soup" "Buffer"
    Ptr (Ptr Word8) ->                      -- data : TCArray False (-1) 2 (TBasicType TUInt8)
    Ptr Word64 ->                           -- length : TBasicType TUInt64
    IO ()


bufferGetData ::
    (MonadIO m) =>
    Buffer ->                               -- _obj
    m (ByteString)
bufferGetData _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    data_ <- allocMem :: IO (Ptr (Ptr Word8))
    length_ <- allocMem :: IO (Ptr Word64)
    soup_buffer_get_data _obj' data_ length_
    length_' <- peek length_
    data_' <- peek data_
    data_'' <- (unpackByteStringWithLength length_') data_'
    touchManagedPtr _obj
    freeMem data_
    freeMem length_
    return data_''

-- method Buffer::get_owner
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_get_owner" soup_buffer_get_owner :: 
    Ptr Buffer ->                           -- _obj : TInterface "Soup" "Buffer"
    IO ()


bufferGetOwner ::
    (MonadIO m) =>
    Buffer ->                               -- _obj
    m ()
bufferGetOwner _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    soup_buffer_get_owner _obj'
    touchManagedPtr _obj
    return ()

-- method Buffer::new_subbuffer
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "offset", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Buffer", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "offset", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Soup" "Buffer"
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_new_subbuffer" soup_buffer_new_subbuffer :: 
    Ptr Buffer ->                           -- _obj : TInterface "Soup" "Buffer"
    Word64 ->                               -- offset : TBasicType TUInt64
    Word64 ->                               -- length : TBasicType TUInt64
    IO (Ptr Buffer)


bufferNewSubbuffer ::
    (MonadIO m) =>
    Buffer ->                               -- _obj
    Word64 ->                               -- offset
    Word64 ->                               -- length
    m Buffer
bufferNewSubbuffer _obj offset length_ = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- soup_buffer_new_subbuffer _obj' offset length_
    checkUnexpectedReturnNULL "soup_buffer_new_subbuffer" result
    result' <- (wrapBoxed Buffer) result
    touchManagedPtr _obj
    return result'