-#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)
+#define Data_Get_Notmuch_Messages(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), ¬much_rb_messages_type, (ptr))
+
+#define Data_Get_Notmuch_Thread(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), ¬much_rb_thread_type, (ptr))
+
+#define Data_Get_Notmuch_Message(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), ¬much_rb_message_type, (ptr))
+
+#define Data_Get_Notmuch_Tags(obj, ptr) \
+ Data_Get_Notmuch_Object ((obj), ¬much_rb_tags_type, (ptr))
+
+typedef struct {
+ void *nm_object;
+} notmuch_rb_object_t;
+
+static inline void *
+notmuch_rb_object_create (void *nm_object, const char *name)
+{
+ notmuch_rb_object_t *rb_wrapper = talloc_named_const (NULL, sizeof (*rb_wrapper), name);
+
+ if (RB_UNLIKELY (!rb_wrapper))
+ return NULL;
+
+ rb_wrapper->nm_object = nm_object;
+ talloc_steal (rb_wrapper, nm_object);
+ return rb_wrapper;
+}
+
+static inline void
+notmuch_rb_object_free (void *rb_wrapper)
+{
+ talloc_free (rb_wrapper);
+}
+
+static inline void
+notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
+{
+ notmuch_rb_object_t *rb_wrapper;
+
+ Data_Get_Notmuch_Rb_Object (rb_object, type, rb_wrapper);
+
+ /* Call the corresponding notmuch_*_destroy function */
+ ((void (*)(void *)) type->data) (rb_wrapper->nm_object);
+ notmuch_rb_object_free (rb_wrapper);
+ DATA_PTR (rb_object) = NULL;
+}