]> git.notmuchmail.org Git - notmuch/commitdiff
ruby: Don't barf if an object is destroyed more than once
authorAli Polatel <alip@exherbo.org>
Wed, 26 May 2010 17:56:07 +0000 (20:56 +0300)
committerAli Polatel <alip@exherbo.org>
Sun, 6 Jun 2010 06:18:00 +0000 (09:18 +0300)
Raise RuntimeError instead.
Also revise Notmuch::Database a bit.
Add Notmuch::Database.open singleton method.

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 6824efdcc7aad99082593e2a9a177d52924f148c..f4edd77303afb2be7ed64b22d49b789cd43b1472 100644 (file)
 
 #include "defs.h"
 
 
 #include "defs.h"
 
+VALUE
+notmuch_rb_database_alloc(VALUE klass)
+{
+    return Data_Wrap_Struct(klass, NULL, NULL, NULL);
+}
+
 /*
 /*
- * call-seq: Notmuch::Database.new(path, [{:create => false, :mode => notmuch::MODE_READ_ONLY}]) => DB
+ * call-seq: Notmuch::Database.new(path [, {:create => false, :mode => Notmuch::MODE_READ_ONLY}]) => DB
  *
  * Create or open a notmuch database using the given path.
  *
  * Create or open a notmuch database using the given path.
+ *
  * If :create is +true+, create the database instead of opening.
  * If :create is +true+, create the database instead of opening.
+ *
  * The argument :mode specifies the open mode of the database.
  */
 VALUE
  * The argument :mode specifies the open mode of the database.
  */
 VALUE
-notmuch_rb_database_new(int argc, VALUE *argv, VALUE klass)
+notmuch_rb_database_initialize(int argc, VALUE *argv, VALUE self)
 {
     const char *path;
     int create, mode;
 {
     const char *path;
     int create, mode;
-    notmuch_database_t *db;
     VALUE pathv, hashv;
     VALUE modev;
 
     VALUE pathv, hashv;
     VALUE modev;
 
@@ -70,11 +77,31 @@ notmuch_rb_database_new(int argc, VALUE *argv, VALUE klass)
         mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
     }
 
         mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
     }
 
-    db = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
-    if (!db)
+    Check_Type(self, T_DATA);
+    DATA_PTR(self) = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
+    if (!DATA_PTR(self))
         rb_raise(notmuch_rb_eDatabaseError, "Failed to open database");
 
         rb_raise(notmuch_rb_eDatabaseError, "Failed to open database");
 
