{- | 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 simple refcounted data type representing an immutable sequence of zero or more bytes from an unspecified origin. The purpose of a #GBytes is to keep the memory region that it holds alive for as long as anyone holds a reference to the bytes. When the last reference count is dropped, the memory is released. Multiple unrelated callers can use byte data in the #GBytes without coordinating their activities, resting assured that the byte data will not change or move while they hold a reference. A #GBytes can come from many different origins that may have different procedures for freeing the memory region. Examples are memory from g_malloc(), from memory slices, from a #GMappedFile or memory from other allocators. #GBytes work well as keys in #GHashTable. Use g_bytes_equal() and g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full(). #GBytes can also be used as keys in a #GTree by passing the g_bytes_compare() function to g_tree_new(). The data pointed to by this bytes must not be modified. For a mutable array of bytes see #GByteArray. Use g_bytes_unref_to_array() to create a mutable array for a #GBytes sequence. To create an immutable #GBytes from a mutable #GByteArray, use the g_byte_array_free_to_bytes() function. -} module GI.GLib.Structs.Bytes ( -- * Exported types Bytes(..) , noBytes , -- * Methods -- ** bytesCompare bytesCompare , -- ** bytesEqual bytesEqual , -- ** bytesGetData bytesGetData , -- ** bytesGetSize bytesGetSize , -- ** bytesHash bytesHash , -- ** bytesNew bytesNew , -- ** bytesNewFromBytes bytesNewFromBytes , -- ** bytesNewTake bytesNewTake , -- ** bytesRef bytesRef , -- ** bytesUnref bytesUnref , -- ** bytesUnrefToArray bytesUnrefToArray , -- ** bytesUnrefToData bytesUnrefToData , ) 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.GLib.Types import GI.GLib.Callbacks newtype Bytes = Bytes (ForeignPtr Bytes) foreign import ccall "g_bytes_get_type" c_g_bytes_get_type :: IO GType instance BoxedObject Bytes where boxedType _ = c_g_bytes_get_type noBytes :: Maybe Bytes noBytes = Nothing -- method Bytes::new -- method type : Constructor -- Args : [Arg {argName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [Arg {argName = "size", 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 = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TInterface "GLib" "Bytes" -- throws : False -- Skip return : False foreign import ccall "g_bytes_new" g_bytes_new :: Ptr Word8 -> -- data : TCArray False (-1) 1 (TBasicType TUInt8) Word64 -> -- size : TBasicType TUInt64 IO (Ptr Bytes) bytesNew :: (MonadIO m) => Maybe (ByteString) -> -- data m Bytes bytesNew data_ = liftIO $ do let size = case data_ of Nothing -> 0 Just jData_ -> fromIntegral $ B.length jData_ maybeData_ <- case data_ of Nothing -> return nullPtr Just jData_ -> do jData_' <- packByteString jData_ return jData_' result <- g_bytes_new maybeData_ size checkUnexpectedReturnNULL "g_bytes_new" result result' <- (wrapBoxed Bytes) result freeMem maybeData_ return result' -- method Bytes::new_take -- method type : Constructor -- Args : [Arg {argName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything},Arg {argName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [Arg {argName = "size", 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 = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}] -- returnType : TInterface "GLib" "Bytes" -- throws : False -- Skip return : False foreign import ccall "g_bytes_new_take" g_bytes_new_take :: Ptr Word8 -> -- data : TCArray False (-1) 1 (TBasicType TUInt8) Word64 -> -- size : TBasicType TUInt64 IO (Ptr Bytes) bytesNewTake :: (MonadIO m) => Maybe (ByteString) -> -- data m Bytes bytesNewTake data_ = liftIO $ do let size = case data_ of Nothing -> 0 Just jData_ -> fromIntegral $ B.length jData_ maybeData_ <- case data_ of Nothing -> return nullPtr Just jData_ -> do jData_' <- packByteString jData_ return jData_' result <- g_bytes_new_take maybeData_ size checkUnexpectedReturnNULL "g_bytes_new_take" result result' <- (wrapBoxed Bytes) result return result' -- method Bytes::compare -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "bytes2", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "bytes2", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TBasicType TInt32 -- throws : False -- Skip return : False foreign import ccall "g_bytes_compare" g_bytes_compare :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" Ptr Bytes -> -- bytes2 : TInterface "GLib" "Bytes" IO Int32 bytesCompare :: (MonadIO m) => Bytes -> -- _obj Bytes -> -- bytes2 m Int32 bytesCompare _obj bytes2 = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj let bytes2' = unsafeManagedPtrGetPtr bytes2 result <- g_bytes_compare _obj' bytes2' touchManagedPtr _obj touchManagedPtr bytes2 return result -- method Bytes::equal -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "bytes2", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "bytes2", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TBasicType TBoolean -- throws : False -- Skip return : False foreign import ccall "g_bytes_equal" g_bytes_equal :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" Ptr Bytes -> -- bytes2 : TInterface "GLib" "Bytes" IO CInt bytesEqual :: (MonadIO m) => Bytes -> -- _obj Bytes -> -- bytes2 m Bool bytesEqual _obj bytes2 = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj let bytes2' = unsafeManagedPtrGetPtr bytes2 result <- g_bytes_equal _obj' bytes2' let result' = (/= 0) result touchManagedPtr _obj touchManagedPtr bytes2 return result' -- method Bytes::get_data -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "size", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}] -- Lengths : [Arg {argName = "size", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TCArray False (-1) 1 (TBasicType TUInt8) -- throws : False -- Skip return : False foreign import ccall "g_bytes_get_data" g_bytes_get_data :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" Ptr Word64 -> -- size : TBasicType TUInt64 IO (Ptr Word8) bytesGetData :: (MonadIO m) => Bytes -> -- _obj m ByteString bytesGetData _obj = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj size <- allocMem :: IO (Ptr Word64) result <- g_bytes_get_data _obj' size size' <- peek size checkUnexpectedReturnNULL "g_bytes_get_data" result result' <- (unpackByteStringWithLength size') result touchManagedPtr _obj freeMem size return result' -- method Bytes::get_size -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TBasicType TUInt64 -- throws : False -- Skip return : False foreign import ccall "g_bytes_get_size" g_bytes_get_size :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" IO Word64 bytesGetSize :: (MonadIO m) => Bytes -> -- _obj m Word64 bytesGetSize _obj = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj result <- g_bytes_get_size _obj' touchManagedPtr _obj return result -- method Bytes::hash -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TBasicType TUInt32 -- throws : False -- Skip return : False foreign import ccall "g_bytes_hash" g_bytes_hash :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" IO Word32 bytesHash :: (MonadIO m) => Bytes -> -- _obj m Word32 bytesHash _obj = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj result <- g_bytes_hash _obj' touchManagedPtr _obj return result -- method Bytes::new_from_bytes -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", 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 "GLib" "Bytes", 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 "GLib" "Bytes" -- throws : False -- Skip return : False foreign import ccall "g_bytes_new_from_bytes" g_bytes_new_from_bytes :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" Word64 -> -- offset : TBasicType TUInt64 Word64 -> -- length : TBasicType TUInt64 IO (Ptr Bytes) bytesNewFromBytes :: (MonadIO m) => Bytes -> -- _obj Word64 -> -- offset Word64 -> -- length m Bytes bytesNewFromBytes _obj offset length_ = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj result <- g_bytes_new_from_bytes _obj' offset length_ checkUnexpectedReturnNULL "g_bytes_new_from_bytes" result result' <- (wrapBoxed Bytes) result touchManagedPtr _obj return result' -- method Bytes::ref -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TInterface "GLib" "Bytes" -- throws : False -- Skip return : False foreign import ccall "g_bytes_ref" g_bytes_ref :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" IO (Ptr Bytes) bytesRef :: (MonadIO m) => Bytes -> -- _obj m Bytes bytesRef _obj = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj result <- g_bytes_ref _obj' checkUnexpectedReturnNULL "g_bytes_ref" result result' <- (wrapBoxed Bytes) result touchManagedPtr _obj return result' -- method Bytes::unref -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TBasicType TVoid -- throws : False -- Skip return : False foreign import ccall "g_bytes_unref" g_bytes_unref :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" IO () bytesUnref :: (MonadIO m) => Bytes -> -- _obj m () bytesUnref _obj = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj g_bytes_unref _obj' touchManagedPtr _obj return () -- method Bytes::unref_to_array -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TByteArray -- throws : False -- Skip return : False foreign import ccall "g_bytes_unref_to_array" g_bytes_unref_to_array :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" IO (Ptr GByteArray) bytesUnrefToArray :: (MonadIO m) => Bytes -> -- _obj m ByteString bytesUnrefToArray _obj = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj result <- g_bytes_unref_to_array _obj' checkUnexpectedReturnNULL "g_bytes_unref_to_array" result result' <- unpackGByteArray result unrefGByteArray result touchManagedPtr _obj return result' -- method Bytes::unref_to_data -- method type : OrdinaryMethod -- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- Lengths : [] -- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "Bytes", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}] -- returnType : TBasicType TVoid -- throws : False -- Skip return : False foreign import ccall "g_bytes_unref_to_data" g_bytes_unref_to_data :: Ptr Bytes -> -- _obj : TInterface "GLib" "Bytes" Word64 -> -- size : TBasicType TUInt64 IO () bytesUnrefToData :: (MonadIO m) => Bytes -> -- _obj Word64 -> -- size m () bytesUnrefToData _obj size = liftIO $ do let _obj' = unsafeManagedPtrGetPtr _obj g_bytes_unref_to_data _obj' size touchManagedPtr _obj return ()