N-Bytes
#------------------------------------------------------------------------------- #--[Class Description]---------------------------------------------------------- #-------------------------------------------------------------------------------
Gnome::Glib::N-Bytes
Description
A simple refcounted data type representing an immutable sequence of zero or more bytes from an unspecified origin.
The purpose of a Gnome::Glib::N-Bytes 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 Gnome::Glib::N-Bytes without coordinating their activities, resting assured that the byte data will not change or move while they hold a reference.
A Gnome::Glib::N-Bytes 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 Gnome::Glib::N-MappedFile or memory from other allocators.
Gnome::Glib::N-Bytes work well as keys in Gnome::Glib::N-HashTable. Use .equal() and
.hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
Gnome::Glib::N-Bytes can also be used as keys in a Gnome::Glib::N-Tree by passing the .compare()
function to g_tree_new().
The data pointed to by this bytes must not be modified. For a mutable
array of bytes see Gnome::Glib::N-ByteArray. Use .unref-to-array() to create a
mutable array for a Gnome::Glib::N-Bytes sequence. To create an immutable Gnome::Glib::N-Bytes from
a mutable Gnome::Glib::N-ByteArray, use the g_byte_array_free_to_bytes() function.
#------------------------------------------------------------------------------- #--[Class Initialization]------------------------------------------------------- #-------------------------------------------------------------------------------
Class initialization
new
:native-object
Create an object using a native object from elsewhere. See also Gnome::N::TopLevelSupportClass.
multi method new ( N-Object :$native-object! )
#-------------------------------------------------------------------------------
new-bytes
Creates a new Gnome::Glib::N-Bytes from $data.
$data is copied. If $size is 0, $data may be undefined.
method new-bytes ( gpointer $data, Int() $size --> Gnome::Glib::Bytes \)
$data; the data to be used for the bytes.
$size; the size of
$data.
#-------------------------------------------------------------------------------
new-static
Creates a new Gnome::Glib::N-Bytes from static data.
$data must be static (ie: never modified or freed). It may be undefined if $size
is 0.
method new-static ( gpointer $data, Int() $size --> Gnome::Glib::Bytes \)
$data; (transfer ownership: full) the data to be used for the bytes.
$size; the size of
$data.
#-------------------------------------------------------------------------------
new-take
Creates a new Gnome::Glib::N-Bytes from $data.
After this call, $data belongs to the bytes and may no longer be
modified by the caller. g_free() will be called on $data when the
bytes is no longer in use. Because of this $data must have been created by
a call to g_malloc(), g_malloc0() or g_realloc() or by one of the many
functions that wrap these calls (such as g_new(), g_strdup(), etc).
For creating Gnome::Glib::N-Bytes with memory from other allocators, see
.new-with-free-func().
$data may be undefined if $size is 0.
method new-take ( gpointer $data, Int() $size --> Gnome::Glib::Bytes \)
$data; (transfer ownership: full) the data to be used for the bytes.
$size; the size of
$data.
#-------------------------------------------------------------------------------
new-with-free-func
Creates a Gnome::Glib::N-Bytes from $data.
When the last reference is dropped, $free-func will be called with the $user-data argument.
$data must not be modified after this call is made until $free-func has
been called to indicate that the bytes is no longer in use.
$data may be undefined if $size is 0.
method new-with-free-func ( gpointer $data, Int() $size, &free-func, gpointer $user-data --> Gnome::Glib::Bytes \)
$data; the data to be used for the bytes.
$size; the size of
$data.&free-func; the function to call to release the data. Tthe function must be specified with following signature;
:( gpointer $data ).$user-data; data to pass to
$free-func.
#------------------------------------------------------------------------------- #--[Methods]-------------------------------------------------------------------- #-------------------------------------------------------------------------------
Methods
#-------------------------------------------------------------------------------
compare
Compares the two Gnome::Glib::N-Bytes values.
This function can be used to sort GBytes instances in lexicographical order.
If $bytes1 and $bytes2 have different length but the shorter one is a
prefix of the longer one then the shorter one is considered to be less than
the longer one. Otherwise the first byte where both differ is used for
comparison. If $bytes1 has a smaller value at that position it is
considered less, otherwise greater than $bytes2.
method compare ( gpointer $bytes2 --> Int )
$bytes2; a pointer to a Gnome::Glib::N-Bytes to compare with
$bytes1.
Return value; a negative value if $bytes1 is less than $bytes2, a positive value
if $bytes1 is greater than $bytes2, and zero if $bytes1 is equal to
$bytes2.
#-------------------------------------------------------------------------------
equal
Compares the two Gnome::Glib::N-Bytes values being pointed to and returns
True if they are equal.
This function can be passed to g_hash_table_new() as the $key-equal-func
parameter, when using non-undefined Gnome::Glib::N-Bytes pointers as keys in a Gnome::Glib::N-HashTable.
method equal ( gpointer $bytes2 --> Bool )
$bytes2; a pointer to a Gnome::Glib::N-Bytes to compare with
$bytes1.
Return value; True if the two keys match..
#-------------------------------------------------------------------------------
get-data
Get the byte data in the Gnome::Glib::N-Bytes. This data should not be modified.
This function will always return the same pointer for a given Gnome::Glib::N-Bytes.
undefined may be returned if $size is 0. This is not guaranteed, as the Gnome::Glib::N-Bytes
may represent an empty string with $data non-undefined and $size as 0. undefined will
not be returned if $size is non-zero.
method get-data ( Array[gsize] $size --> gpointer )
$size; (transfer ownership: full) location to return size of byte data.
Return value; a pointer to the byte data, or undefined.
#-------------------------------------------------------------------------------
get-region
Gets a pointer to a region in $bytes.
The region starts at $offset many bytes from the start of the data
and contains $n-elements many elements of $element-size size.
$n-elements may be zero, but $element-size must always be non-zero.
Ideally, $element-size is a static constant (eg: sizeof a struct).
This function does careful bounds checking (including checking for
arithmetic overflows) and returns a non-undefined pointer if the
specified region lies entirely within the $bytes. If the region is
in some way out of range, or if an overflow has occurred, then undefined
is returned.
Note: it is possible to have a valid zero-size region. In this case,
the returned pointer will be equal to the base pointer of the data of $bytes, plus $offset. This will be non-undefined except for the case
where $bytes itself was a zero-sized region. Since it is unlikely
that you will be using this function to check for a zero-sized region
in a zero-sized $bytes, undefined effectively always means "error".
method get-region ( Int() $element-size, Int() $offset, Int() $n-elements --> gpointer )
$element-size; a non-zero element size.
$offset; an offset to the start of the region within the
$bytes.$n-elements; the number of elements in the region.
Return value; the requested region, or undefined in case of an error.
#-------------------------------------------------------------------------------
get-size
Get the size of the byte data in the Gnome::Glib::N-Bytes.
This function will always return the same value for a given Gnome::Glib::N-Bytes.
method get-size (--> Int )
Return value; the size.
#-------------------------------------------------------------------------------
hash
Creates an integer hash code for the byte data in the Gnome::Glib::N-Bytes.
This function can be passed to g_hash_table_new() as the $key-hash-func
parameter, when using non-undefined Gnome::Glib::N-Bytes pointers as keys in a Gnome::Glib::N-HashTable.
method hash (--> UInt )
Return value; a hash value corresponding to the key..
#-------------------------------------------------------------------------------
new-from-bytes
Creates a Gnome::Glib::N-Bytes which is a subsection of another Gnome::Glib::N-Bytes. The $offset + $length may not be longer than the size of $bytes.
A reference to $bytes will be held by the newly created Gnome::Glib::N-Bytes until
the byte data is no longer needed.
Since 2.56, if $offset is 0 and $length matches the size of $bytes, then $bytes will be returned with the reference count incremented by 1. If $bytes
is a slice of another Gnome::Glib::N-Bytes, then the resulting Gnome::Glib::N-Bytes will reference
the same Gnome::Glib::N-Bytes instead of $bytes. This allows consumers to simplify the
usage of Gnome::Glib::N-Bytes when asynchronously writing to streams.
method new-from-bytes ( Int() $offset, Int() $length --> N-Object )
$offset; offset which subsection starts at.
$length; length of subsection.
Return value; a new Gnome::Glib::N-Bytes.
#-------------------------------------------------------------------------------
ref
Increase the reference count on $bytes.
method ref (--> N-Object )
Return value; the Gnome::Glib::N-Bytes.
#-------------------------------------------------------------------------------
unref
Releases a reference on $bytes. This may result in the bytes being
freed. If $bytes is undefined, it will return immediately.
method unref ( )
#-------------------------------------------------------------------------------
unref-to-array
Unreferences the bytes, and returns a new mutable Gnome::Glib::N-ByteArray containing the same byte data.
As an optimization, the byte data is transferred to the array without copying
if this was the last reference to bytes and bytes was created with
.new-bytes(), .new-take() or g_byte_array_free_to_bytes(). In all
other cases the data is copied.
Do not use it if $bytes contains more than G_MAXUINT
bytes. Gnome::Glib::N-ByteArray stores the length of its data in #guint, which
may be shorter than #gsize, that $bytes is using.
method unref-to-array (--> N-Object )
Return value; a new mutable Gnome::Glib::N-ByteArray containing the same byte data.
#-------------------------------------------------------------------------------
unref-to-data
Unreferences the bytes, and returns a pointer the same byte data contents.
As an optimization, the byte data is returned without copying if this was
the last reference to bytes and bytes was created with .new-bytes(),
.new-take() or g_byte_array_free_to_bytes(). In all other cases the
data is copied.
method unref-to-data ( Array[gsize] $size --> gpointer )
$size; (transfer ownership: full) location to place the length of the returned data.
Return value; a pointer to the same byte data, which should be freed with g_free().