From f92caa4e1aceedf920323f93277991eadd38247a Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Mon, 30 Sep 2013 22:39:46 +0200 Subject: [PATCH] Made GsweMoonPhaseData a refcounted boxed type This is to satisfy #7 --- docs/reference/swe-glib/swe-glib-sections.txt | 5 +- src/gswe-moment.c | 28 ++++---- src/gswe-moon-phase-data-private.h | 5 +- src/gswe-moon-phase-data.c | 65 ++++++++++--------- src/gswe-moon-phase-data.h | 7 +- 5 files changed, 62 insertions(+), 48 deletions(-) diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index ad5e78a..a0b2849 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -42,9 +42,10 @@ gswe_moment_get_type
gswe-moon-phase-data -gswe_moon_phase_data_set_phase +gswe_moon_phase_data_new +gswe_moon_phase_data_ref +gswe_moon_phase_data_unref gswe_moon_phase_data_get_phase -gswe_moon_phase_data_set_illumination gswe_moon_phase_data_get_illumination GsweMoonPhaseData diff --git a/src/gswe-moment.c b/src/gswe-moment.c index 5a5fe00..20929a7 100644 --- a/src/gswe-moment.c +++ b/src/gswe-moment.c @@ -75,7 +75,7 @@ struct _GsweMomentPrivate { GHashTable *element_points; GHashTable *quality_points; guint moon_phase_revision; - GsweMoonPhaseData moon_phase; + GsweMoonPhaseData *moon_phase; GList *aspect_list; guint aspect_revision; GList *antiscia_list; @@ -166,6 +166,7 @@ gswe_moment_init(GsweMoment *moment) moment->priv->planet_list = NULL; moment->priv->aspect_list = NULL; moment->priv->antiscia_list = NULL; + moment->priv->moon_phase = gswe_moon_phase_data_new(); moment->priv->element_points = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL); moment->priv->quality_points = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL); moment->priv->house_revision = 0; @@ -203,6 +204,7 @@ gswe_moment_finalize(GObject *gobject) g_list_free_full(moment->priv->house_list, g_free); 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); + gswe_moon_phase_data_unref(moment->priv->moon_phase); G_OBJECT_CLASS(gswe_moment_parent_class)->finalize(gobject); } @@ -964,7 +966,7 @@ gswe_moment_get_moon_phase(GsweMoment *moment, GError **err) jdb; if (moment->priv->moon_phase_revision == moment->priv->revision) { - return &(moment->priv->moon_phase); + return moment->priv->moon_phase; } jd = gswe_timestamp_get_julian_day(moment->priv->timestamp, err); @@ -990,31 +992,31 @@ gswe_moment_get_moon_phase(GsweMoment *moment, GError **err) g_error("Error during Moon phase calculation!"); } - moment->priv->moon_phase.illumination = (50.0 - fabs(phase_percent - 50.0)) * 2; + moment->priv->moon_phase->illumination = (50.0 - fabs(phase_percent - 50.0)) * 2; if (phase_percent == 0) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_NEW; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_NEW; } else if (phase_percent < 25) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WAXING_CRESCENT; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_CRESCENT; } else if (phase_percent == 25) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WAXING_HALF; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_HALF; } else if (phase_percent < 50) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WAXING_GIBBOUS; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_GIBBOUS; } else if (phase_percent == 50) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_FULL; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_FULL; } else if (phase_percent < 75) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WANING_GIBBOUS; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_GIBBOUS; } else if (phase_percent == 75) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WANING_HALF; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_HALF; } else if (phase_percent < 100) { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WANING_CRESCENT; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_CRESCENT; } else { - moment->priv->moon_phase.phase = GSWE_MOON_PHASE_DARK; + moment->priv->moon_phase->phase = GSWE_MOON_PHASE_DARK; } moment->priv->moon_phase_revision = moment->priv->revision; - return &(moment->priv->moon_phase); + return moment->priv->moon_phase; } static gint diff --git a/src/gswe-moon-phase-data-private.h b/src/gswe-moon-phase-data-private.h index e60acf7..afb4833 100644 --- a/src/gswe-moon-phase-data-private.h +++ b/src/gswe-moon-phase-data-private.h @@ -28,9 +28,10 @@ struct _GsweMoonPhaseData { /* the illumination percentage of the Moon */ gdouble illumination; -}; -GsweMoonPhaseData *gswe_moon_phase_data_copy(GsweMoonPhaseData *moon_phase_data); + /* reference count */ + guint refcount; +}; #endif /* __SWE_GLIB_GSWE_MOON_PHASE_DATA_PRIVATE_H__ */ #else /* not defined __SWE_GLIB_BUILDING__ */ diff --git a/src/gswe-moon-phase-data.c b/src/gswe-moon-phase-data.c index ff6ace8..dc4975e 100644 --- a/src/gswe-moon-phase-data.c +++ b/src/gswe-moon-phase-data.c @@ -32,37 +32,59 @@ * Moon, including its illumination percentage. */ -G_DEFINE_BOXED_TYPE(GsweMoonPhaseData, gswe_moon_phase_data, (GBoxedCopyFunc)gswe_moon_phase_data_copy, (GBoxedFreeFunc)g_free); +G_DEFINE_BOXED_TYPE(GsweMoonPhaseData, gswe_moon_phase_data, (GBoxedCopyFunc)gswe_moon_phase_data_ref, (GBoxedFreeFunc)gswe_moon_phase_data_unref); +/** + * gswe_moon_phase_data_new: + * + * Creates a new #GsweMoonPhaseData object with reference count set to 1. + * + * Returns: (transfer full): a new #GsweMoonPhaseData object + */ GsweMoonPhaseData * -gswe_moon_phase_data_copy(GsweMoonPhaseData *moon_phase_data) +gswe_moon_phase_data_new(void) { - GsweMoonPhaseData *ret = g_new0(struct _GsweMoonPhaseData, 1); + GsweMoonPhaseData *ret; - ret->phase = moon_phase_data->phase; - ret->illumination = moon_phase_data->illumination; + ret = g_new0(GsweMoonPhaseData, 1); + ret->refcount = 1; return ret; } /** - * gswe_moon_phase_data_set_phase: - * @moon_phase_data: a GsweMoonPhaseData - * @phase: the phase to set + * gswe_moon_phase_data_ref: + * @moon_phase_data: (in): a #GsweMoonPhaseData * - * Sets the phase of the Moon in the given GsweMoonPhaseData. + * Increases reference count on @moon_phase_data by one. + * + * Returns: (transfer none): the same #GsweMoonPhaseData + */ +GsweMoonPhaseData * +gswe_moon_phase_data_ref(GsweMoonPhaseData *moon_phase_data) +{ + moon_phase_data->refcount++; + + return moon_phase_data; +} + +/** + * gswe_moon_phase_data_unref: + * @moon_phase_data: (in): a #GsweMoonPhaseData + * + * Decreases reference count on @moon_phase_data by one. If reference count drops to zero, @moon_phase_data is freed. */ void -gswe_moon_phase_data_set_phase(GsweMoonPhaseData *moon_phase_data, GsweMoonPhase phase) +gswe_moon_phase_data_unref(GsweMoonPhaseData *moon_phase_data) { - if (moon_phase_data) { - moon_phase_data->phase = phase; + if (--moon_phase_data->refcount == 0) { + g_free(moon_phase_data); } } /** * gswe_moon_phase_data_get_phase: - * @moon_phase_data: a GsweMoonPhaseData + * @moon_phase_data: (in): a GsweMoonPhaseData * * Gets the phase of the Moon in the given GsweMoonPhaseData. * @@ -78,24 +100,9 @@ gswe_moon_phase_data_get_phase(GsweMoonPhaseData *moon_phase_data) } } -/** - * gswe_moon_phase_data_set_illumination: - * @moon_phase_data: a GsweMoonPhaseData - * @illumination: the illumination to set - * - * Sets the illumination percentage in the given GsweMoonPhaseData. - */ -void -gswe_moon_phase_data_set_illumination(GsweMoonPhaseData *moon_phase_data, gdouble illumination) -{ - if (moon_phase_data) { - moon_phase_data->illumination = illumination; - } -} - /** * gswe_moon_phase_data_get_illumination: - * @moon_phase_data: a GsweMoonPhaseData + * @moon_phase_data: (in): a GsweMoonPhaseData * * Gets the illumination percentage from the given GsweMoonPhaseData. * diff --git a/src/gswe-moon-phase-data.h b/src/gswe-moon-phase-data.h index ddff08b..22ef372 100644 --- a/src/gswe-moon-phase-data.h +++ b/src/gswe-moon-phase-data.h @@ -34,9 +34,12 @@ G_BEGIN_DECLS */ typedef struct _GsweMoonPhaseData GsweMoonPhaseData; -void gswe_moon_phase_data_set_phase(GsweMoonPhaseData *moon_phase_data, GsweMoonPhase phase); +GsweMoonPhaseData *gswe_moon_phase_data_new(void); + +GsweMoonPhaseData *gswe_moon_phase_data_ref(GsweMoonPhaseData *moon_phase_data); +void gswe_moon_phase_data_unref(GsweMoonPhaseData *moon_phase_data); + GsweMoonPhase gswe_moon_phase_data_get_phase(GsweMoonPhaseData *moon_phase_data); -void gswe_moon_phase_data_set_illumination(GsweMoonPhaseData *moon_phase_data, gdouble illumination); gdouble gswe_moon_phase_data_get_illumination(GsweMoonPhaseData *moon_phase_data); GType gswe_moon_phase_data_get_type(void);