-    return Data_Wrap_Struct(klass, NULL, NULL, db);
+    return self;
+}
+
+/*
+ * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
+ *
+ * Identical to new, except that when it is called with a block, it yields with
+ * the new instance and closes it, and returns the result which is returned from
+ * the block.
+ */
+VALUE
+notmuch_rb_database_open(int argc, VALUE *argv, VALUE klass)
+{
+    VALUE obj;
+
+    obj = rb_class_new_instance(argc, argv, klass);
+    if (!rb_block_given_p())
+        return obj;
+
+    return rb_ensure(rb_yield, obj, notmuch_rb_database_close, obj);
 }
 
 /*
 }
 
 /*
@@ -87,8 +114,9 @@ notmuch_rb_database_close(VALUE self)
 {
     notmuch_database_t *db;
 
 {
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
     notmuch_database_close(db);
     notmuch_database_close(db);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -103,7 +131,7 @@ notmuch_rb_database_path(VALUE self)
 {
     notmuch_database_t *db;
 
 {
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
     return rb_str_new2(notmuch_database_get_path(db));
 }
 
     return rb_str_new2(notmuch_database_get_path(db));
 }
@@ -118,7 +146,7 @@ notmuch_rb_database_version(VALUE self)
 {
     notmuch_database_t *db;
 
 {
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
     return INT2FIX(notmuch_database_get_version(db));
 }
 
     return INT2FIX(notmuch_database_get_version(db));
 }
@@ -133,7 +161,7 @@ notmuch_rb_database_needs_upgrade(VALUE self)
 {
     notmuch_database_t *db;
 
 {
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
     return notmuch_database_needs_upgrade(db) ? Qtrue : Qfalse;
 }
 
     return notmuch_database_needs_upgrade(db) ? Qtrue : Qfalse;
 }
@@ -161,7 +189,7 @@ notmuch_rb_database_upgrade(VALUE self)
     notmuch_database_t *db;
     VALUE block;
 
     notmuch_database_t *db;
     VALUE block;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
     if (rb_block_given_p()) {
         pnotify = notmuch_rb_upgrade_notify;
 
     if (rb_block_given_p()) {
         pnotify = notmuch_rb_upgrade_notify;
@@ -188,7 +216,7 @@ notmuch_rb_database_get_directory(VALUE self, VALUE pathv)
     notmuch_directory_t *dir;
     notmuch_database_t *db;
 
     notmuch_directory_t *dir;
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -220,7 +248,7 @@ notmuch_rb_database_add_message(VALUE self, VALUE pathv)
     notmuch_message_t *message;
     notmuch_database_t *db;
 
     notmuch_message_t *message;
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -250,7 +278,7 @@ notmuch_rb_database_remove_message(VALUE self, VALUE pathv)
     notmuch_status_t ret;
     notmuch_database_t *db;
 
     notmuch_status_t ret;
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -276,7 +304,7 @@ notmuch_rb_database_query_create(VALUE self, VALUE qstrv)
     notmuch_query_t *query;
     notmuch_database_t *db;
 
     notmuch_query_t *query;
     notmuch_database_t *db;
 
-    Data_Get_Struct(self, notmuch_database_t, db);
+    Data_Get_Notmuch_Database(self, db);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -287,7 +315,7 @@ notmuch_rb_database_query_create(VALUE self, VALUE qstrv)
 
     query = notmuch_query_create(db, qstr);
     if (!query)
 
     query = notmuch_query_create(db, qstr);
     if (!query)
-        rb_raise(notmuch_rb_eMemoryError, "out of memory");
+        rb_raise(notmuch_rb_eMemoryError, "Out of memory");
 
     return Data_Wrap_Struct(notmuch_rb_cQuery, NULL, NULL, query);
 }
 
     return Data_Wrap_Struct(notmuch_rb_cQuery, NULL, NULL, query);
 }
index f36b7227695b5a8671b14e490e05a2e676fdab6d..b1be5a30865f3c5e42de40d0c8d014168dfa6012 100644 (file)
@@ -49,13 +49,91 @@ ID ID_call;
 ID ID_db_create;
 ID ID_db_mode;
 
 ID ID_db_create;
 ID ID_db_mode;
 
+#define Data_Get_Notmuch_Database(obj, ptr)                \
+    do {                                                   \
+        Check_Type(obj, T_DATA);                           \
+        if (DATA_PTR(obj) == NULL)                         \
+            rb_raise(rb_eRuntimeError, "database closed"); \
+        Data_Get_Struct(obj, notmuch_database_t, ptr);     \
+    } while(0)
+
+#define Data_Get_Notmuch_Directory(obj, ptr)                   \
+    do {                                                       \
+        Check_Type(obj, T_DATA);                               \
+        if (DATA_PTR(obj) == NULL)                             \
+            rb_raise(rb_eRuntimeError, "directory destroyed"); \
+        Data_Get_Struct(obj, notmuch_directory_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_FileNames(obj, ptr)                   \
+    do {                                                       \
+        Check_Type(obj, T_DATA);                               \
+        if (DATA_PTR(obj) == NULL)                             \
+            rb_raise(rb_eRuntimeError, "filenames destroyed"); \
+        Data_Get_Struct(obj, notmuch_filenames_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_Query(obj, ptr)                   \
+    do {                                                   \
+        Check_Type(obj, T_DATA);                           \
+        if (DATA_PTR(obj) == NULL)                         \
+            rb_raise(rb_eRuntimeError, "query destroyed"); \
+        Data_Get_Struct(obj, notmuch_query_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_Threads(obj, ptr)                   \
+    do {                                                     \
+        Check_Type(obj, T_DATA);                             \
+        if (DATA_PTR(obj) == NULL)                           \
+            rb_raise(rb_eRuntimeError, "threads destroyed"); \
+        Data_Get_Struct(obj, notmuch_threads_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_Messages(obj, ptr)                   \
+    do {                                                      \
+        Check_Type(obj, T_DATA);                              \
+        if (DATA_PTR(obj) == NULL)                            \
+            rb_raise(rb_eRuntimeError, "messages destroyed"); \
+        Data_Get_Struct(obj, notmuch_messages_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_Thread(obj, ptr)                   \
+    do {                                                    \
+        Check_Type(obj, T_DATA);                            \
+        if (DATA_PTR(obj) == NULL)                          \
+            rb_raise(rb_eRuntimeError, "thread destroyed"); \
+        Data_Get_Struct(obj, notmuch_thread_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_Message(obj, ptr)                   \
+    do {                                                     \
+        Check_Type(obj, T_DATA);                             \
+        if (DATA_PTR(obj) == NULL)                           \
+            rb_raise(rb_eRuntimeError, "message destroyed"); \
+        Data_Get_Struct(obj, notmuch_message_t, ptr);        \
+    } while(0)
+
+#define Data_Get_Notmuch_Tags(obj, ptr)                   \
+    do {                                                  \
+        Check_Type(obj, T_DATA);                          \
+        if (DATA_PTR(obj) == NULL)                        \
+            rb_raise(rb_eRuntimeError, "tags destroyed"); \
+        Data_Get_Struct(obj, notmuch_tags_t, ptr);        \
+    } while(0)
+
 /* status.c */
 void
 notmuch_rb_status_raise(notmuch_status_t status);
 
 /* database.c */
 VALUE
 /* status.c */
 void
 notmuch_rb_status_raise(notmuch_status_t status);
 
 /* database.c */
 VALUE
-notmuch_rb_database_new(int argc, VALUE *argv, VALUE klass);
+notmuch_rb_database_alloc(VALUE klass);
+
+VALUE
+notmuch_rb_database_initialize(int argc, VALUE *argv, VALUE klass);
+
+VALUE
+notmuch_rb_database_open(int argc, VALUE *argv, VALUE klass);
 
 VALUE
 notmuch_rb_database_close(VALUE self);
 
 VALUE
 notmuch_rb_database_close(VALUE self);
