Website now in git not CVS
[xapian.git] / xapian-core / api / positioniterator.cc
blob1e621a3752a434525fe911edb99e27c418e0cdcf
1 /** @file positioniterator.cc
2 * @brief Class for iterating over term positions.
3 */
4 /* Copyright (C) 2008,2009,2010,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/positioniterator.h"
25 #include "debuglog.h"
26 #include "omassert.h"
27 #include "backends/positionlist.h"
29 using namespace std;
31 namespace Xapian {
33 void
34 PositionIterator::decref()
36 Assert(internal);
37 if (--internal->_refs == 0)
38 delete internal;
41 PositionIterator::PositionIterator(Internal *internal_) : internal(internal_)
43 LOGCALL_CTOR(API, "PositionIterator", internal_);
44 Assert(internal);
45 ++internal->_refs;
46 try {
47 internal->next();
48 } catch (...) {
49 // The destructor only runs if the constructor completes, so we have to
50 // take care of cleaning up for ourselves here.
51 decref();
52 throw;
54 if (internal->at_end()) {
55 decref();
56 internal = NULL;
60 PositionIterator::PositionIterator(const PositionIterator & o)
61 : internal(o.internal)
63 LOGCALL_CTOR(API, "PositionIterator", o);
64 if (internal)
65 ++internal->_refs;
68 PositionIterator &
69 PositionIterator::operator=(const PositionIterator & o)
71 LOGCALL(API, PositionIterator &, "PositionIterator::operator=", o);
72 if (o.internal)
73 ++o.internal->_refs;
74 if (internal)
75 decref();
76 internal = o.internal;
77 RETURN(*this);
80 Xapian::termpos
81 PositionIterator::operator*() const
83 LOGCALL(API, Xapian::termpos, "PositionIterator::operator*", NO_ARGS);
84 Assert(internal);
85 RETURN(internal->get_position());
88 PositionIterator &
89 PositionIterator::operator++()
91 LOGCALL(API, PositionIterator &, "PositionIterator::operator++", NO_ARGS);
92 Assert(internal);
93 internal->next();
94 if (internal->at_end()) {
95 decref();
96 internal = NULL;
98 RETURN(*this);
101 void
102 PositionIterator::skip_to(Xapian::termpos pos)
104 LOGCALL_VOID(API, "PositionIterator::skip_to", pos);
105 if (internal) {
106 internal->skip_to(pos);
107 if (internal->at_end()) {
108 decref();
109 internal = NULL;
114 std::string
115 PositionIterator::get_description() const
117 #if 0 // FIXME: Add PositionIterator::Internal::get_description() method.
118 string desc = "PositionIterator(";
119 if (internal)
120 desc += internal->get_description();
121 desc += ')';
122 return desc;
123 #else
124 return "PositionIterator()";
125 #endif