1 /* tags.cc - Iterator for tags returned from message or thread
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-cxx.h"
22 #include "database-private.h"
26 typedef struct _notmuch_terms {
28 Xapian::TermIterator iterator;
29 Xapian::TermIterator iterator_end;
32 struct _notmuch_tags {
33 notmuch_terms_t terms;
37 _notmuch_terms_create (void *ctx,
39 const char *prefix_name);
41 /* The assertion is to ensure that 'type' is a derivative of
42 * notmuch_terms_t in that it contains a notmuch_terms_t as its first
43 * member. We do this by name of 'terms' as opposed to type, because
44 * that's as clever as I've been so far. */
45 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
46 (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0), \
47 (type *) _notmuch_terms_create (ctx, doc, prefix_name))
49 /* We end up having to call the destructors explicitly because we had
50 * to use "placement new" in order to initialize C++ objects within a
51 * block that we allocated with talloc. So C++ is making talloc
52 * slightly less simple to use, (we wouldn't need
53 * talloc_set_destructor at all otherwise).
56 _notmuch_terms_destructor (notmuch_terms_t *terms)
58 terms->iterator.~TermIterator ();
59 terms->iterator_end.~TermIterator ();
65 _notmuch_terms_create (void *ctx,
67 const char *prefix_name)
69 notmuch_terms_t *terms;
70 const char *prefix = _find_prefix (prefix_name);
72 /* Currently, notmuch_terms_t is written with the assumption that
73 * any prefix its derivatives use will be only a single
75 assert (strlen (prefix) == 1);
77 terms = talloc (ctx, notmuch_terms_t);
78 if (unlikely (terms == NULL))
81 terms->prefix_char = *prefix;
82 new (&terms->iterator) Xapian::TermIterator;
83 new (&terms->iterator_end) Xapian::TermIterator;
85 talloc_set_destructor (terms, _notmuch_terms_destructor);
87 terms->iterator = doc.termlist_begin ();
88 terms->iterator.skip_to (prefix);
89 terms->iterator_end = doc.termlist_end ();
95 _notmuch_terms_has_more (notmuch_terms_t *terms)
99 if (terms->iterator == terms->iterator_end)
102 s = *terms->iterator;
103 if (! s.empty () && s[0] == terms->prefix_char)
110 _notmuch_terms_get (notmuch_terms_t *terms)
112 return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
116 _notmuch_terms_advance (notmuch_terms_t *terms)
122 _notmuch_terms_destroy (notmuch_terms_t *terms)
128 _notmuch_tags_create_terms (void *ctx,
129 Xapian::Document doc,
130 const char *prefix_name)
132 return _notmuch_terms_create_type (ctx, doc, prefix_name, notmuch_tags_t);
136 notmuch_tags_has_more (notmuch_tags_t *tags)
138 return _notmuch_terms_has_more (&tags->terms);
142 notmuch_tags_get (notmuch_tags_t *tags)
144 return _notmuch_terms_get (&tags->terms);
148 notmuch_tags_advance (notmuch_tags_t *tags)
150 return _notmuch_terms_advance (&tags->terms);
154 notmuch_tags_destroy (notmuch_tags_t *tags)
156 return _notmuch_terms_destroy (&tags->terms);