index e73658c14226986ec43d4f4e6a9401e1479c0848..36dcb8bec22629d795862ce269772ea37b234842 100644 (file)
@@ -33,6 +33,7 @@ notmuch_rb_directory_destroy(VALUE self)
     Data_Get_Struct(self, notmuch_directory_t, dir);
 
     notmuch_directory_destroy(dir);
     Data_Get_Struct(self, notmuch_directory_t, dir);
 
     notmuch_directory_destroy(dir);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -48,7 +49,7 @@ notmuch_rb_directory_get_mtime(VALUE self)
 {
     notmuch_directory_t *dir;
 
 {
     notmuch_directory_t *dir;
 
-    Data_Get_Struct(self, notmuch_directory_t, dir);
+    Data_Get_Notmuch_Directory(self, dir);
 
     return UINT2NUM(notmuch_directory_get_mtime(dir));
 }
 
     return UINT2NUM(notmuch_directory_get_mtime(dir));
 }
@@ -64,7 +65,7 @@ notmuch_rb_directory_set_mtime(VALUE self, VALUE mtimev)
     notmuch_status_t ret;
     notmuch_directory_t *dir;
 
     notmuch_status_t ret;
     notmuch_directory_t *dir;
 
-    Data_Get_Struct(self, notmuch_directory_t, dir);
+    Data_Get_Notmuch_Directory(self, dir);
 
     if (!FIXNUM_P(mtimev))
         rb_raise(rb_eTypeError, "First argument not a fixnum");
 
     if (!FIXNUM_P(mtimev))
         rb_raise(rb_eTypeError, "First argument not a fixnum");
@@ -87,7 +88,7 @@ notmuch_rb_directory_get_child_files(VALUE self)
     notmuch_directory_t *dir;
     notmuch_filenames_t *fnames;
 
     notmuch_directory_t *dir;
     notmuch_filenames_t *fnames;
 
-    Data_Get_Struct(self, notmuch_directory_t, dir);
+    Data_Get_Notmuch_Directory(self, dir);
 
     fnames = notmuch_directory_get_child_files(dir);
 
 
     fnames = notmuch_directory_get_child_files(dir);
 
@@ -106,7 +107,7 @@ notmuch_rb_directory_get_child_directories(VALUE self)
     notmuch_directory_t *dir;
     notmuch_filenames_t *fnames;
 
     notmuch_directory_t *dir;
     notmuch_filenames_t *fnames;
 
-    Data_Get_Struct(self, notmuch_directory_t, dir);
+    Data_Get_Notmuch_Directory(self, dir);
 
     fnames = notmuch_directory_get_child_directories(dir);
 
 
     fnames = notmuch_directory_get_child_directories(dir);
 
