From 84195964abebf8fb5940246ffa0a99022f58e59b Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Fri, 27 Sep 2013 03:09:29 +0200 Subject: [PATCH] Made GsweAntiscionAxisInfo a refcounted boxed type --- docs/reference/swe-glib/swe-glib-sections.txt | 8 + src/gswe-antiscion-axis-info-private.h | 6 +- src/gswe-antiscion-axis-info.c | 191 ++++++++++++++---- src/gswe-antiscion-axis-info.h | 15 +- src/swe-glib.c | 2 +- 5 files changed, 175 insertions(+), 47 deletions(-) diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index 7a8dd85..3ca4d20 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -130,9 +130,17 @@ gswe_aspect_data_get_type
gswe-antiscion-axis-info GsweAntiscionAxisInfo +gswe_antiscion_axis_info_new +gswe_antiscion_axis_info_ref +gswe_antiscion_axis_info_unref +gswe_antiscion_axis_info_set_axis gswe_antiscion_axis_info_get_axis +gswe_antiscion_axis_info_set_name gswe_antiscion_axis_info_get_name +gswe_antiscion_axis_info_set_sign_offset gswe_antiscion_axis_info_get_sign_offset +gswe_antiscion_axis_info_set_start_sign +gswe_antiscion_axis_info_set_start_sign_plain gswe_antiscion_axis_info_get_start_sign GSWE_TYPE_ANTISCION_AXIS_INFO diff --git a/src/gswe-antiscion-axis-info-private.h b/src/gswe-antiscion-axis-info-private.h index 55d1ffa..1bb95d2 100644 --- a/src/gswe-antiscion-axis-info-private.h +++ b/src/gswe-antiscion-axis-info-private.h @@ -34,10 +34,10 @@ struct _GsweAntiscionAxisInfo { /* if TRUE, the axis runs through the middle of its starting sign */ gdouble sign_offset; -}; -GsweAntiscionAxisInfo *gswe_antiscion_axis_info_copy(GsweAntiscionAxisInfo *antiscion_axis_info); -void gswe_antiscion_axis_info_free(GsweAntiscionAxisInfo *antiscion_axis_info); + /* Reference counter */ + guint refcount; +}; #endif /* __SWE_GLIB_GSWE_ANTISCION_AXIS_INFO_PRIVATE_H__ */ #else /* not defined __SWE_GLIB_BUILDING__ */ diff --git a/src/gswe-antiscion-axis-info.c b/src/gswe-antiscion-axis-info.c index 0a3fbeb..dae24c8 100644 --- a/src/gswe-antiscion-axis-info.c +++ b/src/gswe-antiscion-axis-info.c @@ -15,6 +15,8 @@ * You should have received a copy of the GNU General Public License * along with this library; if not, see . */ +#include "swe-glib.h" +#include "swe-glib-private.h" #include "gswe-antiscion-axis-info.h" #include "gswe-antiscion-axis-info-private.h" @@ -28,25 +30,7 @@ * The #GsweAntiscionAxisInfo stores information about an antiscion axis. */ -G_DEFINE_BOXED_TYPE(GsweAntiscionAxisInfo, gswe_antiscion_axis_info, (GBoxedCopyFunc)gswe_antiscion_axis_info_copy, (GBoxedFreeFunc)gswe_antiscion_axis_info_free); - -GsweAntiscionAxisInfo * -gswe_antiscion_axis_info_copy(GsweAntiscionAxisInfo *antiscion_axis_info) -{ - GsweAntiscionAxisInfo *ret; - - if (antiscion_axis_info == NULL) { - return NULL; - } - - ret = g_new0(GsweAntiscionAxisInfo, 1); - ret->axis = antiscion_axis_info->axis; - ret->start_sign = antiscion_axis_info->start_sign; - ret->name = g_strdup(antiscion_axis_info->name); - ret->sign_offset = antiscion_axis_info->sign_offset; - - return ret; -} +G_DEFINE_BOXED_TYPE(GsweAntiscionAxisInfo, gswe_antiscion_axis_info, (GBoxedCopyFunc)gswe_antiscion_axis_info_ref, (GBoxedFreeFunc)gswe_antiscion_axis_info_unref); void gswe_antiscion_axis_info_free(GsweAntiscionAxisInfo *antiscion_axis_info) @@ -60,9 +44,70 @@ gswe_antiscion_axis_info_free(GsweAntiscionAxisInfo *antiscion_axis_info) } } +/** + * gswe_antiscion_axis_info_new: + * + * Creates a new #GsweAntiscionAxisInfo object with reference count set to 1. + * + * Returns: (transfer full): a new #GsweAntiscionAxisInfo + */ +GsweAntiscionAxisInfo * +gswe_antiscion_axis_info_new(void) +{ + GsweAntiscionAxisInfo *ret; + + ret = g_new0(GsweAntiscionAxisInfo, 1); + ret->refcount = 1; + + return ret; +} + +/** + * gswe_antiscion_axis_info_ref: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * + * Increases reference count on @antiscion_axis_info. + * + * Returns: (transfer none): the same #GsweAntiscionAxisInfo + */ +GsweAntiscionAxisInfo * +gswe_antiscion_axis_info_ref(GsweAntiscionAxisInfo *antiscion_axis_info) +{ + antiscion_axis_info->refcount++; + + return antiscion_axis_info; +} + +/** + * gswe_antiscion_axis_info_unref: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * + * Decreases reference count on @antiscion_axis_info. If reference count reaches zero, @antiscion_axis_info is freed. + */ +void +gswe_antiscion_axis_info_unref(GsweAntiscionAxisInfo *antiscion_axis_info) +{ + if (--antiscion_axis_info->refcount == 0) { + gswe_antiscion_axis_info_free(antiscion_axis_info); + } +} + +/** + * gswe_antiscion_axis_info_set_axis: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * @axis: the new axis + * + * Sets the axis ID. + */ +void +gswe_antiscion_axis_info_set_axis(GsweAntiscionAxisInfo *antiscion_axis_info, GsweAntiscionAxis axis) +{ + antiscion_axis_info->axis = axis; +} + /** * gswe_antiscion_axis_info_get_axis: - * @antiscion_axis_info: (in) (allow-none): a #GsweAntiscionAxisInfo + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo * * Gets the axis ID. * @@ -71,16 +116,60 @@ gswe_antiscion_axis_info_free(GsweAntiscionAxisInfo *antiscion_axis_info) GsweAntiscionAxis gswe_antiscion_axis_info_get_axis(GsweAntiscionAxisInfo *antiscion_axis_info) { - if (antiscion_axis_info) { - return antiscion_axis_info->axis; - } else { - return GSWE_ANTISCION_AXIS_NONE; + return antiscion_axis_info->axis; +} + +/** + * gswe_antiscion_axis_info_set_start_sign: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * @sign_info: a #GsweSignInfo to set as the starting sign of @antiscion_axis_info + * + * Sets the starting sign of the axis. + */ +void +gswe_antiscion_axis_info_set_start_sign(GsweAntiscionAxisInfo *antiscion_axis_info, GsweSignInfo *sign_info) +{ + if (antiscion_axis_info->start_sign != NULL) { + gswe_sign_info_unref(antiscion_axis_info->start_sign); } + + antiscion_axis_info->start_sign = gswe_sign_info_ref(sign_info); +} + +/** + * gswe_antiscion_axis_info_set_start_sign_plain: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * @sign: the new starting sign of @antiscion_axis_info + * @err: a #GError + * + * Sets the starting sign of the axis. Unlike + * gswe_antiscion_axis_info_set_start_sign(), this method searches through the + * registered signs for a #GsweSignInfo record, and sets that as the starting + * sign. @err is populated with GSWE_ERROR_UNKNOWN_SIGN if such record can not + * be found. + */ +void +gswe_antiscion_axis_info_set_start_sign_plain(GsweAntiscionAxisInfo *antiscion_axis_info, GsweZodiac sign, GError **err) +{ + GsweSignInfo *sign_info; + + if ((sign_info = g_hash_table_lookup(gswe_sign_info_table, GINT_TO_POINTER(sign))) == NULL) { + g_warning("Trying to fetch an unregistered sign"); + g_set_error(err, GSWE_ERROR, GSWE_ERROR_UNKNOWN_SIGN, "The requested sign is not registered"); + + return; + } + + if (antiscion_axis_info->start_sign != NULL) { + gswe_sign_info_unref(antiscion_axis_info->start_sign); + } + + antiscion_axis_info->start_sign = gswe_sign_info_ref(sign_info); } /** * gswe_antiscion_axis_info_get_start_sign: - * @antiscion_axis_info: (in) (allow-none): a #GsweAntiscionAxisInfo + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo * * Gets the starting sign of the axis. * @@ -89,16 +178,29 @@ gswe_antiscion_axis_info_get_axis(GsweAntiscionAxisInfo *antiscion_axis_info) GsweSignInfo * gswe_antiscion_axis_info_get_start_sign(GsweAntiscionAxisInfo *antiscion_axis_info) { - if (antiscion_axis_info) { - return antiscion_axis_info->start_sign; - } else { - return NULL; + return antiscion_axis_info->start_sign; +} + +/** + * gswe_antiscion_axis_info_set_name: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * @name: the new name of the axis + * + * Sets the name of the axis + */ +void +gswe_antiscion_axis_info_set_name(GsweAntiscionAxisInfo *antiscion_axis_info, const gchar *name) +{ + if (antiscion_axis_info->name != NULL) { + g_free(antiscion_axis_info->name); } + + antiscion_axis_info->name = g_strdup(name); } /** * gswe_antiscion_axis_info_get_name: - * @antiscion_axis_info: (in) (allow-none): a #GsweAntiscionAxisInfo + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo * * Gets the name of the axis. * @@ -107,28 +209,33 @@ gswe_antiscion_axis_info_get_start_sign(GsweAntiscionAxisInfo *antiscion_axis_in const gchar * gswe_antiscion_axis_info_get_name(GsweAntiscionAxisInfo *antiscion_axis_info) { - if (antiscion_axis_info) { - return antiscion_axis_info->name; - } else { - return NULL; - } + return antiscion_axis_info->name; +} + +/** + * gswe_antiscion_axis_info_set_sign_offset: + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo + * @sign_offset: the new sign offset, in degree + * + * Sets the offset at which the axis starts. + */ +void +gswe_antiscion_axis_info_set_sign_offset(GsweAntiscionAxisInfo *antiscion_axis_info, gdouble sign_offset) +{ + antiscion_axis_info->sign_offset = sign_offset; } /** * gswe_antiscion_axis_info_get_sign_offset: - * @antiscion_axis_info: (in) (allow-none): a #GsweAntiscionAxisInfo + * @antiscion_axis_info: (in): a #GsweAntiscionAxisInfo * * Gets the offset at which the axis starts. * * Returns: the offset, in degrees */ -gboolean +gdouble gswe_antiscion_axis_info_get_sign_offset(GsweAntiscionAxisInfo *antiscion_axis_info) { - if (antiscion_axis_info) { - return antiscion_axis_info->sign_offset; - } else { - return 0.0; - } + return antiscion_axis_info->sign_offset; } diff --git a/src/gswe-antiscion-axis-info.h b/src/gswe-antiscion-axis-info.h index 547d71f..d677edf 100644 --- a/src/gswe-antiscion-axis-info.h +++ b/src/gswe-antiscion-axis-info.h @@ -38,10 +38,23 @@ typedef struct _GsweAntiscionAxisInfo GsweAntiscionAxisInfo; GType gswe_antiscion_axis_info_get_type(void); #define GSWE_TYPE_ANTISCION_AXIS_INFO (gswe_antiscion_axis_info_get_type()) +GsweAntiscionAxisInfo *gswe_antiscion_axis_info_new(void); + +GsweAntiscionAxisInfo *gswe_antiscion_axis_info_ref(GsweAntiscionAxisInfo *antiscion_axis_info); +void gswe_antiscion_axis_info_unref(GsweAntiscionAxisInfo *antiscion_axis_info); + +void gswe_antiscion_axis_info_set_axis(GsweAntiscionAxisInfo *antiscion_axis_info, GsweAntiscionAxis axis); GsweAntiscionAxis gswe_antiscion_axis_info_get_axis(GsweAntiscionAxisInfo *antiscion_axis_info); + +void gswe_antiscion_axis_info_set_start_sign(GsweAntiscionAxisInfo *antiscion_axis_info, GsweSignInfo *sign_info); +void gswe_antiscion_axis_info_set_start_sign_plain(GsweAntiscionAxisInfo *antiscion_axis_info, GsweZodiac sign, GError **err); GsweSignInfo *gswe_antiscion_axis_info_get_start_sign(GsweAntiscionAxisInfo *antiscion_axis_info); + +void gswe_antiscion_axis_info_set_name(GsweAntiscionAxisInfo *antiscion_axis_info, const gchar *name); const gchar *gswe_antiscion_axis_info_get_name(GsweAntiscionAxisInfo *antiscion_axis_info); -gboolean gswe_antiscion_axis_info_get_sign_offset(GsweAntiscionAxisInfo *antiscion_axis_info); + +void gswe_antiscion_axis_info_set_sign_offset(GsweAntiscionAxisInfo *antiscion_axis_info, gdouble sign_offset); +gdouble gswe_antiscion_axis_info_get_sign_offset(GsweAntiscionAxisInfo *antiscion_axis_info); G_END_DECLS diff --git a/src/swe-glib.c b/src/swe-glib.c index b39b604..768816d 100644 --- a/src/swe-glib.c +++ b/src/swe-glib.c @@ -79,7 +79,7 @@ GsweTimestamp *gswe_full_moon_base_date; g_hash_table_replace((ht), GINT_TO_POINTER(i), (v)); #define ADD_ANTISCION(ht, v, hts, vs, i, n, s, m) \ - (v) = g_new0(GsweAntiscionAxisInfo, 1); \ + (v) = gswe_antiscion_axis_info_new(); \ (vs) = g_hash_table_lookup((hts), GINT_TO_POINTER(i)); \ (v)->axis = (i); \ (v)->start_sign = (vs); \