]> git.cworth.org Git - notmuch/commitdiff
ruby: new notmuch_rb_object_destroy() helper
authorFelipe Contreras <felipe.contreras@gmail.com>
Sat, 15 May 2021 21:21:03 +0000 (16:21 -0500)
committerDavid Bremner <david@tethera.net>
Mon, 17 May 2021 10:25:14 +0000 (07:25 -0300)
The struct used to store the types (rb_data_type_t) contains a "data"
field where we can store whatever we want. I use that field to store a
pointer to the corresponding destroy function. For example
notmuch_rb_database_type contains a pointer to notmuch_database_destroy.

I cast that pointer as a notmuch_status_t (func*)(void *) and call
that function passing the internal object (e.g. notmuch_database_t).

Using the rb_data_type_t data we can call the correct notmuch destroy
function.

Therefore this:

  ret = ((notmuch_status_t (*)(void *)) type->data) (nm_object);

Is effectively the same as this:

  ret = notmuch_database_destroy (database);

The advantage of doing it this way is that much less code is necesary
since each rb_data_type_t has the corresponding destroy function stored
in it.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
bindings/ruby/database.c
bindings/ruby/defs.h
bindings/ruby/directory.c
bindings/ruby/filenames.c
bindings/ruby/init.c
bindings/ruby/message.c
bindings/ruby/messages.c
bindings/ruby/query.c
bindings/ruby/tags.c
bindings/ruby/thread.c
bindings/ruby/threads.c

index 4ecc8f78f99225da983bbf6d16d0bdd0599e588e..bb993d8688848df012f26d4f9a545f31b78c195d 100644 (file)
@@ -114,11 +114,7 @@ VALUE
 notmuch_rb_database_close (VALUE self)
 {
     notmuch_status_t ret;
-    notmuch_database_t *db;
-
-    Data_Get_Notmuch_Database (self, db);
-    ret = notmuch_database_destroy (db);
-    DATA_PTR (self) = NULL;
+    ret = notmuch_rb_object_destroy (self, &notmuch_rb_database_type);
     notmuch_rb_status_raise (ret);
 
     return Qnil;
index fa7b9515c9a8347e895a596b9a3f6b136925114a..9860ee17d9479e1bc1101978585ed986e0e704c1 100644 (file)
@@ -105,6 +105,21 @@ extern const rb_data_type_t notmuch_rb_tags_type;
 #define Data_Get_Notmuch_Tags(obj, ptr) \
     Data_Get_Notmuch_Object ((obj), &notmuch_rb_tags_type, (ptr))
 
+static inline notmuch_status_t
+notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
+{
+    void *nm_object;
+    notmuch_status_t ret;
+
+    Data_Get_Notmuch_Object (rb_object, type, nm_object);
+
+    /* Call the corresponding notmuch_*_destroy function */
+    ret = ((notmuch_status_t (*)(void *)) type->data) (nm_object);
+    DATA_PTR (rb_object) = NULL;
+
+    return ret;
+}
+
 /* status.c */
 void
 notmuch_rb_status_raise (notmuch_status_t status);
index 17d60d1d049c50d6d98457cd8354cb97a2347694..910f0a99f238239f2c5717b2a85b2cbca190bcf5 100644 (file)
 VALUE
 notmuch_rb_directory_destroy (VALUE self)
 {
-    notmuch_directory_t *dir;
-
-    Data_Get_Notmuch_Directory (self, dir);
-
-    notmuch_directory_destroy (dir);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_directory_type);
 
     return Qnil;
 }
index 656c58e6f40dfdc87bcbc86b6dd7b2eb2680ecab..0dec19520b59b0b421df5c7a4cb03826e69d235d 100644 (file)
 VALUE
 notmuch_rb_filenames_destroy (VALUE self)
 {
-    notmuch_filenames_t *fnames;
-
-    Data_Get_Notmuch_FileNames (self, fnames);
-
-    notmuch_filenames_destroy (fnames);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_filenames_type);
 
     return Qnil;
 }
index a9f863ebd22cb1fc4e4c774fd1f8f8729fa47c25..62515ecad54b11120b29e95f816476c9727587cb 100644 (file)
@@ -54,6 +54,7 @@ const rb_data_type_t notmuch_rb_object_type = {
     const rb_data_type_t notmuch_rb_ ## id ## _type = { \
        .wrap_struct_name = "notmuch_" #id, \
        .parent = &notmuch_rb_object_type, \
+       .data = &notmuch_ ## id ## _destroy, \
     }
 
 define_type (database);
index b3aed604033aff41ff49d4eba324bfe502a50f50..f45c95cc5051d8475920c5a59bf07706e829e0be 100644 (file)
 VALUE
 notmuch_rb_message_destroy (VALUE self)
 {
-    notmuch_message_t *message;
-
-    Data_Get_Notmuch_Message (self, message);
-
-    notmuch_message_destroy (message);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_message_type);
 
     return Qnil;
 }
index e04f3af143bb30a4bcffea595615fc7e9ae5e6ff..ca5b10d04b09702629eddbeb1eb217bef84a4274 100644 (file)
 VALUE
 notmuch_rb_messages_destroy (VALUE self)
 {
-    notmuch_messages_t *messages;
-
-    Data_Get_Notmuch_Messages (self, messages);
-
-    notmuch_messages_destroy (messages);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_messages_type);
 
     return Qnil;
 }
index 79727d6a6aeb5dbebf5e977365244e6fd1406826..3ec98c6c1d80db12feb7dd66c8b5e2fa3201a56e 100644 (file)
 VALUE
 notmuch_rb_query_destroy (VALUE self)
 {
-    notmuch_query_t *query;
-
-    Data_Get_Notmuch_Query (self, query);
-
-    notmuch_query_destroy (query);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_query_type);
 
     return Qnil;
 }
index db8b4cfc86f0b3eaa818ce0d7876862158826862..2af85e36598220f1cb93e806a8080ed2c3a3b862 100644 (file)
 VALUE
 notmuch_rb_tags_destroy (VALUE self)
 {
-    notmuch_tags_t *tags;
-
-    Data_Get_Notmuch_Tags (self, tags);
-
-    notmuch_tags_destroy (tags);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_tags_type);
 
     return Qnil;
 }
index f6bf7849b12ab578626fc6f95344c353d2a06c2d..7cb2a3dcc6ebeacdd5c4c37b726a53056c436a5c 100644 (file)
 VALUE
 notmuch_rb_thread_destroy (VALUE self)
 {
-    notmuch_thread_t *thread;
-
-    Data_Get_Notmuch_Thread (self, thread);
-
-    notmuch_thread_destroy (thread);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_thread_type);
 
     return Qnil;
 }
index d809b571d3335ad30d817ed0daeed03bec0133b8..50280260a4d3aa9576f4d09c7d9528ed6be6539c 100644 (file)
 VALUE
 notmuch_rb_threads_destroy (VALUE self)
 {
-    notmuch_threads_t *threads;
-
-    Data_Get_Notmuch_Threads (self, threads);
-
-    notmuch_threads_destroy (threads);
-    DATA_PTR (self) = NULL;
+    notmuch_rb_object_destroy (self, &notmuch_rb_threads_type);
 
     return Qnil;
 }