package GVariant_Class_Properties is new Generic_Internal_Discrete_Property (GVariant_Class);
type Gvariant is new Glib.C_Boxed with null record;
type GVariant_Class is ( Class_Tuple, Class_Array, Class_Boolean, Class_Double, Class_Signature, Class_Handle, Class_Int32, Class_Maybe, Class_Int16, Class_Object_Path, Class_Uint16, Class_String, Class_Uint64, Class_Uint32, Class_Variant, Class_Int64, Class_Byte, Class_Dict_Entry);
type Gvariant_Iter is new Glib.C_Proxy;
type Gvariant_Type is new Glib.C_Proxy;
type Property_GVariant_Class is new GVariant_Class_Properties.Property;
function From_Object
( | Object | : System.Address) return Gvariant; |
procedure G_New_Boolean
( | Self | : out Gvariant; |
Value | : Boolean); |
function Gvariant_New_Boolean
( | Value | : Boolean) return Gvariant; |
procedure G_New_Bytestring
( | Self | : out Gvariant; |
String | : Gint_Array); |
function Gvariant_New_Bytestring
( | String | : Gint_Array) return Gvariant; |
procedure G_New_Bytestring_Array
( | Self | : out Gvariant; |
Strv | : GNAT.Strings.String_List; | |
Length | : Gssize); |
function Gvariant_New_Bytestring_Array
( | Strv | : GNAT.Strings.String_List; |
Length | : Gssize) return Gvariant; |
procedure G_New_Dict_Entry
( | Self | : out Gvariant_Type; |
Key | : Gvariant_Type; | |
Value | : Gvariant_Type); |
procedure G_New_Object_Path
( | Self | : out Gvariant; |
Object_Path | : UTF8_String); |
function Gvariant_New_Object_Path
( | Object_Path | : UTF8_String) return Gvariant; |
procedure G_New_Signature
( | Self | : out Gvariant; |
Signature | : UTF8_String); |
function Gvariant_New_Signature
( | Signature | : UTF8_String) return Gvariant; |
procedure G_New_String
( | Self | : out Gvariant; |
String | : UTF8_String); |
function Gvariant_New_String
( | String | : UTF8_String) return Gvariant; |
procedure G_New
( | Self | : out Gvariant_Type; |
Type_String | : UTF8_String); |
function Gvariant_Type_New
( | Type_String | : UTF8_String) return Gvariant_Type; |
function Gvariant_Type_New_Dict_Entry
( | Key | : Gvariant_Type; |
Value | : Gvariant_Type) return Gvariant_Type; |
function Get_Type return Glib.GType;
function Check_Format_String
( | Self | : Gvariant; |
Format_String | : UTF8_String; | |
Copy_Only | : Boolean) return Boolean; |
function Classify
( | Self | : Gvariant) return GVariant_Class; |
function Dup_Bytestring_Array
( | Self | : Gvariant; |
Length | : access Gsize) return GNAT.Strings.String_List; |
function Dup_String
( | Self | : Gvariant; |
Length | : access Gsize) return UTF8_String; |
function Dup_String
( | Self | : Gvariant_Type) return UTF8_String; |
function Get_Boolean
( | Self | : Gvariant) return Boolean; |
function Get_Bytestring_Array
( | Self | : Gvariant; |
Length | : access Gsize) return GNAT.Strings.String_List; |
function Get_String
( | Self | : Gvariant; |
Length | : access Gsize) return UTF8_String; |
function Get_Type
( | Self | : Gvariant) return Gvariant_Type; |
function Get_Type_String
( | Self | : Gvariant) return UTF8_String; |
function Is_Container
( | Self | : Gvariant) return Boolean; |
function Is_Container
( | Self | : Gvariant_Type) return Boolean; |
function Is_Floating
( | Self | : Gvariant) return Boolean; |
function Is_Normal_Form
( | Self | : Gvariant) return Boolean; |
function Is_Of_Type
( | Self | : Gvariant; |
The_Type | : Gvariant_Type) return Boolean; |
function Iter_New
( | Self | : Gvariant) return Gvariant_Iter; |
function Lookup_Value
( | Self | : Gvariant; |
Key | : UTF8_String; | |
Expected_Type | : Gvariant_Type) return Gvariant; |
function Print
( | Self | : Gvariant; |
Type_Annotate | : Boolean) return UTF8_String; |
function Print_String
( | Self | : Gvariant; |
String | : Glib.String.Gstring; | |
Type_Annotate | : Boolean) return Glib.String.Gstring; |
procedure Store
( | Self | : Gvariant; |
Data | : System.Address); |
procedure Unref
( | Self | : Gvariant); |
function Init
( | Self | : Gvariant_Iter; |
Value | : Gvariant) return Gsize; |
function Next_Value
( | Self | : Gvariant_Iter) return Gvariant; |
function Copy
( | Self | : Gvariant_Type) return Gvariant_Type; |
function Element
( | Self | : Gvariant_Type) return Gvariant_Type; |
function First
( | Self | : Gvariant_Type) return Gvariant_Type; |
procedure Free
( | Self | : Gvariant_Type); |
function Get_String_Length
( | Self | : Gvariant_Type) return Gsize; |
function Is_Array
( | Self | : Gvariant_Type) return Boolean; |
function Is_Basic
( | Self | : Gvariant_Type) return Boolean; |
function Is_Definite
( | Self | : Gvariant_Type) return Boolean; |
function Is_Dict_Entry
( | Self | : Gvariant_Type) return Boolean; |
function Is_Maybe
( | Self | : Gvariant_Type) return Boolean; |
function Is_Subtype_Of
( | Self | : Gvariant_Type; |
Supertype | : Gvariant_Type) return Boolean; |
function Is_Tuple
( | Self | : Gvariant_Type) return Boolean; |
function Is_Variant
( | Self | : Gvariant_Type) return Boolean; |
function Key
( | Self | : Gvariant_Type) return Gvariant_Type; |
function N_Items
( | Self | : Gvariant_Type) return Gsize; |
function Next
( | Self | : Gvariant_Type) return Gvariant_Type; |
function Peek_String
( | Self | : Gvariant_Type) return UTF8_String; |
function Value
( | Self | : Gvariant_Type) return Gvariant_Type; |
function Is_Object_Path
( | String | : UTF8_String) return Boolean; |
function Is_Signature
( | String | : UTF8_String) return Boolean; |
function Parse
( | The_Type | : Gvariant_Type; |
Text | : UTF8_String; | |
Limit | : UTF8_String := ""; | |
Endptr | : GNAT.Strings.String_List) return Gvariant; |
function Parser_Get_Error_Quark return Glib.GQuark;
function String_Is_Valid
( | Type_String | : UTF8_String) return Boolean; |
Glib.Variant.Gvariant is a variant datatype; it stores a value along with information about the type of that value. The range of possible values is determined by the type. The type system used by Glib.Variant.Gvariant is Glib.Variant.Gvariant_Type.
Glib.Variant.Gvariant instances always have a type and a value (which are given at construction time). The type and value of a Glib.Variant.Gvariant instance can never change other than by the Glib.Variant.Gvariant itself being destroyed. A Glib.Variant.Gvariant cannot contain a pointer.
Glib.Variant.Gvariant is reference counted using Glib.Variant.Ref and Glib.Variant.Unref. Glib.Variant.Gvariant also has floating reference counts -- see Glib.Variant.Ref_Sink.
Glib.Variant.Gvariant is completely threadsafe. A Glib.Variant.Gvariant instance can be concurrently accessed in any way from any number of threads without problems.
Glib.Variant.Gvariant is heavily optimised for dealing with data in serialised form. It works particularly well with data located in memory-mapped files. It can perform nearly all deserialisation operations in a small constant time, usually touching only a single memory page.
Serialised Glib.Variant.Gvariant data can also be sent over the network.
Glib.Variant.Gvariant is largely compatible with D-Bus. Almost all types of Glib.Variant.Gvariant instances can be sent over D-Bus. See Glib.Variant.Gvariant_Type for exceptions. (However, Glib.Variant.Gvariant's serialisation format is not the same as the serialisation format of a D-Bus message body: use Gdbus.Message.Gdbus_Message, in the gio library, for those.) For space-efficiency, the Glib.Variant.Gvariant serialisation format does not automatically include the variant's type or endianness, which must either be implied from context (such as knowledge that a particular file format always contains a little-endian G_VARIANT_TYPE_VARIANT) or supplied out-of-band (for instance, a type and/or endianness indicator could be placed at the beginning of a file, network message or network stream).
A Glib.Variant.Gvariant's size is limited mainly by any lower level operating system constraints, such as the number of bits in Gsize. For example, it is reasonable to have a 2GB file mapped into memory with Gmapped.File.Gmapped_File, and call g_variant_new_from_data on it.
For convenience to C programmers, Glib.Variant.Gvariant features powerful varargs-based value construction and destruction. This feature is designed to be embedded in other libraries.
There is a Python-inspired text language for describing Glib.Variant.Gvariant values. Glib.Variant.Gvariant includes a printer for this language and a parser with type inferencing. == Memory Use == Glib.Variant.Gvariant tries to be quite efficient with respect to memory use. This section gives a rough idea of how much memory is used by the current implementation. The information here is subject to change in the future.
The memory allocated by Glib.Variant.Gvariant can be grouped into 4 broad purposes: memory for serialised data, memory for the type information cache, buffer management memory and memory for the Glib.Variant.Gvariant structure itself. == Serialised Data Memory == This is the memory that is used for storing GVariant data in serialised form. This is what would be sent over the network or what would end up on disk.
The amount of memory required to store a boolean is 1 byte. 16, 32 and 64 bit integers and double precision floating point numbers use their "natural" size. Strings (including object path and signature strings) are stored with a nul terminator, and as such use the length of the string plus 1 byte.
Maybe types use no space at all to represent the null value and use the same amount of space (sometimes plus one byte) as the equivalent non-maybe-typed value to represent the non-null case.
Arrays use the amount of space required to store each of their members, concatenated. Additionally, if the items stored in an array are not of a fixed-size (ie: strings, other arrays, etc) then an additional framing offset is stored for each item. The size of this offset is either 1, 2 or 4 bytes depending on the overall size of the container. Additionally, extra padding bytes are added as required for alignment of child values.
Tuples (including dictionary entries) use the amount of space required to store each of their members, concatenated, plus one framing offset (as per arrays) for each non-fixed-sized item in the tuple, except for the last one. Additionally, extra padding bytes are added as required for alignment of child values.
Variants use the same amount of space as the item inside of the variant, plus 1 byte, plus the length of the type string for the item inside the variant.
As an example, consider a dictionary mapping strings to variants. In the case that the dictionary is empty, 0 bytes are required for the serialisation.
If we add an item "width" that maps to the int32 value of 500 then we will use 4 byte to store the int32 (so 6 for the variant containing it) and 6 bytes for the string. The variant must be aligned to 8 after the 6 bytes of the string, so that's 2 extra bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used for the dictionary entry. An additional 1 byte is added to the array as a framing offset making a total of 15 bytes.
If we add another entry, "title" that maps to a nullable string that happens to have a value of null, then we use 0 bytes for the null value (and 3 bytes for the variant to contain it along with its type string) plus 6 bytes for the string. Again, we need 2 padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
We now require extra padding between the two items in the array. After the 14 bytes of the first item, that's 2 bytes required. We now require 2 framing offsets for an extra two bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item dictionary. == Type Information Cache == For each GVariant type that currently exists in the program a type information structure is kept in the type information cache. The type information structure is required for rapid deserialisation.
Continuing with the above example, if a Glib.Variant.Gvariant exists with the type "a{sv}" then a type information struct will exist for "a{sv}", "{sv}", "s", and "v". Multiple uses of the same type will share the same type information. Additionally, all single-digit types are stored in read-only static memory and do not contribute to the writable memory footprint of a program using Glib.Variant.Gvariant.
Aside from the type information structures stored in read-only memory, there are two forms of type information. One is used for container types where there is a single element type: arrays and maybe types. The other is used for container types where there are multiple element types: tuples and dictionary entries.
Array type info structures are 6 * sizeof (void *), plus the memory required to store the type string itself. This means that on 32bit systems, the cache entry for "a{sv}" would require 30 bytes of memory (plus malloc overhead).
Tuple type info structures are 6 * sizeof (void *), plus 4 * sizeof (void *) for each item in the tuple, plus the memory required to store the type string itself. A 2-item tuple, for example, would have a type information structure that consumed writable memory in the size of 14 * sizeof (void *) (plus type string) This means that on 32bit systems, the cache entry for "{sv}" would require 61 bytes of memory (plus malloc overhead).
This means that in total, for our "a{sv}" example, 91 bytes of type information would be allocated.
The type information cache, additionally, uses a GHash_Table to store and lookup the cached items and stores a pointer to this hash table in static storage. The hash table is freed when there are zero items in the type cache.
Although these sizes may seem large it is important to remember that a program will probably only have a very small number of different types of values in it and that only one type information structure is required for many different values of the same type. == Buffer Management Memory == Glib.Variant.Gvariant uses an internal buffer management structure to deal with the various different possible sources of serialised data that it uses. The buffer is responsible for ensuring that the correct call is made when the data is no longer in use by Glib.Variant.Gvariant. This may involve a g_free or a g_slice_free or even g_mapped_file_unref.
One buffer management structure is used for each chunk of serialised data.
The size of the buffer management structure is 4 * (void *). On 32bit systems, that's 16 bytes. == GVariant structure == The size of a Glib.Variant.Gvariant structure is 6 * (void *). On 32 bit systems, that's 24 bytes.
Glib.Variant.Gvariant structures only exist if they are explicitly created with API calls. For example, if a Glib.Variant.Gvariant is constructed out of serialised data for the example given above (with the dictionary) then although there are 9 individual values that comprise the entire dictionary (two keys, two values, two variants containing the values, two dictionary entries, plus the dictionary itself), only 1 Glib.Variant.Gvariant instance exists -- the one referring to the dictionary.
If calls are made to start accessing the other values then Glib.Variant.Gvariant instances will exist for those values only for as long as they are in use (ie: until you call Glib.Variant.Unref). The type information is shared. The serialised data and the buffer management structure for that serialised data is shared by the child. == Summary == To put the entire example together, for our dictionary mapping strings to variants (with two entries, as given above), we are using 91 bytes of memory for type information, 29 byes of memory for the serialised data, 16 bytes for buffer management and 24 bytes for the Glib.Variant.Gvariant instance, or a total of 160 bytes, plus malloc overhead. If we were to use Glib.Variant.Get_Child_Value to access the two dictionary entries, we would use an additional 48 bytes. If we were to have other dictionaries of the same type, we would use more memory for the serialised data and buffer management for those dictionaries, but the type information would be shared.