From 6b545ecaac832a1a753dab0481ccc087ef966d69 Mon Sep 17 00:00:00 2001 From: Bruce Cowan <bruce@bcowan.eu> Date: Wed, 31 Jan 2018 11:32:22 +0000 Subject: [PATCH] Make RugbyPossibility a GObject --- src/meson.build | 1 + src/rugby-possibility.c | 134 ++++++++++++++++++++++++++++++++++++++++ src/rugby-possibility.h | 33 ++++++++++ src/rugby-score-store.c | 13 ++-- src/rugby-scoring.c | 20 ++---- src/rugby-scoring.h | 7 --- 6 files changed, 182 insertions(+), 26 deletions(-) create mode 100644 src/rugby-possibility.c create mode 100644 src/rugby-possibility.h diff --git a/src/meson.build b/src/meson.build index e3712d5..98fdb69 100644 --- a/src/meson.build +++ b/src/meson.build @@ -5,6 +5,7 @@ sources = files( 'rugby-application.c', 'rugby-app-window.c', 'rugby-cell-renderer-score.c', + 'rugby-possibility.c', 'rugby-score-store.c', 'rugby-scoring.c' ) diff --git a/src/rugby-possibility.c b/src/rugby-possibility.c new file mode 100644 index 0000000..2930141 --- /dev/null +++ b/src/rugby-possibility.c @@ -0,0 +1,134 @@ +/* rugby-possibility.c + * + * Copyright Š 2018 Bruce Cowan <bruce@bcowan.eu> + * + * This file is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "rugby-possibility.h" + +struct _RugbyPossibility +{ + GObject parent_instance; + + gint tries; + gint utries; + gint kicks; +}; + +G_DEFINE_TYPE (RugbyPossibility, rugby_possibility, G_TYPE_OBJECT) + +enum +{ + PROP_0, + PROP_TRIES, + PROP_UTRIES, + PROP_KICKS, + N_PROPS +}; + +static GParamSpec *properties [N_PROPS]; + +static void +rugby_possibility_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + RugbyPossibility *self = RUGBY_POSSIBILITY (object); + + switch (prop_id) + { + case PROP_TRIES: + g_value_set_int (value, self->tries); + break; + case PROP_UTRIES: + g_value_set_int (value, self->utries); + break; + case PROP_KICKS: + g_value_set_int (value, self->kicks); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +rugby_possibility_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + RugbyPossibility *self = RUGBY_POSSIBILITY (object); + + switch (prop_id) + { + case PROP_TRIES: + self->tries = g_value_get_int (value); + break; + case PROP_UTRIES: + self->utries = g_value_get_int (value); + break; + case PROP_KICKS: + self->kicks = g_value_get_int (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +rugby_possibility_class_init (RugbyPossibilityClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->get_property = rugby_possibility_get_property; + object_class->set_property = rugby_possibility_set_property; + + properties[PROP_TRIES] = + g_param_spec_int ("tries", "Tries", "Converted tries", + 0, 20, 0, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + properties[PROP_UTRIES] = + g_param_spec_int ("utries", "Utries", "Unconverted tries", + 0, 20, 0, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + properties[PROP_KICKS] = + g_param_spec_int ("kicks", "Kicks", "Penalties and drop goals", + 0, 20, 0, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + + g_object_class_install_properties (object_class, + N_PROPS, + properties); + +} + +static void +rugby_possibility_init (RugbyPossibility *self) +{ +} + +RugbyPossibility * +rugby_possibility_new (gint tries, + gint utries, + gint kicks) +{ + return g_object_new (RUGBY_TYPE_POSSIBILITY, + "tries", tries, + "utries", utries, + "kicks", kicks, + NULL); +} + diff --git a/src/rugby-possibility.h b/src/rugby-possibility.h new file mode 100644 index 0000000..be405fb --- /dev/null +++ b/src/rugby-possibility.h @@ -0,0 +1,33 @@ +/* rugby-possibility.h + * + * Copyright Š 2018 Bruce Cowan <bruce@bcowan.eu> + * + * This file is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define RUGBY_TYPE_POSSIBILITY (rugby_possibility_get_type()) + +G_DECLARE_FINAL_TYPE (RugbyPossibility, rugby_possibility, RUGBY, POSSIBILITY, GObject) + +RugbyPossibility * rugby_possibility_new (gint tries, + gint utries, + gint kicks); + +G_END_DECLS diff --git a/src/rugby-score-store.c b/src/rugby-score-store.c index ba83973..1365226 100644 --- a/src/rugby-score-store.c +++ b/src/rugby-score-store.c @@ -16,6 +16,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "rugby-possibility.h" #include "rugby-score-store.h" #include "rugby-scoring.h" @@ -110,13 +111,17 @@ static void populate_store_foreach (gpointer data, gpointer user_data) { - RugbyPossibility *possiblity = (RugbyPossibility *) data; + RugbyPossibility *possiblity = RUGBY_POSSIBILITY (data); GtkListStore *store = GTK_LIST_STORE (user_data); + gint tries, utries, kicks; GtkTreeIter iter; - gint tries = possiblity->tries; - gint utries = possiblity->utries; - gint kicks = possiblity->kicks; + + g_object_get (possiblity, + "tries", &tries, + "utries", &utries, + "kicks", &kicks, + NULL); g_autoptr (GString) string = g_string_new (NULL); diff --git a/src/rugby-scoring.c b/src/rugby-scoring.c index dd9038d..1e2e889 100644 --- a/src/rugby-scoring.c +++ b/src/rugby-scoring.c @@ -16,15 +16,9 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ - +#include "rugby-possibility.h" #include "rugby-scoring.h" -static inline void -possibility_free (gpointer possibility) -{ - g_slice_free (RugbyPossibility, possibility); -} - GPtrArray * rugby_scoring_get_possibilities (gint score) { @@ -32,9 +26,8 @@ rugby_scoring_get_possibilities (gint score) g_return_val_if_fail (score >= 0, NULL); - array = g_ptr_array_new_with_free_func (possibility_free); + array = g_ptr_array_new_with_free_func (g_object_unref); - gint possibilities = 0; gint max_tries = score / TRY_POINTS; gint max_utries = score / UTRY_POINTS; @@ -51,13 +44,10 @@ rugby_scoring_get_possibilities (gint score) { gint kicks = left / KICK_POINTS; - RugbyPossibility *possibility = g_slice_new (RugbyPossibility); - possibility->tries = tries; - possibility->utries = utries; - possibility->kicks = kicks; - + RugbyPossibility *possibility = rugby_possibility_new (tries, + utries, + kicks); g_ptr_array_add (array, possibility); - possibilities++; } } } diff --git a/src/rugby-scoring.h b/src/rugby-scoring.h index 9ad7a0d..3050b17 100644 --- a/src/rugby-scoring.h +++ b/src/rugby-scoring.h @@ -33,13 +33,6 @@ typedef enum RUGBY_SCORE_TYPE_KICK } RugbyScoreType; -typedef struct -{ - gint tries; - gint utries; - gint kicks; -} RugbyPossibility; - GPtrArray * rugby_scoring_get_possibilities (gint score); gint rugby_scoring_get_max_tries (gint score); gint rugby_scoring_get_max_kicks (gint score); -- GitLab