From 95b709ed55d82adcf3a28876441180321bfb0a48 Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Wed, 10 Mar 2010 18:07:10 +0100 Subject: [PATCH] [AdgPoin] Added adg_point_set() Implemented a convenient function compatible with what requested by the setters of AdgPoint properties. --- adg/adg-point.c | 108 ++++++++++++++++++++++++++++++++++++++------------------ adg/adg-point.h | 4 ++- 2 files changed, 76 insertions(+), 36 deletions(-) diff --git a/adg/adg-point.c b/adg/adg-point.c index 19f0becb..6dc4d186 100644 --- a/adg/adg-point.c +++ b/adg/adg-point.c @@ -67,8 +67,25 @@ adg_point_get_type(void) } /** + * adg_point_new: + * + * Creates a new empty #AdgPoint. The returned pointer + * should be freed with adg_point_destroy() when no longer needed. + * + * The returned value should be freed with adg_point_destroy() + * when no longer needed. + * + * Returns: a newly created #AdgPoint + **/ +AdgPoint * +adg_point_new(void) +{ + return g_new0(AdgPoint, 1); +} + +/** * adg_point_dup: - * @src: an #AdgPoint structure + * @src: an #AdgPoint * * Duplicates @src. This operation also adds a new reference * to the internal model if @src is linked to a named pair. @@ -95,40 +112,6 @@ adg_point_dup(const AdgPoint *src) } /** - * adg_point_new: - * - * Creates a new empty #AdgPoint. The returned pointer - * should be freed with adg_point_destroy() when no longer needed. - * - * The returned value should be freed with adg_point_destroy() - * when no longer needed. - * - * Returns: a newly created #AdgPoint - **/ -AdgPoint * -adg_point_new(void) -{ - return g_new0(AdgPoint, 1); -} - -/** - * adg_point_destroy: - * @point: an #AdgPoint - * - * Destroys the @point instance, unreferencing the internal model if - * @point is linked to a named pair. - **/ -void -adg_point_destroy(AdgPoint *point) -{ - g_return_if_fail(point != NULL); - - adg_point_set_pair_from_model(point, NULL, NULL); - - g_free(point); -} - -/** * adg_point_copy: * @point: an #AdgPoint * @src: the source point to copy @@ -156,6 +139,61 @@ adg_point_copy(AdgPoint *point, const AdgPoint *src) } /** + * adg_point_set: + * @p_point: a pointer to an #AdgPoint + * @new_point: the new point to assign + * + * Convenient method to assign @new_point to the #AdgPoint pointed + * by @p_point. This is useful while implementing #AdgPoint + * property setters. + * + * At the end *@p_point will be set to @new_point, allocating a new + * #AdgPoint or destroying the previous one when necessary. It is + * allowed to use %NULL as @new_point to unset *@p_point. + * + * Returns: %TRUE if the pointer pointed by @p_point has been changed, + * %FALSE otherwise + **/ +gboolean +adg_point_set(AdgPoint **p_point, const AdgPoint *new_point) +{ + g_return_val_if_fail(p_point != NULL, FALSE); + + if ((*p_point == new_point) || + (*p_point && new_point && adg_point_equal(*p_point, new_point))) + return FALSE; + + if (*p_point == NULL) + *p_point = adg_point_new(); + + if (new_point) { + adg_point_copy(*p_point, new_point); + } else { + adg_point_destroy(*p_point); + *p_point = NULL; + } + + return TRUE; +} + +/** + * adg_point_destroy: + * @point: an #AdgPoint + * + * Destroys the @point instance, unreferencing the internal model if + * @point is linked to a named pair. + **/ +void +adg_point_destroy(AdgPoint *point) +{ + g_return_if_fail(point != NULL); + + adg_point_set_pair_from_model(point, NULL, NULL); + + g_free(point); +} + +/** * adg_point_set_pair: * @point: an #AdgPoint * @pair: the #AdgPair to use diff --git a/adg/adg-point.h b/adg/adg-point.h index 7fd8ad53..22cd6005 100644 --- a/adg/adg-point.h +++ b/adg/adg-point.h @@ -42,9 +42,11 @@ GType adg_point_get_type (void) G_GNUC_CONST; AdgPoint * adg_point_new (void); AdgPoint * adg_point_dup (const AdgPoint *src); -void adg_point_destroy (AdgPoint *point); void adg_point_copy (AdgPoint *point, const AdgPoint *src); +gboolean adg_point_set (AdgPoint **p_point, + const AdgPoint *new_point); +void adg_point_destroy (AdgPoint *point); void adg_point_set_pair (AdgPoint *point, const AdgPair *pair); void adg_point_set_pair_explicit (AdgPoint *point, -- 2.11.4.GIT