2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2376
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2376
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_ops_h) |
|
24 #define octave_ops_h 1 |
|
25 |
4669
|
26 #include "Array-util.h" |
|
27 |
2376
|
28 extern void install_ops (void); |
|
29 |
3203
|
30 #define INSTALL_UNOP(op, t, f) \ |
|
31 octave_value_typeinfo::register_unary_op \ |
|
32 (octave_value::op, t::static_type_id (), oct_unop_ ## f); |
|
33 |
|
34 #define INSTALL_NCUNOP(op, t, f) \ |
|
35 octave_value_typeinfo::register_non_const_unary_op \ |
|
36 (octave_value::op, t::static_type_id (), oct_unop_ ## f); |
|
37 |
2376
|
38 #define INSTALL_BINOP(op, t1, t2, f) \ |
|
39 octave_value_typeinfo::register_binary_op \ |
3203
|
40 (octave_value::op, t1::static_type_id (), t2::static_type_id (), \ |
|
41 oct_binop_ ## f); |
2376
|
42 |
4915
|
43 #define INSTALL_CATOP(t1, t2, f) \ |
|
44 octave_value_typeinfo::register_cat_op \ |
|
45 (t1::static_type_id (), t2::static_type_id (), oct_catop_ ## f); |
|
46 |
2879
|
47 #define INSTALL_ASSIGNOP(op, t1, t2, f) \ |
2376
|
48 octave_value_typeinfo::register_assign_op \ |
3203
|
49 (octave_value::op, t1::static_type_id (), t2::static_type_id (), \ |
|
50 oct_assignop_ ## f); |
2376
|
51 |
3195
|
52 #define INSTALL_ASSIGNANYOP(op, t1, f) \ |
|
53 octave_value_typeinfo::register_assignany_op \ |
3203
|
54 (octave_value::op, t1::static_type_id (), oct_assignop_ ## f); |
3195
|
55 |
2376
|
56 #define INSTALL_ASSIGNCONV(t1, t2, tr) \ |
|
57 octave_value_typeinfo::register_pref_assign_conv \ |
|
58 (t1::static_type_id (), t2::static_type_id (), tr::static_type_id ()); |
|
59 |
4901
|
60 #define INSTALL_CONVOP(t1, t2, f) \ |
|
61 octave_value_typeinfo::register_type_conv_op \ |
|
62 (t1::static_type_id (), t2::static_type_id (), oct_conv_ ## f); |
|
63 |
2376
|
64 #define INSTALL_WIDENOP(t1, t2, f) \ |
|
65 octave_value_typeinfo::register_widening_op \ |
3203
|
66 (t1::static_type_id (), t2::static_type_id (), oct_conv_ ## f); |
2376
|
67 |
|
68 #define BOOL_OP1(xt, xn, get_x, yt, yn, get_y) \ |
|
69 xt xn = get_x; \ |
|
70 yt yn = get_y; |
|
71 |
|
72 #define BOOL_OP2(x) \ |
5275
|
73 octave_idx_type nr = x.rows (); \ |
|
74 octave_idx_type nc = x.columns (); |
2376
|
75 |
|
76 #define BOOL_OP3(test) \ |
2825
|
77 boolMatrix retval (nr, nc); \ |
5275
|
78 for (octave_idx_type j = 0; j < nc; j++) \ |
|
79 for (octave_idx_type i = 0; i < nr; i++) \ |
2376
|
80 retval (i, j) = test; \ |
|
81 return retval; |
|
82 |
2601
|
83 #define SC_MX_BOOL_OP(st, sn, get_s, mt, mn, get_m, test, empty_result) \ |
2376
|
84 do \ |
|
85 { \ |
|
86 BOOL_OP1 (st, sn, get_s, mt, mn, get_m) \ |
|
87 BOOL_OP2 (mn) \ |
2612
|
88 if (nr == 0 || nc == 0) \ |
2601
|
89 return empty_result; \ |
2376
|
90 BOOL_OP3 (test) \ |
|
91 } \ |
|
92 while (0) |
|
93 |
2601
|
94 #define MX_SC_BOOL_OP(mt, mn, get_m, st, sn, get_s, test, empty_result) \ |
2376
|
95 do \ |
|
96 { \ |
|
97 BOOL_OP1 (mt, mn, get_m, st, sn, get_s) \ |
|
98 BOOL_OP2 (mn) \ |
2612
|
99 if (nr == 0 || nc == 0) \ |
2601
|
100 return empty_result; \ |
2376
|
101 BOOL_OP3 (test) \ |
|
102 } \ |
|
103 while (0) |
|
104 |
|
105 #define MX_MX_BOOL_OP(m1t, m1n, get_m1, m2t, m2n, get_m2, test, op, \ |
2613
|
106 one_empty_result, two_empty_result) \ |
2376
|
107 do \ |
|
108 { \ |
|
109 BOOL_OP1 (m1t, m1n, get_m1, m2t, m2n, get_m2) \ |
5275
|
110 octave_idx_type m1_nr = m1n.rows (); \ |
|
111 octave_idx_type m1_nc = m1n.cols (); \ |
|
112 octave_idx_type m2_nr = m2n.rows (); \ |
|
113 octave_idx_type m2_nc = m2n.cols (); \ |
2613
|
114 if (m1_nr == m2_nr && m1_nc == m2_nc) \ |
2376
|
115 { \ |
2613
|
116 if (m1_nr == 0 && m1_nc == 0) \ |
|
117 return two_empty_result; \ |
|
118 else \ |
|
119 { \ |
|
120 BOOL_OP2 (m1n) \ |
|
121 BOOL_OP3 (test) \ |
|
122 } \ |
2376
|
123 } \ |
2613
|
124 else \ |
|
125 { \ |
|
126 if ((m1_nr == 0 && m1_nc == 0) || (m2_nr == 0 && m2_nc == 0)) \ |
|
127 return one_empty_result; \ |
|
128 else \ |
|
129 { \ |
|
130 gripe_nonconformant ("operator " op, m1_nr, m1_nc, \ |
|
131 m2_nr, m2_nc); \ |
2825
|
132 return boolMatrix (); \ |
2613
|
133 } \ |
|
134 } \ |
2376
|
135 } \ |
|
136 while (0) |
|
137 |
3203
|
138 #define CAST_UNOP_ARG(t) \ |
5760
|
139 t v = dynamic_cast<t> (a) |
3203
|
140 |
2376
|
141 #define CAST_BINOP_ARGS(t1, t2) \ |
5760
|
142 t1 v1 = dynamic_cast<t1> (a1); \ |
|
143 t2 v2 = dynamic_cast<t2> (a2) |
2376
|
144 |
|
145 #define CAST_CONV_ARG(t) \ |
5760
|
146 t v = dynamic_cast<t> (a) |
2376
|
147 |
2914
|
148 #define ASSIGNOPDECL(name) \ |
|
149 static octave_value \ |
5759
|
150 oct_assignop_ ## name (octave_base_value& a1, \ |
|
151 const octave_value_list& idx, \ |
|
152 const octave_base_value& a2) |
|
153 |
|
154 #define ASSIGNANYOPDECL(name) \ |
|
155 static octave_value \ |
|
156 oct_assignop_ ## name (octave_base_value& a1, \ |
3203
|
157 const octave_value_list& idx, \ |
|
158 const octave_value& a2) |
2914
|
159 |
|
160 #define DEFASSIGNOP(name, t1, t2) \ |
|
161 ASSIGNOPDECL (name) |
|
162 |
|
163 #define DEFASSIGNOP_FN(name, t1, t2, f) \ |
|
164 ASSIGNOPDECL (name) \ |
|
165 { \ |
|
166 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
167 \ |
|
168 v1.f (idx, v2.t1 ## _value ()); \ |
|
169 return octave_value (); \ |
|
170 } |
|
171 |
4686
|
172 #define DEFNDASSIGNOP_FN(name, t1, t2, e, f) \ |
|
173 ASSIGNOPDECL (name) \ |
|
174 { \ |
|
175 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
176 \ |
|
177 v1.f (idx, v2.e ## _value ()); \ |
|
178 return octave_value (); \ |
|
179 } |
|
180 |
3195
|
181 #define DEFASSIGNANYOP_FN(name, t1, f) \ |
5759
|
182 ASSIGNANYOPDECL (name) \ |
3195
|
183 { \ |
5760
|
184 octave_ ## t1& v1 = dynamic_cast<octave_ ## t1&> (a1); \ |
3195
|
185 \ |
|
186 v1.f (idx, a2); \ |
|
187 return octave_value (); \ |
|
188 } |
|
189 |
2914
|
190 #define CONVDECL(name) \ |
5759
|
191 static octave_base_value * \ |
|
192 oct_conv_ ## name (const octave_base_value& a) |
3203
|
193 |
|
194 #define CONVDECLX(name) \ |
5759
|
195 static octave_base_value * \ |
|
196 oct_conv_ ## name (const octave_base_value&) |
2914
|
197 |
4901
|
198 #define DEFCONV(name, a_dummy, b_dummy) \ |
2914
|
199 CONVDECL (name) |
|
200 |
4901
|
201 #define DEFCONVFNX(name, tfrom, ovtto, tto, e) \ |
|
202 CONVDECL (name) \ |
|
203 { \ |
|
204 CAST_CONV_ARG (const octave_ ## tfrom&); \ |
|
205 \ |
|
206 return new octave_ ## ovtto (tto ## NDArray (v.e ## array_value ())); \ |
|
207 } |
|
208 |
|
209 #define DEFDBLCONVFN(name, ovtfrom, e) \ |
|
210 CONVDECL (name) \ |
|
211 { \ |
|
212 CAST_CONV_ARG (const octave_ ## ovtfrom&); \ |
|
213 \ |
|
214 return new octave_matrix (NDArray (v.e ## _value ())); \ |
|
215 } |
|
216 |
5032
|
217 #define DEFSTRINTCONVFN(name, tto) \ |
|
218 DEFCONVFNX(name, char_matrix_str, tto ## _matrix, tto, char_) |
|
219 |
5992
|
220 #define DEFSTRDBLCONVFN(name, tfrom) \ |
|
221 DEFCONVFNX(name, tfrom, matrix, , char_) |
5032
|
222 |
4901
|
223 #define DEFCONVFN(name, tfrom, tto) \ |
|
224 DEFCONVFNX (name, tfrom, tto ## _matrix, tto, ) |
|
225 |
|
226 #define DEFCONVFN2(name, tfrom, sm, tto) \ |
|
227 DEFCONVFNX (name, tfrom ## _ ## sm, tto ## _matrix, tto, tfrom ## _) |
|
228 |
3203
|
229 #define UNOPDECL(name, a) \ |
|
230 static octave_value \ |
5759
|
231 oct_unop_ ## name (const octave_base_value& a) |
3203
|
232 |
|
233 #define DEFUNOPX(name, t) \ |
|
234 UNOPDECL (name, , ) |
|
235 |
|
236 #define DEFUNOP(name, t) \ |
|
237 UNOPDECL (name, a) |
|
238 |
|
239 #define DEFUNOP_OP(name, t, op) \ |
|
240 UNOPDECL (name, a) \ |
|
241 { \ |
|
242 CAST_UNOP_ARG (const octave_ ## t&); \ |
|
243 return octave_value (op v.t ## _value ()); \ |
|
244 } |
|
245 |
4550
|
246 #define DEFNDUNOP_OP(name, t, e, op) \ |
4543
|
247 UNOPDECL (name, a) \ |
|
248 { \ |
|
249 CAST_UNOP_ARG (const octave_ ## t&); \ |
4550
|
250 return octave_value (op v.e ## _value ()); \ |
4543
|
251 } |
|
252 |
5775
|
253 // FIXME -- in some cases, the constructor isn't necessary. |
3203
|
254 |
|
255 #define DEFUNOP_FN(name, t, f) \ |
|
256 UNOPDECL (name, a) \ |
|
257 { \ |
|
258 CAST_UNOP_ARG (const octave_ ## t&); \ |
|
259 return octave_value (f (v.t ## _value ())); \ |
|
260 } |
|
261 |
4550
|
262 #define DEFNDUNOP_FN(name, t, e, f) \ |
4543
|
263 UNOPDECL (name, a) \ |
|
264 { \ |
|
265 CAST_UNOP_ARG (const octave_ ## t&); \ |
4550
|
266 return octave_value (f (v.e ## _value ())); \ |
4543
|
267 } |
|
268 |
3203
|
269 #define DEFNCUNOP_METHOD(name, t, method) \ |
|
270 static void \ |
5759
|
271 oct_unop_ ## name (octave_base_value& a) \ |
3203
|
272 { \ |
|
273 CAST_UNOP_ARG (octave_ ## t&); \ |
|
274 v.method (); \ |
|
275 } |
|
276 |
2914
|
277 #define BINOPDECL(name, a1, a2) \ |
|
278 static octave_value \ |
5759
|
279 oct_binop_ ## name (const octave_base_value& a1, const octave_base_value& a2) |
2914
|
280 |
|
281 #define DEFBINOPX(name, t1, t2) \ |
|
282 BINOPDECL (name, , ) |
|
283 |
|
284 #define DEFBINOP(name, t1, t2) \ |
|
285 BINOPDECL (name, a1, a2) |
|
286 |
|
287 #define DEFBINOP_OP(name, t1, t2, op) \ |
|
288 BINOPDECL (name, a1, a2) \ |
|
289 { \ |
|
290 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
291 return octave_value \ |
|
292 (v1.t1 ## _value () op v2.t2 ## _value ()); \ |
|
293 } |
|
294 |
4543
|
295 #define DEFNDBINOP_OP(name, t1, t2, e1, e2, op) \ |
|
296 BINOPDECL (name, a1, a2) \ |
|
297 { \ |
|
298 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
299 return octave_value \ |
|
300 (v1.e1 ## _value () op v2.e2 ## _value ()); \ |
|
301 } |
|
302 |
5775
|
303 // FIXME -- in some cases, the constructor isn't necessary. |
2914
|
304 |
|
305 #define DEFBINOP_FN(name, t1, t2, f) \ |
|
306 BINOPDECL (name, a1, a2) \ |
|
307 { \ |
|
308 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
309 return octave_value (f (v1.t1 ## _value (), v2.t2 ## _value ())); \ |
|
310 } |
|
311 |
4543
|
312 #define DEFNDBINOP_FN(name, t1, t2, e1, e2, f) \ |
|
313 BINOPDECL (name, a1, a2) \ |
|
314 { \ |
|
315 CAST_BINOP_ARGS (const octave_ ## t1&, const octave_ ## t2&); \ |
|
316 return octave_value (f (v1.e1 ## _value (), v2.e2 ## _value ())); \ |
|
317 } |
|
318 |
2914
|
319 #define BINOP_NONCONFORMANT(msg) \ |
|
320 gripe_nonconformant (msg, \ |
|
321 a1.rows (), a1.columns (), \ |
|
322 a2.rows (), a2.columns ()); \ |
|
323 return octave_value () |
|
324 |
4915
|
325 #define CATOPDECL(name, a1, a2) \ |
|
326 static octave_value \ |
5759
|
327 oct_catop_ ## name (octave_base_value& a1, const octave_base_value& a2, \ |
6871
|
328 const Array<octave_idx_type>& ra_idx) |
4915
|
329 |
|
330 #define DEFCATOPX(name, t1, t2) \ |
|
331 CATOPDECL (name, , ) |
|
332 |
|
333 #define DEFCATOP(name, t1, t2) \ |
|
334 CATOPDECL (name, a1, a2) |
|
335 |
5775
|
336 // FIXME -- in some cases, the constructor isn't necessary. |
4915
|
337 |
|
338 #define DEFCATOP_FN(name, t1, t2, f) \ |
5075
|
339 CATOPDECL (name, a1, a2) \ |
4915
|
340 { \ |
5073
|
341 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
5533
|
342 return octave_value (v1.t1 ## _value () . f (v2.t2 ## _value (), ra_idx)); \ |
4915
|
343 } |
|
344 |
5075
|
345 #define DEFNDCATOP_FN(name, t1, t2, e1, e2, f) \ |
|
346 CATOPDECL (name, a1, a2) \ |
4915
|
347 { \ |
5073
|
348 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
5533
|
349 return octave_value (v1.e1 ## _value () . f (v2.e2 ## _value (), ra_idx)); \ |
|
350 } |
|
351 |
|
352 #define DEFNDCHARCATOP_FN(name, t1, t2, f) \ |
|
353 CATOPDECL (name, a1, a2) \ |
|
354 { \ |
|
355 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
356 \ |
|
357 return octave_value (v1.char_array_value () . f (v2.char_array_value (), ra_idx), \ |
|
358 true, ((a1.is_sq_string () || a2.is_sq_string ()) \ |
|
359 ? '\'' : '"')); \ |
4915
|
360 } |
|
361 |
5075
|
362 // For compatibility, the second arg is always converted to the type |
|
363 // of the first. Hmm. |
|
364 |
|
365 #define DEFNDCATOP_FN2(name, t1, t2, tc1, tc2, e1, e2, f) \ |
|
366 CATOPDECL (name, a1, a2) \ |
|
367 { \ |
|
368 CAST_BINOP_ARGS (octave_ ## t1&, const octave_ ## t2&); \ |
|
369 return octave_value (tc1 (v1.e1 ## _value ()) . f (tc2 (v2.e2 ## _value ()), ra_idx)); \ |
|
370 } |
|
371 |
4915
|
372 #define CATOP_NONCONFORMANT(msg) \ |
|
373 gripe_nonconformant (msg, \ |
|
374 a1.rows (), a1.columns (), \ |
|
375 a2.rows (), a2.columns ()); \ |
|
376 return octave_value () |
|
377 |
2376
|
378 #endif |
|
379 |
|
380 /* |
|
381 ;;; Local Variables: *** |
|
382 ;;; mode: C++ *** |
|
383 ;;; End: *** |
|
384 */ |