Commit e9879247 authored by Benjamin Berg's avatar Benjamin Berg Committed by Guido Gunther

value-object: Add an object to stuff a GValue into a GListModel

This is useful to store arbitrary (but simple) values inside a
HdyComboRow.
parent 7c869153
......@@ -50,6 +50,7 @@
<xi:include href="xml/hdy-leaflet.xml"/>
<xi:include href="xml/hdy-search-bar.xml"/>
<xi:include href="xml/hdy-title-bar.xml"/>
<xi:include href="xml/hdy-value-object.xml"/>
</chapter>
<chapter id="helpers">
......@@ -86,6 +87,11 @@
<xi:include href="xml/api-index-0.0.7.xml"><xi:fallback /></xi:include>
</index>
<index id="api-index-0-0-8" role="0.0.8">
<title>Index of new symbols in 0.0.8</title>
<xi:include href="xml/api-index-0.0.8.xml"><xi:fallback /></xi:include>
</index>
<index id="annotations-glossary">
<title>Annotations glossary</title>
<xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
......
......@@ -43,6 +43,7 @@ G_BEGIN_DECLS
#include "hdy-search-bar.h"
#include "hdy-string-utf8.h"
#include "hdy-title-bar.h"
#include "hdy-value-object.h"
#undef _HANDY_INSIDE
......
/*
* Copyright (C) 2019 Red Hat Inc.
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include <glib/gi18n.h>
#include <gobject/gvaluecollector.h>
#include "hdy-value-object.h"
/**
* SECTION:hdy-value-object
* @short_description: An object representing a #GValue.
* @Title: HdyValueObject
*
* The #HdyValueObject object represents a #GValue, allowing it to be
* used with #GListModel.
*
* Since: 0.0.8
*/
struct _HdyValueObject
{
GObject parent_instance;
GValue value;
};
G_DEFINE_TYPE (HdyValueObject, hdy_value_object, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_VALUE,
N_PROPS
};
static GParamSpec *props [N_PROPS];
/**
* hdy_value_object_new:
* @value: the #GValue to store
*
* Create a new #HdyValueObject.
*
* Returns: a new #HdyValueObject
* Since: 0.0.8
*/
HdyValueObject *
hdy_value_object_new (const GValue *value)
{
return g_object_new (HDY_TYPE_VALUE_OBJECT,
"value", value,
NULL);
}
/**
* hdy_value_object_new_collect: (skip)
* @type: the #GType of the value
* @...: the value to store
*
* Creates a new #HdyValueObject. This is a convenience method which uses
* the G_VALUE_COLLECT() macro internally.
*
* Returns: a new #HdyValueObject
* Since: 0.0.8
*/
HdyValueObject*
hdy_value_object_new_collect (GType type, ...)
{
g_auto(GValue) value = G_VALUE_INIT;
g_autofree gchar *error = NULL;
va_list var_args;
va_start (var_args, type);
G_VALUE_COLLECT_INIT (&value, type, var_args, 0, &error);
va_end (var_args);
if (error)
g_critical ("%s: %s", G_STRFUNC, error);
return g_object_new (HDY_TYPE_VALUE_OBJECT,
"value", &value,
NULL);
}
/**
* hdy_value_object_new_string: (skip)
* @string: (transfer none): the string to store
*
* Creates a new #HdyValueObject. This is a convenience method to create a
* #HdyValueObject that stores a string.
*
* Returns: a new #HdyValueObject
* Since: 0.0.8
*/
HdyValueObject*
hdy_value_object_new_string (const gchar *string)
{
g_auto(GValue) value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, string);
return hdy_value_object_new (&value);
}
/**
* hdy_value_object_new_take_string: (skip)
* @string: (transfer full): the string to store
*
* Creates a new #HdyValueObject. This is a convenience method to create a
* #HdyValueObject that stores a string taking ownership of it.
*
* Returns: a new #HdyValueObject
* Since: 0.0.8
*/
HdyValueObject*
hdy_value_object_new_take_string (gchar *string)
{
g_auto(GValue) value = G_VALUE_INIT;
g_value_init (&value, G_TYPE_STRING);
g_value_take_string (&value, string);
return hdy_value_object_new (&value);
}
static void
hdy_value_object_finalize (GObject *object)
{
HdyValueObject *self = HDY_VALUE_OBJECT (object);
g_value_unset (&self->value);
G_OBJECT_CLASS (hdy_value_object_parent_class)->finalize (object);
}
static void
hdy_value_object_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
HdyValueObject *self = HDY_VALUE_OBJECT (object);
switch (prop_id)
{
case PROP_VALUE:
g_value_set_boxed (value, &self->value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
hdy_value_object_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
HdyValueObject *self = HDY_VALUE_OBJECT (object);
GValue *real_value;
switch (prop_id)
{
case PROP_VALUE:
/* construct only */
real_value = g_value_get_boxed (value);
g_value_init (&self->value, real_value->g_type);
g_value_copy (real_value, &self->value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
hdy_value_object_class_init (HdyValueObjectClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = hdy_value_object_finalize;
object_class->get_property = hdy_value_object_get_property;
object_class->set_property = hdy_value_object_set_property;
props[PROP_VALUE] =
g_param_spec_boxed ("value", C_("HdyValueObjectClass", "Value"),
C_("HdyValueObjectClass", "The contained value"),
G_TYPE_VALUE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class,
N_PROPS,
props);
}
static void
hdy_value_object_init (HdyValueObject *self)
{
}
/**
* hdy_value_object_get_value:
* @value: the #HdyValueObject
*
* Return the contained value.
*
* Returns: (transfer none): the contained #GValue
* Since: 0.0.8
*/
const GValue*
hdy_value_object_get_value (HdyValueObject *value)
{
return &value->value;
}
/**
* hdy_value_object_copy_value:
* @value: the #HdyValueObject
* @dest: #GValue with correct type to copy into
*
* Copy data from the contained #GValue into @dest.
*
* Since: 0.0.8
*/
void
hdy_value_object_copy_value (HdyValueObject *value,
GValue *dest)
{
g_value_copy (&value->value, dest);
}
/**
* hdy_value_object_get_string:
* @value: the #HdyValueObject
*
* Returns the contained string if the value is of type #G_TYPE_STRING.
*
* Returns: (transfer none): the contained string
* Since: 0.0.8
*/
const gchar*
hdy_value_object_get_string (HdyValueObject *value)
{
return g_value_get_string (&value->value);
}
/**
* hdy_value_object_dup_string:
* @value: the #HdyValueObject
*
* Returns a copy of the contained string if the value is of type
* #G_TYPE_STRING.
*
* Returns: (transfer full): a copy of the contained string
* Since: 0.0.8
*/
gchar*
hdy_value_object_dup_string (HdyValueObject *value)
{
return g_value_dup_string (&value->value);
}
/*
* Copyright (C) 2019 Red Hat Inc.
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#pragma once
#if !defined(_HANDY_INSIDE) && !defined(HANDY_COMPILATION)
#error "Only <handy.h> can be included directly."
#endif
#include <gio/gio.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define HDY_TYPE_VALUE_OBJECT (hdy_value_object_get_type())
G_DECLARE_FINAL_TYPE (HdyValueObject, hdy_value_object, HDY, VALUE_OBJECT, GObject)
HdyValueObject *hdy_value_object_new (const GValue *value);
HdyValueObject *hdy_value_object_new_collect (GType type,
...);
HdyValueObject *hdy_value_object_new_string (const gchar *string);
HdyValueObject *hdy_value_object_new_take_string (gchar *string);
const GValue* hdy_value_object_get_value (HdyValueObject *value);
void hdy_value_object_copy_value (HdyValueObject *value,
GValue *dest);
const gchar* hdy_value_object_get_string (HdyValueObject *value);
gchar* hdy_value_object_dup_string (HdyValueObject *value);
G_END_DECLS
......@@ -66,6 +66,7 @@ src_headers = [
'hdy-search-bar.h',
'hdy-string-utf8.h',
'hdy-title-bar.h',
'hdy-value-object.h',
]
sed = find_program('sed', required: true)
......@@ -98,6 +99,7 @@ src_sources = [
'hdy-search-bar.c',
'hdy-string-utf8.c',
'hdy-title-bar.c',
'hdy-value-object.c',
]
libhandy_public_headers += files(src_headers)
......
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