From 0b15005e3a42a6613939587fc51abdae750504b2 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Mon, 30 Sep 2013 23:11:52 +0200 Subject: [PATCH] Made GsweHouseData a refcounted boxed type This is to satisfy #7 --- docs/reference/swe-glib/swe-glib-sections.txt | 6 +- src/gswe-house-data-private.h | 7 +- src/gswe-house-data.c | 108 +++++++++++++----- src/gswe-house-data.h | 8 +- src/gswe-moment.c | 8 +- 5 files changed, 101 insertions(+), 36 deletions(-) diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index e50cb58..397293b 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -216,9 +216,13 @@ gswe_house_system_info_get_type
gswe-house-data GsweHouseData -gswe_house_data_get_cusp_position +gswe_house_data_new +gswe_house_data_ref +gswe_house_data_unref gswe_house_data_get_house +gswe_house_data_get_cusp_position gswe_house_data_get_sign +gswe_house_data_get_sign_info GSWE_TYPE_HOUSE_DATA gswe_house_data_get_type diff --git a/src/gswe-house-data-private.h b/src/gswe-house-data-private.h index 98fe5dd..c32a47e 100644 --- a/src/gswe-house-data-private.h +++ b/src/gswe-house-data-private.h @@ -29,10 +29,11 @@ struct _GsweHouseData { gdouble cusp_position; /* the #GsweSignInfo structure associated with the sign in which the house cusp is in */ - GsweSignInfo *sign; -}; + GsweSignInfo *sign_info; -GsweHouseData *gswe_house_data_copy(GsweHouseData *house_data); + /* reference count */ + guint refcount; +}; #endif /* __SWE_GLIB_GSWE_HOUSE_DATA_PRIVATE_H__ */ #else /* not defined __SWE_GLIB_BUILDING__ */ diff --git a/src/gswe-house-data.c b/src/gswe-house-data.c index 254ef93..f92d145 100644 --- a/src/gswe-house-data.c +++ b/src/gswe-house-data.c @@ -17,6 +17,8 @@ */ #include "gswe-types.h" +#include "swe-glib-private.h" +#include "swe-glib.h" #include "gswe-house-data.h" #include "gswe-house-data-private.h" @@ -31,23 +33,69 @@ * #GsweHouseData is a structure that represents a house's position. */ -G_DEFINE_BOXED_TYPE(GsweHouseData, gswe_house_data, (GBoxedCopyFunc)gswe_house_data_copy, (GBoxedFreeFunc)g_free); +G_DEFINE_BOXED_TYPE(GsweHouseData, gswe_house_data, (GBoxedCopyFunc)gswe_house_data_ref, (GBoxedFreeFunc)gswe_house_data_unref); -GsweHouseData * -gswe_house_data_copy(GsweHouseData *house_data) +static void +gswe_house_data_free(GsweHouseData *house_data) { - GsweHouseData *ret = g_new0(GsweHouseData, 1); + if (house_data->sign_info) { + gswe_sign_info_unref(house_data->sign_info); + } - ret->house = house_data->house; - ret->cusp_position = house_data->cusp_position; - ret->sign = house_data->sign; + g_free(house_data); +} + +/** + * gswe_house_data_new: + * + * Creates a new #GsweHouseData with reference count set to 1. + * + * Returns: (transfer full): a new #GsweHouseData + */ +GsweHouseData * +gswe_house_data_new(void) +{ + GsweHouseData *ret; + + ret = g_new0(GsweHouseData, 1); + ret->refcount = 1; return ret; } +/** + * gswe_house_data_ref: + * @house_data: a #GsweHouseData + * + * Increases reference count on @house_data by one. + * + * Returns: (transfer none): the same #GsweHouseData + */ +GsweHouseData * +gswe_house_data_ref(GsweHouseData *house_data) +{ + house_data->refcount++; + + return house_data; +} + +/** + * gswe_house_data_unref: + * @house_data: a #GsweHouseData + * + * Decreases reference count on @house_data by one. If reference count drops to zero, @house_data is freed. + */ +void +gswe_house_data_unref(GsweHouseData *house_data) +{ + if (--house_data->refcount == 0) { + gswe_house_data_free(house_data); + } +} + /** * gswe_house_data_get_house: - * @house_data: (in) (allow-none): a #GsweHouseData + * @house_data: (in): a #GsweHouseData * * Gets the number of the house. * @@ -56,16 +104,12 @@ gswe_house_data_copy(GsweHouseData *house_data) guint gswe_house_data_get_house(GsweHouseData *house_data) { - if (house_data) { - return house_data->house; - } else { - return 0; - } + return house_data->house; } /** * gswe_house_data_get_cusp_position: - * @house_data: (in) (allow-none): a #GsweHouseData + * @house_data: (in): a #GsweHouseData * * Gets the position of the house's cusp. * @@ -74,28 +118,38 @@ gswe_house_data_get_house(GsweHouseData *house_data) gdouble gswe_house_data_get_cusp_position(GsweHouseData *house_data) { - if (house_data) { - return house_data->cusp_position; - } else { - return 0.0; - } + return house_data->cusp_position; } /** * gswe_house_data_get_sign: - * @house_data: (in) (allow-none): a #GsweHouseData + * @house_data: a #GsweHouseData * - * Gets the sign in which the house's cusp is. + * Gets the sign which the house's cusp is in. + * + * Returns: the GsweZodiac of the house cusp's sign + */ +GsweZodiac +gswe_house_data_get_sign(GsweHouseData *house_data) +{ + if (house_data->sign_info) { + return house_data->sign_info->sign; + } else { + return GSWE_SIGN_NONE; + } +} + +/** + * gswe_house_data_get_sign_info: + * @house_data: (in): a #GsweHouseData + * + * Gets the #GsweSignInfo that represents the sign which the house's cusp is in. * * Returns: (transfer none): a #GsweSignInfo representing the sign */ GsweSignInfo * -gswe_house_data_get_sign(GsweHouseData *house_data) +gswe_house_data_get_sign_info(GsweHouseData *house_data) { - if (house_data) { - return house_data->sign; - } else { - return NULL; - } + return house_data->sign_info; } diff --git a/src/gswe-house-data.h b/src/gswe-house-data.h index 93285d0..d700f42 100644 --- a/src/gswe-house-data.h +++ b/src/gswe-house-data.h @@ -37,9 +37,15 @@ typedef struct _GsweHouseData GsweHouseData; GType gswe_house_data_get_type(void); #define GSWE_TYPE_HOUSE_DATA (gswe_house_data_get_type()) +GsweHouseData *gswe_house_data_new(void); + +GsweHouseData *gswe_house_data_ref(GsweHouseData *house_data); +void gswe_house_data_unref(GsweHouseData *house_data); + guint gswe_house_data_get_house(GsweHouseData *house_data); gdouble gswe_house_data_get_cusp_position(GsweHouseData *house_data); -GsweSignInfo *gswe_house_data_get_sign(GsweHouseData *house_data); +GsweZodiac gswe_house_data_get_sign(GsweHouseData *house_data); +GsweSignInfo *gswe_house_data_get_sign_info(GsweHouseData *house_data); G_END_DECLS diff --git a/src/gswe-moment.c b/src/gswe-moment.c index cb3e77d..138bd6c 100644 --- a/src/gswe-moment.c +++ b/src/gswe-moment.c @@ -201,7 +201,7 @@ gswe_moment_finalize(GObject *gobject) { GsweMoment *moment = GSWE_MOMENT(gobject); - g_list_free_full(moment->priv->house_list, g_free); + g_list_free_full(moment->priv->house_list, (GDestroyNotify)gswe_house_data_unref); g_list_free_full(moment->priv->planet_list, (GDestroyNotify)gswe_planet_data_unref); g_list_free_full(moment->priv->aspect_list, (GDestroyNotify)gswe_aspect_data_unref); g_list_free_full(moment->priv->antiscia_list, (GDestroyNotify)gswe_antiscion_data_unref); @@ -502,7 +502,7 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err) return; } - g_list_free_full(moment->priv->house_list, g_free); + g_list_free_full(moment->priv->house_list, (GDestroyNotify)gswe_house_data_unref); moment->priv->house_list = NULL; // If no house system is set, we need no calculations at all. Just leave @@ -535,13 +535,13 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err) * this should not cause trouble yet, though) */ for (i = 12; i >= 1; i--) { GsweSignInfo *sign_info; - GsweHouseData *house_data = g_new0(GsweHouseData, 1); + GsweHouseData *house_data = gswe_house_data_new(); house_data->house = i; house_data->cusp_position = cusps[i]; if ((sign_info = g_hash_table_lookup(gswe_sign_info_table, GINT_TO_POINTER((gint)ceilf(cusps[i] / 30.0)))) == NULL) { - g_list_free_full(moment->priv->house_list, g_free); + g_list_free_full(moment->priv->house_list, (GDestroyNotify)gswe_house_data_unref); moment->priv->house_list = NULL; moment->priv->house_revision = 0; g_set_error(err, GSWE_ERROR, GSWE_ERROR_UNKNOWN_SIGN, "Calculation brought an unknown sign");