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

A data buffer, generally used to represent a chunk of a
'GI.Soup.Structs.MessageBody.MessageBody'.

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

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

module GI.Soup.Structs.Buffer
    (

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


 -- * Methods
-- ** copy #method:copy#

#if ENABLE_OVERLOADING
    BufferCopyMethodInfo                    ,
#endif
    bufferCopy                              ,


-- ** free #method:free#

#if ENABLE_OVERLOADING
    BufferFreeMethodInfo                    ,
#endif
    bufferFree                              ,


-- ** getAsBytes #method:getAsBytes#

#if ENABLE_OVERLOADING
    BufferGetAsBytesMethodInfo              ,
#endif
    bufferGetAsBytes                        ,


-- ** getData #method:getData#

#if ENABLE_OVERLOADING
    BufferGetDataMethodInfo                 ,
#endif
    bufferGetData                           ,


-- ** getOwner #method:getOwner#

#if ENABLE_OVERLOADING
    BufferGetOwnerMethodInfo                ,
#endif
    bufferGetOwner                          ,


-- ** new #method:new#

    bufferNew                               ,


-- ** newSubbuffer #method:newSubbuffer#

#if ENABLE_OVERLOADING
    BufferNewSubbufferMethodInfo            ,
#endif
    bufferNewSubbuffer                      ,


-- ** newWithOwner #method:newWithOwner#

    bufferNewWithOwner                      ,




 -- * Properties
-- ** data #attr:data#
{- | the data
-}
#if ENABLE_OVERLOADING
    buffer_data                             ,
#endif
    clearBufferData                         ,
    getBufferData                           ,
    setBufferData                           ,


-- ** length #attr:length#
{- | length of /@data@/
-}
#if ENABLE_OVERLOADING
    buffer_length                           ,
#endif
    getBufferLength                         ,
    setBufferLength                         ,




    ) 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

import qualified GI.GLib.Callbacks as GLib.Callbacks
import qualified GI.GLib.Structs.Bytes as GLib.Bytes

-- | Memory-managed wrapper type.
newtype Buffer = Buffer (ManagedPtr 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

-- | Construct a `Buffer` struct initialized to zero.
newZeroBuffer :: MonadIO m => m Buffer
newZeroBuffer = liftIO $ callocBoxedBytes 16 >>= wrapBoxed Buffer

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


-- | A convenience alias for `Nothing` :: `Maybe` `Buffer`.
noBuffer :: Maybe Buffer
noBuffer = Nothing

{- |
Get the value of the “@data@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' buffer #data
@
-}
getBufferData :: MonadIO m => Buffer -> m (Ptr ())
getBufferData s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO (Ptr ())
    return val

{- |
Set the value of the “@data@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' buffer [ #data 'Data.GI.Base.Attributes.:=' value ]
@
-}
setBufferData :: MonadIO m => Buffer -> Ptr () -> m ()
setBufferData s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: Ptr ())

{- |
Set the value of the “@data@” field to `Nothing`.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.clear' #data
@
-}
clearBufferData :: MonadIO m => Buffer -> m ()
clearBufferData s = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (FP.nullPtr :: Ptr ())

#if ENABLE_OVERLOADING
data BufferDataFieldInfo
instance AttrInfo BufferDataFieldInfo where
    type AttrAllowedOps BufferDataFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint BufferDataFieldInfo = (~) (Ptr ())
    type AttrBaseTypeConstraint BufferDataFieldInfo = (~) Buffer
    type AttrGetType BufferDataFieldInfo = Ptr ()
    type AttrLabel BufferDataFieldInfo = "data"
    type AttrOrigin BufferDataFieldInfo = Buffer
    attrGet _ = getBufferData
    attrSet _ = setBufferData
    attrConstruct = undefined
    attrClear _ = clearBufferData

buffer_data :: AttrLabelProxy "data"
buffer_data = AttrLabelProxy

#endif


