Made GsweMoonPhaseData a refcounted boxed type

This is to satisfy #7
This commit is contained in:
Gergely Polonkai 2013-09-30 22:39:46 +02:00
parent 3a06d4bcee
commit f92caa4e1a
5 changed files with 62 additions and 48 deletions

View File

@ -42,9 +42,10 @@ gswe_moment_get_type
<SECTION>
<FILE>gswe-moon-phase-data</FILE>
gswe_moon_phase_data_set_phase
gswe_moon_phase_data_new
gswe_moon_phase_data_ref
gswe_moon_phase_data_unref
gswe_moon_phase_data_get_phase
gswe_moon_phase_data_set_illumination
gswe_moon_phase_data_get_illumination
GsweMoonPhaseData
<SUBSECTION Standard>

View File

@ -75,7 +75,7 @@ struct _GsweMomentPrivate {
GHashTable *element_points;
GHashTable *quality_points;
guint moon_phase_revision;
GsweMoonPhaseData moon_phase;
GsweMoonPhaseData *moon_phase;
GList *aspect_list;
guint aspect_revision;
GList *antiscia_list;
@ -166,6 +166,7 @@ gswe_moment_init(GsweMoment *moment)
moment->priv->planet_list = NULL;
moment->priv->aspect_list = NULL;
moment->priv->antiscia_list = NULL;
moment->priv->moon_phase = gswe_moon_phase_data_new();
moment->priv->element_points = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
moment->priv->quality_points = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
moment->priv->house_revision = 0;
@ -203,6 +204,7 @@ gswe_moment_finalize(GObject *gobject)
g_list_free_full(moment->priv->house_list, g_free);
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);
gswe_moon_phase_data_unref(moment->priv->moon_phase);
G_OBJECT_CLASS(gswe_moment_parent_class)->finalize(gobject);
}
@ -964,7 +966,7 @@ gswe_moment_get_moon_phase(GsweMoment *moment, GError **err)
jdb;
if (moment->priv->moon_phase_revision == moment->priv->revision) {
return &(moment->priv->moon_phase);
return moment->priv->moon_phase;
}
jd = gswe_timestamp_get_julian_day(moment->priv->timestamp, err);
@ -990,31 +992,31 @@ gswe_moment_get_moon_phase(GsweMoment *moment, GError **err)
g_error("Error during Moon phase calculation!");
}
moment->priv->moon_phase.illumination = (50.0 - fabs(phase_percent - 50.0)) * 2;
moment->priv->moon_phase->illumination = (50.0 - fabs(phase_percent - 50.0)) * 2;
if (phase_percent == 0) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_NEW;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_NEW;
} else if (phase_percent < 25) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WAXING_CRESCENT;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_CRESCENT;
} else if (phase_percent == 25) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WAXING_HALF;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_HALF;
} else if (phase_percent < 50) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WAXING_GIBBOUS;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WAXING_GIBBOUS;
} else if (phase_percent == 50) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_FULL;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_FULL;
} else if (phase_percent < 75) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WANING_GIBBOUS;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_GIBBOUS;
} else if (phase_percent == 75) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WANING_HALF;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_HALF;
} else if (phase_percent < 100) {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_WANING_CRESCENT;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_WANING_CRESCENT;
} else {
moment->priv->moon_phase.phase = GSWE_MOON_PHASE_DARK;
moment->priv->moon_phase->phase = GSWE_MOON_PHASE_DARK;
}
moment->priv->moon_phase_revision = moment->priv->revision;
return &(moment->priv->moon_phase);
return moment->priv->moon_phase;
}
static gint

View File

@ -28,9 +28,10 @@ struct _GsweMoonPhaseData {
/* the illumination percentage of the Moon */
gdouble illumination;
};
GsweMoonPhaseData *gswe_moon_phase_data_copy(GsweMoonPhaseData *moon_phase_data);
/* reference count */
guint refcount;
};
#endif /* __SWE_GLIB_GSWE_MOON_PHASE_DATA_PRIVATE_H__ */
#else /* not defined __SWE_GLIB_BUILDING__ */

View File

