1 /* messages.c - Iterator for a set of messages
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
23 /* Create a new notmuch_message_list_t object, with 'ctx' as its
26 * This function can return NULL in case of out-of-memory.
28 notmuch_message_list_t *
29 _notmuch_message_list_create (const void *ctx)
31 notmuch_message_list_t *list;
33 list = talloc (ctx, notmuch_message_list_t);
34 if (unlikely (list == NULL))
38 list->tail = &list->head;
43 /* Append 'node' (which can of course point to an arbitrarily long
44 * list of nodes) to the end of 'list'.
47 _notmuch_message_list_append (notmuch_message_list_t *list,
48 notmuch_message_node_t *node)
51 list->tail = &node->next;
54 /* Allocate a new node for 'message' and append it to the end of
58 _notmuch_message_list_add_message (notmuch_message_list_t *list,
59 notmuch_message_t *message)
61 notmuch_message_node_t *node = talloc (list, notmuch_message_node_t);
63 node->message = message;
66 _notmuch_message_list_append (list, node);
70 _notmuch_messages_create (notmuch_message_list_t *list)
72 notmuch_messages_t *messages;
74 if (list->head == NULL)
77 messages = talloc (list, notmuch_messages_t);
78 if (unlikely (messages == NULL))
81 messages->is_of_list_type = TRUE;
82 messages->iterator = list->head;
87 /* We're using the "is_of_type_list" to conditionally defer to the
88 * notmuch_mset_messages_t implementation of notmuch_messages_t in
89 * query.cc. It's ugly that that's over in query.cc, and it's ugly
90 * that we're not using a union here. Both of those uglies are due to
93 * 1. I didn't want to force a C++ header file onto
94 * notmuch-private.h and suddenly subject all our code to a
95 * C++ compiler and its rules.
97 * 2. C++ won't allow me to put C++ objects, (with non-trivial
98 * constructors) into a union anyway. Even though I'd
99 * carefully control object construction with placement new
103 notmuch_messages_has_more (notmuch_messages_t *messages)
105 if (messages == NULL)
108 if (! messages->is_of_list_type)
109 return _notmuch_mset_messages_has_more (messages);
111 return (messages->iterator != NULL);
115 notmuch_messages_get (notmuch_messages_t *messages)
117 if (! messages->is_of_list_type)
118 return _notmuch_mset_messages_get (messages);
120 if (messages->iterator == NULL)
123 return messages->iterator->message;
127 notmuch_messages_advance (notmuch_messages_t *messages)
129 if (! messages->is_of_list_type)
130 return _notmuch_mset_messages_advance (messages);
132 if (messages->iterator == NULL)
135 messages->iterator = messages->iterator->next;
139 notmuch_messages_destroy (notmuch_messages_t *messages)
141 talloc_free (messages);