2 * @brief Class for iterating over document values.
4 /* Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (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
22 #ifndef XAPIAN_INCLUDED_VALUEITERATOR_H
23 #define XAPIAN_INCLUDED_VALUEITERATOR_H
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error Never use <xapian/valueiterator.h> directly; include <xapian.h> instead.
32 #include <xapian/attributes.h>
33 #include <xapian/derefwrapper.h>
34 #include <xapian/types.h>
35 #include <xapian/visibility.h>
39 /// Class for iterating over document values.
40 class XAPIAN_VISIBILITY_DEFAULT ValueIterator
{
44 /// Class representing the ValueIterator internals.
46 /// @private @internal Reference counted internals.
49 /** @private @internal Wrap an existing Internal. */
50 XAPIAN_VISIBILITY_INTERNAL
51 explicit ValueIterator(Internal
*internal_
);
54 ValueIterator(const ValueIterator
& o
);
57 ValueIterator
& operator=(const ValueIterator
& o
);
60 ValueIterator(ValueIterator
&& o
)
61 : internal(o
.internal
) {
65 /// Move assignment operator.
66 ValueIterator
& operator=(ValueIterator
&& o
) {
68 if (internal
) decref();
69 internal
= o
.internal
;
75 /** Default constructor.
77 * Creates an uninitialised iterator, which can't be used before being
78 * assigned to, but is sometimes syntactically convenient.
80 ValueIterator() noexcept
85 if (internal
) decref();
88 /// Return the value at the current position.
89 std::string
operator*() const;
91 /// Advance the iterator to the next position.
92 ValueIterator
& operator++();
94 /// Advance the iterator to the next position (postfix version).
95 DerefWrapper_
<std::string
> operator++(int) {
96 const std::string
& value(**this);
98 return DerefWrapper_
<std::string
>(value
);
101 /** Return the docid at the current position.
103 * If we're iterating over values of a document, this method will throw
104 * Xapian::InvalidOperationError.
106 Xapian::docid
get_docid() const;
108 /** Return the value slot number for the current position.
110 * If the iterator is over all values in a slot, this returns that slot's
111 * number. If the iterator is over the values in a particular document,
112 * it returns the number of each slot in turn.
114 Xapian::valueno
get_valueno() const;
116 /** Advance the iterator to document id or value slot @a docid_or_slot.
118 * If this iterator is over values in a document, then this method
119 * advances the iterator to value slot @a docid_or_slot, or the first slot
120 * after it if there is no value in slot @a slot.
122 * If this iterator is over values in a particular slot, then this
123 * method advances the iterator to document id @a docid_or_slot, or the
124 * first document id after it if there is no value in the slot we're
125 * iterating over for document @a docid_or_slot.
127 * Note: The "two-faced" nature of this method is due to how C++
128 * overloading works. Xapian::docid and Xapian::valueno are both typedefs
129 * for the same unsigned integer type, so overloading can't distinguish
132 * @param docid_or_slot The docid/slot to advance to.
134 void skip_to(Xapian::docid docid_or_slot
);
136 /** Check if the specified docid occurs.
138 * The caller is required to ensure that the specified document id
139 * @a did actually exists in the database.
141 * This method acts like skip_to() if that can be done at little extra
142 * cost, in which case it then returns true. This is how
143 * glass databases behave because they store values in streams which allow
144 * for an efficient implementation of skip_to().
146 * Otherwise it simply checks if a particular docid is present. If it
147 * is, it returns true. If it isn't, it returns false, and leaves the
148 * position unspecified (and hence the result of calling methods which
149 * depend on the current position, such as get_docid(), are also
150 * unspecified). In this state, next() will advance to the first matching
151 * position after document @a did, and skip_to() will act as it would if
152 * the position was the first matching position after document @a did.
154 * Currently the inmemory and remote backends behave in the
155 * latter way because they don't support streamed values and so skip_to()
156 * must check each document it skips over which is significantly slower.
158 * @param docid The document id to check.
161 bool check(Xapian::docid docid
);
163 // The AssertMacros.h header in the macOS SDK currently defines a check
164 // macro. Apple have deprecated check() in favour of __Check() and
165 // plan to remove check() in a "future release", but for now prevent
166 // expansion of check by adding parentheses in the method prototype:
167 // https://www.opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/AssertMacros.h
169 // We do this conditionally, as these parentheses trip up SWIG's
171 // https://github.com/swig/swig/issues/45
172 bool (check
)(Xapian::docid docid
);
175 /// Return a string describing this object.
176 std::string
get_description() const;
178 /** @private @internal ValueIterator is what the C++ STL calls an
181 * The following typedefs allow std::iterator_traits<> to work so that
182 * this iterator can be used with the STL.
184 * These are deliberately hidden from the Doxygen-generated docs, as the
185 * machinery here isn't interesting to API users. They just need to know
186 * that Xapian iterator classes are compatible with the STL.
190 typedef std::input_iterator_tag iterator_category
;
192 typedef std::string value_type
;
194 typedef Xapian::doccount_diff difference_type
;
196 typedef std::string
* pointer
;
198 typedef std::string
& reference
;
202 /// Equality test for ValueIterator objects.
204 operator==(const ValueIterator
& a
, const ValueIterator
& b
) noexcept
206 // Use a pointer comparison - this ensures both that (a == a) and correct
207 // handling of end iterators (which we ensure have NULL internals).
208 return a
.internal
== b
.internal
;
211 /// Inequality test for ValueIterator objects.
213 operator!=(const ValueIterator
& a
, const ValueIterator
& b
) noexcept
220 #endif // XAPIAN_INCLUDED_VALUEITERATOR_H