Made GsweHouseData a refcounted boxed type

This is to satisfy #7
This commit is contained in:
Gergely Polonkai 2013-09-30 23:11:52 +02:00
parent 6d88993fa3
commit 0b15005e3a
5 changed files with 101 additions and 36 deletions

View File

@ -216,9 +216,13 @@ gswe_house_system_info_get_type
<SECTION>
<FILE>gswe-house-data</FILE>
GsweHouseData
gswe_house_data_get_cusp_position
gswe_house_data_new
gswe_house_data_ref
gswe_house_data_unref
gswe_house_data_get_house
gswe_house_data_get_cusp_position
gswe_house_data_get_sign
gswe_house_data_get_sign_info
<SUBSECTION Standard>
GSWE_TYPE_HOUSE_DATA
gswe_house_data_get_type

View File

@ -29,10 +29,11 @@ struct _GsweHouseData {
gdouble cusp_position;
/* the #GsweSignInfo structure associated with the sign in which the house cusp is in */
GsweSignInfo *sign;
};
GsweSignInfo *sign_info;
GsweHouseData *gswe_house_data_copy(GsweHouseData *house_data);
/* reference count */
guint refcount;
};
#endif /* __SWE_GLIB_GSWE_HOUSE_DATA_PRIVATE_H__ */
#else /* not defined __SWE_GLIB_BUILDING__ */

View File

@ -17,6 +17,8 @@
*/
#include "gswe-types.h"
#include "swe-glib-private.h"
#include "swe-glib.h"
#include "gswe-house-data.h"
#include "gswe-house-data-private.h"
@ -31,23 +33,69 @@
* #GsweHouseData is a structure that represents a house's position.
*/
G_DEFINE_BOXED_TYPE(GsweHouseData, gswe_house_data, (GBoxedCopyFunc)gswe_house_data_copy, (GBoxedFreeFunc)g_free);
G_DEFINE_BOXED_TYPE(GsweHouseData, gswe_house_data, (GBoxedCopyFunc)gswe_house_data_ref, (GBoxedFreeFunc)gswe_house_data_unref);
GsweHouseData *
gswe_house_data_copy(GsweHouseData *house_data)
static void
gswe_house_data_free(GsweHouseData *house_data)
{
GsweHouseData *ret = g_new0(GsweHouseData, 1);
if (house_data->sign_info) {
gswe_sign_info_unref(house_data->sign_info);
}
ret->house = house_data->house;
ret->cusp_position = house_data->cusp_position;
ret->sign = house_data->sign;
g_free(house_data);
}
/**
* gswe_house_data_new:
*
* Creates a new #GsweHouseData with reference count set to 1.
*
* Returns: (transfer full): a new #GsweHouseData
*/
GsweHouseData *
gswe_house_data_new(void)
{
GsweHouseData *ret;
ret = g_new0(GsweHouseData, 1);
ret->refcount = 1;
return ret;
}
/**
* gswe_house_data_ref:
* @house_data: a #GsweHouseData
*
* Increases reference count on @house_data by one.
*
* Returns: (transfer none): the same #GsweHouseData
*/
GsweHouseData *
gswe_house_data_ref(GsweHouseData *house_data)
{
house_data->refcount++;
return house_data;
}
/**
* gswe_house_data_unref:
* @house_data: a #GsweHouseData
*
* Decreases reference count on @house_data by one. If reference count drops to zero, @house_data is freed.
*/
void
gswe_house_data_unref(GsweHouseData *house_data)
{
if (--house_data->refcount == 0) {
gswe_house_data_free(house_data);
}
}
/**
* gswe_house_data_get_house:
* @house_data: (in) (allow-none): a #GsweHouseData
* @house_data: (in): a #GsweHouseData
*
* Gets the number of the house.
*
@ -56,16 +104,12 @@ gswe_house_data_copy(GsweHouseData *house_data)
guint
gswe_house_data_get_house(GsweHouseData *house_data)
{
if (house_data) {
return house_data->house;
} else {
return 0;
}
return house_data->house;
}
/**
* gswe_house_data_get_cusp_position:
* @house_data: (in) (allow-none): a #GsweHouseData
* @house_data: (in): a #GsweHouseData
*
* Gets the position of the house's cusp.
*
@ -74,28 +118,38 @@ gswe_house_data_get_house(GsweHouseData *house_data)
gdouble
gswe_house_data_get_cusp_position(GsweHouseData *house_data)
{
if (house_data) {
return house_data->cusp_position;
} else {
return 0.0;
}
return house_data->cusp_position;
}
/**
* gswe_house_data_get_sign:
* @house_data: (in) (allow-none): a #GsweHouseData
* @house_data: a #GsweHouseData
*
* Gets the sign in which the house's cusp is.
* Gets the sign which the house's cusp is in.
*
* Returns: the GsweZodiac of the house cusp's sign
*/
GsweZodiac
gswe_house_data_get_sign(GsweHouseData *house_data)
{
if (house_data->sign_info) {
return house_data->sign_info->sign;
} else {
return GSWE_SIGN_NONE;
}
}
/**
* gswe_house_data_get_sign_info:
* @house_data: (in): a #GsweHouseData
*
* Gets the #GsweSignInfo that represents the sign which the house's cusp is in.
*
* Returns: (transfer none): a #GsweSignInfo representing the sign
*/
GsweSignInfo *
gswe_house_data_get_sign(GsweHouseData *house_data)
gswe_house_data_get_sign_info(GsweHouseData *house_data)
{
if (house_data) {
return house_data->sign;
} else {
return NULL;
}
return house_data->sign_info;
}

