From 7564183aa2ebe51dd50b776ef904441bec783714 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Mon, 30 Sep 2013 21:31:21 +0200 Subject: [PATCH] Made GsweAspectInfo a refcounted boxed type This is to satisfy #7 --- docs/reference/swe-glib/swe-glib-sections.txt | 15 +- src/gswe-aspect-info-private.h | 6 +- src/gswe-aspect-info.c | 182 ++++++++++++++---- src/gswe-aspect-info.h | 16 ++ src/swe-glib.c | 11 +- 5 files changed, 176 insertions(+), 54 deletions(-) diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index 981ffb5..c1f75ee 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -111,12 +111,21 @@ gswe_planet_data_get_type
gswe-aspect-info GsweAspectInfo +gswe_aspect_info_new +gswe_aspect_info_ref +gswe_aspect_info_unref +gswe_aspect_info_set_aspect gswe_aspect_info_get_aspect -gswe_aspect_info_get_harmonic -gswe_aspect_info_get_major +gswe_aspect_info_set_name gswe_aspect_info_get_name -gswe_aspect_info_get_orb_modifier +gswe_aspect_info_set_size gswe_aspect_info_get_size +gswe_aspect_info_set_orb_modifier +gswe_aspect_info_get_orb_modifier +gswe_aspect_info_set_harmonic +gswe_aspect_info_get_harmonic +gswe_aspect_info_set_major +gswe_aspect_info_get_major GSWE_TYPE_ASPECT_INFO gswe_aspect_info_get_type diff --git a/src/gswe-aspect-info-private.h b/src/gswe-aspect-info-private.h index 14a2347..804ac16 100644 --- a/src/gswe-aspect-info-private.h +++ b/src/gswe-aspect-info-private.h @@ -40,10 +40,10 @@ struct _GsweAspectInfo { /* shows whether this aspect is major (Ptolemaic) or not */ gboolean major; -}; -void gswe_aspect_info_free(GsweAspectInfo *aspect_info); -GsweAspectInfo *gswe_aspect_info_copy(GsweAspectInfo *aspect_info); + /** reference count */ + guint refcount; +}; #endif /* __SWE_GLIB_GSWE_ASPECT_INFO_PRIVATE_H__ */ #else /* not defined __SWE_GLIB_BUILDING__ */ diff --git a/src/gswe-aspect-info.c b/src/gswe-aspect-info.c index a858e97..28d1421 100644 --- a/src/gswe-aspect-info.c +++ b/src/gswe-aspect-info.c @@ -29,9 +29,17 @@ * @include: swe-glib.h * * The #GsweAspectInfo stores information about an aspect. + * + * Using set_* type of funcions on an already registered + * #GsweAspectInfo can currently cause undocumented side effects, if a + * #GsweMoment is already instantiated. Currently, this covers all + * #GsweAspectInfo objects. In the future, registering custom aspects may be + * possible; until then, you should never use such functions. */ -void +G_DEFINE_BOXED_TYPE(GsweAspectInfo, gswe_aspect_info, (GBoxedCopyFunc)gswe_aspect_info_ref, (GBoxedFreeFunc)gswe_aspect_info_unref); + +static void gswe_aspect_info_free(GsweAspectInfo *aspect_info) { if (aspect_info) { @@ -43,48 +51,100 @@ gswe_aspect_info_free(GsweAspectInfo *aspect_info) } } +/** + * gswe_aspect_info_new: + * + * Creates a new #GsweAspectInfo with reference count set to 1. + * + * Returns: (transfer full): a new #GsweAspectInfo + */ GsweAspectInfo * -gswe_aspect_info_copy(GsweAspectInfo *aspect_info) +gswe_aspect_info_new(void) { GsweAspectInfo *ret; - if (aspect_info == NULL) { - return NULL; - } - ret = g_new0(GsweAspectInfo, 1); - - ret->aspect = aspect_info->aspect; - ret->name = g_strdup(aspect_info->name); - ret->size = aspect_info->size; - ret->orb_modifier = aspect_info->orb_modifier; - ret->harmonic = aspect_info->harmonic; - ret->major = aspect_info->major; + ret->refcount = 1; return ret; } -G_DEFINE_BOXED_TYPE(GsweAspectInfo, gswe_aspect_info, (GBoxedCopyFunc)gswe_aspect_info_copy, (GBoxedFreeFunc)gswe_aspect_info_free); +/** + * gswe_aspect_info_ref: + * @aspect_info: (in): a #GsweAspectInfo + * + * Increases reference count on @aspect_info by one. + * + * Returns: (transfer none): the same #GsweAspectInfo + */ +GsweAspectInfo * +gswe_aspect_info_ref(GsweAspectInfo *aspect_info) +{ + aspect_info->refcount++; + + return aspect_info; +} + +/** + * gswe_aspect_info_unref: + * @aspect_info: (in): a #GsweAspectInfo + * + * Decreases reference count of @aspect_info by one. If reference count drops + * to zero, @aspect_info is freed. + */ +void +gswe_aspect_info_unref(GsweAspectInfo *aspect_info) +{ + if (--aspect_info->refcount == 0) { + gswe_aspect_info_free(aspect_info); + } +} + +/** + * gswe_aspect_info_set_aspect: + * @aspect_info: (in): a #GsweAspectInfo + * @aspect: the aspect to set in @aspect_info + * + * Sets @aspect_info to represenc @aspect. + */ +void +gswe_aspect_info_set_aspect(GsweAspectInfo *aspect_info, GsweAspect aspect) +{ + aspect_info->aspect = aspect; +} /** * gswe_aspect_info_get_aspect: - * @aspect_info: (in) (allow-none): a #GsweAspectInfo + * @aspect_info: (in): a #GsweAspectInfo * * Gets the aspect ID */ GsweAspect gswe_aspect_info_get_aspect(GsweAspectInfo *aspect_info) { - if (aspect_info) { - return aspect_info->aspect; - } else { - return GSWE_ASPECT_NONE; + return aspect_info->aspect; +} + +/** + * gswe_aspect_info_set_name: + * @aspect_info: (in): a #GsweAspectInfo + * @name: (in): the name to be set as @aspect_info’s name + * + * Sets the name of @aspect_info to @name. + */ +void +gswe_aspect_info_set_name(GsweAspectInfo *aspect_info, const gchar *name) +{ + if (aspect_info->name) { + g_free(aspect_info->name); } + + aspect_info->name = g_strdup(name); } /** * gswe_aspect_info_get_name: - * @aspect_info: (in) (allow-none): a #GsweAspectInfo + * @aspect_info: (in): a #GsweAspectInfo * * Gets the name of this aspect. If NLS is enabled, name is translated in * gswe_init(), so if you switch locale in your program, it will remain in the @@ -96,16 +156,25 @@ gswe_aspect_info_get_aspect(GsweAspectInfo *aspect_info) const gchar * gswe_aspect_info_get_name(GsweAspectInfo *aspect_info) { - if (aspect_info) { - return aspect_info->name; - } else { - return NULL; - } + return aspect_info->name; +} + +/** + * gswe_aspect_info_set_size: + * @aspect_info: (in): a #GsweAspectInfo + * @size: the new size for @aspect_info, in degrees + * + * Sets the size of @aspect info. + */ +void +gswe_aspect_info_set_size(GsweAspectInfo *aspect_info, gdouble size) +{ + aspect_info->size = size; } /** * gswe_aspect_info_get_size: - * @aspect_info: (in) (allow-none): a #GsweAspectInfo + * @aspect_info: (in): a #GsweAspectInfo * * Gets the size of the aspect. * @@ -114,16 +183,27 @@ gswe_aspect_info_get_name(GsweAspectInfo *aspect_info) gdouble gswe_aspect_info_get_size(GsweAspectInfo *aspect_info) { - if (aspect_info) { - return aspect_info->size; - } else { - return 0.0; - } + return aspect_info->size; +} + +/** + * gswe_aspect_info_set_orb_modifier: + * @aspect_info: (in): a #GsweAspectInfo + * @orb_modifier: the new orb modifier of @aspect_info + * + * Sets the orb modifier for @aspect_info. The orb modifier is used in aspect + * calculation; if the difference between an exact aspect and the distance + * between two positions exceeds this limit, the aspect is not considered. + */ +void +gswe_aspect_info_set_orb_modifier(GsweAspectInfo *aspect_info, gdouble orb_modifier) +{ + aspect_info->orb_modifier = orb_modifier; } /** * gswe_aspect_info_get_orb_modifier: - * @aspect_info: (in) (allow-none): a #GsweAspectInfo + * @aspect_info: (in): a #GsweAspectInfo * * Gets the orb modifier of this aspect. The orb modifier is subtracted from * the planets' orb during aspect calculation. @@ -133,16 +213,25 @@ gswe_aspect_info_get_size(GsweAspectInfo *aspect_info) gdouble gswe_aspect_info_get_orb_modifier(GsweAspectInfo *aspect_info) { - if (aspect_info) { - return aspect_info->orb_modifier; - } else { - return 0.0; - } + return aspect_info->orb_modifier; +} + +/** + * gswe_aspect_info_set_harmonic: + * @aspect_info: (in): a #GsweAspectInfo + * @harmonic: TRUE, if @aspect_info should be considered harmonic; FALSE otherwise + * + * Sets the harmonic state of @aspect_info. + */ +void +gswe_aspect_info_set_harmonic(GsweAspectInfo *aspect_info, gboolean harmonic) +{ + aspect_info->harmonic = harmonic; } /** * gswe_aspect_info_get_harmonic: - * @aspect_info: (in) (allow-none): a #GsweAspectInfo + * @aspect_info: (in): a #GsweAspectInfo * * Tells if this aspect is considered harmonic. * @@ -158,9 +247,24 @@ gswe_aspect_info_get_harmonic(GsweAspectInfo *aspect_info) } } +/** + * gswe_aspect_info_set_major: + * @aspect_info: (in): a #GsweAspectInfo + * @major: TRUE, if @aspect_info should be considered major (Ptolemaic); FALSE otherwise + * + * Sets the major state of @aspect_info. + * + * As all Ptolemaic aspects are registered during gswe_init(), you should never set @major to TRUE on new aspects. + */ +void +gswe_aspect_info_set_major(GsweAspectInfo *aspect_info, gboolean major) +{ + aspect_info->major = major; +} + /** * gswe_aspect_info_get_major: - * @aspect_info: (in) (allow-none): a #GsweAspectInfo + * @aspect_info: (in): a #GsweAspectInfo * * Gets the significance of the aspect, e.g. if its Ptolemaic or note. * diff --git a/src/gswe-aspect-info.h b/src/gswe-aspect-info.h index a836c83..d54c6f6 100644 --- a/src/gswe-aspect-info.h +++ b/src/gswe-aspect-info.h @@ -37,11 +37,27 @@ typedef struct _GsweAspectInfo GsweAspectInfo; GType gswe_aspect_info_get_type(void); #define GSWE_TYPE_ASPECT_INFO (gswe_aspect_info_get_type()) +GsweAspectInfo *gswe_aspect_info_new(void); + +GsweAspectInfo *gswe_aspect_info_ref(GsweAspectInfo *aspect_info); +void gswe_aspect_info_unref(GsweAspectInfo *aspect_info); + +void gswe_aspect_info_set_aspect(GsweAspectInfo *aspect_info, GsweAspect aspect); GsweAspect gswe_aspect_info_get_aspect(GsweAspectInfo *aspect_info); + +void gswe_aspect_info_set_name(GsweAspectInfo *aspect_info, const gchar *name); const gchar *gswe_aspect_info_get_name(GsweAspectInfo *aspect_info); + +void gswe_aspect_info_set_size(GsweAspectInfo *aspect_info, gdouble size); gdouble gswe_aspect_info_get_size(GsweAspectInfo *aspect_info); + +void gswe_aspect_info_set_orb_modifier(GsweAspectInfo *aspect_info, gdouble orb_modifier); gdouble gswe_aspect_info_get_orb_modifier(GsweAspectInfo *aspect_info); + +void gswe_aspect_info_set_harmonic(GsweAspectInfo *aspect_info, gboolean harmonic); gboolean gswe_aspect_info_get_harmonic(GsweAspectInfo *aspect_info); + +void gswe_aspect_info_set_major(GsweAspectInfo *aspect_info, gboolean major); gboolean gswe_aspect_info_get_major(GsweAspectInfo *aspect_info); G_END_DECLS diff --git a/src/swe-glib.c b/src/swe-glib.c index d93b48f..159220d 100644 --- a/src/swe-glib.c +++ b/src/swe-glib.c @@ -69,7 +69,7 @@ GsweTimestamp *gswe_full_moon_base_date; g_hash_table_replace((ht), GINT_TO_POINTER(i), (v)); #define ADD_ASPECT(ht, v, i, n, s, o, h, m) \ - (v) = g_new0(GsweAspectInfo, 1); \ + (v) = gswe_aspect_info_new(); \ (v)->aspect = (i); \ (v)->name = g_strdup(n); \ (v)->size = (s); \ @@ -117,13 +117,6 @@ gswe_free_house_system_info(gpointer house_system_info) g_free(house_system_info); } -void -gswe_free_aspect_info(gpointer aspect_info) -{ - g_free(((GsweAspectInfo *)aspect_info)->name); - g_free(aspect_info); -} - void gswe_free_antiscion_axis_info(GsweAntiscionAxisInfo *antiscion_axis_info) { @@ -198,7 +191,7 @@ gswe_init(void) ADD_HOUSE_SYSTEM(gswe_house_system_info_table, house_system_info, GSWE_HOUSE_SYSTEM_KOCH, 'K', _("Koch")); ADD_HOUSE_SYSTEM(gswe_house_system_info_table, house_system_info, GSWE_HOUSE_SYSTEM_EQUAL, 'E', _("Equal")); - gswe_aspect_info_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, gswe_free_aspect_info); + gswe_aspect_info_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gswe_aspect_info_unref); // Note that because all aspects must be <= 180°, GSWE_ASPECT_NONE can // never really exist. It is provided for name fetching purposes only.