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 Construct given internals.
50 explicit ValueIterator(Internal
*internal_
);
53 ValueIterator(const ValueIterator
& o
);
56 ValueIterator
& operator=(const ValueIterator
& o
);
58 #ifdef XAPIAN_MOVE_SEMANTICS
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
;
76 /** Default constructor.
78 * Creates an uninitialised iterator, which can't be used before being
79 * assigned to, but is sometimes syntactically convenient.
81 XAPIAN_NOTHROW(ValueIterator())
86 if (internal
) decref();
89 /// Return the value at the current position.
90 std::string
operator*() const;
92 /// Advance the iterator to the next position.
93 ValueIterator
& operator++();
95 /// Advance the iterator to the next position (postfix version).
96 DerefWrapper_
<std::string
> operator++(int) {
97 const std::string
& value(**this);
99 return DerefWrapper_
<std::string
>(value
);
102 /** Return the docid at the current position.
104 * If we're iterating over values of a document, this method will throw
105 * Xapian::InvalidOperationError.
107 Xapian::docid
get_docid() const;
109 /** Return the value slot number for the current position.
111 * If the iterator is over all values in a slot, this returns that slot's
112 * number. If the iterator is over the values in a particular document,
113 * it returns the number of each slot in turn.
115 Xapian::valueno
get_valueno() const;
117 /** Advance the iterator to document id or value slot @a docid_or_slot.
119 * If this iterator is over values in a document, then this method
120 * advances the iterator to value slot @a docid_or_slot, or the first slot
121 * after it if there is no value in slot @a slot.
123 * If this iterator is over values in a particular slot, then this
124 * method advances the iterator to document id @a docid_or_slot, or the
125 * first document id after it if there is no value in the slot we're
126 * iterating over for document @a docid_or_slot.
128 * Note: The "two-faced" nature of this method is due to how C++
129 * overloading works. Xapian::docid and Xapian::valueno are both typedefs
130 * for the same unsigned integer type, so overloading can't distinguish
133 * @param docid_or_slot The docid/slot to advance to.
135 void skip_to(Xapian::docid docid_or_slot
);
137 /** Check if the specified docid occurs.
139 * The caller is required to ensure that the specified document id
140 * @a did actually exists in the database.
142 * This method acts like skip_to() if that can be done at little extra
143 * cost, in which case it then returns true. This is how chert and
144 * glass databases behave because they store values in streams which allow
145 * for an efficient implementation of skip_to().
147 * Otherwise it simply checks if a particular docid is present. If it
148 * is, it returns true. If it isn't, it returns false, and leaves the
149 * position unspecified (and hence the result of calling methods which
150 * depend on the current position, such as get_docid(), are also
151 * unspecified). In this state, next() will advance to the first matching
152 * position after document @a did, and skip_to() will act as it would if
153 * the position was the first matching position after document @a did.
155 * Currently the inmemory and remote backends behave in the
156 * latter way because they don't support streamed values and so skip_to()
157 * must check each document it skips over which is significantly slower.
159 * @param docid The document id to check.
162 bool check(Xapian::docid docid
);
164 // The AssertMacros.h header in the OS X SDK currently defines a check
165 // macro. Apple have deprecated check() in favour of __Check() and
166 // plan to remove check() in a "future release", but for now prevent
167 // expansion of check by adding parentheses in the method prototype:
168 // https://www.opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/AssertMacros.h
170 // We do this conditionally, as these parentheses trip up SWIG's
172 // https://github.com/swig/swig/issues/45
173 bool (check
)(Xapian::docid docid
);
176 /// Return a string describing this object.
177 std::string
get_description() const;
179 /** @private @internal ValueIterator is what the C++ STL calls an
182 * The following typedefs allow std::iterator_traits<> to work so that
183 * this iterator can be used with the STL.
185 * These are deliberately hidden from the Doxygen-generated docs, as the
186 * machinery here isn't interesting to API users. They just need to know
187 * that Xapian iterator classes are compatible with the STL.
191 typedef std::input_iterator_tag iterator_category
;
193 typedef std::string value_type
;
195 typedef Xapian::doccount_diff difference_type
;
197 typedef std::string
* pointer
;
199 typedef std::string
& reference
;
204 XAPIAN_NOTHROW(operator==(const ValueIterator
&a
, const ValueIterator
&b
));
206 /// Equality test for ValueIterator objects.
208 operator==(const ValueIterator
&a
, const ValueIterator
&b
) XAPIAN_NOEXCEPT
210 // Use a pointer comparison - this ensures both that (a == a) and correct
211 // handling of end iterators (which we ensure have NULL internals).
212 return a
.internal
== b
.internal
;
216 XAPIAN_NOTHROW(operator!=(const ValueIterator
&a
, const ValueIterator
&b
));
218 /// Inequality test for ValueIterator objects.
220 operator!=(const ValueIterator
&a
, const ValueIterator
&b
) XAPIAN_NOEXCEPT
227 #endif // XAPIAN_INCLUDED_VALUEITERATOR_H