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 /* We end up having to call the destructors explicitly because we had
27 * to use "placement new" in order to initialize C++ objects within a
28 * block that we allocated with talloc. So C++ is making talloc
29 * slightly less simple to use, (we wouldn't need
30 * talloc_set_destructor at all otherwise).
33 _notmuch_terms_destructor (notmuch_terms_t *terms)
35 terms->iterator.~TermIterator ();
36 terms->iterator_end.~TermIterator ();
42 _notmuch_terms_create (void *ctx,
44 const char *prefix_name)
46 notmuch_terms_t *terms;
47 const char *prefix = _find_prefix (prefix_name);
49 /* Currently, notmuch_terms_t is written with the assumption that
50 * any prefix its derivatives use will be only a single
52 assert (strlen (prefix) == 1);
54 terms = talloc (ctx, notmuch_terms_t);
55 if (unlikely (terms == NULL))
58 terms->prefix_char = *prefix;
59 new (&terms->iterator) Xapian::TermIterator;
60 new (&terms->iterator_end) Xapian::TermIterator;
62 talloc_set_destructor (terms, _notmuch_terms_destructor);
64 terms->iterator = doc.termlist_begin ();
65 terms->iterator.skip_to (prefix);
66 terms->iterator_end = doc.termlist_end ();
72 _notmuch_terms_has_more (notmuch_terms_t *terms)
76 if (terms->iterator == terms->iterator_end)
80 if (! s.empty () && s[0] == terms->prefix_char)
87 _notmuch_terms_get (notmuch_terms_t *terms)
89 return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
93 _notmuch_terms_advance (notmuch_terms_t *terms)
99 _notmuch_terms_destroy (notmuch_terms_t *terms)
105 notmuch_tags_has_more (notmuch_tags_t *tags)
107 return _notmuch_terms_has_more (&tags->terms);
111 notmuch_tags_get (notmuch_tags_t *tags)
113 return _notmuch_terms_get (&tags->terms);
117 notmuch_tags_advance (notmuch_tags_t *tags)
119 return _notmuch_terms_advance (&tags->terms);
123 notmuch_tags_destroy (notmuch_tags_t *tags)
125 return _notmuch_terms_destroy (&tags->terms);