[ghex] HexBufferIface: implement properties



commit 6945ab6558e37c4c6a84f7bd879a45bec27981ac
Author: Logan Rathbone <poprocks gmail com>
Date:   Thu Dec 23 01:05:54 2021 -0500

    HexBufferIface: implement properties
    
    Just for GFile so far.

 src/hex-buffer-iface.c  |  7 ++++-
 src/hex-buffer-iface.h  |  2 ++
 src/hex-buffer-malloc.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++
 src/hex-buffer-mmap.c   | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 160 insertions(+), 2 deletions(-)
---
diff --git a/src/hex-buffer-iface.c b/src/hex-buffer-iface.c
index 9fb5f98..11d5fc8 100644
--- a/src/hex-buffer-iface.c
+++ b/src/hex-buffer-iface.c
@@ -29,7 +29,12 @@ G_DEFINE_INTERFACE (HexBuffer, hex_buffer, G_TYPE_OBJECT)
 static void
 hex_buffer_default_init (HexBufferInterface *iface)
 {
-    /* add properties and signals to the interface here */
+       g_object_interface_install_property (iface,
+                       g_param_spec_object ("file",
+                               "File",
+                               "File (as GFile) being utilized by the buffer",
+                               G_TYPE_FILE,
+                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY));
 }
 
 /* PUBLIC INTERFACE FUNCTIONS */
diff --git a/src/hex-buffer-iface.h b/src/hex-buffer-iface.h
index f9e50f9..b55aa83 100644
--- a/src/hex-buffer-iface.h
+++ b/src/hex-buffer-iface.h
@@ -53,6 +53,8 @@ struct _HexBufferInterface
                        size_t rep_len,
                        char *data);
 
+       GFile * (*get_file) (HexBuffer *self);
+
        gboolean (*set_file) (HexBuffer *self,
                        GFile *file);
 
diff --git a/src/hex-buffer-malloc.c b/src/hex-buffer-malloc.c
index dc8ee78..ef35413 100644
--- a/src/hex-buffer-malloc.c
+++ b/src/hex-buffer-malloc.c
@@ -26,6 +26,15 @@
 
 #include "hex-buffer-malloc.h"
 
