diff --git a/src/meson.build b/src/meson.build index e3712d540cb36daddbb9568ce7be222635d582a0..98fdb69f3189f5c0eb409006398340e44e912599 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 0000000000000000000000000000000000000000..29301414c10eb690adbe1421555a5d817eec007a --- /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 0000000000000000000000000000000000000000..be405fbf2e3f9cc1377f238109fe567f81ec018e --- /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 ba839731488b8aa3b4b0b951fcc18431ce346c39..13652264f6cdf84417904826c1ff296e7843c651 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 dd9038dab946fa3ae70eb1a2b50e079fbf093f78..1e2e889390f0b301616119b4ec08410b678e5b8b 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 9ad7a0da429c711790079c2c03796e3510fa9212..3050b1768c52fd811b3954a68769c6481130f556 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);