[nautilus/wip/ernestask/attributes: 26/29] Object-oriented attribute implementation



commit 9d80fa4b84d97fab37f7776f040829632c0da52f
Author: Ernestas Kulik <ernestask gnome org>
Date:   Tue Aug 15 21:38:59 2017 +0300

    Object-oriented attribute implementation
    
    …or something.

 src-ng/nautilus-attribute.c |  152 +++++++++++++++++++++++++++++++++++++++++++
 src-ng/nautilus-attribute.h |   94 ++++++++++++++++++++++++++
 2 files changed, 246 insertions(+), 0 deletions(-)
---
diff --git a/src-ng/nautilus-attribute.c b/src-ng/nautilus-attribute.c
new file mode 100644
index 0000000..9f84f4c
--- /dev/null
+++ b/src-ng/nautilus-attribute.c
@@ -0,0 +1,152 @@
+/* Copyright (C) 2017 Ernestas Kulik <ernestask gnome org>
+ *
+ * This file is part of Nautilus.
+ *
+ * Nautilus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Nautilus 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Nautilus.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "nautilus-attribute.h"
+
+typedef struct
+{
+    gpointer value;
+    NautilusAttributeState state;
+
+    NautilusCopyFunc copy_func;
+    GDestroyNotify destroy_func;
+
+    GMutex mutex;
+} NautilusAttributePrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (NautilusAttribute, nautilus_attribute, G_TYPE_OBJECT)
+
+static void
+finalize (GObject *object)
+{
+    NautilusAttribute *self;
+    NautilusAttributePrivate *priv;
+
+    self = NAUTILUS_ATTRIBUTE (object);
+    priv = nautilus_attribute_get_instance_private (self);
+
+    if (priv->destroy_func != NULL && priv->value != NULL)
+    {
+        g_clear_pointer (&priv->value, priv->destroy_func);
+    }
+
+    g_mutex_clear (&priv->mutex);
+
+    G_OBJECT_CLASS (nautilus_attribute_parent_class)->finalize (object);
+}
+
+static void
+nautilus_attribute_class_init (NautilusAttributeClass *klass)
+{
+    GObjectClass *object_class;
+
+    object_class = G_OBJECT_CLASS (klass);
+
+    object_class->finalize = finalize;
+}
+
+static void
+nautilus_attribute_init (NautilusAttribute *self)
+{
+    NautilusAttributePrivate *priv;
+
+    priv = nautilus_attribute_get_instance_private (self);
+
+    g_mutex_init (&priv->mutex);
+}
+
+void
+nautilus_attribute_get_value (NautilusAttribute                    *attribute,
+                              gboolean                              update,
+                              NautilusAttributeUpdateValueCallback  callback,
+                              gpointer                              user_data)
+{
+    NautilusAttributePrivate *priv;
+    gpointer value;
+
+    g_return_if_fail (NAUTILUS_IS_ATTRIBUTE (attribute));
+
+    if (callback == NULL)
+    {
+        return;
+    }
+
+    priv = nautilus_attribute_get_instance_private (attribute);
+
+    if (!update)
+    {
+        if (priv->copy_func != NULL && priv->value != NULL)
+        {
+            value = priv->copy_func (priv->value);
+        }
+        else
+        {
+            value = priv->value;
+        }
+    }
+}
+
+void
+nautilus_attribute_set_value (NautilusAttribute *attribute,
+                              gpointer           value)
+{
+    NautilusAttributePrivate *priv;
+
+    g_return_if_fail (NAUTILUS_IS_ATTRIBUTE (attribute));
+
+    priv = nautilus_attribute_get_instance_private (attribute);
+
+    g_mutex_lock (&priv->mutex);
+
+    if (priv->destroy_func != NULL && priv->value != NULL)
+    {
+        priv->destroy_func (priv->value);
+    }
+
+    if (priv->copy_func != NULL)
+    {
+        priv->value = priv->copy_func (value);
+    }
+    else
+    {
+        priv->value = value;
+    }
+
+    /* If an update is pending,
+     * the new value divined shall be discarded after the state check.
+     */
+    priv->state = NAUTILUS_ATTRIBUTE_STATE_VALID;
+
+    g_mutex_unlock (&priv->mutex);
+}
+
+NautilusAttribute *
+nautilus_attribute_new (NautilusCopyFunc copy_func,
+                        GDestroyNotify   destroy_func)
+{
+    NautilusAttribute *instance;
+    NautilusAttributePrivate *priv;
+
+    instance = g_object_new (NAUTILUS_TYPE_ATTRIBUTE, NULL);
+    priv = nautilus_attribute_get_instance_private (instance);
+
+    priv->copy_func = copy_func;
+    priv->destroy_func = destroy_func;
+
+    return instance;
+}
diff --git a/src-ng/nautilus-attribute.h b/src-ng/nautilus-attribute.h
new file mode 100644
index 0000000..49a6677
--- /dev/null
+++ b/src-ng/nautilus-attribute.h
@@ -0,0 +1,94 @@
+/* Copyright (C) 2017 Ernestas Kulik <ernestask gnome org>
+ *
+ * This file is part of Nautilus.
+ *
+ * Nautilus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Nautilus 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Nautilus.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef NAUTILUS_ATTRIBUTE_H_INCLUDED
+#define NAUTILUS_ATTRIBUTE_H_INCLUDED
+
+#include <glib-object.h>
+
+#define NAUTILUS_TYPE_ATTRIBUTE (nautilus_attribute_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (NautilusAttribute, nautilus_attribute, NAUTILUS, ATTRIBUTE, GObject)
+
+/* GCopyFunc has too many parameters for our taste. */
+typedef gpointer (*NautilusCopyFunc) (gpointer data);
+#define NAUTILUS_COPY_FUNC(x) ((NautilusCopyFunc) x)
+
+typedef void (*NautilusAttributeUpdateValueCallback) (NautilusAttribute *attribute,
+                                                      gpointer           value,
+                                                      gpointer           user_data);
+
+typedef enum
+{
+    NAUTILUS_ATTRIBUTE_STATE_INVALID,
+    NAUTILUS_ATTRIBUTE_STATE_PENDING,
+    NAUTILUS_ATTRIBUTE_STATE_VALID
+} NautilusAttributeState;
+
+struct _NautilusAttributeClass
+{
+    GObjectClass parent_class;
+
+    void (*update) (NautilusAttribute *attribute);
+};
+
+/**
+ * nautilus_attribute_get_state:
+ * @attribute: an initialized #NautilusAttribute
+ *
+ * Returns: the current state of @attribute
+ */
+NautilusAttributeState nautilus_attribute_get_state  (NautilusAttribute *attribute);
+/**
+ * nautilus_attribute_invalidate:
+ * @attribute: an initialized #NautilusAttribute
+ *
+ * Mark the value of @attribute as no longer valid.
+ */
+void                   nautilus_attribute_invalidate (NautilusAttribute *attribute);
+
+/**
+ * nautilus_attribute_get_value:
+ * @attribute: an initialized #NautilusAttribute
+ * @update: whether the value should be updated if invalid
+ * @callback: the function to call with the value of @attribute
+ * @user_data: additional data to pass to @callback
+ */
+void nautilus_attribute_get_value (NautilusAttribute                    *attribute,
+                                   gboolean                              update,
+                                   NautilusAttributeUpdateValueCallback  callback,
+                                   gpointer                              user_data);
+/**
+ * nautilus_attribute_set_value:
+ * @attribute: an initialized #NautilusAttribute
+ * @value: (nullable) (transfer full): the new value of @attribute
+ */
+void nautilus_attribute_set_value (NautilusAttribute *attribute,
+                                   gpointer           value);
+
+/**
+ * nautilus_attribute_new:
+ * @copy_func: (nullable): the function to call when copying the value
+ * @destroy_func: (nullable): the function to call when destroying the value
+ *
+ * Returns: a new #NautilusAttribute
+ */
+NautilusAttribute *nautilus_attribute_new (NautilusCopyFunc copy_func,
+                                           GDestroyNotify   destroy_func);
+
+#endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]