From: Felipe Contreras <felipe.contreras@gmail.com>
To: notmuch@notmuchmail.org
Cc: Ali Polatel <alip@exherbo.org>,
Austin Clements <austin@google.com>,
Tomi Ollila <tomi.ollila@iki.fi>,
Ludovic LANGE <ll-notmuchmail@lange.nom.fr>,
Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Subject: [PATCH v3 7/7] ruby: new notmuch_rb_object_destroy() helper
Date: Sat, 15 May 2021 16:21:03 -0500 [thread overview]
Message-ID: <20210515212103.1001295-8-felipe.contreras@gmail.com> (raw)
In-Reply-To: <20210515212103.1001295-1-felipe.contreras@gmail.com>
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 | 6 +-----
bindings/ruby/defs.h | 15 +++++++++++++++
bindings/ruby/directory.c | 7 +------
bindings/ruby/filenames.c | 7 +------
bindings/ruby/init.c | 1 +
bindings/ruby/message.c | 7 +------
bindings/ruby/messages.c | 7 +------
bindings/ruby/query.c | 7 +------
bindings/ruby/tags.c | 7 +------
bindings/ruby/thread.c | 7 +------
bindings/ruby/threads.c | 7 +------
11 files changed, 25 insertions(+), 53 deletions(-)
diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c
index 4ecc8f78..bb993d86 100644
--- a/bindings/ruby/database.c
+++ b/bindings/ruby/database.c
@@ -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, ¬much_rb_database_type);
notmuch_rb_status_raise (ret);
return Qnil;
diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h
index fa7b9515..9860ee17 100644
--- a/bindings/ruby/defs.h
+++ b/bindings/ruby/defs.h
@@ -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), ¬much_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);
diff --git a/bindings/ruby/directory.c b/bindings/ruby/directory.c
index 17d60d1d..910f0a99 100644
--- a/bindings/ruby/directory.c
+++ b/bindings/ruby/directory.c
@@ -28,12 +28,7 @@
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, ¬much_rb_directory_type);
return Qnil;
}
diff --git a/bindings/ruby/filenames.c b/bindings/ruby/filenames.c
index 656c58e6..0dec1952 100644
--- a/bindings/ruby/filenames.c
+++ b/bindings/ruby/filenames.c
@@ -28,12 +28,7 @@
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, ¬much_rb_filenames_type);
return Qnil;
}
diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c
index a9f863eb..62515eca 100644
--- a/bindings/ruby/init.c
+++ b/bindings/ruby/init.c
@@ -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 = ¬much_rb_object_type, \
+ .data = ¬much_ ## id ## _destroy, \
}
define_type (database);
diff --git a/bindings/ruby/message.c b/bindings/ruby/message.c
index b3aed604..f45c95cc 100644
--- a/bindings/ruby/message.c
+++ b/bindings/ruby/message.c
@@ -28,12 +28,7 @@
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, ¬much_rb_message_type);
return Qnil;
}
diff --git a/bindings/ruby/messages.c b/bindings/ruby/messages.c
index e04f3af1..ca5b10d0 100644
--- a/bindings/ruby/messages.c
+++ b/bindings/ruby/messages.c
@@ -28,12 +28,7 @@
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, ¬much_rb_messages_type);
return Qnil;
}
diff --git a/bindings/ruby/query.c b/bindings/ruby/query.c
index 79727d6a..3ec98c6c 100644
--- a/bindings/ruby/query.c
+++ b/bindings/ruby/query.c
@@ -28,12 +28,7 @@
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, ¬much_rb_query_type);
return Qnil;
}
diff --git a/bindings/ruby/tags.c b/bindings/ruby/tags.c
index db8b4cfc..2af85e36 100644
--- a/bindings/ruby/tags.c
+++ b/bindings/ruby/tags.c
@@ -28,12 +28,7 @@
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, ¬much_rb_tags_type);
return Qnil;
}
diff --git a/bindings/ruby/thread.c b/bindings/ruby/thread.c
index f6bf7849..7cb2a3dc 100644
--- a/bindings/ruby/thread.c
+++ b/bindings/ruby/thread.c
@@ -28,12 +28,7 @@
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, ¬much_rb_thread_type);
return Qnil;
}
diff --git a/bindings/ruby/threads.c b/bindings/ruby/threads.c
index d809b571..50280260 100644
--- a/bindings/ruby/threads.c
+++ b/bindings/ruby/threads.c
@@ -28,12 +28,7 @@
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, ¬much_rb_threads_type);
return Qnil;
}
--
2.31.1
prev parent reply other threads:[~2021-05-15 21:21 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-15 21:20 [PATCH v3 0/7] ruby: object cleanups Felipe Contreras
2021-05-15 21:20 ` [PATCH v3 1/7] ruby: simplify data get helper Felipe Contreras
2021-05-17 10:55 ` David Bremner
2021-05-15 21:20 ` [PATCH v3 2/7] ruby: fetch class name in case of error Felipe Contreras
2021-05-15 21:20 ` [PATCH v3 3/7] ruby: add unlikely hint Felipe Contreras
2021-05-15 21:21 ` [PATCH v3 4/7] ruby: create Data_Wrap_Notmuch_Object helper Felipe Contreras
2021-05-15 21:21 ` [PATCH v3 5/7] ruby: move towards more modern RTypedData Felipe Contreras
2021-05-15 21:21 ` [PATCH v3 6/7] ruby: add all data types Felipe Contreras
2021-05-15 21:21 ` Felipe Contreras [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210515212103.1001295-8-felipe.contreras@gmail.com \
--to=felipe.contreras@gmail.com \
--cc=alip@exherbo.org \
--cc=austin@google.com \
--cc=dkg@fifthhorseman.net \
--cc=ll-notmuchmail@lange.nom.fr \
--cc=notmuch@notmuchmail.org \
--cc=tomi.ollila@iki.fi \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).