[ci] Update macos jobs
[xapian.git] / xapian-bindings / tcl8 / tcl.i
blobd42339a793b86ae5b45ba585c0958699e4291cf6
1 %module xapian
2 %{
3 /* tcl.i: SWIG interface file for the Tcl bindings
5 * Copyright (c) 2006,2007,2011,2012,2015,2019 Olly Betts
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
20 * USA
24 %include ../xapian-head.i
26 #define XAPIAN_MIXED_SUBQUERIES_BY_ITERATOR_TYPEMAP
28 %typemap(typecheck, precedence=100) (XapianSWIGQueryItor qbegin, XapianSWIGQueryItor qend) {
29 int dummy;
30 $1 = (Tcl_ListObjLength(interp, $input, &dummy) == TCL_OK);
31 /* FIXME: if we add more array typemaps, we'll need to check the elements
32 * of the array here to disambiguate. */
36 class XapianSWIGQueryItor {
37 Tcl_Interp *interp;
39 Tcl_Obj ** items;
41 int n;
43 public:
44 typedef std::random_access_iterator_tag iterator_category;
45 typedef Xapian::Query value_type;
46 typedef Xapian::termcount_diff difference_type;
47 typedef Xapian::Query * pointer;
48 typedef Xapian::Query & reference;
50 XapianSWIGQueryItor()
51 : n(0) { }
53 XapianSWIGQueryItor(Tcl_Interp * interp_, Tcl_Obj **items_, int n_)
54 : interp(interp_), items(items_), n(n_) { }
56 XapianSWIGQueryItor & operator++() {
57 ++items;
58 --n;
59 return *this;
62 Xapian::Query operator*() const {
63 Tcl_Obj* item = *items;
64 Xapian::Query* subq = 0;
65 if (SWIG_ConvertPtr(item, (void **)&subq,
66 SWIGTYPE_p_Xapian__Query, 0) == TCL_OK) {
67 return *subq;
70 int len;
71 const char *p = Tcl_GetStringFromObj(item, &len);
72 return Xapian::Query(string(p, len));
75 bool operator==(const XapianSWIGQueryItor & o) {
76 return n == o.n;
79 bool operator!=(const XapianSWIGQueryItor & o) {
80 return !(*this == o);
83 difference_type operator-(const XapianSWIGQueryItor &o) const {
84 // Note: n counts *DOWN*, so reverse subtract.
85 return o.n - n;
91 %typemap(in) (XapianSWIGQueryItor qbegin, XapianSWIGQueryItor qend) {
92 Tcl_Obj ** items;
93 int numitems;
94 if (Tcl_ListObjGetElements(interp, $input, &numitems, &items) != TCL_OK) {
95 return TCL_ERROR;
97 $1 = XapianSWIGQueryItor(interp, items, numitems);
98 // $2 is default initialised where SWIG declares it.
101 #define XAPIAN_TERMITERATOR_PAIR_OUTPUT_TYPEMAP
102 %typemap(out) std::pair<Xapian::TermIterator, Xapian::TermIterator> {
103 Tcl_Obj * list = Tcl_NewListObj(0, NULL);
105 for (Xapian::TermIterator i = $1.first; i != $1.second; ++i) {
106 Tcl_Obj * str = Tcl_NewStringObj((*i).data(), (*i).length());
107 if (Tcl_ListObjAppendElement(interp, list, str) != TCL_OK)
108 return TCL_ERROR;
110 Tcl_SetObjResult(interp, list);
113 // Custom Tcl exception handling:
116 static int XapianTclHandleError(Tcl_Interp * interp, const Xapian::Error &e) {
117 Tcl_ResetResult(interp);
118 Tcl_SetErrorCode(interp, "XAPIAN", e.get_type(), NULL);
119 Tcl_AppendResult(interp, e.get_msg().c_str(), NULL);
120 return TCL_ERROR;
123 static int XapianTclHandleError(Tcl_Interp * interp) {
124 Tcl_ResetResult(interp);
125 Tcl_SetErrorCode(interp, "XAPIAN ?", NULL);
126 Tcl_AppendResult(interp, "Unknown Error", NULL);
127 return TCL_ERROR;
131 %exception {
132 try {
133 $function
134 } catch (const Xapian::Error &e) {
135 return XapianTclHandleError(interp, e);
136 } catch (...) {
137 return XapianTclHandleError(interp);
141 %include ../xapian-headers.i
143 /* vim:set syntax=cpp: */