From 7b0dd7299c75d2b2aaa9ffc5a0e986a9fa943ba7 Mon Sep 17 00:00:00 2001 From: Igor Gnatenko Date: Thu, 20 Aug 2015 01:32:44 +0300 Subject: [PATCH] tests/gnome/gir: use modern GLib stuff * less boilerplate GObject code * fixes memory leaks in main function * drop unused deps Signed-off-by: Igor Gnatenko --- test cases/frameworks/7 gnome/gir/golib.c | 94 --------------- test cases/frameworks/7 gnome/gir/golib.h | 41 ------- .../frameworks/7 gnome/gir/meson-sample.c | 108 ++++++++++++++++++ .../frameworks/7 gnome/gir/meson-sample.h | 21 ++++ test cases/frameworks/7 gnome/gir/meson.build | 27 +++-- test cases/frameworks/7 gnome/gir/prog.c | 33 +++--- 6 files changed, 160 insertions(+), 164 deletions(-) delete mode 100644 test cases/frameworks/7 gnome/gir/golib.c delete mode 100644 test cases/frameworks/7 gnome/gir/golib.h create mode 100644 test cases/frameworks/7 gnome/gir/meson-sample.c create mode 100644 test cases/frameworks/7 gnome/gir/meson-sample.h diff --git a/test cases/frameworks/7 gnome/gir/golib.c b/test cases/frameworks/7 gnome/gir/golib.c deleted file mode 100644 index 8d70649e3..000000000 --- a/test cases/frameworks/7 gnome/gir/golib.c +++ /dev/null @@ -1,94 +0,0 @@ -#include "golib.h" -#include - -G_DEFINE_TYPE (MesonSample, meson_sample, G_TYPE_OBJECT) - -#define MESON_SAMPLE_GET_PRIVATE(o) \ - (G_TYPE_INSTANCE_GET_PRIVATE ((o), MESON_SAMPLE_TYPE, MesonSamplePrivate)) - -struct _MesonSamplePrivate { - gchar *msg; -}; - -enum { - PROP_0, - PROP_MSG, - N_PROPERTIES -}; - -static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; - -static void meson_sample_init (MesonSample *object) -{ - MesonSamplePrivate *priv = MESON_SAMPLE_GET_PRIVATE (object); - priv->msg = NULL; -} -static void meson_sample_finalize (GObject *object) -{ - MesonSamplePrivate *priv = MESON_SAMPLE_GET_PRIVATE (object); - g_free (priv->msg); - G_OBJECT_CLASS (meson_sample_parent_class)->finalize (object); -} - -static void meson_sample_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) { - MesonSamplePrivate *priv = MESON_SAMPLE_GET_PRIVATE (object); - switch (property_id) { - case PROP_MSG: - g_free (priv->msg); - priv->msg = g_value_dup_string (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} - -static void meson_sample_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) { - MesonSamplePrivate *priv = MESON_SAMPLE_GET_PRIVATE (object); - switch (property_id) { - case PROP_MSG: - g_value_set_string (value, priv->msg); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} - -static void meson_sample_class_init (MesonSampleClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - object_class->set_property = meson_sample_set_property; - object_class->get_property = meson_sample_get_property; - object_class->finalize = meson_sample_finalize; - - obj_properties[PROP_MSG] = - g_param_spec_string ("msg", - "Msg", - "The message to print.", - "propertytext", - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT); - g_object_class_install_properties (object_class, - N_PROPERTIES, - obj_properties); - g_type_class_add_private (object_class, sizeof (MesonSamplePrivate)); -} - -MesonSample* meson_sample_new () { - MesonSample *sample; - sample = g_object_new(MESON_SAMPLE_TYPE, NULL); - return sample; -} - -void meson_sample_func (MesonSample *sample) { - MesonSamplePrivate *priv; - g_return_if_fail (sample != NULL); - priv = MESON_SAMPLE_GET_PRIVATE(sample); - printf("GObject introspection is working, %s!\n", priv->msg); -} diff --git a/test cases/frameworks/7 gnome/gir/golib.h b/test cases/frameworks/7 gnome/gir/golib.h deleted file mode 100644 index 272d65c25..000000000 --- a/test cases/frameworks/7 gnome/gir/golib.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef GOLIB_H -#define GOLIB_H - -#if !defined (MESON_TEST) -#error "MESON_TEST not defined." -#endif - -#include -#include - -#define MESON_SAMPLE_TYPE \ - (meson_sample_get_type()) -#define MESON_SAMPLE(o) \ - (G_TYPE_CHECK_INSTANCE_CAST ((o), MESON_SAMPLE_TYPE, MesonSample)) -#define MESON_SAMPLE_CLASS(c) \ - (G_TYPE_CHECK_CLASS_CAST ((c), MESON_SAMPLE_TYPE, MesonSampleClass)) -#define MESON_IS_SAMPLE(o) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((o), MESON_SAMPLE_TYPE)) -#define MESON_IS_SAMPLE_CLASS(c) \ - (G_TYPE_CHECK_CLASS_TYPE ((c), MESON_SAMPLE_TYPE)) -#define MESON_SAMPLE_GET_CLASS(o) \ - (G_TYPE_INSTANCE_GET_CLASS ((o), MESON_SAMPLE_TYPE, MesonSampleClass)) - -typedef struct _MesonSample MesonSample; -typedef struct _MesonSamplePrivate MesonSamplePrivate; -typedef struct _MesonSampleClass MesonSampleClass; - -struct _MesonSample { - GObject parent; -}; - -struct _MesonSampleClass { - GObjectClass parent; -}; - -GType meson_sample_get_type () G_GNUC_CONST; -MesonSample* meson_sample_new (void); - -void meson_sample_func (MesonSample *sample); - -#endif diff --git a/test cases/frameworks/7 gnome/gir/meson-sample.c b/test cases/frameworks/7 gnome/gir/meson-sample.c new file mode 100644 index 000000000..8f467085f --- /dev/null +++ b/test cases/frameworks/7 gnome/gir/meson-sample.c @@ -0,0 +1,108 @@ +#include "meson-sample.h" + +struct _MesonSample +{ + GObject parent_instance; + + gchar *msg; +}; + +G_DEFINE_TYPE (MesonSample, meson_sample, G_TYPE_OBJECT) + +enum { + PROP_0, + PROP_MSG, + LAST_PROP +}; + +static GParamSpec *gParamSpecs [LAST_PROP]; + +MesonSample * +meson_sample_new (const gchar *msg) +{ + g_return_val_if_fail (msg != NULL, NULL); + + return g_object_new (MESON_TYPE_SAMPLE, + "message", msg, + NULL); +} + +static void +meson_sample_finalize (GObject *object) +{ + MesonSample *self = (MesonSample *)object; + + g_clear_pointer (&self->msg, g_free); + + G_OBJECT_CLASS (meson_sample_parent_class)->finalize (object); +} + +static void +meson_sample_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MesonSample *self = MESON_SAMPLE (object); + + switch (prop_id) + { + case PROP_MSG: + g_value_set_string (value, self->msg); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +meson_sample_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MesonSample *self = MESON_SAMPLE (object); + + switch (prop_id) + { + case PROP_MSG: + self->msg = g_value_dup_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +meson_sample_class_init (MesonSampleClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = meson_sample_finalize; + object_class->get_property = meson_sample_get_property; + object_class->set_property = meson_sample_set_property; + + gParamSpecs [PROP_MSG] = + g_param_spec_string ("message", + "Message", + "The message to print.", + NULL, + (G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs); +} + +static void +meson_sample_init (MesonSample *self) +{ +} + +void +meson_sample_print (MesonSample *self) +{ + g_return_if_fail (MESON_IS_SAMPLE (self)); + + g_print ("Message: %s\n", self->msg); +} diff --git a/test cases/frameworks/7 gnome/gir/meson-sample.h b/test cases/frameworks/7 gnome/gir/meson-sample.h new file mode 100644 index 000000000..0bbe9dbfb --- /dev/null +++ b/test cases/frameworks/7 gnome/gir/meson-sample.h @@ -0,0 +1,21 @@ +#ifndef MESON_SAMPLE_H +#define MESON_SAMPLE_H + +#if !defined (MESON_TEST) +#error "MESON_TEST not defined." +#endif + +#include + +G_BEGIN_DECLS + +#define MESON_TYPE_SAMPLE (meson_sample_get_type()) + +G_DECLARE_FINAL_TYPE (MesonSample, meson_sample, MESON, SAMPLE, GObject) + +MesonSample *meson_sample_new (const gchar *msg); +void meson_sample_print (MesonSample *self); + +G_END_DECLS + +#endif /* MESON_SAMPLE_H */ diff --git a/test cases/frameworks/7 gnome/gir/meson.build b/test cases/frameworks/7 gnome/gir/meson.build index dcc0ad851..c5fce63b8 100644 --- a/test cases/frameworks/7 gnome/gir/meson.build +++ b/test cases/frameworks/7 gnome/gir/meson.build @@ -1,17 +1,20 @@ +libsources = ['meson-sample.c', 'meson-sample.h'] -libsources = ['golib.c', 'golib.h'] - -girexe = executable('girprog', libsources, 'prog.c', -dependencies : [glib, gobj, gir, gmod]) +girexe = executable( + 'girprog', + sources : [libsources, 'prog.c'], + dependencies : [glib, gobj, gir] +) -gnome.generate_gir(girexe, -sources : libsources, -nsversion : '1.0', -namespace : 'Meson', -symbol_prefix : 'meson_', -identifier_prefix : 'Meson', -includes : ['GObject-2.0', 'Gio-2.0'], -install : true +gnome.generate_gir( + girexe, + sources : libsources, + nsversion : '1.0', + namespace : 'Meson', + symbol_prefix : 'meson_', + identifier_prefix : 'Meson', + includes : ['GObject-2.0'], + install : true ) test('gobject introspection', girexe) diff --git a/test cases/frameworks/7 gnome/gir/prog.c b/test cases/frameworks/7 gnome/gir/prog.c index 71584d6f2..64e3cdc58 100644 --- a/test cases/frameworks/7 gnome/gir/prog.c +++ b/test cases/frameworks/7 gnome/gir/prog.c @@ -1,24 +1,23 @@ -#include"golib.h" +#include -#include +#include "meson-sample.h" -int main(int argc, char *argv[]) { - GOptionContext *ctx; - GError *error = NULL; - MesonSample *i; +gint +main (gint argc, + gchar *argv[]) +{ + g_autoptr(GError) error = NULL; - ctx = g_option_context_new(NULL); - g_option_context_add_group(ctx, g_irepository_get_option_group ()); + g_autoptr(GOptionContext) ctx = g_option_context_new (NULL); + g_option_context_add_group (ctx, g_irepository_get_option_group ()); - if (!g_option_context_parse(ctx, &argc, &argv, &error)) { - g_print("sample: %s\n", error->message); - return 1; - } + if (!g_option_context_parse (ctx, &argc, &argv, &error)) { + g_print ("sample: %s\n", error->message); + return 1; + } - i = meson_sample_new(); - meson_sample_func(i); - g_object_unref(G_OBJECT(i)); + g_autoptr(MesonSample) i = meson_sample_new ("Hello, meson!"); + meson_sample_print (i); - return 0; + return 0; } -