View File

@ -37,9 +37,15 @@ typedef struct _GsweHouseData GsweHouseData;
GType gswe_house_data_get_type(void);
#define GSWE_TYPE_HOUSE_DATA (gswe_house_data_get_type())
GsweHouseData *gswe_house_data_new(void);
GsweHouseData *gswe_house_data_ref(GsweHouseData *house_data);
void gswe_house_data_unref(GsweHouseData *house_data);
guint gswe_house_data_get_house(GsweHouseData *house_data);
gdouble gswe_house_data_get_cusp_position(GsweHouseData *house_data);
GsweSignInfo *gswe_house_data_get_sign(GsweHouseData *house_data);
GsweZodiac gswe_house_data_get_sign(GsweHouseData *house_data);
GsweSignInfo *gswe_house_data_get_sign_info(GsweHouseData *house_data);
G_END_DECLS

View File

@ -201,7 +201,7 @@ gswe_moment_finalize(GObject *gobject)
{
GsweMoment *moment = GSWE_MOMENT(gobject);
g_list_free_full(moment->priv->house_list, g_free);
g_list_free_full(moment->priv->house_list, (GDestroyNotify)gswe_house_data_unref);
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);
g_list_free_full(moment->priv->antiscia_list, (GDestroyNotify)gswe_antiscion_data_unref);
@ -502,7 +502,7 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err)
return;
}
g_list_free_full(moment->priv->house_list, g_free);
g_list_free_full(moment->priv->house_list, (GDestroyNotify)gswe_house_data_unref);
moment->priv->house_list = NULL;
// If no house system is set, we need no calculations at all. Just leave
@ -535,13 +535,13 @@ gswe_moment_calculate_house_positions(GsweMoment *moment, GError **err)
* this should not cause trouble yet, though) */
for (i = 12; i >= 1; i--) {
GsweSignInfo *sign_info;
GsweHouseData *house_data = g_new0(GsweHouseData, 1);
GsweHouseData *house_data = gswe_house_data_new();
house_data->house = i;
house_data->cusp_position = cusps[i];
if ((sign_info = g_hash_table_lookup(gswe_sign_info_table, GINT_TO_POINTER((gint)ceilf(cusps[i] / 30.0)))) == NULL) {
g_list_free_full(moment->priv->house_list, g_free);
g_list_free_full(moment->priv->house_list, (GDestroyNotify)gswe_house_data_unref);
moment->priv->house_list = NULL;
moment->priv->house_revision = 0;
g_set_error(err, GSWE_ERROR, GSWE_ERROR_UNKNOWN_SIGN, "Calculation brought an unknown sign");