index 23553ab14e15f6f322b2f653737a61479a13cc0f..faaa9a0ae15655b51ab26840bd1d32a6a54ebb45 100644 (file)
@@ -30,9 +30,10 @@ notmuch_rb_filenames_destroy(VALUE self)
 {
     notmuch_filenames_t *fnames;
 
 {
     notmuch_filenames_t *fnames;
 
-    Data_Get_Struct(self, notmuch_filenames_t, fnames);
+    Data_Get_Notmuch_FileNames(self, fnames);
 
     notmuch_filenames_destroy(fnames);
 
     notmuch_filenames_destroy(fnames);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -48,9 +49,7 @@ notmuch_rb_filenames_each(VALUE self)
 {
     notmuch_filenames_t *fnames;
 
 {
     notmuch_filenames_t *fnames;
 
-    Data_Get_Struct(self, notmuch_filenames_t, fnames);
-    if (!fnames)
-        return self;
+    Data_Get_Notmuch_FileNames(self, fnames);
 
     for (; notmuch_filenames_valid(fnames); notmuch_filenames_move_to_next(fnames))
         rb_yield(rb_str_new2(notmuch_filenames_get(fnames)));
 
     for (; notmuch_filenames_valid(fnames); notmuch_filenames_move_to_next(fnames))
         rb_yield(rb_str_new2(notmuch_filenames_get(fnames)));
index 4fdd018cedefad165d102e2976342cb8748d7c9a..e19b0356ffc08c874f009a257608beb09d95e36b 100644 (file)
@@ -193,8 +193,10 @@ Init_notmuch(void)
     notmuch_rb_eUnbalancedFreezeThawError = rb_define_class_under(mod, "UnbalancedFreezeThawError",
             notmuch_rb_eBaseError);
 
     notmuch_rb_eUnbalancedFreezeThawError = rb_define_class_under(mod, "UnbalancedFreezeThawError",
             notmuch_rb_eBaseError);
 
-    notmuch_rb_cDatabase = rb_define_class_under(mod, "Database", rb_cObject);
-    rb_define_singleton_method(notmuch_rb_cDatabase, "new", notmuch_rb_database_new, -1);
+    notmuch_rb_cDatabase = rb_define_class_under(mod, "Database", rb_cData);
+    rb_define_alloc_func(notmuch_rb_cDatabase, notmuch_rb_database_alloc);
+    rb_define_singleton_method(notmuch_rb_cDatabase, "open", notmuch_rb_database_open, -1);
+    rb_define_method(notmuch_rb_cDatabase, "initialize", notmuch_rb_database_initialize, -1);
     rb_define_method(notmuch_rb_cDatabase, "close", notmuch_rb_database_close, 0);
     rb_define_method(notmuch_rb_cDatabase, "path", notmuch_rb_database_path, 0);
     rb_define_method(notmuch_rb_cDatabase, "version", notmuch_rb_database_version, 0);
     rb_define_method(notmuch_rb_cDatabase, "close", notmuch_rb_database_close, 0);
     rb_define_method(notmuch_rb_cDatabase, "path", notmuch_rb_database_path, 0);
     rb_define_method(notmuch_rb_cDatabase, "version", notmuch_rb_database_version, 0);
@@ -205,7 +207,7 @@ Init_notmuch(void)
     rb_define_method(notmuch_rb_cDatabase, "remove_message", notmuch_rb_database_remove_message, 1);
     rb_define_method(notmuch_rb_cDatabase, "query", notmuch_rb_database_query_create, 1);
 
     rb_define_method(notmuch_rb_cDatabase, "remove_message", notmuch_rb_database_remove_message, 1);
     rb_define_method(notmuch_rb_cDatabase, "query", notmuch_rb_database_query_create, 1);
 
-    notmuch_rb_cDirectory = rb_define_class_under(mod, "Directory", rb_cObject);
+    notmuch_rb_cDirectory = rb_define_class_under(mod, "Directory", rb_cData);
     rb_undef_method(notmuch_rb_cDirectory, "initialize");
     rb_define_method(notmuch_rb_cDirectory, "destroy", notmuch_rb_directory_destroy, 0);
     rb_define_method(notmuch_rb_cDirectory, "mtime", notmuch_rb_directory_get_mtime, 0);
     rb_undef_method(notmuch_rb_cDirectory, "initialize");
     rb_define_method(notmuch_rb_cDirectory, "destroy", notmuch_rb_directory_destroy, 0);
     rb_define_method(notmuch_rb_cDirectory, "mtime", notmuch_rb_directory_get_mtime, 0);
@@ -213,33 +215,33 @@ Init_notmuch(void)
     rb_define_method(notmuch_rb_cDirectory, "child_files", notmuch_rb_directory_get_child_files, 0);
     rb_define_method(notmuch_rb_cDirectory, "child_directories", notmuch_rb_directory_get_child_directories, 0);
 
     rb_define_method(notmuch_rb_cDirectory, "child_files", notmuch_rb_directory_get_child_files, 0);
     rb_define_method(notmuch_rb_cDirectory, "child_directories", notmuch_rb_directory_get_child_directories, 0);
 
-    notmuch_rb_cFileNames = rb_define_class_under(mod, "FileNames", rb_cObject);
+    notmuch_rb_cFileNames = rb_define_class_under(mod, "FileNames", rb_cData);
     rb_undef_method(notmuch_rb_cFileNames, "initialize");
     rb_define_method(notmuch_rb_cFileNames, "destroy", notmuch_rb_filenames_destroy, 0);
     rb_define_method(notmuch_rb_cFileNames, "each", notmuch_rb_filenames_each, 0);
     rb_include_module(notmuch_rb_cFileNames, rb_mEnumerable);
 
     rb_undef_method(notmuch_rb_cFileNames, "initialize");
     rb_define_method(notmuch_rb_cFileNames, "destroy", notmuch_rb_filenames_destroy, 0);
     rb_define_method(notmuch_rb_cFileNames, "each", notmuch_rb_filenames_each, 0);
     rb_include_module(notmuch_rb_cFileNames, rb_mEnumerable);
 
-    notmuch_rb_cQuery = rb_define_class_under(mod, "Query", rb_cObject);
+    notmuch_rb_cQuery = rb_define_class_under(mod, "Query", rb_cData);
     rb_undef_method(notmuch_rb_cQuery, "initialize");
     rb_define_method(notmuch_rb_cQuery, "destroy", notmuch_rb_query_destroy, 0);
     rb_define_method(notmuch_rb_cQuery, "sort=", notmuch_rb_query_set_sort, 1);
     rb_define_method(notmuch_rb_cQuery, "search_threads", notmuch_rb_query_search_threads, 0);
     rb_define_method(notmuch_rb_cQuery, "search_messages", notmuch_rb_query_search_messages, 0);
 
     rb_undef_method(notmuch_rb_cQuery, "initialize");
     rb_define_method(notmuch_rb_cQuery, "destroy", notmuch_rb_query_destroy, 0);
     rb_define_method(notmuch_rb_cQuery, "sort=", notmuch_rb_query_set_sort, 1);
     rb_define_method(notmuch_rb_cQuery, "search_threads", notmuch_rb_query_search_threads, 0);
     rb_define_method(notmuch_rb_cQuery, "search_messages", notmuch_rb_query_search_messages, 0);
 
-    notmuch_rb_cThreads = rb_define_class_under(mod, "Threads", rb_cObject);
+    notmuch_rb_cThreads = rb_define_class_under(mod, "Threads", rb_cData);
     rb_undef_method(notmuch_rb_cThreads, "initialize");
     rb_define_method(notmuch_rb_cThreads, "destroy", notmuch_rb_threads_destroy, 0);
     rb_define_method(notmuch_rb_cThreads, "each", notmuch_rb_threads_each, 0);
     rb_include_module(notmuch_rb_cThreads, rb_mEnumerable);
 
     rb_undef_method(notmuch_rb_cThreads, "initialize");
     rb_define_method(notmuch_rb_cThreads, "destroy", notmuch_rb_threads_destroy, 0);
     rb_define_method(notmuch_rb_cThreads, "each", notmuch_rb_threads_each, 0);
     rb_include_module(notmuch_rb_cThreads, rb_mEnumerable);
 
-    notmuch_rb_cMessages = rb_define_class_under(mod, "Messages", rb_cObject);
+    notmuch_rb_cMessages = rb_define_class_under(mod, "Messages", rb_cData);
     rb_undef_method(notmuch_rb_cMessages, "initialize");
     rb_define_method(notmuch_rb_cMessages, "destroy", notmuch_rb_messages_destroy, 0);
     rb_define_method(notmuch_rb_cMessages, "each", notmuch_rb_messages_each, 0);
     rb_define_method(notmuch_rb_cMessages, "tags", notmuch_rb_messages_collect_tags, 0);
     rb_include_module(notmuch_rb_cMessages, rb_mEnumerable);
 
     rb_undef_method(notmuch_rb_cMessages, "initialize");
     rb_define_method(notmuch_rb_cMessages, "destroy", notmuch_rb_messages_destroy, 0);
     rb_define_method(notmuch_rb_cMessages, "each", notmuch_rb_messages_each, 0);
     rb_define_method(notmuch_rb_cMessages, "tags", notmuch_rb_messages_collect_tags, 0);
     rb_include_module(notmuch_rb_cMessages, rb_mEnumerable);
 
-    notmuch_rb_cThread = rb_define_class_under(mod, "Thread", rb_cObject);
+    notmuch_rb_cThread = rb_define_class_under(mod, "Thread", rb_cData);
     rb_undef_method(notmuch_rb_cThread, "initialize");
     rb_define_method(notmuch_rb_cThread, "destroy", notmuch_rb_thread_destroy, 0);
     rb_define_method(notmuch_rb_cThread, "thread_id", notmuch_rb_thread_get_thread_id, 0);
     rb_undef_method(notmuch_rb_cThread, "initialize");
     rb_define_method(notmuch_rb_cThread, "destroy", notmuch_rb_thread_destroy, 0);
     rb_define_method(notmuch_rb_cThread, "thread_id", notmuch_rb_thread_get_thread_id, 0);
@@ -252,7 +254,7 @@ Init_notmuch(void)
     rb_define_method(notmuch_rb_cThread, "newest_date", notmuch_rb_thread_get_newest_date, 0);
     rb_define_method(notmuch_rb_cThread, "tags", notmuch_rb_thread_get_tags, 0);
 
     rb_define_method(notmuch_rb_cThread, "newest_date", notmuch_rb_thread_get_newest_date, 0);
     rb_define_method(notmuch_rb_cThread, "tags", notmuch_rb_thread_get_tags, 0);
 
-    notmuch_rb_cMessage = rb_define_class_under(mod, "Message", rb_cObject);
+    notmuch_rb_cMessage = rb_define_class_under(mod, "Message", rb_cData);
     rb_undef_method(notmuch_rb_cMessage, "initialize");
     rb_define_method(notmuch_rb_cMessage, "destroy", notmuch_rb_message_destroy, 0);
     rb_define_method(notmuch_rb_cMessage, "message_id", notmuch_rb_message_get_message_id, 0);
     rb_undef_method(notmuch_rb_cMessage, "initialize");
     rb_define_method(notmuch_rb_cMessage, "destroy", notmuch_rb_message_destroy, 0);
     rb_define_method(notmuch_rb_cMessage, "message_id", notmuch_rb_message_get_message_id, 0);
@@ -272,7 +274,7 @@ Init_notmuch(void)
     rb_define_method(notmuch_rb_cMessage, "freeze", notmuch_rb_message_freeze, 0);
     rb_define_method(notmuch_rb_cMessage, "thaw", notmuch_rb_message_thaw, 0);
 
     rb_define_method(notmuch_rb_cMessage, "freeze", notmuch_rb_message_freeze, 0);
     rb_define_method(notmuch_rb_cMessage, "thaw", notmuch_rb_message_thaw, 0);
 
-    notmuch_rb_cTags = rb_define_class_under(mod, "Tags", rb_cObject);
+    notmuch_rb_cTags = rb_define_class_under(mod, "Tags", rb_cData);
     rb_undef_method(notmuch_rb_cTags, "initialize");
     rb_define_method(notmuch_rb_cTags, "destroy", notmuch_rb_tags_destroy, 0);
     rb_define_method(notmuch_rb_cTags, "each", notmuch_rb_tags_each, 0);
     rb_undef_method(notmuch_rb_cTags, "initialize");
     rb_define_method(notmuch_rb_cTags, "destroy", notmuch_rb_tags_destroy, 0);
     rb_define_method(notmuch_rb_cTags, "each", notmuch_rb_tags_each, 0);
index 2babfd047e64cf0db9c6ebd4778d4c0ba59439aa..f97e1a4ef1f0f04bad3ed4a1941eb5aeb4bb39fd 100644 (file)
@@ -30,9 +30,10 @@ notmuch_rb_message_destroy(VALUE self)
 {
     notmuch_message_t *message;
 
 {
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     notmuch_message_destroy(message);
 
     notmuch_message_destroy(message);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -48,7 +49,7 @@ notmuch_rb_message_get_message_id(VALUE self)
     const char *msgid;
     notmuch_message_t *message;
 
     const char *msgid;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     msgid = notmuch_message_get_message_id(message);
 
 
     msgid = notmuch_message_get_message_id(message);
 
@@ -66,7 +67,7 @@ notmuch_rb_message_get_thread_id(VALUE self)
     const char *tid;
     notmuch_message_t *message;
 
     const char *tid;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     tid = notmuch_message_get_thread_id(message);
 
 
     tid = notmuch_message_get_thread_id(message);
 
@@ -84,7 +85,7 @@ notmuch_rb_message_get_replies(VALUE self)
     notmuch_messages_t *messages;
     notmuch_message_t *message;
 
     notmuch_messages_t *messages;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     messages = notmuch_message_get_replies(message);
 
 
     messages = notmuch_message_get_replies(message);
 
@@ -102,7 +103,7 @@ notmuch_rb_message_get_filename(VALUE self)
     const char *fname;
     notmuch_message_t *message;
 
     const char *fname;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     fname = notmuch_message_get_filename(message);
 
 
     fname = notmuch_message_get_filename(message);
 
@@ -119,7 +120,7 @@ notmuch_rb_message_get_flag(VALUE self, VALUE flagv)
 {
     notmuch_message_t *message;
 
 {
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     if (!FIXNUM_P(flagv))
         rb_raise(rb_eTypeError, "Flag not a Fixnum");
 
     if (!FIXNUM_P(flagv))
         rb_raise(rb_eTypeError, "Flag not a Fixnum");
@@ -137,7 +138,7 @@ notmuch_rb_message_set_flag(VALUE self, VALUE flagv, VALUE valuev)
 {
     notmuch_message_t *message;
 
 {
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     if (!FIXNUM_P(flagv))
         rb_raise(rb_eTypeError, "Flag not a Fixnum");
 
     if (!FIXNUM_P(flagv))
         rb_raise(rb_eTypeError, "Flag not a Fixnum");
@@ -157,7 +158,7 @@ notmuch_rb_message_get_date(VALUE self)
 {
     notmuch_message_t *message;
 
 {
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     return UINT2NUM(notmuch_message_get_date(message));
 }
 
     return UINT2NUM(notmuch_message_get_date(message));
 }
@@ -173,7 +174,7 @@ notmuch_rb_message_get_header(VALUE self, VALUE headerv)
     const char *header, *value;
     notmuch_message_t *message;
 
     const char *header, *value;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -200,7 +201,7 @@ notmuch_rb_message_get_tags(VALUE self)
     notmuch_message_t *message;
     notmuch_tags_t *tags;
 
     notmuch_message_t *message;
     notmuch_tags_t *tags;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     tags = notmuch_message_get_tags(message);
     if (!tags)
 
     tags = notmuch_message_get_tags(message);
     if (!tags)
@@ -221,7 +222,7 @@ notmuch_rb_message_add_tag(VALUE self, VALUE tagv)
     notmuch_status_t ret;
     notmuch_message_t *message;
 
     notmuch_status_t ret;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -248,7 +249,7 @@ notmuch_rb_message_remove_tag(VALUE self, VALUE tagv)
     notmuch_status_t ret;
     notmuch_message_t *message;
 
     notmuch_status_t ret;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
 
 #if !defined(RSTRING_PTR)
 #define RSTRING_PTR(v) (RSTRING((v))->ptr)
@@ -274,7 +275,7 @@ notmuch_rb_message_remove_all_tags(VALUE self)
     notmuch_status_t ret;
     notmuch_message_t *message;
 
     notmuch_status_t ret;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     ret = notmuch_message_remove_all_tags(message);
     notmuch_rb_status_raise(ret);
 
     ret = notmuch_message_remove_all_tags(message);
     notmuch_rb_status_raise(ret);
@@ -293,7 +294,7 @@ notmuch_rb_message_freeze(VALUE self)
     notmuch_status_t ret;
     notmuch_message_t *message;
 
     notmuch_status_t ret;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     ret = notmuch_message_freeze(message);
     notmuch_rb_status_raise(ret);
 
     ret = notmuch_message_freeze(message);
     notmuch_rb_status_raise(ret);
@@ -312,7 +313,7 @@ notmuch_rb_message_thaw(VALUE self)
     notmuch_status_t ret;
     notmuch_message_t *message;
 
     notmuch_status_t ret;
     notmuch_message_t *message;
 
-    Data_Get_Struct(self, notmuch_message_t, message);
+    Data_Get_Notmuch_Message(self, message);
 
     ret = notmuch_message_thaw(message);
     notmuch_rb_status_raise(ret);
 
     ret = notmuch_message_thaw(message);
     notmuch_rb_status_raise(ret);
index 94b0b0daad836f7e1c54d4e38a4ac0f10e49b655..35b5d146b06a9824bf421e3f25c330c23b45db7b 100644 (file)
 VALUE
 notmuch_rb_messages_destroy(VALUE self)
 {
 VALUE
 notmuch_rb_messages_destroy(VALUE self)
 {
-    notmuch_messages_t *fnames;
+    notmuch_messages_t *messages;
 
 
-    Data_Get_Struct(self, notmuch_messages_t, fnames);
+    Data_Get_Notmuch_Messages(self, messages);
 
 
-    notmuch_messages_destroy(fnames);
+    notmuch_messages_destroy(messages);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -48,9 +49,7 @@ notmuch_rb_messages_each(VALUE self)
     notmuch_message_t *message;
     notmuch_messages_t *messages;
 
     notmuch_message_t *message;
     notmuch_messages_t *messages;
 
-    Data_Get_Struct(self, notmuch_messages_t, messages);
-    if (!messages)
-        return self;
+    Data_Get_Notmuch_Messages(self, messages);
 
     for (; notmuch_messages_valid(messages); notmuch_messages_move_to_next(messages)) {
         message = notmuch_messages_get(messages);
 
     for (; notmuch_messages_valid(messages); notmuch_messages_move_to_next(messages)) {
         message = notmuch_messages_get(messages);
@@ -71,7 +70,7 @@ notmuch_rb_messages_collect_tags(VALUE self)
     notmuch_tags_t *tags;
     notmuch_messages_t *messages;
 
     notmuch_tags_t *tags;
     notmuch_messages_t *messages;
 
-    Data_Get_Struct(self, notmuch_messages_t, messages);
+    Data_Get_Notmuch_Messages(self, messages);
 
     tags = notmuch_messages_collect_tags(messages);
     if (!tags)
 
     tags = notmuch_messages_collect_tags(messages);
     if (!tags)
index 5140c0083b25c0becbc85223ef568117fc5e5fdc..c5b8a4cc324f278ed6ff0ec17007c8eb90c01538 100644 (file)
@@ -30,9 +30,10 @@ notmuch_rb_query_destroy(VALUE self)
 {
     notmuch_query_t *query;
 
 {
     notmuch_query_t *query;
 
-    Data_Get_Struct(self, notmuch_query_t, query);
+    Data_Get_Notmuch_Query(self, query);
 
     notmuch_query_destroy(query);
 
     notmuch_query_destroy(query);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -47,10 +48,10 @@ notmuch_rb_query_set_sort(VALUE self, VALUE sortv)
 {
     notmuch_query_t *query;
 
 {
     notmuch_query_t *query;
 
-    Data_Get_Struct(self, notmuch_query_t, query);
+    Data_Get_Notmuch_Query(self, query);
 
     if (!FIXNUM_P(sortv))
 
     if (!FIXNUM_P(sortv))
-        rb_raise(rb_eTypeError, "Not a fixnum");
+        rb_raise(rb_eTypeError, "Not a Fixnum");
 
     notmuch_query_set_sort(query, FIX2UINT(sortv));
 
 
     notmuch_query_set_sort(query, FIX2UINT(sortv));
 
@@ -68,7 +69,7 @@ notmuch_rb_query_search_threads(VALUE self)
     notmuch_query_t *query;
     notmuch_threads_t *threads;
 
     notmuch_query_t *query;
     notmuch_threads_t *threads;
 
-    Data_Get_Struct(self, notmuch_query_t, query);
+    Data_Get_Notmuch_Query(self, query);
 
     threads = notmuch_query_search_threads(query);
     if (!threads)
 
     threads = notmuch_query_search_threads(query);
     if (!threads)
@@ -88,7 +89,7 @@ notmuch_rb_query_search_messages(VALUE self)
     notmuch_query_t *query;
     notmuch_messages_t *messages;
 
     notmuch_query_t *query;
     notmuch_messages_t *messages;
 
-    Data_Get_Struct(self, notmuch_query_t, query);
+    Data_Get_Notmuch_Query(self, query);
 
     messages = notmuch_query_search_messages(query);
     if (!messages)
 
     messages = notmuch_query_search_messages(query);
     if (!messages)
index 7ca03b6f91135aa896e2c22994ebef2b653253d2..d7cd941d3bdba825d29f771114d158c61d7327f9 100644 (file)
@@ -21,7 +21,7 @@
 #include "defs.h"
 
 /*
 #include "defs.h"
 
 /*
- * call-seq: tags.destroy => nil
+ * call-seq: TAGS.destroy => nil
  *
  * Destroys the tags, freeing all resources allocated for it.
  */
  *
  * Destroys the tags, freeing all resources allocated for it.
  */
@@ -30,9 +30,10 @@ notmuch_rb_tags_destroy(VALUE self)
 {
     notmuch_tags_t *tags;
 
 {
     notmuch_tags_t *tags;
 
-    Data_Get_Struct(self, notmuch_tags_t, tags);
+    Data_Get_Notmuch_Tags(self, tags);
 
     notmuch_tags_destroy(tags);
 
     notmuch_tags_destroy(tags);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -49,9 +50,7 @@ notmuch_rb_tags_each(VALUE self)
     const char *tag;
     notmuch_tags_t *tags;
 
     const char *tag;
     notmuch_tags_t *tags;
 
-    Data_Get_Struct(self, notmuch_tags_t, tags);
-    if (!tags)
-        return self;
+    Data_Get_Notmuch_Tags(self, tags);
 
     for (; notmuch_tags_valid(tags); notmuch_tags_move_to_next(tags)) {
         tag = notmuch_tags_get(tags);
 
     for (; notmuch_tags_valid(tags); notmuch_tags_move_to_next(tags)) {
         tag = notmuch_tags_get(tags);
index 5534fe3f38334323fa14b13976f75e91e52dc1ae..e5c873667a20ce05973b7b30cf0a9e748ee51553 100644 (file)
@@ -30,9 +30,10 @@ notmuch_rb_thread_destroy(VALUE self)
 {
     notmuch_thread_t *thread;
 
 {
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     notmuch_thread_destroy(thread);
 
     notmuch_thread_destroy(thread);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
     return Qnil;
 }
@@ -48,7 +49,7 @@ notmuch_rb_thread_get_thread_id(VALUE self)
     const char *tid;
     notmuch_thread_t *thread;
 
     const char *tid;
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     tid = notmuch_thread_get_thread_id(thread);
 
 
     tid = notmuch_thread_get_thread_id(thread);
 
@@ -65,7 +66,7 @@ notmuch_rb_thread_get_total_messages(VALUE self)
 {
     notmuch_thread_t *thread;
 
 {
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     return INT2FIX(notmuch_thread_get_total_messages(thread));
 }
 
     return INT2FIX(notmuch_thread_get_total_messages(thread));
 }
@@ -81,7 +82,7 @@ notmuch_rb_thread_get_toplevel_messages(VALUE self)
     notmuch_messages_t *messages;
     notmuch_thread_t *thread;
 
     notmuch_messages_t *messages;
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     messages = notmuch_thread_get_toplevel_messages(thread);
     if (!messages)
 
     messages = notmuch_thread_get_toplevel_messages(thread);
     if (!messages)
@@ -100,7 +101,7 @@ notmuch_rb_thread_get_matched_messages(VALUE self)
 {
     notmuch_thread_t *thread;
 
 {
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     return INT2FIX(notmuch_thread_get_matched_messages(thread));
 }
 
     return INT2FIX(notmuch_thread_get_matched_messages(thread));
 }
@@ -116,7 +117,7 @@ notmuch_rb_thread_get_authors(VALUE self)
     const char *authors;
     notmuch_thread_t *thread;
 
     const char *authors;
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     authors = notmuch_thread_get_authors(thread);
 
 
     authors = notmuch_thread_get_authors(thread);
 
@@ -134,7 +135,7 @@ notmuch_rb_thread_get_subject(VALUE self)
     const char *subject;
     notmuch_thread_t *thread;
 
     const char *subject;
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     subject = notmuch_thread_get_subject(thread);
 
 
     subject = notmuch_thread_get_subject(thread);
 
@@ -151,7 +152,7 @@ notmuch_rb_thread_get_oldest_date(VALUE self)
 {
     notmuch_thread_t *thread;
 
 {
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     return UINT2NUM(notmuch_thread_get_oldest_date(thread));
 }
 
     return UINT2NUM(notmuch_thread_get_oldest_date(thread));
 }
@@ -166,7 +167,7 @@ notmuch_rb_thread_get_newest_date(VALUE self)
 {
     notmuch_thread_t *thread;
 
 {
     notmuch_thread_t *thread;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     return UINT2NUM(notmuch_thread_get_newest_date(thread));
 }
 
     return UINT2NUM(notmuch_thread_get_newest_date(thread));
 }
@@ -182,7 +183,7 @@ notmuch_rb_thread_get_tags(VALUE self)
     notmuch_thread_t *thread;
     notmuch_tags_t *tags;
 
     notmuch_thread_t *thread;
     notmuch_tags_t *tags;
 
-    Data_Get_Struct(self, notmuch_thread_t, thread);
+    Data_Get_Notmuch_Thread(self, thread);
 
     tags = notmuch_thread_get_tags(thread);
     if (!tags)
 
     tags = notmuch_thread_get_tags(thread);
     if (!tags)
index 09906975f1e84091ead20878a06b96b60e55d0b1..abd51212664c0a10c5b58b34475e049419fba288 100644 (file)
@@ -33,11 +33,11 @@ notmuch_rb_threads_destroy(VALUE self)
     Data_Get_Struct(self, notmuch_threads_t, threads);
 
     notmuch_threads_destroy(threads);
     Data_Get_Struct(self, notmuch_threads_t, threads);
 
     notmuch_threads_destroy(threads);
+    DATA_PTR(self) = NULL;
 
     return Qnil;
 }
 
 
     return Qnil;
 }
 
-
 /* call-seq: THREADS.each {|item| block } => THREADS
  *
  * Calls +block+ once for each thread in +self+, passing that element as a
 /* call-seq: THREADS.each {|item| block } => THREADS
  *
  * Calls +block+ once for each thread in +self+, passing that element as a
@@ -49,9 +49,7 @@ notmuch_rb_threads_each(VALUE self)
     notmuch_thread_t *thread;
     notmuch_threads_t *threads;
 
     notmuch_thread_t *thread;
     notmuch_threads_t *threads;
 
-    Data_Get_Struct(self, notmuch_threads_t, threads);
-    if (!threads)
-        return self;
+    Data_Get_Notmuch_Threads(self, threads);
 
     for (; notmuch_threads_valid(threads); notmuch_threads_move_to_next(threads)) {
         thread = notmuch_threads_get(threads);
 
     for (; notmuch_threads_valid(threads); notmuch_threads_move_to_next(threads)) {
         thread = notmuch_threads_get(threads);