Made GsweAspectInfo a refcounted boxed type

This is to satisfy #7
This commit is contained in:
Gergely Polonkai 2013-09-30 21:31:21 +02:00
parent dcd58b5dd1
commit 7564183aa2
5 changed files with 176 additions and 54 deletions

View File

@ -111,12 +111,21 @@ gswe_planet_data_get_type
<SECTION>
<FILE>gswe-aspect-info</FILE>
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
<SUBSECTION Standard>
GSWE_TYPE_ASPECT_INFO
gswe_aspect_info_get_type

View File

@ -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__ */

View File

@ -29,9 +29,17 @@
* @include: swe-glib.h
*
* The #GsweAspectInfo stores information about an aspect.
*
* <warning><para>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.</para></warning>
*/
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_infos 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.
*
* <note><para>As all Ptolemaic aspects are registered during gswe_init(), you should never set @major to TRUE on new aspects.</para></note>
*/
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.
*

View File

@ -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

View File

@ -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.