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>
notmuch_rb_database_close (VALUE self)
{
notmuch_status_t ret;
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;
notmuch_rb_status_raise (ret);
return Qnil;
#define Data_Get_Notmuch_Tags(obj, ptr) \
Data_Get_Notmuch_Object ((obj), ¬much_rb_tags_type, (ptr))
#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);
/* status.c */
void
notmuch_rb_status_raise (notmuch_status_t status);
VALUE
notmuch_rb_directory_destroy (VALUE self)
{
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);
VALUE
notmuch_rb_filenames_destroy (VALUE self)
{
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);
const rb_data_type_t notmuch_rb_ ## id ## _type = { \
.wrap_struct_name = "notmuch_" #id, \
.parent = ¬much_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);
}
define_type (database);
VALUE
notmuch_rb_message_destroy (VALUE self)
{
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);
VALUE
notmuch_rb_messages_destroy (VALUE self)
{
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);
VALUE
notmuch_rb_query_destroy (VALUE self)
{
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);
VALUE
notmuch_rb_tags_destroy (VALUE self)
{
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);
VALUE
notmuch_rb_thread_destroy (VALUE self)
{
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);
VALUE
notmuch_rb_threads_destroy (VALUE self)
{
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);