From dcd58b5dd1bb6696bfc0083a47520c30278bdec7 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Fri, 27 Sep 2013 18:32:44 +0200 Subject: [PATCH] Changed GswePlanetInfo to a refcounted boxed type --- docs/reference/swe-glib/swe-glib-sections.txt | 9 + src/gswe-planet-info-private.h | 6 +- src/gswe-planet-info.c | 183 +++++++++++++----- src/gswe-planet-info.h | 16 ++ src/swe-glib.c | 2 +- 5 files changed, 167 insertions(+), 49 deletions(-) diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index 3ca4d20..981ffb5 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -74,11 +74,20 @@ gswe_sign_info_get_type
gswe-planet-info GswePlanetInfo +gswe_planet_info_new +gswe_planet_info_ref +gswe_planet_info_unref +gswe_planet_info_set_name gswe_planet_info_get_name +gswe_planet_info_set_orb gswe_planet_info_get_orb +gswe_planet_info_set_planet gswe_planet_info_get_planet +gswe_planet_info_set_points gswe_planet_info_get_points +gswe_planet_info_set_real_body gswe_planet_info_get_real_body +gswe_planet_info_set_sweph_id gswe_planet_info_get_sweph_id GSWE_TYPE_PLANET_INFO diff --git a/src/gswe-planet-info-private.h b/src/gswe-planet-info-private.h index 245801d..aed7909 100644 --- a/src/gswe-planet-info-private.h +++ b/src/gswe-planet-info-private.h @@ -40,10 +40,10 @@ struct _GswePlanetInfo { /* the value this planet counts in the element/quality points table */ gint points; -}; -void gswe_planet_info_free(GswePlanetInfo *planet_info); -GswePlanetInfo *gswe_planet_info_copy(GswePlanetInfo *planet_info); + /* reference counter */ + guint refcount; +}; #endif /* __SWE_GLIB_GSWE_PLANET_INFO_PRIVATE_H__ */ #else /* not defined __SWE_GLIB_BUILDING__ */ diff --git a/src/gswe-planet-info.c b/src/gswe-planet-info.c index 5393db3..368f8a5 100644 --- a/src/gswe-planet-info.c +++ b/src/gswe-planet-info.c @@ -30,33 +30,79 @@ * The #GswePlanetInfo structure stores information about a planet. */ -G_DEFINE_BOXED_TYPE(GswePlanetInfo, gswe_planet_info, (GBoxedCopyFunc)gswe_planet_info_copy, (GBoxedFreeFunc)gswe_planet_info_free); +G_DEFINE_BOXED_TYPE(GswePlanetInfo, gswe_planet_info, (GBoxedCopyFunc)gswe_planet_info_ref, (GBoxedFreeFunc)gswe_planet_info_unref); +/** + * gswe_planet_info_new: + * + * Creates a new #GswePlanetInfo object with reference count set to 1. + * + * Returns: a new #GswePlanetInfo + */ GswePlanetInfo * -gswe_planet_info_copy(GswePlanetInfo *planet_info) +gswe_planet_info_new(void) { - GswePlanetInfo *ret = g_new0(GswePlanetInfo, 1); + GswePlanetInfo *ret; - ret->planet = planet_info->planet; - ret->sweph_id = planet_info->sweph_id; - ret->real_body = planet_info->real_body; - ret->orb = planet_info->orb; - ret->name = g_strdup(planet_info->name); - ret->points = planet_info->points; + ret = g_new0(GswePlanetInfo, 1); + ret->refcount = 1; return ret; } -void +/** + * gswe_planet_info_ref: + * @planet_info: (in): a #GswePlanetInfo + * + * Increases reference count on @planet_info. + * + * Returns: (transfer none): the same #GswePlanetData + */ +GswePlanetInfo * +gswe_planet_info_ref(GswePlanetInfo *planet_info) +{ + planet_info->refcount++; + + return planet_info; +} + +static void gswe_planet_info_free(GswePlanetInfo *planet_info) { g_free(planet_info->name); g_free(planet_info); } +/** + * gswe_planet_info_unref: + * @planet_info: a #GswePlanetInfo + * + * Decreases reference count on @planet_info. If reference count reaches zero, @planet_info is freed. + */ +void +gswe_planet_info_unref(GswePlanetInfo *planet_info) +{ + if (--planet_info->refcount == 0) { + gswe_planet_info_free(planet_info); + } +} + +/** + * gswe_planet_info_set_planet: + * @planet_info: a #GswePlanetInfo + * @planet: the new planet ID + * + * Sets the planet ID that will be represented by @planet_info. + */ +void +gswe_planet_info_set_planet(GswePlanetInfo *planet_info, GswePlanet planet) +{ + planet_info->planet = planet; +} + /** * gswe_planet_info_get_planet: - * @planet_info: (in) (allow-none): A #GswePlanetInfo + * @planet_info: (in): A #GswePlanetInfo * * Gets the planet ID represented by this #GswePlanetInfo. * @@ -65,16 +111,25 @@ gswe_planet_info_free(GswePlanetInfo *planet_info) GswePlanet gswe_planet_info_get_planet(GswePlanetInfo *planet_info) { - if (planet_info) { - return planet_info->planet; - } else { - return GSWE_PLANET_NONE; - } + return planet_info->planet; +} + +/** + * gswe_planet_info_set_sweph_id: + * @planet_info: a #GswePlanetInfo + * @sweph_id: the new Swiss Ephemeris planet ID + * + * Sets the Swiss Ephemeris planet_id associated with this planet. + */ +void +gswe_planet_info_set_sweph_id(GswePlanetInfo *planet_info, gint32 sweph_id) +{ + planet_info->sweph_id = sweph_id; } /** * gswe_planet_info_get_sweph_id: - * @planet_info: (in) (allow-none): A #GswePlanetInfo + * @planet_info: (in): A #GswePlanetInfo * * Gets the Swiss Ephemeris planet ID associated with this planet. * @@ -83,16 +138,26 @@ gswe_planet_info_get_planet(GswePlanetInfo *planet_info) gint32 gswe_planet_info_get_sweph_id(GswePlanetInfo *planet_info) { - if (planet_info) { - return planet_info->sweph_id; - } else { - return -1; - } + return planet_info->sweph_id; +} + +/** + * gswe_planet_info_set_real_body: + * @planet_info: a #GswePlanetInfo + * @real_body: a boolean that indicates if this planet is an object recognized + * by Swiss Ephemeris + * + * Sets whether this planet is an object recognized by the Swiss Ephemeris library. + */ +void +gswe_planet_info_set_real_body(GswePlanetInfo *planet_info, gboolean real_body) +{ + planet_info->real_body = real_body; } /** * gswe_planet_info_get_real_body: - * @planet_info: (in) (allow-none): A #GswePlanetInfo + * @planet_info: (in): A #GswePlanetInfo * * Checks weather this planet is a real celestial body (e.g. it has a Swiss Ephemeris planet ID) or not. * @@ -101,16 +166,26 @@ gswe_planet_info_get_sweph_id(GswePlanetInfo *planet_info) gboolean gswe_planet_info_get_real_body(GswePlanetInfo *planet_info) { - if (planet_info) { - return planet_info->real_body; - } else { - return FALSE; - } + return planet_info->real_body; +} + +/** + * gswe_planet_info_set_orb: + * @planet_info: a #GswePlanetInfo + * @orb: the planet's own orb + * + * Sets the orb of @planet_info. This value is used in aspect and antiscion + * calculations. + */ +void +gswe_planet_info_set_orb(GswePlanetInfo *planet_info, gdouble orb) +{ + planet_info->orb = orb; } /** * gswe_planet_info_get_orb: - * @planet_info: (in) (allow-none): A #GswePlanetInfo + * @planet_info: (in): A #GswePlanetInfo * * Gets the orb of the planet. * @@ -119,16 +194,29 @@ gswe_planet_info_get_real_body(GswePlanetInfo *planet_info) gdouble gswe_planet_info_get_orb(GswePlanetInfo *planet_info) { - if (planet_info) { - return planet_info->orb; - } else { - return -1.0; + return planet_info->orb; +} + +/** + * gswe_planet_info_set_name: + * @planet_info: (in): a #GswePlanetInfo + * @name: (in): the new name of the planet + * + * Sets the name of @planet_info. + */ +void +gswe_planet_info_set_name(GswePlanetInfo *planet_info, const gchar *name) +{ + if (planet_info->name) { + g_free(planet_info->name); } + + planet_info->name = g_strdup(name); } /** * gswe_planet_info_get_name: - * @planet_info: (in) (allow-none): A #GswePlanetInfo + * @planet_info: (in): A #GswePlanetInfo * * Gets the name of the planet. * @@ -137,16 +225,25 @@ gswe_planet_info_get_orb(GswePlanetInfo *planet_info) const gchar * gswe_planet_info_get_name(GswePlanetInfo *planet_info) { - if (planet_info) { - return planet_info->name; - } else { - return NULL; - } + return planet_info->name; +} + +/** + * gswe_planet_info_set_points: + * @planet_info: a #GswePlanetInfo + * @points: the new point value + * + * Sets the point value of @planet_info. This value is used in points calculations. + */ +void +gswe_planet_info_set_points(GswePlanetInfo *planet_info, gint points) +{ + planet_info->points = points; } /** * gswe_planet_info_get_points: - * @planet_info: (in) (allow-none): A #GswePlanetInfo + * @planet_info: (in): A #GswePlanetInfo * * Gets the value this planet counts in point calculations. * @@ -155,10 +252,6 @@ gswe_planet_info_get_name(GswePlanetInfo *planet_info) gint gswe_planet_info_get_points(GswePlanetInfo *planet_info) { - if (planet_info) { - return planet_info->points; - } else { - return 0; - } + return planet_info->points; } diff --git a/src/gswe-planet-info.h b/src/gswe-planet-info.h index e832b90..5e1e26f 100644 --- a/src/gswe-planet-info.h +++ b/src/gswe-planet-info.h @@ -37,11 +37,27 @@ typedef struct _GswePlanetInfo GswePlanetInfo; GType gswe_planet_info_get_type(void); #define GSWE_TYPE_PLANET_INFO (gswe_planet_info_get_type()) +GswePlanetInfo *gswe_planet_info_new(void); + +GswePlanetInfo *gswe_planet_info_ref(GswePlanetInfo *planet_info); +void gswe_planet_info_unref(GswePlanetInfo *planet_info); + +void gswe_planet_info_set_planet(GswePlanetInfo *planet_info, GswePlanet planet); GswePlanet gswe_planet_info_get_planet(GswePlanetInfo *planet_info); + +void gswe_planet_info_set_sweph_id(GswePlanetInfo *planet_info, gint32 sweph_id); gint32 gswe_planet_info_get_sweph_id(GswePlanetInfo *planet_info); + +void gswe_planet_info_set_real_body(GswePlanetInfo *planet_info, gboolean real_body); gboolean gswe_planet_info_get_real_body(GswePlanetInfo *planet_info); + +void gswe_planet_info_set_orb(GswePlanetInfo *planet_info, gdouble orb); gdouble gswe_planet_info_get_orb(GswePlanetInfo *planet_info); + +void gswe_planet_info_set_name(GswePlanetInfo *planet_info, const gchar *name); const gchar *gswe_planet_info_get_name(GswePlanetInfo *planet_info); + +void gswe_planet_info_set_points(GswePlanetInfo *planet_info, gint points); gint gswe_planet_info_get_points(GswePlanetInfo *planet_info); G_END_DECLS diff --git a/src/swe-glib.c b/src/swe-glib.c index 8767f26..d93b48f 100644 --- a/src/swe-glib.c +++ b/src/swe-glib.c @@ -44,7 +44,7 @@ GHashTable *gswe_antiscion_axis_info_table; GsweTimestamp *gswe_full_moon_base_date; #define ADD_PLANET(ht, v, i, s, r, n, o, h) \ - (v) = g_new0(GswePlanetInfo, 1); \ + (v) = gswe_planet_info_new(); \ (v)->planet = (i); \ (v)->sweph_id = (s); \ (v)->real_body = (r); \