diff options
Diffstat (limited to 'src/libical-glib/i-cal-object.c.in')
-rw-r--r-- | src/libical-glib/i-cal-object.c.in | 190 |
1 files changed, 91 insertions, 99 deletions
diff --git a/src/libical-glib/i-cal-object.c.in b/src/libical-glib/i-cal-object.c.in index 686c7bcd..95ab14b4 100644 --- a/src/libical-glib/i-cal-object.c.in +++ b/src/libical-glib/i-cal-object.c.in @@ -1,17 +1,7 @@ /* - * Copyright (C) 2015 William Yu <williamyu@gnome.org> + * SPDX-FileCopyrightText: 2015 William Yu <williamyu@gnome.org> * - * This library is free software: you can redistribute it and/or modify it - * under the terms of version 2.1. of the GNU Lesser General Public License - * as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library. If not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0 */ #ifdef HAVE_CONFIG_H @@ -20,9 +10,6 @@ #include "i-cal-object.h" -#define LOCK_PROPS(x) g_mutex_lock (&((x)->priv->props_lock)) -#define UNLOCK_PROPS(x) g_mutex_unlock (&((x)->priv->props_lock)) - typedef struct _GlobalData { GType object_type; gpointer native; @@ -103,7 +90,7 @@ void i_cal_object_free_global_objects(void) } } -struct _ICalObjectPrivate +typedef struct { GMutex props_lock; /* to guard all the below members */ @@ -113,9 +100,12 @@ struct _ICalObjectPrivate gboolean always_destroy; GObject *owner; GSList *dependers; /* referenced GObject-s */ -}; +} ICalObjectPrivate; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(ICalObject, i_cal_object, G_TYPE_OBJECT) -G_DEFINE_ABSTRACT_TYPE(ICalObject, i_cal_object, G_TYPE_OBJECT) +#define LOCK_PROPS(x) g_mutex_lock (&((x)->props_lock)) +#define UNLOCK_PROPS(x) g_mutex_unlock (&((x)->props_lock)) enum { @@ -130,17 +120,16 @@ enum static void i_cal_object_set_property(GObject *object, guint property_id, const GValue * value, GParamSpec * pspec) { - ICalObject *iobject; + ICalObject *iobject = I_CAL_OBJECT(object); + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); g_return_if_fail(I_CAL_IS_OBJECT(object)); - iobject = I_CAL_OBJECT(object); - switch (property_id) { case PROP_NATIVE: /* no need for LOCK_PROPS() here, these can be set only during construction time */ - g_return_if_fail(iobject->priv->native == NULL); - iobject->priv->native = g_value_get_pointer(value); + g_return_if_fail(priv->native == NULL); + priv->native = g_value_get_pointer(value); return; case PROP_NATIVE_DESTROY_FUNC: @@ -149,7 +138,7 @@ static void i_cal_object_set_property(GObject *object, guint property_id, case PROP_IS_GLOBAL_MEMORY: /* no need for LOCK_PROPS() here, these can be set only during construction time */ - iobject->priv->is_global_memory = g_value_get_boolean(value); + priv->is_global_memory = g_value_get_boolean(value); return; case PROP_ALWAYS_DESTROY: @@ -167,12 +156,10 @@ static void i_cal_object_set_property(GObject *object, guint property_id, static void i_cal_object_get_property(GObject *object, guint property_id, GValue * value, GParamSpec * pspec) { - ICalObject *iobject; + ICalObject *iobject = I_CAL_OBJECT(object); g_return_if_fail(I_CAL_IS_OBJECT(object)); - iobject = I_CAL_OBJECT(object); - switch (property_id) { case PROP_NATIVE: g_value_set_pointer(value, i_cal_object_get_native(iobject)); @@ -201,20 +188,19 @@ static void i_cal_object_get_property(GObject *object, guint property_id, static void i_cal_object_finalize(GObject *object) { ICalObject *iobject = I_CAL_OBJECT(object); + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); - if ((iobject->priv->always_destroy || !iobject->priv->owner) && - !iobject->priv->is_global_memory && - iobject->priv->native && iobject->priv->native_destroy_func) { - iobject->priv->native_destroy_func(iobject->priv->native); + if ((priv->always_destroy || !priv->owner) && + !priv->is_global_memory && + priv->native && priv->native_destroy_func) { + g_clear_pointer(&priv->native, priv->native_destroy_func); } - if (iobject->priv->owner) { - g_object_unref(iobject->priv->owner); - } + g_clear_object(&priv->owner); - g_slist_free_full(iobject->priv->dependers, g_object_unref); + g_slist_free_full(priv->dependers, g_object_unref); - g_mutex_clear(&iobject->priv->props_lock); + g_mutex_clear(&priv->props_lock); /* Chain up to parent's method. */ G_OBJECT_CLASS(i_cal_object_parent_class)->finalize(object); @@ -224,8 +210,6 @@ static void i_cal_object_class_init(ICalObjectClass * class) { GObjectClass *object_class; - g_type_class_add_private(class, sizeof(ICalObjectPrivate)); - object_class = G_OBJECT_CLASS(class); object_class->set_property = i_cal_object_set_property; object_class->get_property = i_cal_object_get_property; @@ -319,9 +303,9 @@ static void i_cal_object_class_init(ICalObjectClass * class) static void i_cal_object_init(ICalObject *iobject) { - iobject->priv = G_TYPE_INSTANCE_GET_PRIVATE(iobject, I_CAL_TYPE_OBJECT, ICalObjectPrivate); + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); - g_mutex_init(&iobject->priv->props_lock); + g_mutex_init(&priv->props_lock); } /** @@ -354,6 +338,7 @@ i_cal_object_construct(GType object_type, GObject *owner) { ICalObject *iobject; + ICalObjectPrivate *priv; g_return_val_if_fail(object_type != G_TYPE_INVALID, NULL); g_return_val_if_fail(native != NULL, NULL); @@ -379,14 +364,15 @@ i_cal_object_construct(GType object_type, } iobject = g_object_new(object_type, NULL); + priv = i_cal_object_get_instance_private(iobject); /* LOCK_PROPS (iobject); */ - g_warn_if_fail(iobject->priv->native == NULL); + g_warn_if_fail(priv->native == NULL); - iobject->priv->native = native; - iobject->priv->native_destroy_func = native_destroy_func; - iobject->priv->is_global_memory = is_global_memory; + priv->native = native; + priv->native_destroy_func = native_destroy_func; + priv->is_global_memory = is_global_memory; i_cal_object_set_owner(iobject, owner); /* UNLOCK_PROPS (iobject); */ @@ -427,14 +413,15 @@ i_cal_object_construct(GType object_type, gpointer i_cal_object_get_native(ICalObject *iobject) { gpointer native; + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); g_return_val_if_fail(I_CAL_IS_OBJECT(iobject), NULL); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - native = iobject->priv->native; + native = priv->native; - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return native; } @@ -453,15 +440,15 @@ gpointer i_cal_object_get_native(ICalObject *iobject) gpointer i_cal_object_steal_native(ICalObject *iobject) { gpointer native; + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); g_return_val_if_fail(I_CAL_IS_OBJECT(iobject), NULL); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - native = iobject->priv->native; - iobject->priv->native = NULL; + native = g_steal_pointer(&priv->native); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return native; } @@ -480,14 +467,15 @@ gpointer i_cal_object_steal_native(ICalObject *iobject) GDestroyNotify i_cal_object_get_native_destroy_func(ICalObject *iobject) { GDestroyNotify func; + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); g_return_val_if_fail(I_CAL_IS_OBJECT(iobject), NULL); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - func = iobject->priv->native_destroy_func; + func = priv->native_destroy_func; - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return func; } @@ -503,18 +491,20 @@ GDestroyNotify i_cal_object_get_native_destroy_func(ICalObject *iobject) **/ void i_cal_object_set_native_destroy_func(ICalObject *iobject, GDestroyNotify native_destroy_func) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); + g_return_if_fail(I_CAL_IS_OBJECT(iobject)); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - if (native_destroy_func == iobject->priv->native_destroy_func) { - UNLOCK_PROPS(iobject); + if (native_destroy_func == priv->native_destroy_func) { + UNLOCK_PROPS(priv); return; } - iobject->priv->native_destroy_func = native_destroy_func; + priv->native_destroy_func = native_destroy_func; - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); g_object_notify(G_OBJECT(iobject), "native-destroy-func"); } @@ -532,15 +522,16 @@ void i_cal_object_set_native_destroy_func(ICalObject *iobject, GDestroyNotify na **/ gboolean i_cal_object_get_is_global_memory(ICalObject *iobject) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); gboolean is_global_memory; g_return_val_if_fail(I_CAL_IS_OBJECT(iobject), FALSE); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - is_global_memory = iobject->priv->is_global_memory; + is_global_memory = priv->is_global_memory; - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return is_global_memory; } @@ -557,24 +548,22 @@ gboolean i_cal_object_get_is_global_memory(ICalObject *iobject) **/ void i_cal_object_set_owner(ICalObject *iobject, GObject *owner) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); + g_return_if_fail(I_CAL_IS_OBJECT(iobject)); if (owner) g_return_if_fail(G_IS_OBJECT(owner)); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - if (owner == iobject->priv->owner) { - UNLOCK_PROPS(iobject); + if (owner == priv->owner) { + UNLOCK_PROPS(priv); return; } - if (owner) { - g_object_ref(owner); - } - g_clear_object(&iobject->priv->owner); - iobject->priv->owner = owner; + g_set_object(&priv->owner, owner); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); g_object_notify(G_OBJECT(iobject), "owner"); } @@ -594,17 +583,18 @@ void i_cal_object_set_owner(ICalObject *iobject, GObject *owner) **/ GObject *i_cal_object_ref_owner(ICalObject *iobject) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); GObject *owner; g_return_val_if_fail(I_CAL_IS_OBJECT(iobject), NULL); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - owner = iobject->priv->owner; + owner = priv->owner; if (owner) g_object_ref(owner); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return owner; } @@ -619,19 +609,15 @@ GObject *i_cal_object_ref_owner(ICalObject *iobject) **/ void i_cal_object_remove_owner(ICalObject *iobject) { - GObject *owner; + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); g_return_if_fail(I_CAL_IS_OBJECT(iobject)); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - owner = iobject->priv->owner; - if (owner) { - g_object_unref(owner); - iobject->priv->owner = NULL; - } + g_clear_object(&priv->owner); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); } /** @@ -648,20 +634,22 @@ void i_cal_object_remove_owner(ICalObject *iobject) **/ void i_cal_object_add_depender(ICalObject *iobject, GObject *depender) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); + g_return_if_fail(I_CAL_IS_OBJECT(iobject)); g_return_if_fail(G_IS_OBJECT(depender)); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - if (g_slist_find(iobject->priv->dependers, depender)) { + if (g_slist_find(priv->dependers, depender)) { g_warn_if_reached(); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return; } - iobject->priv->dependers = g_slist_prepend(iobject->priv->dependers, g_object_ref(depender)); + priv->dependers = g_slist_prepend(priv->dependers, g_object_ref(depender)); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); } /** @@ -677,21 +665,23 @@ void i_cal_object_add_depender(ICalObject *iobject, GObject *depender) **/ void i_cal_object_remove_depender(ICalObject *iobject, GObject *depender) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); + g_return_if_fail(I_CAL_IS_OBJECT(iobject)); g_return_if_fail(G_IS_OBJECT(depender)); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - if (!g_slist_find(iobject->priv->dependers, depender)) { + if (!g_slist_find(priv->dependers, depender)) { g_warn_if_reached(); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return; } - iobject->priv->dependers = g_slist_remove(iobject->priv->dependers, depender); + priv->dependers = g_slist_remove(priv->dependers, depender); g_object_unref(depender); - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); } /** @@ -706,17 +696,18 @@ void i_cal_object_remove_depender(ICalObject *iobject, GObject *depender) **/ void i_cal_object_set_always_destroy(ICalObject *iobject, gboolean value) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); gboolean changed; g_return_if_fail(I_CAL_IS_OBJECT(iobject)); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - changed = (value ? 1 : 0) != (iobject->priv->always_destroy ? 1 : 0); + changed = (value ? 1 : 0) != (priv->always_destroy ? 1 : 0); if (changed) - iobject->priv->always_destroy = value; + priv->always_destroy = value; - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); if (changed) g_object_notify(G_OBJECT(iobject), "always-destroy"); @@ -734,15 +725,16 @@ void i_cal_object_set_always_destroy(ICalObject *iobject, gboolean value) **/ gboolean i_cal_object_get_always_destroy(ICalObject *iobject) { + ICalObjectPrivate *priv = i_cal_object_get_instance_private(iobject); gboolean value; g_return_val_if_fail(I_CAL_IS_OBJECT(iobject), FALSE); - LOCK_PROPS(iobject); + LOCK_PROPS(priv); - value = iobject->priv->always_destroy; + value = priv->always_destroy; - UNLOCK_PROPS(iobject); + UNLOCK_PROPS(priv); return value; } |