Use new lemon /*X-overwrites-Y*/ magic comments
[xapian.git] / xapian-bindings / ruby / ruby.i
blob2a98119415506faed064e16caf24dd1cdbeb7338
1 %module(directors="1") xapian
2 %{
3 /* ruby.i: SWIG interface file for the Ruby bindings
5 * Original version by Paul Legato (plegato@nks.net), 4/17/06.
6 * Based on the php4 and python util.i files.
8 * Copyright (C) 2006 Networked Knowledge Systems, Inc.
9 * Copyright (C) 2006,2007,2008,2009,2010,2011,2012 Olly Betts
10 * Copyright (C) 2010 Richard Boulton
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 * USA
30 // Use SWIG directors for Ruby wrappers.
31 #define XAPIAN_SWIG_DIRECTORS
33 // We don't use the Iterator and ConstIterator wrappers which SWIG now offers,
34 // so disable them to reduce the generated C++ wrapper code.
35 #define SWIG_NO_EXPORT_ITERATOR_METHODS
37 %include ../xapian-head.i
39 /////////////////////////////////////
40 // Rename function and method names to match Ruby conventions
41 // e.g. from get_data to data and from set_data to data=
43 // Getters
44 %rename("available_languages") get_available_languages;
45 %rename("avlength") get_avlength;
46 %rename("collapse_count") get_collapse_count;
47 %rename("collection_freq") get_collection_freq;
48 %rename("context") get_context;
49 %rename("data") get_data;
50 %rename("default_op") get_default_op;
51 %rename("description") get_description;
52 %rename("docid") get_docid;
53 %rename("document_id") get_document_id;
54 %rename("document_percentage") get_document_percentage;
55 %rename("doccount") get_doccount;
56 %rename("doclength") get_doclength;
57 %rename("document") get_document;
58 %rename("ebound") get_ebound;
59 %rename("eset") get_eset;
60 %rename("firstitem") get_firstitem;
61 %rename("hit") get_hit;
62 %rename("lastdocid") get_lastdocid;
63 %rename("length") get_length;
64 %rename("matches_estimated") get_matches_estimated;
65 %rename("matches_lower_bound") get_matches_lower_bound;
66 %rename("matches_upper_bound") get_matches_upper_bound;
67 %rename("matching_terms") get_matching_terms;
68 %rename("max_attained") get_max_attained;
69 %rename("max_possible") get_max_possible;
70 %rename("maxextra") get_maxextra;
71 %rename("maxpart") get_maxpart;
72 %rename("mset") get_mset;
73 %rename("msg") get_msg;
74 %rename("op_name") get_op_name;
75 %rename("percent") get_percent;
76 %rename("query") get_query;
77 %rename("rank") get_rank;
78 %rename("sumextra") get_sumextra;
79 %rename("sumpart") get_sumpart;
80 %rename("termfreq") get_termfreq;
81 %rename("terms") get_terms;
82 %rename("term") get_term;
83 %rename("termpos") get_termpos;
84 %rename("termweight") get_termweight;
85 %rename("type") get_type;
86 %rename("value") get_value;
87 %rename("valueno") get_valueno;
88 %rename("wdf") get_wdf;
89 %rename("weight") get_weight;
91 // These are 'dangerous' methods; i.e. they can cause a segfault if used
92 // improperly. We prefix with _dangerous_ so that Ruby users will not use them
93 // inadvertently.
95 // There is a safe wrapper for their functionality provided in xapian.rb.
97 // in Xapian::Document and Xapian::Database
98 %rename("_dangerous_termlist_begin") termlist_begin;
99 %rename("_dangerous_termlist_end") termlist_end;
100 // in Xapian::Query
101 %rename("_dangerous_terms_begin") get_terms_begin;
102 %rename("_dangerous_terms_end") get_terms_end;
103 // in Xapian::Enquire
104 %rename("_dangerous_matching_terms_begin") get_matching_terms_begin;
105 %rename("_dangerous_matching_terms_end") get_matching_terms_end;
106 // in Xapian::Database
107 %rename("_dangerous_allterms_begin") allterms_begin;
108 %rename("_dangerous_allterms_end") allterms_end;
109 // in Xapian::Database
110 %rename("_dangerous_postlist_begin") postlist_begin;
111 %rename("_dangerous_postlist_end") postlist_end;
112 // in Xapian::Database
113 %rename("_dangerous_positionlist_begin") positionlist_begin;
114 %rename("_dangerous_positionlist_end") positionlist_end;
115 // in Xapian::Database
116 %rename("_dangerous_valuestream_begin") valuestream_begin;
117 %rename("_dangerous_valuestream_end") valuestream_end;
118 // in Xapian::Document and Xapian::ValueCountMatchSpy
119 %rename("_dangerous_values_begin") values_begin;
120 %rename("_dangerous_values_end") values_end;
121 // in Xapian::ValueCountMatchSpy
122 %rename("_dangerous_top_values_begin") top_values_begin;
123 %rename("_dangerous_top_values_end") top_values_end;
126 // MSetIterators are not dangerous, just inconvenient to use within a Ruby
127 // idiom.
128 %rename ("_begin") begin;
129 %rename ("_end") end;
130 %rename ("_back") back;
134 // Setters
135 %rename("collapse_key=") set_collapse_key;
136 %rename("cutoff!") set_cutoff;
137 %rename("data=") set_data;
138 %rename("database=") set_database;
139 %rename("default_op=") set_default_op;
140 %rename("docid_order=") set_docid_order;
141 %rename("document=") set_document;
142 %rename("query=") set_query(const Query&);
143 %rename("query!") set_query(const Query&, termcount);
144 %rename("sort_by_relevance!") set_sort_by_relevance;
145 %rename("sort_by_relevance_then_value!") set_sort_by_relevance_then_value;
146 %rename("sort_by_value_then_relevance!") set_sort_by_value_then_relevance;
147 %rename("sort_by_value!") set_sort_by_value;
148 %rename("stemmer=") set_stemmer;
149 %rename("stemming_strategy=") set_stemming_strategy;
150 %rename("stopper=") set_stopper;
151 %rename("weighting_scheme=") set_weighting_scheme;
153 // Booleans
154 %predicate empty;
156 #define XAPIAN_MIXED_SUBQUERIES_BY_ITERATOR_TYPEMAP
158 /* FIXME:
159 * Check to see what is equivalent to a C++ Vector for the purposes of a Query
160 * instantiation.
161 * At the moment, we take Ruby Arrays.
163 %typemap(typecheck, precedence=500) (XapianSWIGQueryItor qbegin, XapianSWIGQueryItor qend) {
164 $1 = (TYPE($input) == T_ARRAY);
165 /* Currently, the only wrapped method which takes a Ruby array is the
166 * "extra" constructor Query(OP, ARRAY), where ARRAY can contain any mix of
167 * strings and Query objects.
169 * If we ever had a method (or function) which had two overloaded forms
170 * only differentiated by what type of array can be passed we'd need to
171 * look at the type of the array elements in the typecheck typemaps.
176 class XapianSWIGQueryItor {
177 VALUE array;
179 int i;
181 public:
182 typedef std::random_access_iterator_tag iterator_category;
183 typedef Xapian::Query value_type;
184 typedef Xapian::termcount_diff difference_type;
185 typedef Xapian::Query * pointer;
186 typedef Xapian::Query & reference;
188 XapianSWIGQueryItor() { }
190 void begin(VALUE array_) {
191 array = array_;
192 i = 0;
195 void end(int n) {
196 i = n;
199 XapianSWIGQueryItor & operator++() {
200 ++i;
201 return *this;
204 Xapian::Query operator*() const {
205 VALUE entry = rb_ary_entry(array, i);
206 if (TYPE(entry) == T_STRING) {
207 return Xapian::Query(string(RSTRING_PTR(entry),
208 RSTRING_LEN(entry)));
211 // array element may be a Xapian::Query object. Add it if it is,
212 // otherwise error out.
213 Xapian::Query *subq = 0;
214 if (SWIG_ConvertPtr(entry, (void **)&subq,
215 SWIGTYPE_p_Xapian__Query, 0) < 0 || !subq) {
216 SWIG_exception(SWIG_ValueError, "Elements of Arrays passed to Query must be either Strings or other Query objects");
217 return Xapian::Query();
219 return *subq;
222 bool operator==(const XapianSWIGQueryItor & o) {
223 return i == o.i;
226 bool operator!=(const XapianSWIGQueryItor & o) {
227 return !(*this == o);
230 difference_type operator-(const XapianSWIGQueryItor &o) const {
231 return i - o.i;
237 %typemap(in) (XapianSWIGQueryItor qbegin, XapianSWIGQueryItor qend) {
238 if (TYPE($input) == T_ARRAY) {
239 // The typecheck typemap should have ensured this is an array.
240 $1.begin($input);
241 $2.end(RARRAY_LEN($input));
242 } else {
243 $1.end(0);
244 $2.end(0);
248 %typemap(directorin) (size_t num_tags, const std::string tags[]) {
249 $input = rb_ary_new();
250 for (size_t i = 0; i != num_tags; ++i) {
251 VALUE str = rb_str_new(tags[i].data(), tags[i].size());
252 rb_ary_push($input, str);
256 // For MatchDecider::operator() and ExpandDecider::operator().
257 %typemap(directorout) int = bool;
259 %include ../generic/except.i
261 %include ../xapian-headers.i
263 %include extra.i