[python3] Simplify generated wrapper post-processing
[xapian.git] / xapian-core / include / xapian / keymaker.h
blob298730804c15db4f4ca61f73c01142f7a46099de
1 /** @file keymaker.h
2 * @brief Build key strings for MSet ordering or collapsing.
3 */
4 /* Copyright (C) 2007,2009,2011,2013,2014,2015,2016 Olly Betts
5 * Copyright (C) 2010 Richard Boulton
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef XAPIAN_INCLUDED_KEYMAKER_H
23 #define XAPIAN_INCLUDED_KEYMAKER_H
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error "Never use <xapian/keymaker.h> directly; include <xapian.h> instead."
27 #endif
29 #include <string>
30 #include <vector>
32 #include <xapian/intrusive_ptr.h>
33 #include <xapian/types.h>
34 #include <xapian/visibility.h>
36 namespace Xapian {
38 class Document;
40 /** Virtual base class for key making functors. */
41 class XAPIAN_VISIBILITY_DEFAULT KeyMaker
42 : public Xapian::Internal::opt_intrusive_base {
43 /// Don't allow assignment.
44 void operator=(const KeyMaker &);
46 /// Don't allow copying.
47 KeyMaker(const KeyMaker &);
49 public:
50 /// Default constructor.
51 KeyMaker() { }
53 /** Build a key string for a Document.
55 * These keys can be used for sorting or collapsing matching documents.
57 * @param doc Document object to build a key for.
59 virtual std::string operator()(const Xapian::Document & doc) const = 0;
61 /** Virtual destructor, because we have virtual methods. */
62 virtual ~KeyMaker();
64 /** Start reference counting this object.
66 * You can hand ownership of a dynamically allocated KeyMaker
67 * object to Xapian by calling release() and then passing the object to a
68 * Xapian method. Xapian will arrange to delete the object once it is no
69 * longer required.
71 KeyMaker * release() {
72 opt_intrusive_base::release();
73 return this;
76 /** Start reference counting this object.
78 * You can hand ownership of a dynamically allocated KeyMaker
79 * object to Xapian by calling release() and then passing the object to a
80 * Xapian method. Xapian will arrange to delete the object once it is no
81 * longer required.
83 const KeyMaker * release() const {
84 opt_intrusive_base::release();
85 return this;
89 /** KeyMaker subclass which combines several values.
91 * When the result is used for sorting, results are ordered by the first
92 * value. In the event of a tie, the second is used. If this is the same for
93 * both, the third is used, and so on. If @a reverse is true for a value,
94 * then the sort order for that value is reversed.
96 * When used for collapsing, the documents will only be considered equal if
97 * all the values specified match. If none of the specified values are set
98 * then the generated key will be empty, so such documents won't be collapsed
99 * (which is consistent with the behaviour in the "collapse on a value" case).
100 * If you'd prefer that documents with none of the keys set are collapsed
101 * together, then you can set @a reverse for at least one of the values.
102 * Other than this, it isn't useful to set @a reverse for collapsing.
104 class XAPIAN_VISIBILITY_DEFAULT MultiValueKeyMaker : public KeyMaker {
105 struct KeySpec {
106 Xapian::valueno slot;
107 bool reverse;
108 std::string defvalue;
109 KeySpec(Xapian::valueno slot_, bool reverse_,
110 const std::string & defvalue_)
111 : slot(slot_), reverse(reverse_), defvalue(defvalue_)
114 std::vector<KeySpec> slots;
116 public:
117 MultiValueKeyMaker() { }
119 /** Construct a MultiValueKeyMaker from a pair of iterators.
121 * The iterators must be a begin/end pair returning Xapian::valueno (or
122 * a compatible type) when dereferenced.
124 template <class Iterator>
125 MultiValueKeyMaker(Iterator begin, Iterator end) {
126 while (begin != end) add_value(*begin++);
129 virtual std::string operator()(const Xapian::Document & doc) const;
131 /** Add a value slot to the list to build a key from.
133 * @param slot The value slot to add
134 * @param reverse Adjust values from this slot to reverse their sort
135 * order (default: false)
136 * @param defvalue Value to use for documents which don't have a value
137 * set in this slot (default: empty). This can be used
138 * to make such documents sort after all others by
139 * passing <code>get_value_upper_bound(slot) + "x"</code>
140 * - this is guaranteed to be greater than any value in
141 * this slot.
143 void add_value(Xapian::valueno slot, bool reverse = false,
144 const std::string & defvalue = std::string()) {
145 slots.push_back(KeySpec(slot, reverse, defvalue));
151 #endif // XAPIAN_INCLUDED_KEYMAKER_H