+/* PROPERTIES */
+
+enum
+{
+       PROP_FILE = 1,
+       N_PROPERTIES
+};
+
+static GParamSpec *properties[N_PROPERTIES];
 struct _HexBufferMalloc
 {
        GObject parent_instance;
@@ -44,6 +53,57 @@ static void hex_buffer_malloc_iface_init (HexBufferInterface *iface);
 G_DEFINE_TYPE_WITH_CODE (HexBufferMalloc, hex_buffer_malloc, G_TYPE_OBJECT,
                G_IMPLEMENT_INTERFACE (HEX_TYPE_BUFFER, hex_buffer_malloc_iface_init))
 
+/* FORWARD DECLARATIONS */
+       
+static gboolean        hex_buffer_malloc_set_file (HexBuffer *buf, GFile *file);
+static GFile * hex_buffer_malloc_get_file (HexBuffer *buf);
+
+/* PROPERTIES - GETTERS AND SETTERS */
+
+static void
+hex_buffer_malloc_set_property (GObject *object,
+               guint property_id,
+               const GValue *value,
+               GParamSpec *pspec)
+{
+       HexBufferMalloc *self = HEX_BUFFER_MALLOC(object);
+       HexBuffer *buf = HEX_BUFFER(object);
+
+       switch (property_id)
+       {
+               case PROP_FILE:
+                       hex_buffer_malloc_set_file (buf, g_value_get_object (value));
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
+static void
+hex_buffer_malloc_get_property (GObject *object,
+               guint property_id,
+               GValue *value,
+               GParamSpec *pspec)
+{
+       HexBufferMalloc *self = HEX_BUFFER_MALLOC(object);
+       HexBuffer *buf = HEX_BUFFER(object);
+
+       switch (property_id)
+       {
+               case PROP_FILE:
+                       g_value_set_pointer (value, self->file);
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
 /* PRIVATE FUNCTIONS */
 
 static gboolean
@@ -57,6 +117,15 @@ update_payload_size_from_file (HexBufferMalloc *self)
                return TRUE;
 }
 
+/* transfer: none */
+static GFile *
+hex_buffer_malloc_get_file (HexBuffer *buf)
+{
+       HexBufferMalloc *self = HEX_BUFFER_MALLOC (buf);
+
+       return self->file;
+}
+
 static gboolean
 hex_buffer_malloc_set_file (HexBuffer *buf, GFile *file)
 {
@@ -389,6 +458,11 @@ hex_buffer_malloc_class_init (HexBufferMallocClass *klass)
        
        gobject_class->finalize = hex_buffer_malloc_finalize;
        gobject_class->dispose = hex_buffer_malloc_dispose;
+
+       gobject_class->set_property = hex_buffer_malloc_set_property;
+       gobject_class->get_property = hex_buffer_malloc_get_property;
+
+       g_object_class_override_property (gobject_class, PROP_FILE, "file");
 }
 
 
@@ -418,6 +492,7 @@ hex_buffer_malloc_iface_init (HexBufferInterface *iface)
        iface->get_data = hex_buffer_malloc_get_data;
        iface->get_byte = hex_buffer_malloc_get_byte;
        iface->set_data = hex_buffer_malloc_set_data;
+       iface->get_file = hex_buffer_malloc_get_file;
        iface->set_file = hex_buffer_malloc_set_file;
        iface->read = hex_buffer_malloc_read;
        iface->read_async = hex_buffer_malloc_read_async;
diff --git a/src/hex-buffer-mmap.c b/src/hex-buffer-mmap.c
index 4bca2fc..33137d0 100644
--- a/src/hex-buffer-mmap.c
+++ b/src/hex-buffer-mmap.c
@@ -37,6 +37,16 @@ hex_buffer_mmap_error_quark (void)
   return g_quark_from_static_string ("hex-buffer-mmap-error-quark");
 }
 
+
+/* PROPERTIES */
+
+enum
+{
+       PROP_FILE = 1,
+       N_PROPERTIES
+};
+static GParamSpec *properties[N_PROPERTIES];
+
 static char *invalid_path_msg = N_("The file appears to have an invalid path.");
 
 struct _HexBufferMmap
@@ -66,6 +76,56 @@ static void hex_buffer_mmap_iface_init (HexBufferInterface *iface);
 G_DEFINE_TYPE_WITH_CODE (HexBufferMmap, hex_buffer_mmap, G_TYPE_OBJECT,
                G_IMPLEMENT_INTERFACE (HEX_TYPE_BUFFER, hex_buffer_mmap_iface_init))
 
+/* FORWARD DECLARATIONS */
+       
+static gboolean        hex_buffer_mmap_set_file (HexBuffer *buf, GFile *file);
+static GFile * hex_buffer_mmap_get_file (HexBuffer *buf);
+
+/* PROPERTIES - GETTERS AND SETTERS */
+
+static void
+hex_buffer_mmap_set_property (GObject *object,
+               guint property_id,
+               const GValue *value,
+               GParamSpec *pspec)
+{
+       HexBufferMmap *self = HEX_BUFFER_MMAP(object);
+       HexBuffer *buf = HEX_BUFFER(object);
+
+       switch (property_id)
+       {
+               case PROP_FILE:
+                       hex_buffer_mmap_set_file (buf, g_value_get_object (value));
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
+
+static void
+hex_buffer_mmap_get_property (GObject *object,
+               guint property_id,
+               GValue *value,
+               GParamSpec *pspec)
+{
+       HexBufferMmap *self = HEX_BUFFER_MMAP(object);
+       HexBuffer *buf = HEX_BUFFER(object);
+
+       switch (property_id)
+       {
+               case PROP_FILE:
+                       g_value_set_pointer (value, self->file);
+                       break;
+
+               default:
+                       /* We don't have any other property... */
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
+       }
+}
 
 /* PRIVATE FUNCTIONS */
 
@@ -163,6 +223,11 @@ hex_buffer_mmap_class_init (HexBufferMmapClass *klass)
        
        gobject_class->finalize = hex_buffer_mmap_finalize;
        gobject_class->dispose = hex_buffer_mmap_dispose;
+
+       gobject_class->set_property = hex_buffer_mmap_set_property;
+       gobject_class->get_property = hex_buffer_mmap_get_property;
+
+       g_object_class_override_property (gobject_class, PROP_FILE, "file");
 }
 
 static void
@@ -452,6 +517,15 @@ hex_buffer_mmap_get_payload_size (HexBuffer *buf)
        return self->payload;
 }
 
+/* transfer: none */
+static GFile *
+hex_buffer_mmap_get_file (HexBuffer *buf)
+{
+       HexBufferMmap *self = HEX_BUFFER_MMAP (buf);
+
+       return self->file;
+}
+
 static gboolean
 hex_buffer_mmap_set_file (HexBuffer *buf, GFile *file)
 {
@@ -466,8 +540,9 @@ hex_buffer_mmap_set_file (HexBuffer *buf, GFile *file)
                set_error (self, _(invalid_path_msg));
                return FALSE;
        }
-       self->file = file;
 
+       self->file = file;
+       g_object_notify_by_pspec (G_OBJECT(self), properties[PROP_FILE]);
        return TRUE;
 }
 
@@ -726,6 +801,7 @@ hex_buffer_mmap_iface_init (HexBufferInterface *iface)
        iface->get_data = hex_buffer_mmap_get_data;
        iface->get_byte = hex_buffer_mmap_get_byte;
        iface->set_data = hex_buffer_mmap_set_data;
+       iface->get_file = hex_buffer_mmap_get_file;
        iface->set_file = hex_buffer_mmap_set_file;
        iface->read = hex_buffer_mmap_read;
        iface->read_async = hex_buffer_mmap_read_async;


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