package ocgtk

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module GvariantSource

GVariant - GLib's variant type for structured data serialization

GVariant is a variant data type used for:

  • GSettings value storage and retrieval
  • GAction parameters and state
  • GDBus structured data
  • GtkActionable action targets

Type Codes:

  • b: boolean
  • y: byte (uint8)
  • n: int16
  • q: uint16
  • i: int32
  • u: uint32
  • x: int64
  • t: uint64
  • d: double
  • s: string
  • o: object_path
  • g: signature
  • h: handle (file descriptor)
  • v: variant
  • m*: maybe (nullable)
  • a*: array
  • () : tuple
  • {

}

: dictionary entry

Unsigned Integer Types

Sourcetype uint32 = UInt32.t

Unsigned 32-bit integer

Sourcetype uint64 = UInt64.t

Unsigned 64-bit integer

Sourcetype uint16 = UInt16.t

Unsigned 16-bit integer

Sourcetype int16 = int

Signed 16-bit integer - stored as OCaml int

Opaque GVariant Type

Sourcetype t

Opaque GVariant type - backed by GVariant* with g_variant_unref finalizer

Type Introspection

Sourceval type_string : t -> string

Get the type string of the variant (e.g., "i" for int32, "s" for string)

Sourceval is_of_type : t -> string -> bool

Check if the variant is of the given type string

Boolean

Sourceval of_boolean : bool -> t

Create a boolean variant.

Sourceval to_boolean : t -> bool

Get the boolean value.

  • raises Failure

    if the variant is not a boolean.

Byte (uint8)

Sourceval of_byte : int -> t

Create a byte variant.

Sourceval to_byte : t -> int

Get the byte value.

  • raises Failure

    if the variant is not a byte.

Signed Integers

Sourceval of_int16 : int16 -> t

Create an int16 variant.

Sourceval to_int16 : t -> int16

Get the int16 value.

  • raises Failure

    if the variant is not an int16.

Sourceval of_int32 : int32 -> t

Create an int32 variant.

Sourceval to_int32 : t -> int32

Get the int32 value.

  • raises Failure

    if the variant is not an int32.

Sourceval of_int64 : int64 -> t

Create an int64 variant.

Sourceval to_int64 : t -> int64

Get the int64 value.

  • raises Failure

    if the variant is not an int64.

Unsigned Integers

Sourceval of_uint16 : uint16 -> t

Create a uint16 variant.

Sourceval to_uint16 : t -> uint16

Get the uint16 value.

  • raises Failure

    if the variant is not a uint16.

Sourceval of_uint32 : uint32 -> t

Create a uint32 variant.

Sourceval to_uint32 : t -> uint32

Get the uint32 value.

  • raises Failure

    if the variant is not a uint32.

Sourceval of_uint64 : uint64 -> t

Create a uint64 variant.

Sourceval to_uint64 : t -> uint64

Get the uint64 value.

  • raises Failure

    if the variant is not a uint64.

Floating Point

Sourceval of_double : float -> t

Create a double variant.

Sourceval to_double : t -> float

Get the double value.

  • raises Failure

    if the variant is not a double.

Text Types

Sourceval of_string : string -> t

Create a string variant.

Sourceval to_string : t -> string

Get the string value.

  • raises Failure

    if the variant is not a string.

Sourceval of_object_path : string -> t

D-Bus object path - validated to be a valid object path

Sourceval of_signature : string -> t

D-Bus type signature

Handle (File Descriptor)

Sourceval of_handle : int -> t

Create a handle (file descriptor) variant.

Sourceval to_handle : t -> int

Get the handle value.

  • raises Failure

    if the variant is not a handle.

Variant (Boxed Value)

Sourceval of_variant : t -> t

Create a variant containing another variant (type 'v').

Sourceval to_variant : t -> t

Unwrap a variant inside a variant.

  • raises Failure

    if the variant is not a variant type.

Maybe (Nullable)

Sourceval of_maybe : Gvariant_type.t -> t option -> t

Create a maybe type variant. of_maybe ty None creates a null variant, of_maybe ty (Some v) wraps the value.

Sourceval to_maybe : t -> t option

Unwrap a maybe type variant. Returns None for null, Some v for wrapped value.

  • raises Failure

    if the variant is not a maybe type.

Arrays

Sourceval of_string_array : string array -> t

Create a string array variant.

Sourceval to_string_array : t -> string array

Get the string array value.

  • raises Failure

    if the variant is not a string array.

Sourceval of_object_path_array : string array -> t

Create an object path array variant.

Sourceval to_object_path_array : t -> string array

Get the object path array value.

  • raises Failure

    if the variant is not an object path array.

Dictionary Lookups (asv pattern)

These functions look up values in asv (string->variant) dictionaries. They return None if the key is not found or if the value is not of the expected type.

For dictionaries with different value types, use get_child_value with the appropriate index.

Sourceval lookup_string : t -> string -> string option

Lookup a string value in a dictionary. Returns None if the key is not found or the value is not a string.

Sourceval lookup_int32 : t -> string -> int32 option

Lookup an int32 value in a dictionary. Returns None if the key is not found or the value is not an int32.

Sourceval lookup_boolean : t -> string -> bool option

Lookup a boolean value in a dictionary. Returns None if the key is not found or the value is not a boolean.

Sourceval lookup_byte : t -> string -> int option

Lookup a byte (uint8) value in a dictionary. Returns None if the key is not found or the value is not a byte.

Sourceval lookup_int16 : t -> string -> int option

Lookup an int16 value in a dictionary. Returns None if the key is not found or the value is not an int16.

Sourceval lookup_uint16 : t -> string -> int option

Lookup a uint16 value in a dictionary. Returns None if the key is not found or the value is not a uint16.

Sourceval lookup_uint32 : t -> string -> uint32 option

Lookup a uint32 value in a dictionary. Returns None if the key is not found or the value is not a uint32.

Sourceval lookup_int64 : t -> string -> int64 option

Lookup an int64 value in a dictionary. Returns None if the key is not found or the value is not an int64.

Sourceval lookup_uint64 : t -> string -> uint64 option

Lookup a uint64 value in a dictionary. Returns None if the key is not found or the value is not a uint64.

Sourceval lookup_double : t -> string -> float option

Lookup a double value in a dictionary. Returns None if the key is not found or the value is not a double.

Sourceval lookup_object_path : t -> string -> string option

Lookup an object path value in a dictionary. Returns None if the key is not found or the value is not an object path.

Sourceval lookup_signature : t -> string -> string option

Lookup a signature value in a dictionary. Returns None if the key is not found or the value is not a signature.

Sourceval lookup_handle : t -> string -> int option

Lookup a handle (file descriptor) value in a dictionary. Returns None if the key is not found or the value is not a handle.

Child Access

Sourceval n_children : t -> int

Get the number of children in a tuple, array, or dictionary

Sourceval get_child_value : t -> int -> t

Get the nth child value. The returned value is a new reference and must be unreferenced by the caller (handled by GC).

Serialization

Sourceval print : t -> bool -> string

Print the variant in GVariant text format. First arg is the variant, second is whether to annotate types.

Sourceval parse : string -> t

Parse a GVariant from its text format representation. Raises Failure on parse error.