diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index a0b2849..80aebed 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -194,9 +194,15 @@ gswe_antiscion_data_get_type
gswe-house-system-info GsweHouseSystemInfo +gswe_house_system_info_new +gswe_house_system_info_ref +gswe_house_system_info_unref +gswe_house_system_info_set_house_system gswe_house_system_info_get_house_system -gswe_house_system_info_get_name +gswe_house_system_info_set_sweph_id gswe_house_system_info_get_sweph_id +gswe_house_system_info_set_name +gswe_house_system_info_get_name GSWE_TYPE_HOUSE_SYSTEM_INFO gswe_house_system_info_get_type diff --git a/src/gswe-house-system-info-private.h b/src/gswe-house-system-info-private.h index a276d3f..c10009d 100644 --- a/src/gswe-house-system-info-private.h +++ b/src/gswe-house-system-info-private.h @@ -32,10 +32,10 @@ struct _GsweHouseSystemInfo { /* the name of this house system */ gchar *name; -}; -GsweHouseSystemInfo *gswe_house_system_info_copy(GsweHouseSystemInfo *house_system_info); -void gswe_house_system_info_free(GsweHouseSystemInfo *house_system_info); + /* reference count */ + guint refcount; +}; #endif /* __SWE_GLIB_GSWE_HOUSE_SYSTEM_INFO_PRIVATE_H__ */ #else /* not defined __SWE_GLIB_BUILDING__ */ diff --git a/src/gswe-house-system-info.c b/src/gswe-house-system-info.c index 34d096f..552cc9a 100644 --- a/src/gswe-house-system-info.c +++ b/src/gswe-house-system-info.c @@ -29,40 +29,83 @@ * #GsweHouseSystemInfo stores information of a house system. */ -G_DEFINE_BOXED_TYPE(GsweHouseSystemInfo, gswe_house_system_info, (GBoxedCopyFunc)gswe_house_system_info_copy, (GBoxedFreeFunc)gswe_house_system_info_free); +G_DEFINE_BOXED_TYPE(GsweHouseSystemInfo, gswe_house_system_info, (GBoxedCopyFunc)gswe_house_system_info_ref, (GBoxedFreeFunc)gswe_house_system_info_unref); +static void +gswe_house_system_info_free(GsweHouseSystemInfo *house_system_info) +{ + if (house_system_info->name) { + g_free(house_system_info->name); + } + + g_free(house_system_info); +} + +/** + * gswe_house_system_info_new: + * + * Creates a new #GsweHouseSystemInfo with reference count of 1. + * + * Returns: (transfer full): a new #GsweHouseSystemInfo + */ GsweHouseSystemInfo * -gswe_house_system_info_copy(GsweHouseSystemInfo *house_system_info) +gswe_house_system_info_new(void) { GsweHouseSystemInfo *ret; - if (house_system_info == NULL) { - return NULL; - } - - ret = g_new0(GsweHouseSystemInfo, 1); - ret->house_system = house_system_info->house_system; - ret->sweph_id = house_system_info->sweph_id; - ret->name = g_strdup(house_system_info->name); + ret = g_new0(GsweHouseSystemInfo, 1); + ret->refcount = 1; return ret; } -void -gswe_house_system_info_free(GsweHouseSystemInfo *house_system_info) +/** + * gswe_house_system_info_ref: + * @house_system_info: (in): a #GsweHouseSystemInfo + * + * Increases reference count on @house_system_info by one. + * + * Returns: (transfer none): the same #GsweHouseSystemInfo + */ +GsweHouseSystemInfo * +gswe_house_system_info_ref(GsweHouseSystemInfo *house_system_info) { - if (house_system_info) { - if (house_system_info->name) { - g_free(house_system_info->name); - } + house_system_info->refcount++; - g_free(house_system_info); + return house_system_info; +} + +/** + * gswe_house_system_info_unref: + * @house_system_info: a #GsweHouseSystemInfo + * + * Decreases reference count on @house_system_info by one. If reference count + * drops to zero, @house_system_info is freed. + */ +void +gswe_house_system_info_unref(GsweHouseSystemInfo *house_system_info) +{ + if (--house_system_info->refcount == 0) { + gswe_house_system_info_free(house_system_info); } } +/** + * gswe_house_system_info_set_house_system: + * @house_system_info: (in): a #GsweHouseSystemInfo + * @house_system: a #GsweHouseSystem + * + * Sets up @house_system_info to represent @house_system. + */ +void +gswe_house_system_info_set_house_system(GsweHouseSystemInfo *house_system_info, GsweHouseSystem house_system) +{ + house_system_info->house_system = house_system; +} + /** * gswe_house_system_info_get_house_system: - * @house_system_info: (in) (allow-none): a #GsweHouseSystemInfo + * @house_system_info: (in): a #GsweHouseSystemInfo * * Gets the house system ID this #GsweHouseSystemInfo represents. * @@ -71,16 +114,26 @@ gswe_house_system_info_free(GsweHouseSystemInfo *house_system_info) GsweHouseSystem gswe_house_system_info_get_house_system(GsweHouseSystemInfo *house_system_info) { - if (house_system_info) { - return house_system_info->house_system; - } else { - return GSWE_HOUSE_SYSTEM_NONE; - } + return house_system_info->house_system; +} + +/** + * gswe_house_system_info_set_sweph_id: + * @house_system_info: a #GsweHouseSystemInfo + * @sweph_id: a character recognized by Swiss Ephemeris as a house system + * + * Sets up @house_system_info to represent the Swiss Ephemeris house system + * marked by @sweph_id. + */ +void +gswe_house_system_info_set_sweph_id(GsweHouseSystemInfo *house_system_info, gchar sweph_id) +{ + house_system_info->sweph_id = sweph_id; } /** * gswe_house_system_info_get_sweph_id: - * @house_system_info: (in) (allow-none): a #GsweHouseSystemInfo + * @house_system_info: (in): a #GsweHouseSystemInfo * * Gets the Swiss Ephemeris ID for the house system. * @@ -89,16 +142,29 @@ gswe_house_system_info_get_house_system(GsweHouseSystemInfo *house_system_info) gchar gswe_house_system_info_get_sweph_id(GsweHouseSystemInfo *house_system_info) { - if (house_system_info) { - return house_system_info->sweph_id; - } else { - return 0; + return house_system_info->sweph_id; +} + +/** + * gswe_house_system_info_set_name: + * @house_system_info: a #GsweHouseSystemInfo + * @name: the new name for this house system + * + * Sets the name of @house_system_info. + */ +void +gswe_house_system_info_set_name(GsweHouseSystemInfo *house_system_info, const gchar *name) +{ + if (house_system_info->name) { + g_free(house_system_info->name); } + + house_system_info->name = g_strdup(name); } /** * gswe_house_system_info_get_name: - * @house_system_info: (in) (allow-none): a #GsweHouseSystemInfo + * @house_system_info: (in): a #GsweHouseSystemInfo * * Gets the name of the house system. * @@ -107,10 +173,6 @@ gswe_house_system_info_get_sweph_id(GsweHouseSystemInfo *house_system_info) const gchar * gswe_house_system_info_get_name(GsweHouseSystemInfo *house_system_info) { - if (house_system_info) { - return house_system_info->name; - } else { - return NULL; - } + return house_system_info->name; } diff --git a/src/gswe-house-system-info.h b/src/gswe-house-system-info.h index c4c9b45..e2de9b4 100644 --- a/src/gswe-house-system-info.h +++ b/src/gswe-house-system-info.h @@ -37,8 +37,18 @@ typedef struct _GsweHouseSystemInfo GsweHouseSystemInfo; GType gswe_house_system_info_get_type(void); #define GSWE_TYPE_HOUSE_SYSTEM_INFO (gswe_house_system_info_get_type()) +GsweHouseSystemInfo *gswe_house_system_info_new(void); + +GsweHouseSystemInfo *gswe_house_system_info_ref(GsweHouseSystemInfo *house_system_info); +void gswe_house_system_info_unref(GsweHouseSystemInfo *house_system_info); + +void gswe_house_system_info_set_house_system(GsweHouseSystemInfo *house_system_info, GsweHouseSystem house_system); GsweHouseSystem gswe_house_system_info_get_house_system(GsweHouseSystemInfo *house_system_info); + +void gswe_house_system_info_set_sweph_id(GsweHouseSystemInfo *house_system_info, gchar sweph_id); gchar gswe_house_system_info_get_sweph_id(GsweHouseSystemInfo *house_system_info); + +void gswe_house_system_info_set_name(GsweHouseSystemInfo *house_system_info, const gchar *name); const gchar *gswe_house_system_info_get_name(GsweHouseSystemInfo *house_system_info); G_END_DECLS diff --git a/src/gswe-moment.c b/src/gswe-moment.c index 20929a7..5537601 100644 --- a/src/gswe-moment.c +++ b/src/gswe-moment.c @@ -495,7 +495,7 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err) ascmc[10], jd; gint i; - GsweHouseSystemInfo *house_system_data; + GsweHouseSystemInfo *house_system_info; if (moment->priv->house_revision == moment->priv->revision) { return; @@ -512,7 +512,7 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err) return; } - if ((house_system_data = g_hash_table_lookup(gswe_house_system_info_table, GINT_TO_POINTER(moment->priv->house_system))) == NULL) { + if ((house_system_info = g_hash_table_lookup(gswe_house_system_info_table, GINT_TO_POINTER(moment->priv->house_system))) == NULL) { g_set_error(err, GSWE_ERROR, GSWE_ERROR_UNKNOWN_HSYS, "Unknown house system"); return; @@ -526,7 +526,7 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err) return; } - swe_houses(jd, moment->priv->coordinates.latitude, moment->priv->coordinates.longitude, house_system_data->sweph_id, cusps, ascmc); + swe_houses(jd, moment->priv->coordinates.latitude, moment->priv->coordinates.longitude, house_system_info->sweph_id, cusps, ascmc); /* TODO: SWE house system 'G' (Gauquelin sector cusps) have 36 houses; we * should detect that somehow (house system 'G' is not implemented yet in diff --git a/src/swe-glib.c b/src/swe-glib.c index fd2d157..149b102 100644 --- a/src/swe-glib.c +++ b/src/swe-glib.c @@ -62,7 +62,7 @@ GsweTimestamp *gswe_full_moon_base_date; g_hash_table_replace((ht), GINT_TO_POINTER(s), (v)); #define ADD_HOUSE_SYSTEM(ht, v, i, s, n) \ - (v) = g_new0(GsweHouseSystemInfo, 1); \ + (v) = gswe_house_system_info_new(); \ (v)->house_system = i; \ (v)->sweph_id = s; \ (v)->name = g_strdup(n); \ @@ -96,13 +96,6 @@ GsweTimestamp *gswe_full_moon_base_date; */ G_DEFINE_QUARK(gswe-error-quark, gswe_error); -void -gswe_free_house_system_info(gpointer house_system_info) -{ - g_free(((GsweHouseSystemInfo *)house_system_info)->name); - g_free(house_system_info); -} - /** * gswe_init: * @@ -163,7 +156,7 @@ gswe_init(void) ADD_SIGN(gswe_sign_info_table, sign_info, GSWE_SIGN_AQUARIUS, _("Aquarius"), GSWE_ELEMENT_AIR, GSWE_QUALITY_FIX); ADD_SIGN(gswe_sign_info_table, sign_info, GSWE_SIGN_PISCES, _("Pisces"), GSWE_ELEMENT_WATER, GSWE_QUALITY_MUTABLE); - gswe_house_system_info_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, gswe_free_house_system_info); + gswe_house_system_info_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gswe_house_system_info_unref); ADD_HOUSE_SYSTEM(gswe_house_system_info_table, house_system_info, GSWE_HOUSE_SYSTEM_NONE, 0, _("None")); ADD_HOUSE_SYSTEM(gswe_house_system_info_table, house_system_info, GSWE_HOUSE_SYSTEM_PLACIDUS, 'P', _("Placidus"));