@ -32,37 +32,59 @@
* Moon, including its illumination percentage.
*/
G_DEFINE_BOXED_TYPE(GsweMoonPhaseData, gswe_moon_phase_data, (GBoxedCopyFunc)gswe_moon_phase_data_copy, (GBoxedFreeFunc)g_free);
G_DEFINE_BOXED_TYPE(GsweMoonPhaseData, gswe_moon_phase_data, (GBoxedCopyFunc)gswe_moon_phase_data_ref, (GBoxedFreeFunc)gswe_moon_phase_data_unref);
/**
* gswe_moon_phase_data_new:
*
* Creates a new #GsweMoonPhaseData object with reference count set to 1.
*
* Returns: (transfer full): a new #GsweMoonPhaseData object
*/
GsweMoonPhaseData *
gswe_moon_phase_data_copy(GsweMoonPhaseData *moon_phase_data)
gswe_moon_phase_data_new(void)
{
GsweMoonPhaseData *ret = g_new0(struct _GsweMoonPhaseData, 1);
GsweMoonPhaseData *ret;
ret->phase = moon_phase_data->phase;
ret->illumination = moon_phase_data->illumination;
ret = g_new0(GsweMoonPhaseData, 1);
ret->refcount = 1;
return ret;
}
/**
* gswe_moon_phase_data_set_phase:
* @moon_phase_data: a GsweMoonPhaseData
* @phase: the phase to set
* gswe_moon_phase_data_ref:
* @moon_phase_data: (in): a #GsweMoonPhaseData
*
* Sets the phase of the Moon in the given GsweMoonPhaseData.
* Increases reference count on @moon_phase_data by one.
*
* Returns: (transfer none): the same #GsweMoonPhaseData
*/
GsweMoonPhaseData *
gswe_moon_phase_data_ref(GsweMoonPhaseData *moon_phase_data)
{
moon_phase_data->refcount++;
return moon_phase_data;
}
/**
* gswe_moon_phase_data_unref:
* @moon_phase_data: (in): a #GsweMoonPhaseData
*
* Decreases reference count on @moon_phase_data by one. If reference count drops to zero, @moon_phase_data is freed.
*/
void
gswe_moon_phase_data_set_phase(GsweMoonPhaseData *moon_phase_data, GsweMoonPhase phase)
gswe_moon_phase_data_unref(GsweMoonPhaseData *moon_phase_data)
{
if (moon_phase_data) {
moon_phase_data->phase = phase;
if (--moon_phase_data->refcount == 0) {
g_free(moon_phase_data);
}
}
/**
* gswe_moon_phase_data_get_phase:
* @moon_phase_data: a GsweMoonPhaseData
* @moon_phase_data: (in): a GsweMoonPhaseData
*
* Gets the phase of the Moon in the given GsweMoonPhaseData.
*
@ -78,24 +100,9 @@ gswe_moon_phase_data_get_phase(GsweMoonPhaseData *moon_phase_data)
}
}
/**
* gswe_moon_phase_data_set_illumination:
* @moon_phase_data: a GsweMoonPhaseData
* @illumination: the illumination to set
*
* Sets the illumination percentage in the given GsweMoonPhaseData.
*/
void
gswe_moon_phase_data_set_illumination(GsweMoonPhaseData *moon_phase_data, gdouble illumination)
{
if (moon_phase_data) {
moon_phase_data->illumination = illumination;
}
}
/**
* gswe_moon_phase_data_get_illumination:
* @moon_phase_data: a GsweMoonPhaseData
* @moon_phase_data: (in): a GsweMoonPhaseData
*
* Gets the illumination percentage from the given GsweMoonPhaseData.
*

View File

@ -34,9 +34,12 @@ G_BEGIN_DECLS
*/
typedef struct _GsweMoonPhaseData GsweMoonPhaseData;
void gswe_moon_phase_data_set_phase(GsweMoonPhaseData *moon_phase_data, GsweMoonPhase phase);
GsweMoonPhaseData *gswe_moon_phase_data_new(void);
GsweMoonPhaseData *gswe_moon_phase_data_ref(GsweMoonPhaseData *moon_phase_data);
void gswe_moon_phase_data_unref(GsweMoonPhaseData *moon_phase_data);
GsweMoonPhase gswe_moon_phase_data_get_phase(GsweMoonPhaseData *moon_phase_data);
void gswe_moon_phase_data_set_illumination(GsweMoonPhaseData *moon_phase_data, gdouble illumination);
gdouble gswe_moon_phase_data_get_illumination(GsweMoonPhaseData *moon_phase_data);
GType gswe_moon_phase_data_get_type(void);