Commit 57e3c77d authored by Zander's avatar Zander 💥

Allow for to iterate over strv stored as either a boxed value or variant

Various parts of this implementation are potentially a little naive

Also adds type annotation to `tmpl_symbol_assign_object` allowing
introspection based languages to throw appropriate errors when passing
non-gobjects
parent 138182ef
......@@ -57,6 +57,42 @@ string_get_value (TmplIterator *iter,
return FALSE;
}
static gboolean
strv_move_next (TmplIterator *iter)
{
guint index = GPOINTER_TO_INT (iter->data1);
index++;
if (iter->instance)
{
gchar **strv = iter->instance;
iter->data1 = GINT_TO_POINTER (index);
return strv[index] != 0;
}
return FALSE;
}
static gboolean
strv_get_value (TmplIterator *iter,
GValue *value)
{
guint index = GPOINTER_TO_INT (iter->data1);
if (iter->instance)
{
gchar **strv = iter->instance;
gchar *str = strv[index];
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, str);
return TRUE;
}
return FALSE;
}
static gboolean
list_model_move_next (TmplIterator *iter)
{
......@@ -127,6 +163,32 @@ tmpl_iterator_init (TmplIterator *iter,
iter->data2 = GUINT_TO_POINTER (n_items);
}
}
else if (G_VALUE_HOLDS_VARIANT(value) &&
g_variant_is_of_type (
g_value_get_variant(value), G_VARIANT_TYPE_STRING_ARRAY))
{
iter->instance = (const gchar **) g_variant_get_strv (
g_value_get_variant (value), NULL);
iter->move_next = strv_move_next;
iter->get_value = strv_get_value;
iter->destroy = NULL;
iter->data1 = GINT_TO_POINTER (-1);
}
else if (G_VALUE_HOLDS_BOXED(value))
{
// TODO: On the basis more than just strv can be boxed there
// should be more checks here
iter->instance = (const gchar **) g_value_get_boxed (value);
iter->move_next = strv_move_next;
iter->get_value = strv_get_value;
iter->destroy = NULL;
iter->data1 = GINT_TO_POINTER (-1);
}
else
{
g_critical ("Don't know how to iterate %s",
g_strdup_value_contents (value));
}
/* TODO: More iter types */
}
......
......@@ -300,6 +300,46 @@ tmpl_scope_set_object (TmplScope *self,
tmpl_symbol_assign_object (tmpl_scope_get_full (self, name, TRUE), value);
}
/**
* tmpl_scope_set_variant:
* @self: A #TmplScope
* @name: a name for the symbol
* @value: (nullable): the variant to set it to, or %NULL
*
* Sets the value of the symbol named @name to the variant @value.
*/
void
tmpl_scope_set_variant (TmplScope *self,
const gchar *name,
GVariant *value)
{
g_return_if_fail (self != NULL);
g_return_if_fail (name != NULL);
tmpl_symbol_assign_variant (tmpl_scope_get_full (self, name, TRUE),
value);
}
/**
* tmpl_scope_set_strv:
* @self: A #TmplScope
* @name: a name for the symbol
* @value: (nullable) (array zero-terminated=1): the value to set it to, or %NULL
*
* Sets the value of the symbol named @name to the strv @value.
*/
void
tmpl_scope_set_strv (TmplScope *self,
const gchar *name,
const gchar **value)
{
g_return_if_fail (self != NULL);
g_return_if_fail (name != NULL);
tmpl_symbol_assign_variant (tmpl_scope_get_full (self, name, TRUE),
g_variant_new_strv (value, -1));
}
/**
* tmpl_scope_set_string:
* @self: A #TmplScope
......
......@@ -76,6 +76,17 @@ TMPL_AVAILABLE_IN_ALL
void tmpl_scope_set_object (TmplScope *self,
const gchar *name,
gpointer value);
TMPL_AVAILABLE_IN_3_28
void tmpl_scope_set_strv (TmplScope *self,
const gchar *name,
const gchar **value);
TMPL_AVAILABLE_IN_3_28
void tmpl_scope_set_variant (TmplScope *self,
const gchar *name,
GVariant *value);
TMPL_AVAILABLE_IN_ALL
void tmpl_scope_set_resolver (TmplScope *self,
TmplScopeResolver resolver,
......
......@@ -222,6 +222,13 @@ tmpl_symbol_assign_string (TmplSymbol *self,
g_value_unset (&value);
}
/**
* tmpl_symbol_assign_object:
* @self: A #TmplSymbol
* @v_object: (type GObject.Object) (nullable): a #GObject or %NULL.
*
* Sets the value to the object @v_object.
*/
void
tmpl_symbol_assign_object (TmplSymbol *self,
gpointer v_object)
......@@ -235,3 +242,38 @@ tmpl_symbol_assign_object (TmplSymbol *self,
tmpl_symbol_assign_value (self, &value);
g_value_unset (&value);
}
void
tmpl_symbol_assign_variant (TmplSymbol *self,
GVariant *v_variant)
{
GValue value = G_VALUE_INIT;
g_return_if_fail (self != NULL);
g_value_init (&value, G_TYPE_VARIANT);
g_value_set_variant (&value, v_variant);
tmpl_symbol_assign_value (self, &value);
g_value_unset (&value);
}
/**
* tmpl_symbol_assign_strv:
* @self: A #TmplSymbol
* @strv: (nullable) (array zero-terminated=1): the value to set, or %NULL
*
* Sets the value to the strv @strv.
*/
void
tmpl_symbol_assign_strv (TmplSymbol *self,
const gchar **strv)
{
GValue value = G_VALUE_INIT;
g_return_if_fail (self != NULL);
g_value_init (&value, G_TYPE_VARIANT);
g_value_set_variant (&value, g_variant_new_strv (strv, -1));
tmpl_symbol_assign_value (self, &value);
g_value_unset (&value);
}
......@@ -58,6 +58,14 @@ void tmpl_symbol_assign_string (TmplSymbol *self,
TMPL_AVAILABLE_IN_ALL
void tmpl_symbol_assign_object (TmplSymbol *self,
gpointer v_object);
TMPL_AVAILABLE_IN_3_28
void tmpl_symbol_assign_variant (TmplSymbol *self,
GVariant *v_variant);
TMPL_AVAILABLE_IN_3_28
void tmpl_symbol_assign_strv (TmplSymbol *self,
const gchar **strv);
TMPL_AVAILABLE_IN_ALL
void tmpl_symbol_assign_expr (TmplSymbol *self,
TmplExpr *expr,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment