Made GsweSignInfo a refcounted boxed type

This commit is contained in:
Gergely Polonkai 2013-09-27 00:44:47 +02:00
parent 347dae00d3
commit a1f8d1f7da
5 changed files with 139 additions and 51 deletions

View File

@ -58,10 +58,17 @@ gswe_moon_phase_data_get_type
<SECTION>
<FILE>gswe-sign-info</FILE>
GsweSignInfo
gswe_sign_info_get_element
gswe_sign_info_get_name
gswe_sign_info_get_quality
gswe_sign_info_new
gswe_sign_info_ref
gswe_sign_info_unref
gswe_sign_info_set_sign
gswe_sign_info_get_sign
gswe_sign_info_set_name
gswe_sign_info_get_name
gswe_sign_info_set_element
gswe_sign_info_get_element
gswe_sign_info_set_quality
gswe_sign_info_get_quality
<SUBSECTION Standard>
GSWE_TYPE_SIGN_INFO
gswe_sign_info_get_type

View File

@ -35,10 +35,10 @@ struct _GsweSignInfo {
/* the quality of the sign */
GsweQuality quality;
};
GsweSignInfo *gswe_sign_info_copy(GsweSignInfo *sign_info);
void gswe_sign_info_free(GsweSignInfo *sign_info);
/* Reference counter */
guint refcount;
};
#endif /* __SWE_GLIB_GSWE_SIGN_INFO_PRIVATE_H__ */
#else /* not defined __SWE_GLIB_BUILDING__ */

View File

@ -30,28 +30,9 @@
* The #GsweSignInfo stores information about a zodiac sign.
*/
G_DEFINE_BOXED_TYPE(GsweSignInfo, gswe_sign_info, (GBoxedCopyFunc)gswe_sign_info_copy, (GBoxedFreeFunc)gswe_sign_info_free);
G_DEFINE_BOXED_TYPE(GsweSignInfo, gswe_sign_info, (GBoxedCopyFunc)gswe_sign_info_ref, (GBoxedFreeFunc)gswe_sign_info_unref);
GsweSignInfo *
gswe_sign_info_copy(GsweSignInfo *sign_info)
{
GsweSignInfo *ret;
if (sign_info == NULL) {
return NULL;
}
ret = g_new0(GsweSignInfo, 1);
ret->sign = sign_info->sign;
ret->name = g_strdup(sign_info->name);
ret->element = sign_info->element;
ret->quality = sign_info->quality;
return ret;
}
void
static void
gswe_sign_info_free(GsweSignInfo *sign_info)
{
if (sign_info) {
@ -63,9 +44,70 @@ gswe_sign_info_free(GsweSignInfo *sign_info)
}
}
/**
* gswe_sign_info_new:
*
* Creates a new GsweSignInfo object with reference count set to 1.
*
* Returns: (transfer full): a new #GsweSignInfo
*/
GsweSignInfo *
gswe_sign_info_new(void)
{
GsweSignInfo *ret;
ret = g_new0(GsweSignInfo, 1);
ret->refcount = 1;
return ret;
}
/**
* gswe_sign_info_ref:
* @sign_info: (in): a #GsweSignInfo
*
* Increases reference count on @sign_info.
*
* Returns: (transfer none): the same #GsweSignInfo
*/
GsweSignInfo *
gswe_sign_info_ref(GsweSignInfo *sign_info)
{
sign_info->refcount++;
return sign_info;
}
/**
* gswe_sign_info_unref:
* @sign_info: a #GsweSignInfo
*
* Decreases reference count or @sign_info. If reference count reaches zero, @sign_info is freed.
*/
void
gswe_sign_info_unref(GsweSignInfo *sign_info)
{
if (--sign_info->refcount == 0) {
gswe_sign_info_free(sign_info);
}
}
/**
* gswe_sign_info_set_sign:
* @sign_info: (in): a #GsweSignInfo
* @sign: the sign to set in @sign_info
*
* Sets the sign ID that is represented by this #GsweSignInfo.
*/
void
gswe_sign_info_set_sign(GsweSignInfo *sign_info, GsweZodiac sign)
{
sign_info->sign = sign;
}
/**
* gswe_sign_info_get_sign:
* @sign_info: (in) (allow-none): a #GsweSignInfo
* @sign_info: (in): a #GsweSignInfo
*
* Gets the sign ID that is represented by this #GsweSignInfo.
*
@ -74,16 +116,29 @@ gswe_sign_info_free(GsweSignInfo *sign_info)
GsweZodiac
gswe_sign_info_get_sign(GsweSignInfo *sign_info)
{
if (sign_info) {
return sign_info->sign;
} else {
return GSWE_SIGN_NONE;
return sign_info->sign;
}
/**
* gswe_sign_info_set_name:
* @sign_info: (in): a #GsweSignInfo
* @name: (in): the name of the sign
*
* Sets the name of the sign.
*/
void
gswe_sign_info_set_name(GsweSignInfo *sign_info, const gchar *name)
{
if (sign_info->name) {
g_free(sign_info->name);
}
sign_info->name = g_strdup(name);
}
/**
* gswe_sign_info_get_name:
* @sign_info: (in) (allow-none): a #GsweSignInfo
* @sign_info: (in): a #GsweSignInfo
*
* Gets the name associated with this sign.
*
@ -92,16 +147,25 @@ gswe_sign_info_get_sign(GsweSignInfo *sign_info)
const gchar *
gswe_sign_info_get_name(GsweSignInfo *sign_info)
{
if (sign_info) {
return sign_info->name;
} else {
return NULL;
}
return sign_info->name;
}
/**
* gswe_sign_info_set_element:
* @sign_info: (in): a #GsweSignInfo
* @element: a #GsweElement
*
* Sets the element @sign_info should belong to.
*/
void
gswe_sign_info_set_element(GsweSignInfo *sign_info, GsweElement element)
{
sign_info->element = element;
}
/**
* gswe_sign_info_get_element:
* @sign_info: (in) (allow-none): a #GsweSignInfo
* @sign_info: (in): a #GsweSignInfo
*
* Gets the element this sign belongs to.
*
@ -110,16 +174,25 @@ gswe_sign_info_get_name(GsweSignInfo *sign_info)
GsweElement
gswe_sign_info_get_element(GsweSignInfo *sign_info)
{
if (sign_info) {
return sign_info->element;
} else {
return GSWE_ELEMENT_NONE;
}
return sign_info->element;
}
/**
* gswe_sign_info_set_quality:
* @sign_info: (in): a #GsweSignInfo
* @quality: a #GsweQuality
*
* Sets the quality @sign should belong to
*/
void
gswe_sign_info_set_quality(GsweSignInfo *sign_info, GsweQuality quality)
{
sign_info->quality = quality;
}
/**
* gswe_sign_info_get_quality:
* @sign_info: (in) (allow-none): a #GsweSignInfo
* @sign_info: (in): a #GsweSignInfo
*
* Gets the quality this sign belongs to.
*
@ -128,10 +201,6 @@ gswe_sign_info_get_element(GsweSignInfo *sign_info)
GsweQuality
gswe_sign_info_get_quality(GsweSignInfo *sign_info)
{
if (sign_info) {
return sign_info->quality;
} else {
return GSWE_QUALITY_NONE;
}
return sign_info->quality;
}

View File

@ -37,9 +37,21 @@ typedef struct _GsweSignInfo GsweSignInfo;
GType gswe_sign_info_get_type(void);
#define GSWE_TYPE_SIGN_INFO (gswe_sign_info_get_type())
GsweSignInfo *gswe_sign_info_new(void);
GsweSignInfo *gswe_sign_info_ref(GsweSignInfo *sign_info);
void gswe_sign_info_unref(GsweSignInfo *sign_info);
void gswe_sign_info_set_sign(GsweSignInfo *sign_info, GsweZodiac sign);
GsweZodiac gswe_sign_info_get_sign(GsweSignInfo *sign_info);
void gswe_sign_info_set_name(GsweSignInfo *sign_info, const gchar *name);
const gchar *gswe_sign_info_get_name(GsweSignInfo *sign_info);
void gswe_sign_info_set_element(GsweSignInfo *sign_info, GsweElement element);
GsweElement gswe_sign_info_get_element(GsweSignInfo *sign_info);
void gswe_sign_info_set_quality(GsweSignInfo *sign_info, GsweQuality quality);
GsweQuality gswe_sign_info_get_quality(GsweSignInfo *sign_info);
G_END_DECLS

View File

@ -54,7 +54,7 @@ GsweTimestamp *gswe_full_moon_base_date;
g_hash_table_replace((ht), GINT_TO_POINTER(i), (v));
#define ADD_SIGN(ht, v, s, n, e, q) \
(v) = g_new0(GsweSignInfo, 1); \
(v) = gswe_sign_info_new(); \
(v)->sign = (s); \
(v)->name = g_strdup(n); \
(v)->element = (e); \