{- |
Get the value of the “@length@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' buffer #length
@
-}
getBufferLength :: MonadIO m => Buffer -> m Word64
getBufferLength s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO Word64
    return val

{- |
Set the value of the “@length@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' buffer [ #length 'Data.GI.Base.Attributes.:=' value ]
@
-}
setBufferLength :: MonadIO m => Buffer -> Word64 -> m ()
setBufferLength s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (val :: Word64)

#if ENABLE_OVERLOADING
data BufferLengthFieldInfo
instance AttrInfo BufferLengthFieldInfo where
    type AttrAllowedOps BufferLengthFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint BufferLengthFieldInfo = (~) Word64
    type AttrBaseTypeConstraint BufferLengthFieldInfo = (~) Buffer
    type AttrGetType BufferLengthFieldInfo = Word64
    type AttrLabel BufferLengthFieldInfo = "length"
    type AttrOrigin BufferLengthFieldInfo = Buffer
    attrGet _ = getBufferLength
    attrSet _ = setBufferLength
    attrConstruct = undefined
    attrClear _ = undefined

buffer_length :: AttrLabelProxy "length"
buffer_length = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList Buffer
type instance O.AttributeList Buffer = BufferAttributeList
type BufferAttributeList = ('[ '("data", BufferDataFieldInfo), '("length", BufferLengthFieldInfo)] :: [(Symbol, *)])
#endif

-- method Buffer::new
-- method type : Constructor
-- Args : [Arg {argCName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "length of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "length of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TInterface (Name {namespace = "Soup", name = "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)

{- |
Creates a new 'GI.Soup.Structs.Buffer.Buffer' containing /@length@/ bytes from /@data@/.

This function is exactly equivalent to @/soup_buffer_new()/@ with
'GI.Soup.Enums.MemoryUseTake' as first argument; it exists mainly for
convenience and simplifying language bindings.

/Since: 2.32/
-}
bufferNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ByteString
    {- ^ /@data@/: data -}
    -> m Buffer
    {- ^ __Returns:__ the new 'GI.Soup.Structs.Buffer.Buffer'. -}
bufferNew data_ = liftIO $ do
    let length_ = fromIntegral $ B.length data_
    data_' <- packByteString data_
    result <- soup_buffer_new_take data_' length_
    checkUnexpectedReturnNULL "bufferNew" result
    result' <- (wrapBoxed Buffer) result
    return result'

#if ENABLE_OVERLOADING
#endif

-- method Buffer::new_with_owner
-- method type : Constructor
-- Args : [Arg {argCName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "length of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "owner", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "pointer to an object that owns @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "owner_dnotify", argType = TInterface (Name {namespace = "GLib", name = "DestroyNotify"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a function to free/unref @owner when\nthe buffer is freed", sinceVersion = Nothing}, argScope = ScopeTypeAsync, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "length of @data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TInterface (Name {namespace = "Soup", name = "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 TPtr
    FunPtr GLib.Callbacks.C_DestroyNotify -> -- owner_dnotify : TInterface (Name {namespace = "GLib", name = "DestroyNotify"})
    IO (Ptr Buffer)

{- |
Creates a new 'GI.Soup.Structs.Buffer.Buffer' containing /@length@/ bytes from /@data@/. When
the 'GI.Soup.Structs.Buffer.Buffer' is freed, it will call /@ownerDnotify@/, passing
/@owner@/ to it. You must ensure that /@data@/ will remain valid until
/@ownerDnotify@/ is called.

For example, you could use this to create a buffer containing data
returned from libxml without needing to do an extra copy:

\<informalexample>\<programlisting>
xmlDocDumpMemory (doc, &xmlbody, &len);
return soup_buffer_new_with_owner (xmlbody, len, xmlbody,
                                   (GDestroyNotify)xmlFree);
\<\/programlisting>\<\/informalexample>

In this example, /@data@/ and /@owner@/ are the same, but in other cases
they would be different (eg, /@owner@/ would be a object, and /@data@/
would be a pointer to one of the object\'s fields).
-}
bufferNewWithOwner ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ByteString
    {- ^ /@data@/: data -}
    -> Ptr ()
    {- ^ /@owner@/: pointer to an object that owns /@data@/ -}
    -> Maybe (GLib.Callbacks.DestroyNotify)
    {- ^ /@ownerDnotify@/: a function to free\/unref /@owner@/ when
the buffer is freed -}
    -> m Buffer
    {- ^ __Returns:__ the new 'GI.Soup.Structs.Buffer.Buffer'. -}
bufferNewWithOwner data_ owner ownerDnotify = liftIO $ do
    let length_ = fromIntegral $ B.length data_
    data_' <- packByteString data_
    maybeOwnerDnotify <- case ownerDnotify of
        Nothing -> return (castPtrToFunPtr nullPtr)
        Just jOwnerDnotify -> do
            ptrownerDnotify <- callocMem :: IO (Ptr (FunPtr GLib.Callbacks.C_DestroyNotify))
            jOwnerDnotify' <- GLib.Callbacks.mk_DestroyNotify (GLib.Callbacks.wrap_DestroyNotify (Just ptrownerDnotify) jOwnerDnotify)
            poke ptrownerDnotify jOwnerDnotify'
            return jOwnerDnotify'
    result <- soup_buffer_new_with_owner data_' length_ owner maybeOwnerDnotify
    checkUnexpectedReturnNULL "bufferNewWithOwner" result
    result' <- (wrapBoxed Buffer) result
    freeMem data_'
    return result'

#if ENABLE_OVERLOADING
#endif

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

foreign import ccall "soup_buffer_copy" soup_buffer_copy ::
    Ptr Buffer ->                           -- buffer : TInterface (Name {namespace = "Soup", name = "Buffer"})
    IO (Ptr Buffer)

{- |
Makes a copy of /@buffer@/. In reality, 'GI.Soup.Structs.Buffer.Buffer' is a refcounted
type, and calling 'GI.Soup.Structs.Buffer.bufferCopy' will normally just increment
the refcount on /@buffer@/ and return it. However, if /@buffer@/ was
created with @/SOUP_MEMORY_TEMPORARY/@ memory, then 'GI.Soup.Structs.Buffer.bufferCopy'
will actually return a copy of it, so that the data in the copy
will remain valid after the temporary buffer is freed.
-}
bufferCopy ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Buffer
    {- ^ /@buffer@/: a 'GI.Soup.Structs.Buffer.Buffer' -}
    -> m Buffer
    {- ^ __Returns:__ the new (or newly-reffed) buffer -}
bufferCopy buffer = liftIO $ do
    buffer' <- unsafeManagedPtrGetPtr buffer
    result <- soup_buffer_copy buffer'
    checkUnexpectedReturnNULL "bufferCopy" result
    result' <- (wrapBoxed Buffer) result
    touchManagedPtr buffer
    return result'

#if ENABLE_OVERLOADING
data BufferCopyMethodInfo
instance (signature ~ (m Buffer), MonadIO m) => O.MethodInfo BufferCopyMethodInfo Buffer signature where
    overloadedMethod _ = bufferCopy

#endif

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

foreign import ccall "soup_buffer_free" soup_buffer_free ::
    Ptr Buffer ->                           -- buffer : TInterface (Name {namespace = "Soup", name = "Buffer"})
    IO ()

{- |
Frees /@buffer@/. (In reality, as described in the documentation for
'GI.Soup.Structs.Buffer.bufferCopy', this is actually an \"unref\" operation, and may
or may not actually free /@buffer@/.)
-}
bufferFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Buffer
    {- ^ /@buffer@/: a 'GI.Soup.Structs.Buffer.Buffer' -}
    -> m ()
bufferFree buffer = liftIO $ do
    buffer' <- unsafeManagedPtrGetPtr buffer
    soup_buffer_free buffer'
    touchManagedPtr buffer
    return ()

#if ENABLE_OVERLOADING
data BufferFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo BufferFreeMethodInfo Buffer signature where
    overloadedMethod _ = bufferFree

#endif

-- method Buffer::get_as_bytes
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "buffer", argType = TInterface (Name {namespace = "Soup", name = "Buffer"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #SoupBuffer", 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 "soup_buffer_get_as_bytes" soup_buffer_get_as_bytes ::
    Ptr Buffer ->                           -- buffer : TInterface (Name {namespace = "Soup", name = "Buffer"})
    IO (Ptr GLib.Bytes.Bytes)

{- |
Creates a 'GI.GLib.Structs.Bytes.Bytes' pointing to the same memory as /@buffer@/. The
'GI.GLib.Structs.Bytes.Bytes' will hold a reference on /@buffer@/ to ensure that it is not
freed while the 'GI.GLib.Structs.Bytes.Bytes' is still valid.

/Since: 2.40/
-}
bufferGetAsBytes ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Buffer
    {- ^ /@buffer@/: a 'GI.Soup.Structs.Buffer.Buffer' -}
    -> m GLib.Bytes.Bytes
    {- ^ __Returns:__ a new 'GI.GLib.Structs.Bytes.Bytes' which has the same content
as the 'GI.Soup.Structs.Buffer.Buffer'. -}
bufferGetAsBytes buffer = liftIO $ do
    buffer' <- unsafeManagedPtrGetPtr buffer
    result <- soup_buffer_get_as_bytes buffer'
    checkUnexpectedReturnNULL "bufferGetAsBytes" result
    result' <- (wrapBoxed GLib.Bytes.Bytes) result
    touchManagedPtr buffer
    return result'

#if ENABLE_OVERLOADING
data BufferGetAsBytesMethodInfo
instance (signature ~ (m GLib.Bytes.Bytes), MonadIO m) => O.MethodInfo BufferGetAsBytesMethodInfo Buffer signature where
    overloadedMethod _ = bufferGetAsBytes

#endif

-- method Buffer::get_data
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "buffer", argType = TInterface (Name {namespace = "Soup", name = "Buffer"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #SoupBuffer", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "data", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the pointer\nto the buffer data is stored here", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the length of the buffer data is stored here", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the length of the buffer data is stored here", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_get_data" soup_buffer_get_data ::
    Ptr Buffer ->                           -- buffer : TInterface (Name {namespace = "Soup", name = "Buffer"})
    Ptr (Ptr Word8) ->                      -- data : TCArray False (-1) 2 (TBasicType TUInt8)
    Ptr Word64 ->                           -- length : TBasicType TUInt64
    IO ()

{- |
This function exists for use by language bindings, because it\'s not
currently possible to get the right effect by annotating the fields
of 'GI.Soup.Structs.Buffer.Buffer'.

/Since: 2.32/
-}
bufferGetData ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Buffer
    {- ^ /@buffer@/: a 'GI.Soup.Structs.Buffer.Buffer' -}
    -> m (ByteString)
bufferGetData buffer = liftIO $ do
    buffer' <- unsafeManagedPtrGetPtr buffer
    data_ <- allocMem :: IO (Ptr (Ptr Word8))
    length_ <- allocMem :: IO (Ptr Word64)
    soup_buffer_get_data buffer' data_ length_
    length_' <- peek length_
    data_' <- peek data_
    data_'' <- (unpackByteStringWithLength length_') data_'
    touchManagedPtr buffer
    freeMem data_
    freeMem length_
    return data_''

#if ENABLE_OVERLOADING
data BufferGetDataMethodInfo
instance (signature ~ (m (ByteString)), MonadIO m) => O.MethodInfo BufferGetDataMethodInfo Buffer signature where
    overloadedMethod _ = bufferGetData

#endif

-- method Buffer::get_owner
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "buffer", argType = TInterface (Name {namespace = "Soup", name = "Buffer"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #SoupBuffer created with soup_buffer_new_with_owner()", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TPtr)
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_get_owner" soup_buffer_get_owner ::
    Ptr Buffer ->                           -- buffer : TInterface (Name {namespace = "Soup", name = "Buffer"})
    IO (Ptr ())

{- |
Gets the \"owner\" object for a buffer created with
'GI.Soup.Structs.Buffer.bufferNewWithOwner'.
-}
bufferGetOwner ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Buffer
    {- ^ /@buffer@/: a 'GI.Soup.Structs.Buffer.Buffer' created with 'GI.Soup.Structs.Buffer.bufferNewWithOwner' -}
    -> m (Ptr ())
    {- ^ __Returns:__ the owner pointer -}
bufferGetOwner buffer = liftIO $ do
    buffer' <- unsafeManagedPtrGetPtr buffer
    result <- soup_buffer_get_owner buffer'
    touchManagedPtr buffer
    return result

#if ENABLE_OVERLOADING
data BufferGetOwnerMethodInfo
instance (signature ~ (m (Ptr ())), MonadIO m) => O.MethodInfo BufferGetOwnerMethodInfo Buffer signature where
    overloadedMethod _ = bufferGetOwner

#endif

-- method Buffer::new_subbuffer
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "parent", argType = TInterface (Name {namespace = "Soup", name = "Buffer"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the parent #SoupBuffer", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "offset", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "offset within @parent to start at", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of bytes to copy from @parent", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Soup", name = "Buffer"}))
-- throws : False
-- Skip return : False

foreign import ccall "soup_buffer_new_subbuffer" soup_buffer_new_subbuffer ::
    Ptr Buffer ->                           -- parent : TInterface (Name {namespace = "Soup", name = "Buffer"})
    Word64 ->                               -- offset : TBasicType TUInt64
    Word64 ->                               -- length : TBasicType TUInt64
    IO (Ptr Buffer)

{- |
Creates a new 'GI.Soup.Structs.Buffer.Buffer' containing /@length@/ bytes \"copied\" from
/@parent@/ starting at /@offset@/. (Normally this will not actually copy
any data, but will instead simply reference the same data as
/@parent@/ does.)
-}
bufferNewSubbuffer ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Buffer
    {- ^ /@parent@/: the parent 'GI.Soup.Structs.Buffer.Buffer' -}
    -> Word64
    {- ^ /@offset@/: offset within /@parent@/ to start at -}
    -> Word64
    {- ^ /@length@/: number of bytes to copy from /@parent@/ -}
    -> m Buffer
    {- ^ __Returns:__ the new 'GI.Soup.Structs.Buffer.Buffer'. -}
bufferNewSubbuffer parent offset length_ = liftIO $ do
    parent' <- unsafeManagedPtrGetPtr parent
    result <- soup_buffer_new_subbuffer parent' offset length_
    checkUnexpectedReturnNULL "bufferNewSubbuffer" result
    result' <- (wrapBoxed Buffer) result
    touchManagedPtr parent
    return result'

#if ENABLE_OVERLOADING
data BufferNewSubbufferMethodInfo
instance (signature ~ (Word64 -> Word64 -> m Buffer), MonadIO m) => O.MethodInfo BufferNewSubbufferMethodInfo Buffer signature where
    overloadedMethod _ = bufferNewSubbuffer

#endif

#if ENABLE_OVERLOADING
type family ResolveBufferMethod (t :: Symbol) (o :: *) :: * where
    ResolveBufferMethod "copy" o = BufferCopyMethodInfo
    ResolveBufferMethod "free" o = BufferFreeMethodInfo
    ResolveBufferMethod "newSubbuffer" o = BufferNewSubbufferMethodInfo
    ResolveBufferMethod "getAsBytes" o = BufferGetAsBytesMethodInfo
    ResolveBufferMethod "getData" o = BufferGetDataMethodInfo
    ResolveBufferMethod "getOwner" o = BufferGetOwnerMethodInfo
    ResolveBufferMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveBufferMethod t Buffer, O.MethodInfo info Buffer p) => OL.IsLabel t (Buffer -> 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