Make testsuite fd leak check work on more platforms
[xapian.git] / xapian-core / api / valueiterator.cc
blob7733dcfa8be301c5cbe24cb2d3ade2db3d49c9d5
1 /** @file valueiterator.cc
2 * @brief Class for iterating over document values.
3 */
4 /* Copyright (C) 2008,2009,2011,2013 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #include "xapian/valueiterator.h"
25 #include "debuglog.h"
26 #include "omassert.h"
27 #include "backends/valuelist.h"
29 using namespace std;
31 namespace Xapian {
33 void
34 ValueIterator::decref()
36 Assert(internal);
37 if (--internal->_refs == 0)
38 delete internal;
41 ValueIterator::ValueIterator(Internal *internal_) : internal(internal_)
43 LOGCALL_CTOR(API, "ValueIterator", internal_);
44 if (!internal)
45 return;
46 ++internal->_refs;
47 try {
48 internal->next();
49 } catch (...) {
50 // The destructor only runs if the constructor completes, so we have to
51 // take care of cleaning up for ourselves here.
52 decref();
53 throw;
55 if (internal->at_end()) {
56 decref();
57 internal = NULL;
61 ValueIterator::ValueIterator(const ValueIterator & o)
62 : internal(o.internal)
64 LOGCALL_CTOR(API, "ValueIterator", o);
65 if (internal)
66 ++internal->_refs;
69 ValueIterator &
70 ValueIterator::operator=(const ValueIterator & o)
72 LOGCALL(API, ValueIterator &, "ValueIterator::operator=", o);
73 if (o.internal)
74 ++o.internal->_refs;
75 if (internal)
76 decref();
77 internal = o.internal;
78 RETURN(*this);
81 string
82 ValueIterator::operator*() const
84 LOGCALL(API, string, "ValueIterator::operator*", NO_ARGS);
85 Assert(internal);
86 RETURN(internal->get_value());
89 ValueIterator &
90 ValueIterator::operator++()
92 LOGCALL(API, ValueIterator &, "ValueIterator::operator++", NO_ARGS);
93 Assert(internal);
94 internal->next();
95 if (internal->at_end()) {
96 decref();
97 internal = NULL;
99 RETURN(*this);
102 Xapian::docid
103 ValueIterator::get_docid() const
105 LOGCALL(API, Xapian::docid, "ValueIterator::get_docid", NO_ARGS);
106 Assert(internal);
107 RETURN(internal->get_docid());
110 Xapian::valueno
111 ValueIterator::get_valueno() const
113 LOGCALL(API, Xapian::valueno, "ValueIterator::get_valueno", NO_ARGS);
114 Assert(internal);
115 RETURN(internal->get_valueno());
118 void
119 ValueIterator::skip_to(Xapian::docid docid_or_slot)
121 LOGCALL_VOID(API, "ValueIterator::skip_to", docid_or_slot);
122 if (internal) {
123 internal->skip_to(docid_or_slot);
124 if (internal->at_end()) {
125 decref();
126 internal = NULL;
131 bool
132 ValueIterator::check(Xapian::docid did)
134 LOGCALL(API, bool, "ValueIterator::check", did);
135 if (internal) {
136 if (!internal->check(did)) RETURN(false);
137 if (internal->at_end()) {
138 decref();
139 internal = NULL;
142 RETURN(true);
145 std::string
146 ValueIterator::get_description() const
148 string desc = "ValueIterator(";
149 if (internal)
150 desc += internal->get_description();
151 desc += ')';
152 return desc;