Skip to content

Instantly share code, notes, and snippets.

@talisein
Created January 4, 2016 04:01
Show Gist options
  • Save talisein/a2245d0f1ec0c89df190 to your computer and use it in GitHub Desktop.
Save talisein/a2245d0f1ec0c89df190 to your computer and use it in GitHub Desktop.
GDate copy problem
/* Compile with:
* gcc `pkg-config --cflags --libs glib-2.0 gobject-2.0` gdate.c
*/
#include <glib-object.h>
#include <stdlib.h>
#include <stdio.h>
#define FOO_TYPE_BAR (foo_bar_get_type ())
G_DECLARE_FINAL_TYPE(FooBar, foo_bar, FOO, BAR, GObject)
typedef struct _FooBar
{
GObject parent;
GDate date;
} FooBar;
G_DEFINE_TYPE(FooBar,foo_bar, G_TYPE_OBJECT);
enum {
PROP_0,
PROP_DATE,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
static void foo_bar_set_property(GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
FooBar *self = FOO_BAR (object);
switch (property_id) {
case PROP_DATE:
self->date = *(GDate*)g_value_get_boxed(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
foo_bar_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
FooBar *self = FOO_BAR (object);
switch (property_id) {
case PROP_DATE:
if (g_date_valid(&self->date))
g_value_set_boxed (value, &self->date);
else {
GDate *d = g_date_new();
g_value_take_boxed (value, d);
}
break;
default:
/* We don't have any other property... */
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
foo_bar_class_init (FooBarClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = foo_bar_set_property;
gobject_class->get_property = foo_bar_get_property;
obj_properties[PROP_DATE] =
g_param_spec_boxed ("date", "Date", "Its a date", G_TYPE_DATE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class,
N_PROPERTIES,
obj_properties);
}
static void
foo_bar_init (FooBar *self)
{
g_date_clear(&self->date, 1);
}
FooBar *foo_bar_new(void)
{
return g_object_new(FOO_TYPE_BAR, NULL);
}
int main()
{
FooBar *bar = foo_bar_new();
GDate *d;
g_object_get(G_OBJECT(bar), "date", &d, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment