Changed GswePlanetInfo to a refcounted boxed type

This commit is contained in:
Gergely Polonkai 2013-09-27 18:32:44 +02:00
parent c5d8ee49eb
commit dcd58b5dd1
5 changed files with 167 additions and 49 deletions

View File

@ -74,11 +74,20 @@ gswe_sign_info_get_type
<SECTION>
<FILE>gswe-planet-info</FILE>
GswePlanetInfo
gswe_planet_info_new
gswe_planet_info_ref
gswe_planet_info_unref
gswe_planet_info_set_name
gswe_planet_info_get_name
gswe_planet_info_set_orb
gswe_planet_info_get_orb
gswe_planet_info_set_planet
gswe_planet_info_get_planet
gswe_planet_info_set_points
gswe_planet_info_get_points
gswe_planet_info_set_real_body
gswe_planet_info_get_real_body
gswe_planet_info_set_sweph_id
gswe_planet_info_get_sweph_id
<SUBSECTION Standard>
GSWE_TYPE_PLANET_INFO

View File

@ -40,10 +40,10 @@ struct _GswePlanetInfo {
/* the value this planet counts in the element/quality points table */
gint points;
};
void gswe_planet_info_free(GswePlanetInfo *planet_info);
GswePlanetInfo *gswe_planet_info_copy(GswePlanetInfo *planet_info);
/* reference counter */
guint refcount;
};
#endif /* __SWE_GLIB_GSWE_PLANET_INFO_PRIVATE_H__ */
#else /* not defined __SWE_GLIB_BUILDING__ */

View File

@ -30,33 +30,79 @@
* The #GswePlanetInfo structure stores information about a planet.
*/
G_DEFINE_BOXED_TYPE(GswePlanetInfo, gswe_planet_info, (GBoxedCopyFunc)gswe_planet_info_copy, (GBoxedFreeFunc)gswe_planet_info_free);
G_DEFINE_BOXED_TYPE(GswePlanetInfo, gswe_planet_info, (GBoxedCopyFunc)gswe_planet_info_ref, (GBoxedFreeFunc)gswe_planet_info_unref);
/**
* gswe_planet_info_new:
*
* Creates a new #GswePlanetInfo object with reference count set to 1.
*
* Returns: a new #GswePlanetInfo
*/
GswePlanetInfo *
gswe_planet_info_copy(GswePlanetInfo *planet_info)
gswe_planet_info_new(void)
{
GswePlanetInfo *ret = g_new0(GswePlanetInfo, 1);
GswePlanetInfo *ret;
ret->planet = planet_info->planet;
ret->sweph_id = planet_info->sweph_id;
ret->real_body = planet_info->real_body;
ret->orb = planet_info->orb;
ret->name = g_strdup(planet_info->name);
ret->points = planet_info->points;
ret = g_new0(GswePlanetInfo, 1);
ret->refcount = 1;
return ret;
}
void
/**
* gswe_planet_info_ref:
* @planet_info: (in): a #GswePlanetInfo
*
* Increases reference count on @planet_info.
*
* Returns: (transfer none): the same #GswePlanetData
*/
GswePlanetInfo *
gswe_planet_info_ref(GswePlanetInfo *planet_info)
{
planet_info->refcount++;
return planet_info;
}
static void
gswe_planet_info_free(GswePlanetInfo *planet_info)
{
g_free(planet_info->name);
g_free(planet_info);
}
/**
* gswe_planet_info_unref:
* @planet_info: a #GswePlanetInfo
*
* Decreases reference count on @planet_info. If reference count reaches zero, @planet_info is freed.
*/
void
gswe_planet_info_unref(GswePlanetInfo *planet_info)
{
if (--planet_info->refcount == 0) {
gswe_planet_info_free(planet_info);
}
}
/**
* gswe_planet_info_set_planet:
* @planet_info: a #GswePlanetInfo
* @planet: the new planet ID
*
* Sets the planet ID that will be represented by @planet_info.
*/
void
gswe_planet_info_set_planet(GswePlanetInfo *planet_info, GswePlanet planet)
{
planet_info->planet = planet;
}
/**
* gswe_planet_info_get_planet:
* @planet_info: (in) (allow-none): A #GswePlanetInfo
* @planet_info: (in): A #GswePlanetInfo
*
* Gets the planet ID represented by this #GswePlanetInfo.
*
@ -65,16 +111,25 @@ gswe_planet_info_free(GswePlanetInfo *planet_info)
GswePlanet
gswe_planet_info_get_planet(GswePlanetInfo *planet_info)
{
if (planet_info) {
return planet_info->planet;
} else {
return GSWE_PLANET_NONE;
}
return planet_info->planet;
}
/**
* gswe_planet_info_set_sweph_id:
* @planet_info: a #GswePlanetInfo
* @sweph_id: the new Swiss Ephemeris planet ID
*
* Sets the Swiss Ephemeris planet_id associated with this planet.
*/
void
gswe_planet_info_set_sweph_id(GswePlanetInfo *planet_info, gint32 sweph_id)
{
planet_info->sweph_id = sweph_id;
}
/**
* gswe_planet_info_get_sweph_id:
* @planet_info: (in) (allow-none): A #GswePlanetInfo
* @planet_info: (in): A #GswePlanetInfo
*
* Gets the Swiss Ephemeris planet ID associated with this planet.
*
@ -83,16 +138,26 @@ gswe_planet_info_get_planet(GswePlanetInfo *planet_info)
gint32
gswe_planet_info_get_sweph_id(GswePlanetInfo *planet_info)
{
if (planet_info) {
return planet_info->sweph_id;
} else {
return -1;
}
return planet_info->sweph_id;
}
/**
* gswe_planet_info_set_real_body:
* @planet_info: a #GswePlanetInfo
* @real_body: a boolean that indicates if this planet is an object recognized
* by Swiss Ephemeris
*
* Sets whether this planet is an object recognized by the Swiss Ephemeris library.
*/
void
gswe_planet_info_set_real_body(GswePlanetInfo *planet_info, gboolean real_body)
{
planet_info->real_body = real_body;
}
/**
* gswe_planet_info_get_real_body:
* @planet_info: (in) (allow-none): A #GswePlanetInfo
* @planet_info: (in): A #GswePlanetInfo
*
* Checks weather this planet is a real celestial body (e.g. it has a Swiss Ephemeris planet ID) or not.
*
@ -101,16 +166,26 @@ gswe_planet_info_get_sweph_id(GswePlanetInfo *planet_info)
gboolean
gswe_planet_info_get_real_body(GswePlanetInfo *planet_info)
{
if (planet_info) {
return planet_info->real_body;
} else {
return FALSE;
}
return planet_info->real_body;
}
/**
* gswe_planet_info_set_orb:
* @planet_info: a #GswePlanetInfo
* @orb: the planet's own orb
*
* Sets the orb of @planet_info. This value is used in aspect and antiscion
* calculations.
*/
void
gswe_planet_info_set_orb(GswePlanetInfo *planet_info, gdouble orb)
{
planet_info->orb = orb;
}
/**
* gswe_planet_info_get_orb:
* @planet_info: (in) (allow-none): A #GswePlanetInfo
* @planet_info: (in): A #GswePlanetInfo
*
* Gets the orb of the planet.
*
@ -119,16 +194,29 @@ gswe_planet_info_get_real_body(GswePlanetInfo *planet_info)
gdouble
gswe_planet_info_get_orb(GswePlanetInfo *planet_info)
{
if (planet_info) {
return planet_info->orb;
} else {
return -1.0;
return planet_info->orb;
}
/**
* gswe_planet_info_set_name:
* @planet_info: (in): a #GswePlanetInfo
* @name: (in): the new name of the planet
*
* Sets the name of @planet_info.
*/
void
gswe_planet_info_set_name(GswePlanetInfo *planet_info, const gchar *name)
{
if (planet_info->name) {
g_free(planet_info->name);
}
planet_info->name = g_strdup(name);
}
/**
* gswe_planet_info_get_name:
* @planet_info: (in) (allow-none): A #GswePlanetInfo
* @planet_info: (in): A #GswePlanetInfo
*
* Gets the name of the planet.
*
@ -137,16 +225,25 @@ gswe_planet_info_get_orb(GswePlanetInfo *planet_info)
const gchar *
gswe_planet_info_get_name(GswePlanetInfo *planet_info)
{
if (planet_info) {
return planet_info->name;
} else {
return NULL;
}
return planet_info->name;
}
/**
* gswe_planet_info_set_points:
* @planet_info: a #GswePlanetInfo
* @points: the new point value
*
* Sets the point value of @planet_info. This value is used in points calculations.
*/
void
gswe_planet_info_set_points(GswePlanetInfo *planet_info, gint points)
{
planet_info->points = points;
}
/**
* gswe_planet_info_get_points:
* @planet_info: (in) (allow-none): A #GswePlanetInfo
* @planet_info: (in): A #GswePlanetInfo
*
* Gets the value this planet counts in point calculations.
*
@ -155,10 +252,6 @@ gswe_planet_info_get_name(GswePlanetInfo *planet_info)
gint
gswe_planet_info_get_points(GswePlanetInfo *planet_info)
{
if (planet_info) {
return planet_info->points;
} else {
return 0;
}
return planet_info->points;
}

View File

@ -37,11 +37,27 @@ typedef struct _GswePlanetInfo GswePlanetInfo;
GType gswe_planet_info_get_type(void);
#define GSWE_TYPE_PLANET_INFO (gswe_planet_info_get_type())
GswePlanetInfo *gswe_planet_info_new(void);
GswePlanetInfo *gswe_planet_info_ref(GswePlanetInfo *planet_info);
void gswe_planet_info_unref(GswePlanetInfo *planet_info);
void gswe_planet_info_set_planet(GswePlanetInfo *planet_info, GswePlanet planet);
GswePlanet gswe_planet_info_get_planet(GswePlanetInfo *planet_info);
void gswe_planet_info_set_sweph_id(GswePlanetInfo *planet_info, gint32 sweph_id);
gint32 gswe_planet_info_get_sweph_id(GswePlanetInfo *planet_info);
void gswe_planet_info_set_real_body(GswePlanetInfo *planet_info, gboolean real_body);
gboolean gswe_planet_info_get_real_body(GswePlanetInfo *planet_info);
void gswe_planet_info_set_orb(GswePlanetInfo *planet_info, gdouble orb);
gdouble gswe_planet_info_get_orb(GswePlanetInfo *planet_info);
void gswe_planet_info_set_name(GswePlanetInfo *planet_info, const gchar *name);
const gchar *gswe_planet_info_get_name(GswePlanetInfo *planet_info);
void gswe_planet_info_set_points(GswePlanetInfo *planet_info, gint points);
gint gswe_planet_info_get_points(GswePlanetInfo *planet_info);
G_END_DECLS

View File

@ -44,7 +44,7 @@ GHashTable *gswe_antiscion_axis_info_table;
GsweTimestamp *gswe_full_moon_base_date;
#define ADD_PLANET(ht, v, i, s, r, n, o, h) \
(v) = g_new0(GswePlanetInfo, 1); \
(v) = gswe_planet_info_new(); \
(v)->planet = (i); \
(v)->sweph_id = (s); \
(v)->real_body = (r); \