Mercurial > octave-antonio
annotate src/ov.cc @ 7487:1e01db14700b
catch octave_execution_exception for built-in and mex functions
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 15 Feb 2008 19:49:14 -0500 |
parents | 402168152bb9 |
children | 8c32f95c2639 |
rev | line source |
---|---|
2376 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
4 2006, 2007 John W. Eaton | |
2376 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2376 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2376 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
4970 | 28 #include "data-conv.h" |
29 #include "quit.h" | |
2942 | 30 #include "str-vec.h" |
2376 | 31 |
2974 | 32 #include "oct-obj.h" |
4944 | 33 #include "oct-stream.h" |
2376 | 34 #include "ov.h" |
35 #include "ov-base.h" | |
2825 | 36 #include "ov-bool.h" |
37 #include "ov-bool-mat.h" | |
3351 | 38 #include "ov-cell.h" |
2376 | 39 #include "ov-scalar.h" |
40 #include "ov-re-mat.h" | |
5164 | 41 #include "ov-bool-sparse.h" |
42 #include "ov-cx-sparse.h" | |
43 #include "ov-re-sparse.h" | |
4901 | 44 #include "ov-int8.h" |
45 #include "ov-int16.h" | |
46 #include "ov-int32.h" | |
47 #include "ov-int64.h" | |
48 #include "ov-uint8.h" | |
49 #include "ov-uint16.h" | |
50 #include "ov-uint32.h" | |
51 #include "ov-uint64.h" | |
2376 | 52 #include "ov-complex.h" |
53 #include "ov-cx-mat.h" | |
54 #include "ov-ch-mat.h" | |
55 #include "ov-str-mat.h" | |
56 #include "ov-range.h" | |
57 #include "ov-struct.h" | |
7336 | 58 #include "ov-class.h" |
4643 | 59 #include "ov-streamoff.h" |
2880 | 60 #include "ov-list.h" |
3977 | 61 #include "ov-cs-list.h" |
2376 | 62 #include "ov-colon.h" |
2974 | 63 #include "ov-builtin.h" |
64 #include "ov-mapper.h" | |
4649 | 65 #include "ov-dld-fcn.h" |
2974 | 66 #include "ov-usr-fcn.h" |
4342 | 67 #include "ov-fcn-handle.h" |
4966 | 68 #include "ov-fcn-inline.h" |
2376 | 69 #include "ov-typeinfo.h" |
70 | |
71 #include "defun.h" | |
2880 | 72 #include "error.h" |
2376 | 73 #include "gripes.h" |
74 #include "pager.h" | |
4005 | 75 #include "parse.h" |
2376 | 76 #include "pr-output.h" |
7336 | 77 #include "symtab.h" |
2376 | 78 #include "utils.h" |
79 #include "variables.h" | |
80 | |
2477 | 81 // We are likely to have a lot of octave_value objects to allocate, so |
82 // make the grow_size large. | |
3219 | 83 DEFINE_OCTAVE_ALLOCATOR2(octave_value, 1024); |
2477 | 84 |
5775 | 85 // FIXME |
2880 | 86 |
2376 | 87 // Octave's value type. |
88 | |
3536 | 89 std::string |
3203 | 90 octave_value::unary_op_as_string (unary_op op) |
91 { | |
3523 | 92 std::string retval; |
3203 | 93 |
94 switch (op) | |
95 { | |
3533 | 96 case op_not: |
3203 | 97 retval = "!"; |
98 break; | |
99 | |
4965 | 100 case op_uplus: |
101 retval = "+"; | |
102 break; | |
103 | |
3533 | 104 case op_uminus: |
3203 | 105 retval = "-"; |
106 break; | |
107 | |
3533 | 108 case op_transpose: |
3203 | 109 retval = ".'"; |
110 break; | |
111 | |
3533 | 112 case op_hermitian: |
3203 | 113 retval = "'"; |
114 break; | |
115 | |
3533 | 116 case op_incr: |
3203 | 117 retval = "++"; |
118 break; | |
119 | |
3533 | 120 case op_decr: |
3203 | 121 retval = "--"; |
122 break; | |
123 | |
124 default: | |
125 retval = "<unknown>"; | |
126 } | |
127 | |
128 return retval; | |
129 } | |
130 | |
3536 | 131 std::string |
7336 | 132 octave_value::unary_op_fcn_name (unary_op op) |
133 { | |
134 std::string retval; | |
135 | |
136 switch (op) | |
137 { | |
138 case op_not: | |
139 retval = "not"; | |
140 break; | |
141 | |
142 case op_uplus: | |
143 retval = "uplus"; | |
144 break; | |
145 | |
146 case op_uminus: | |
147 retval = "uminus"; | |
148 break; | |
149 | |
150 case op_transpose: | |
151 retval = "transpose"; | |
152 break; | |
153 | |
154 case op_hermitian: | |
155 retval = "ctranspose"; | |
156 break; | |
157 | |
158 default: | |
159 break; | |
160 } | |
161 | |
162 return retval; | |
163 } | |
164 | |
165 std::string | |
2376 | 166 octave_value::binary_op_as_string (binary_op op) |
167 { | |
3523 | 168 std::string retval; |
2376 | 169 |
170 switch (op) | |
171 { | |
3533 | 172 case op_add: |
2376 | 173 retval = "+"; |
174 break; | |
175 | |
3533 | 176 case op_sub: |
2376 | 177 retval = "-"; |
178 break; | |
179 | |
3533 | 180 case op_mul: |
2376 | 181 retval = "*"; |
182 break; | |
183 | |
3533 | 184 case op_div: |
2376 | 185 retval = "/"; |
186 break; | |
187 | |
3533 | 188 case op_pow: |
2376 | 189 retval = "^"; |
190 break; | |
191 | |
3533 | 192 case op_ldiv: |
2376 | 193 retval = "\\"; |
194 break; | |
195 | |
3533 | 196 case op_lshift: |
2903 | 197 retval = "<<"; |
198 break; | |
199 | |
3533 | 200 case op_rshift: |
2903 | 201 retval = ">>"; |
202 break; | |
203 | |
3533 | 204 case op_lt: |
2376 | 205 retval = "<"; |
206 break; | |
207 | |
3533 | 208 case op_le: |
2376 | 209 retval = "<="; |
210 break; | |
211 | |
3533 | 212 case op_eq: |
2376 | 213 retval = "=="; |
214 break; | |
215 | |
3533 | 216 case op_ge: |
2376 | 217 retval = ">="; |
218 break; | |
219 | |
3533 | 220 case op_gt: |
2376 | 221 retval = ">"; |
222 break; | |
223 | |
3533 | 224 case op_ne: |
2376 | 225 retval = "!="; |
226 break; | |
227 | |
3533 | 228 case op_el_mul: |
2376 | 229 retval = ".*"; |
230 break; | |
231 | |
3533 | 232 case op_el_div: |
2376 | 233 retval = "./"; |
234 break; | |
235 | |
3533 | 236 case op_el_pow: |
2376 | 237 retval = ".^"; |
238 break; | |
239 | |
3533 | 240 case op_el_ldiv: |
2376 | 241 retval = ".\\"; |
242 break; | |
243 | |
3533 | 244 case op_el_and: |
2376 | 245 retval = "&"; |
246 break; | |
247 | |
3533 | 248 case op_el_or: |
2376 | 249 retval = "|"; |
250 break; | |
251 | |
3533 | 252 case op_struct_ref: |
2376 | 253 retval = "."; |
254 break; | |
255 | |
256 default: | |
257 retval = "<unknown>"; | |
258 } | |
259 | |
260 return retval; | |
261 } | |
262 | |
3536 | 263 std::string |
7336 | 264 octave_value::binary_op_fcn_name (binary_op op) |
265 { | |
266 std::string retval; | |
267 | |
268 switch (op) | |
269 { | |
270 case op_add: | |
271 retval = "plus"; | |
272 break; | |
273 | |
274 case op_sub: | |
275 retval = "minus"; | |
276 break; | |
277 | |
278 case op_mul: | |
279 retval = "mtimes"; | |
280 break; | |
281 | |
282 case op_div: | |
283 retval = "mrdivide"; | |
284 break; | |
285 | |
286 case op_pow: | |
287 retval = "mpower"; | |
288 break; | |
289 | |
290 case op_ldiv: | |
291 retval = "mldivide"; | |
292 break; | |
293 | |
294 case op_lt: | |
295 retval = "lt"; | |
296 break; | |
297 | |
298 case op_le: | |
299 retval = "le"; | |
300 break; | |
301 | |
302 case op_eq: | |
303 retval = "eq"; | |
304 break; | |
305 | |
306 case op_ge: | |
307 retval = "ge"; | |
308 break; | |
309 | |
310 case op_gt: | |
311 retval = "gt"; | |
312 break; | |
313 | |
314 case op_ne: | |
315 retval = "ne"; | |
316 break; | |
317 | |
318 case op_el_mul: | |
319 retval = "times"; | |
320 break; | |
321 | |
322 case op_el_div: | |
323 retval = "rdivide"; | |
324 break; | |
325 | |
326 case op_el_pow: | |
327 retval = "power"; | |
328 break; | |
329 | |
330 case op_el_ldiv: | |
331 retval = "ldivide"; | |
332 break; | |
333 | |
334 case op_el_and: | |
335 retval = "and"; | |
336 break; | |
337 | |
338 case op_el_or: | |
339 retval = "or"; | |
340 break; | |
341 | |
342 default: | |
343 break; | |
344 } | |
345 | |
346 return retval; | |
347 } | |
348 | |
349 std::string | |
2880 | 350 octave_value::assign_op_as_string (assign_op op) |
351 { | |
3523 | 352 std::string retval; |
2880 | 353 |
354 switch (op) | |
355 { | |
3533 | 356 case op_asn_eq: |
2880 | 357 retval = "="; |
358 break; | |
359 | |
3533 | 360 case op_add_eq: |
2880 | 361 retval = "+="; |
362 break; | |
363 | |
3533 | 364 case op_sub_eq: |
2880 | 365 retval = "-="; |
366 break; | |
367 | |
3533 | 368 case op_mul_eq: |
2880 | 369 retval = "*="; |
370 break; | |
371 | |
3533 | 372 case op_div_eq: |
2880 | 373 retval = "/="; |
374 break; | |
375 | |
3533 | 376 case op_ldiv_eq: |
3204 | 377 retval = "\\="; |
378 break; | |
379 | |
4018 | 380 case op_pow_eq: |
381 retval = "^="; | |
382 break; | |
383 | |
3533 | 384 case op_lshift_eq: |
2903 | 385 retval = "<<="; |
386 break; | |
387 | |
3533 | 388 case op_rshift_eq: |
2903 | 389 retval = ">>="; |
390 break; | |
391 | |
3533 | 392 case op_el_mul_eq: |
2880 | 393 retval = ".*="; |
394 break; | |
395 | |
3533 | 396 case op_el_div_eq: |
2880 | 397 retval = "./="; |
398 break; | |
399 | |
3533 | 400 case op_el_ldiv_eq: |
3204 | 401 retval = ".\\="; |
402 break; | |
403 | |
4018 | 404 case op_el_pow_eq: |
405 retval = ".^="; | |
406 break; | |
407 | |
3533 | 408 case op_el_and_eq: |
2880 | 409 retval = "&="; |
410 break; | |
411 | |
3533 | 412 case op_el_or_eq: |
2880 | 413 retval = "|="; |
414 break; | |
415 | |
416 default: | |
417 retval = "<unknown>"; | |
418 } | |
419 | |
420 return retval; | |
421 } | |
422 | |
5759 | 423 octave_value::octave_value (void) |
424 : rep (new octave_base_value ()) | |
4513 | 425 { |
2825 | 426 } |
2376 | 427 |
4254 | 428 octave_value::octave_value (short int i) |
429 : rep (new octave_scalar (i)) | |
430 { | |
431 } | |
432 | |
433 octave_value::octave_value (unsigned short int i) | |
434 : rep (new octave_scalar (i)) | |
435 { | |
436 } | |
437 | |
4233 | 438 octave_value::octave_value (int i) |
439 : rep (new octave_scalar (i)) | |
440 { | |
441 } | |
442 | |
4254 | 443 octave_value::octave_value (unsigned int i) |
444 : rep (new octave_scalar (i)) | |
445 { | |
446 } | |
447 | |
448 octave_value::octave_value (long int i) | |
449 : rep (new octave_scalar (i)) | |
450 { | |
451 } | |
452 | |
453 octave_value::octave_value (unsigned long int i) | |
454 : rep (new octave_scalar (i)) | |
455 { | |
456 } | |
457 | |
4353 | 458 #if defined (HAVE_LONG_LONG_INT) |
459 octave_value::octave_value (long long int i) | |
460 : rep (new octave_scalar (i)) | |
461 { | |
462 } | |
463 #endif | |
464 | |
4355 | 465 #if defined (HAVE_UNSIGNED_LONG_LONG_INT) |
4353 | 466 octave_value::octave_value (unsigned long long int i) |
467 : rep (new octave_scalar (i)) | |
468 { | |
469 } | |
470 #endif | |
471 | |
4254 | 472 octave_value::octave_value (octave_time t) |
7065 | 473 : rep (new octave_scalar (t.double_value ())) |
4254 | 474 { |
475 } | |
476 | |
2376 | 477 octave_value::octave_value (double d) |
2825 | 478 : rep (new octave_scalar (d)) |
479 { | |
480 } | |
2376 | 481 |
4587 | 482 octave_value::octave_value (const Cell& c, bool is_csl) |
5477 | 483 : rep (is_csl |
5759 | 484 ? dynamic_cast<octave_base_value *> (new octave_cs_list (c)) |
485 : dynamic_cast<octave_base_value *> (new octave_cell (c))) | |
3351 | 486 { |
487 } | |
488 | |
5147 | 489 octave_value::octave_value (const ArrayN<octave_value>& a, bool is_csl) |
5477 | 490 : rep (is_csl |
5759 | 491 ? dynamic_cast<octave_base_value *> (new octave_cs_list (Cell (a))) |
492 : dynamic_cast<octave_base_value *> (new octave_cell (Cell (a)))) | |
5147 | 493 { |
494 } | |
495 | |
5785 | 496 octave_value::octave_value (const Matrix& m, const MatrixType& t) |
497 : rep (new octave_matrix (m, t)) | |
2423 | 498 { |
499 maybe_mutate (); | |
500 } | |
2376 | 501 |
4507 | 502 octave_value::octave_value (const NDArray& a) |
4513 | 503 : rep (new octave_matrix (a)) |
4478 | 504 { |
505 maybe_mutate (); | |
506 } | |
507 | |
4911 | 508 octave_value::octave_value (const ArrayN<double>& a) |
509 : rep (new octave_matrix (a)) | |
510 { | |
511 maybe_mutate (); | |
512 } | |
513 | |
2376 | 514 octave_value::octave_value (const DiagMatrix& d) |
2423 | 515 : rep (new octave_matrix (d)) |
516 { | |
517 maybe_mutate (); | |
518 } | |
2376 | 519 |
3418 | 520 octave_value::octave_value (const RowVector& v) |
521 : rep (new octave_matrix (v)) | |
2423 | 522 { |
523 maybe_mutate (); | |
524 } | |
2376 | 525 |
3418 | 526 octave_value::octave_value (const ColumnVector& v) |
527 : rep (new octave_matrix (v)) | |
2423 | 528 { |
529 maybe_mutate (); | |
530 } | |
2376 | 531 |
532 octave_value::octave_value (const Complex& C) | |
2423 | 533 : rep (new octave_complex (C)) |
534 { | |
535 maybe_mutate (); | |
536 } | |
2376 | 537 |
5785 | 538 octave_value::octave_value (const ComplexMatrix& m, const MatrixType& t) |
539 : rep (new octave_complex_matrix (m, t)) | |
2423 | 540 { |
541 maybe_mutate (); | |
542 } | |
2376 | 543 |
4513 | 544 octave_value::octave_value (const ComplexNDArray& a) |
545 : rep (new octave_complex_matrix (a)) | |
4478 | 546 { |
547 maybe_mutate (); | |
548 } | |
549 | |
4911 | 550 octave_value::octave_value (const ArrayN<Complex>& a) |
551 : rep (new octave_complex_matrix (a)) | |
552 { | |
553 maybe_mutate (); | |
554 } | |
555 | |
2376 | 556 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423 | 557 : rep (new octave_complex_matrix (d)) |
558 { | |
559 maybe_mutate (); | |
560 } | |
2376 | 561 |
3418 | 562 octave_value::octave_value (const ComplexRowVector& v) |
563 : rep (new octave_complex_matrix (v)) | |
2423 | 564 { |
565 maybe_mutate (); | |
566 } | |
2376 | 567 |
3418 | 568 octave_value::octave_value (const ComplexColumnVector& v) |
569 : rep (new octave_complex_matrix (v)) | |
2423 | 570 { |
571 maybe_mutate (); | |
572 } | |
2376 | 573 |
2825 | 574 octave_value::octave_value (bool b) |
575 : rep (new octave_bool (b)) | |
576 { | |
577 } | |
578 | |
5785 | 579 octave_value::octave_value (const boolMatrix& bm, const MatrixType& t) |
580 : rep (new octave_bool_matrix (bm, t)) | |
2825 | 581 { |
582 maybe_mutate (); | |
583 } | |
584 | |
4513 | 585 octave_value::octave_value (const boolNDArray& bnda) |
586 : rep (new octave_bool_matrix (bnda)) | |
587 { | |
588 maybe_mutate (); | |
589 } | |
590 | |
7433 | 591 octave_value::octave_value (const ArrayN<bool>& bnda) |
592 : rep (new octave_bool_matrix (bnda)) | |
593 { | |
594 maybe_mutate (); | |
595 } | |
596 | |
5279 | 597 octave_value::octave_value (char c, char type) |
598 : rep (type == '"' | |
599 ? new octave_char_matrix_dq_str (c) | |
600 : new octave_char_matrix_sq_str (c)) | |
3189 | 601 { |
602 maybe_mutate (); | |
603 } | |
604 | |
5279 | 605 octave_value::octave_value (const char *s, char type) |
606 : rep (type == '"' | |
607 ? new octave_char_matrix_dq_str (s) | |
608 : new octave_char_matrix_sq_str (s)) | |
2423 | 609 { |
610 maybe_mutate (); | |
611 } | |
2376 | 612 |
5279 | 613 octave_value::octave_value (const std::string& s, char type) |
614 : rep (type == '"' | |
615 ? new octave_char_matrix_dq_str (s) | |
616 : new octave_char_matrix_sq_str (s)) | |
2423 | 617 { |
618 maybe_mutate (); | |
619 } | |
2376 | 620 |
5279 | 621 octave_value::octave_value (const string_vector& s, char type) |
622 : rep (type == '"' | |
623 ? new octave_char_matrix_dq_str (s) | |
624 : new octave_char_matrix_sq_str (s)) | |
2423 | 625 { |
626 maybe_mutate (); | |
627 } | |
2376 | 628 |
5279 | 629 octave_value::octave_value (const charMatrix& chm, bool is_str, char type) |
4587 | 630 : rep (is_str |
5279 | 631 ? (type == '"' |
632 ? new octave_char_matrix_dq_str (chm) | |
633 : new octave_char_matrix_sq_str (chm)) | |
4513 | 634 : new octave_char_matrix (chm)) |
2409 | 635 { |
4513 | 636 maybe_mutate (); |
637 } | |
2376 | 638 |
5279 | 639 octave_value::octave_value (const charNDArray& chm, bool is_str, char type) |
4587 | 640 : rep (is_str |
5279 | 641 ? (type == '"' |
642 ? new octave_char_matrix_dq_str (chm) | |
643 : new octave_char_matrix_sq_str (chm)) | |
4513 | 644 : new octave_char_matrix (chm)) |
645 { | |
2423 | 646 maybe_mutate (); |
2409 | 647 } |
2376 | 648 |
5279 | 649 octave_value::octave_value (const ArrayN<char>& chm, bool is_str, char type) |
4997 | 650 : rep (is_str |
5279 | 651 ? (type == '"' |
652 ? new octave_char_matrix_dq_str (chm) | |
653 : new octave_char_matrix_sq_str (chm)) | |
4997 | 654 : new octave_char_matrix (chm)) |
655 { | |
656 maybe_mutate (); | |
657 } | |
658 | |
5785 | 659 octave_value::octave_value (const SparseMatrix& m, const MatrixType &t) |
5164 | 660 : rep (new octave_sparse_matrix (m, t)) |
661 { | |
662 maybe_mutate (); | |
663 } | |
664 | |
6863 | 665 octave_value::octave_value (const Sparse<double>& m, const MatrixType &t) |
666 : rep (new octave_sparse_matrix (m, t)) | |
667 { | |
668 maybe_mutate (); | |
669 } | |
670 | |
5785 | 671 octave_value::octave_value (const SparseComplexMatrix& m, const MatrixType &t) |
5164 | 672 : rep (new octave_sparse_complex_matrix (m, t)) |
673 { | |
674 maybe_mutate (); | |
675 } | |
676 | |
6863 | 677 octave_value::octave_value (const Sparse<Complex>& m, const MatrixType &t) |
678 : rep (new octave_sparse_complex_matrix (m, t)) | |
679 { | |
680 maybe_mutate (); | |
681 } | |
682 | |
5785 | 683 octave_value::octave_value (const SparseBoolMatrix& bm, const MatrixType &t) |
5164 | 684 : rep (new octave_sparse_bool_matrix (bm, t)) |
685 { | |
686 maybe_mutate (); | |
687 } | |
688 | |
7433 | 689 octave_value::octave_value (const Sparse<bool>& bm, const MatrixType &t) |
690 : rep (new octave_sparse_bool_matrix (bm, t)) | |
691 { | |
692 maybe_mutate (); | |
693 } | |
694 | |
4901 | 695 octave_value::octave_value (const octave_int8& i) |
696 : rep (new octave_int8_scalar (i)) | |
697 { | |
698 maybe_mutate (); | |
699 } | |
700 | |
701 octave_value::octave_value (const octave_uint8& i) | |
702 : rep (new octave_uint8_scalar (i)) | |
703 { | |
704 maybe_mutate (); | |
705 } | |
706 | |
707 octave_value::octave_value (const octave_int16& i) | |
708 : rep (new octave_int16_scalar (i)) | |
709 { | |
710 maybe_mutate (); | |
711 } | |
712 | |
713 octave_value::octave_value (const octave_uint16& i) | |
714 : rep (new octave_uint16_scalar (i)) | |
715 { | |
716 maybe_mutate (); | |
717 } | |
718 | |
719 octave_value::octave_value (const octave_int32& i) | |
720 : rep (new octave_int32_scalar (i)) | |
721 { | |
722 maybe_mutate (); | |
723 } | |
724 | |
725 octave_value::octave_value (const octave_uint32& i) | |
726 : rep (new octave_uint32_scalar (i)) | |
727 { | |
728 maybe_mutate (); | |
729 } | |
730 | |
731 octave_value::octave_value (const octave_int64& i) | |
732 : rep (new octave_int64_scalar (i)) | |
733 { | |
734 maybe_mutate (); | |
735 } | |
736 | |
737 octave_value::octave_value (const octave_uint64& i) | |
738 : rep (new octave_uint64_scalar (i)) | |
739 { | |
740 maybe_mutate (); | |
741 } | |
742 | |
743 octave_value::octave_value (const int8NDArray& inda) | |
744 : rep (new octave_int8_matrix (inda)) | |
745 { | |
746 maybe_mutate (); | |
747 } | |
748 | |
7064 | 749 octave_value::octave_value (const ArrayN<octave_int8>& inda) |
750 : rep (new octave_int8_matrix (inda)) | |
751 { | |
752 maybe_mutate (); | |
753 } | |
754 | |
4901 | 755 octave_value::octave_value (const uint8NDArray& inda) |
756 : rep (new octave_uint8_matrix (inda)) | |
757 { | |
758 maybe_mutate (); | |
759 } | |
760 | |
7064 | 761 octave_value::octave_value (const ArrayN<octave_uint8>& inda) |
762 : rep (new octave_uint8_matrix (inda)) | |
763 { | |
764 maybe_mutate (); | |
765 } | |
766 | |
4901 | 767 octave_value::octave_value (const int16NDArray& inda) |
768 : rep (new octave_int16_matrix (inda)) | |
769 { | |
770 maybe_mutate (); | |
771 } | |
772 | |
7064 | 773 octave_value::octave_value (const ArrayN<octave_int16>& inda) |
774 : rep (new octave_int16_matrix (inda)) | |
775 { | |
776 maybe_mutate (); | |
777 } | |
778 | |
4901 | 779 octave_value::octave_value (const uint16NDArray& inda) |
780 : rep (new octave_uint16_matrix (inda)) | |
781 { | |
782 maybe_mutate (); | |
783 } | |
784 | |
7064 | 785 octave_value::octave_value (const ArrayN<octave_uint16>& inda) |
786 : rep (new octave_uint16_matrix (inda)) | |
787 { | |
788 maybe_mutate (); | |
789 } | |
790 | |
4901 | 791 octave_value::octave_value (const int32NDArray& inda) |
792 : rep (new octave_int32_matrix (inda)) | |
793 { | |
794 maybe_mutate (); | |
795 } | |
796 | |
7064 | 797 octave_value::octave_value (const ArrayN<octave_int32>& inda) |
798 : rep (new octave_int32_matrix (inda)) | |
799 { | |
800 maybe_mutate (); | |
801 } | |
802 | |
4901 | 803 octave_value::octave_value (const uint32NDArray& inda) |
804 : rep (new octave_uint32_matrix (inda)) | |
805 { | |
806 maybe_mutate (); | |
807 } | |
808 | |
7064 | 809 octave_value::octave_value (const ArrayN<octave_uint32>& inda) |
810 : rep (new octave_uint32_matrix (inda)) | |
811 { | |
812 maybe_mutate (); | |
813 } | |
814 | |
4901 | 815 octave_value::octave_value (const int64NDArray& inda) |
816 : rep (new octave_int64_matrix (inda)) | |
817 { | |
818 maybe_mutate (); | |
819 } | |
820 | |
7064 | 821 octave_value::octave_value (const ArrayN<octave_int64>& inda) |
822 : rep (new octave_int64_matrix (inda)) | |
823 { | |
824 maybe_mutate (); | |
825 } | |
826 | |
4901 | 827 octave_value::octave_value (const uint64NDArray& inda) |
828 : rep (new octave_uint64_matrix (inda)) | |
829 { | |
830 maybe_mutate (); | |
831 } | |
832 | |
7064 | 833 octave_value::octave_value (const ArrayN<octave_uint64>& inda) |
834 : rep (new octave_uint64_matrix (inda)) | |
835 { | |
836 maybe_mutate (); | |
837 } | |
838 | |
2376 | 839 octave_value::octave_value (double base, double limit, double inc) |
2423 | 840 : rep (new octave_range (base, limit, inc)) |
841 { | |
842 maybe_mutate (); | |
843 } | |
2376 | 844 |
845 octave_value::octave_value (const Range& r) | |
2423 | 846 : rep (new octave_range (r)) |
847 { | |
848 maybe_mutate (); | |
849 } | |
2376 | 850 |
851 octave_value::octave_value (const Octave_map& m) | |
2825 | 852 : rep (new octave_struct (m)) |
853 { | |
2880 | 854 } |
855 | |
7336 | 856 octave_value::octave_value (const Octave_map& m, const std::string& id) |
857 : rep (new octave_class (m, id)) | |
858 { | |
859 } | |
860 | |
4643 | 861 octave_value::octave_value (const streamoff_array& off) |
862 : rep (new octave_streamoff (off)) | |
863 { | |
864 } | |
865 | |
7433 | 866 octave_value::octave_value (const ArrayN<std::streamoff>& inda) |
867 : rep (new octave_streamoff (inda)) | |
868 { | |
869 } | |
870 | |
4587 | 871 octave_value::octave_value (const octave_value_list& l, bool is_csl) |
5477 | 872 : rep (is_csl |
5759 | 873 ? dynamic_cast<octave_base_value *> (new octave_cs_list (l)) |
874 : dynamic_cast<octave_base_value *> (new octave_list (l))) | |
2880 | 875 { |
876 } | |
2376 | 877 |
878 octave_value::octave_value (octave_value::magic_colon) | |
2825 | 879 : rep (new octave_magic_colon ()) |
880 { | |
881 } | |
2376 | 882 |
5759 | 883 octave_value::octave_value (octave_base_value *new_rep) |
2825 | 884 : rep (new_rep) |
885 { | |
886 } | |
2376 | 887 |
7336 | 888 octave_value::octave_value (octave_base_value *new_rep, int xcount) |
889 : rep (new_rep) | |
890 { | |
891 rep->count = xcount; | |
892 } | |
893 | |
5759 | 894 octave_base_value * |
3933 | 895 octave_value::clone (void) const |
2880 | 896 { |
897 panic_impossible (); | |
3546 | 898 return 0; |
2880 | 899 } |
900 | |
2409 | 901 void |
902 octave_value::maybe_mutate (void) | |
903 { | |
5759 | 904 octave_base_value *tmp = rep->try_narrowing_conversion (); |
2409 | 905 |
906 if (tmp && tmp != rep) | |
907 { | |
908 if (--rep->count == 0) | |
909 delete rep; | |
910 | |
911 rep = tmp; | |
912 } | |
913 } | |
914 | |
4247 | 915 octave_value |
4271 | 916 octave_value::single_subsref (const std::string& type, |
917 const octave_value_list& idx) | |
4247 | 918 { |
919 std::list<octave_value_list> i; | |
920 | |
921 i.push_back (idx); | |
922 | |
923 return rep->subsref (type, i); | |
924 } | |
925 | |
2974 | 926 octave_value_list |
4247 | 927 octave_value::subsref (const std::string& type, |
4219 | 928 const std::list<octave_value_list>& idx, int nargout) |
3933 | 929 { |
930 if (is_constant ()) | |
931 return rep->subsref (type, idx); | |
932 else | |
933 return rep->subsref (type, idx, nargout); | |
934 } | |
935 | |
936 octave_value | |
4247 | 937 octave_value::next_subsref (const std::string& type, |
4219 | 938 const std::list<octave_value_list>& idx, |
4233 | 939 size_t skip) |
3933 | 940 { |
4582 | 941 if (! error_state && idx.size () > skip) |
3933 | 942 { |
4219 | 943 std::list<octave_value_list> new_idx (idx); |
4233 | 944 for (size_t i = 0; i < skip; i++) |
4219 | 945 new_idx.erase (new_idx.begin ()); |
3933 | 946 return subsref (type.substr (skip), new_idx); |
947 } | |
948 else | |
949 return *this; | |
950 } | |
951 | |
952 octave_value_list | |
4994 | 953 octave_value::next_subsref (int nargout, const std::string& type, |
954 const std::list<octave_value_list>& idx, | |
955 size_t skip) | |
956 { | |
957 if (! error_state && idx.size () > skip) | |
958 { | |
959 std::list<octave_value_list> new_idx (idx); | |
960 for (size_t i = 0; i < skip; i++) | |
961 new_idx.erase (new_idx.begin ()); | |
962 return subsref (type.substr (skip), new_idx, nargout); | |
963 } | |
964 else | |
965 return *this; | |
966 } | |
967 | |
968 octave_value_list | |
3544 | 969 octave_value::do_multi_index_op (int nargout, const octave_value_list& idx) |
2974 | 970 { |
3544 | 971 return rep->do_multi_index_op (nargout, idx); |
2974 | 972 } |
973 | |
3933 | 974 #if 0 |
3204 | 975 static void |
3933 | 976 gripe_assign_failed (const std::string& on, const std::string& tn1, |
977 const std::string& tn2) | |
3204 | 978 { |
979 error ("assignment failed for `%s %s %s'", | |
980 tn1.c_str (), on.c_str (), tn2.c_str ()); | |
981 } | |
3933 | 982 #endif |
3204 | 983 |
984 static void | |
3933 | 985 gripe_assign_failed_or_no_method (const std::string& on, |
986 const std::string& tn1, | |
3523 | 987 const std::string& tn2) |
3204 | 988 { |
989 error ("assignment failed, or no method for `%s %s %s'", | |
990 tn1.c_str (), on.c_str (), tn2.c_str ()); | |
991 } | |
992 | |
3933 | 993 octave_value |
4247 | 994 octave_value::subsasgn (const std::string& type, |
4219 | 995 const std::list<octave_value_list>& idx, |
3933 | 996 const octave_value& rhs) |
997 { | |
998 return rep->subsasgn (type, idx, rhs); | |
999 } | |
1000 | |
1001 octave_value | |
4247 | 1002 octave_value::assign (assign_op op, const std::string& type, |
4219 | 1003 const std::list<octave_value_list>& idx, |
3933 | 1004 const octave_value& rhs) |
1005 { | |
1006 octave_value retval; | |
1007 | |
1008 make_unique (); | |
1009 | |
1010 octave_value t_rhs = rhs; | |
1011 | |
1012 if (op != op_asn_eq) | |
1013 { | |
5775 | 1014 // FIXME -- only do the following stuff if we can't find |
3933 | 1015 // a specific function to call to handle the op= operation for |
1016 // the types we have. | |
1017 | |
5001 | 1018 octave_value t; |
1019 if (is_constant ()) | |
1020 t = subsref (type, idx); | |
1021 else | |
1022 { | |
1023 octave_value_list tl = subsref (type, idx, 1); | |
1024 if (tl.length () > 0) | |
1025 t = tl(0); | |
1026 } | |
3933 | 1027 |
1028 if (! error_state) | |
1029 { | |
1030 binary_op binop = op_eq_to_binary_op (op); | |
1031 | |
1032 if (! error_state) | |
1033 t_rhs = do_binary_op (binop, t, rhs); | |
1034 } | |
1035 } | |
1036 | |
1037 if (! error_state) | |
1038 { | |
7336 | 1039 if (type[0] == '.' && ! (is_map () || is_object ())) |
3933 | 1040 { |
1041 octave_value tmp = Octave_map (); | |
1042 retval = tmp.subsasgn (type, idx, t_rhs); | |
1043 } | |
1044 else | |
1045 retval = subsasgn (type, idx, t_rhs); | |
1046 } | |
1047 | |
1048 if (error_state) | |
1049 gripe_assign_failed_or_no_method (assign_op_as_string (op), | |
1050 type_name (), rhs.type_name ()); | |
1051 | |
1052 return retval; | |
1053 } | |
1054 | |
1055 const octave_value& | |
3203 | 1056 octave_value::assign (assign_op op, const octave_value& rhs) |
2880 | 1057 { |
3533 | 1058 if (op == op_asn_eq) |
3203 | 1059 operator = (rhs); |
1060 else | |
1061 { | |
5775 | 1062 // FIXME -- only do the following stuff if we can't find |
3204 | 1063 // a specific function to call to handle the op= operation for |
1064 // the types we have. | |
1065 | |
1066 binary_op binop = op_eq_to_binary_op (op); | |
1067 | |
1068 if (! error_state) | |
1069 { | |
1070 octave_value t = do_binary_op (binop, *this, rhs); | |
1071 | |
1072 if (! error_state) | |
1073 operator = (t); | |
1074 } | |
1075 | |
1076 if (error_state) | |
1077 gripe_assign_failed_or_no_method (assign_op_as_string (op), | |
1078 type_name (), rhs.type_name ()); | |
1079 } | |
1080 | |
3933 | 1081 return *this; |
2376 | 1082 } |
1083 | |
5275 | 1084 octave_idx_type |
4563 | 1085 octave_value::length (void) const |
1086 { | |
1087 int retval = 0; | |
1088 | |
1089 dim_vector dv = dims (); | |
4584 | 1090 |
4563 | 1091 for (int i = 0; i < dv.length (); i++) |
1092 { | |
1093 if (dv(i) < 0) | |
1094 { | |
1095 retval = -1; | |
1096 break; | |
1097 } | |
1098 | |
4584 | 1099 if (dv(i) == 0) |
1100 { | |
1101 retval = 0; | |
1102 break; | |
1103 } | |
1104 | |
4563 | 1105 if (dv(i) > retval) |
1106 retval = dv(i); | |
1107 } | |
1108 | |
1109 return retval; | |
1110 } | |
1111 | |
5659 | 1112 Matrix |
1113 octave_value::size (void) const | |
1114 { | |
1115 dim_vector dv = dims (); | |
1116 | |
1117 int n_dims = dv.length (); | |
1118 | |
1119 Matrix retval (1, n_dims); | |
1120 | |
1121 while (n_dims--) | |
1122 retval(n_dims) = dv(n_dims); | |
1123 | |
1124 return retval; | |
1125 } | |
1126 | |
3351 | 1127 Cell |
1128 octave_value::cell_value (void) const | |
1129 { | |
1130 return rep->cell_value (); | |
1131 } | |
1132 | |
2376 | 1133 Octave_map |
1134 octave_value::map_value (void) const | |
1135 { | |
1136 return rep->map_value (); | |
1137 } | |
1138 | |
4645 | 1139 std::streamoff |
4643 | 1140 octave_value::streamoff_value (void) const |
1141 { | |
1142 return rep->streamoff_value (); | |
1143 } | |
1144 | |
4645 | 1145 streamoff_array |
1146 octave_value::streamoff_array_value (void) const | |
1147 { | |
1148 return rep->streamoff_array_value (); | |
1149 } | |
1150 | |
2974 | 1151 octave_function * |
1152 octave_value::function_value (bool silent) | |
1153 { | |
1154 return rep->function_value (silent); | |
1155 } | |
1156 | |
4700 | 1157 octave_user_function * |
1158 octave_value::user_function_value (bool silent) | |
1159 { | |
1160 return rep->user_function_value (silent); | |
1161 } | |
1162 | |
4346 | 1163 octave_fcn_handle * |
4343 | 1164 octave_value::fcn_handle_value (bool silent) |
1165 { | |
1166 return rep->fcn_handle_value (silent); | |
1167 } | |
1168 | |
4933 | 1169 octave_fcn_inline * |
1170 octave_value::fcn_inline_value (bool silent) | |
1171 { | |
1172 return rep->fcn_inline_value (silent); | |
1173 } | |
1174 | |
2880 | 1175 octave_value_list |
1176 octave_value::list_value (void) const | |
1177 { | |
1178 return rep->list_value (); | |
1179 } | |
1180 | |
2376 | 1181 ColumnVector |
3419 | 1182 octave_value::column_vector_value (bool force_string_conv, |
4661 | 1183 bool /* frc_vec_conv */) const |
3419 | 1184 { |
1185 ColumnVector retval; | |
1186 | |
1187 Matrix m = matrix_value (force_string_conv); | |
1188 | |
1189 if (error_state) | |
1190 return retval; | |
1191 | |
5275 | 1192 octave_idx_type nr = m.rows (); |
1193 octave_idx_type nc = m.columns (); | |
3419 | 1194 |
1195 if (nc == 1) | |
1196 { | |
1197 retval.resize (nr); | |
5275 | 1198 for (octave_idx_type i = 0; i < nr; i++) |
3419 | 1199 retval (i) = m (i, 0); |
1200 } | |
1201 else | |
1202 { | |
3523 | 1203 std::string tn = type_name (); |
3419 | 1204 gripe_invalid_conversion (tn.c_str (), "real column vector"); |
1205 } | |
1206 | |
1207 return retval; | |
1208 } | |
1209 | |
1210 ComplexColumnVector | |
1211 octave_value::complex_column_vector_value (bool force_string_conv, | |
4661 | 1212 bool /* frc_vec_conv */) const |
3419 | 1213 { |
1214 ComplexColumnVector retval; | |
1215 | |
1216 ComplexMatrix m = complex_matrix_value (force_string_conv); | |
1217 | |
1218 if (error_state) | |
1219 return retval; | |
1220 | |
5275 | 1221 octave_idx_type nr = m.rows (); |
1222 octave_idx_type nc = m.columns (); | |
3419 | 1223 |
1224 if (nc == 1) | |
1225 { | |
3465 | 1226 retval.resize (nr); |
5275 | 1227 for (octave_idx_type i = 0; i < nr; i++) |
3419 | 1228 retval (i) = m (i, 0); |
1229 } | |
1230 else | |
1231 { | |
3523 | 1232 std::string tn = type_name (); |
3419 | 1233 gripe_invalid_conversion (tn.c_str (), "complex column vector"); |
1234 } | |
1235 | |
1236 return retval; | |
1237 } | |
1238 | |
1239 RowVector | |
1240 octave_value::row_vector_value (bool force_string_conv, | |
4661 | 1241 bool /* frc_vec_conv */) const |
3419 | 1242 { |
1243 RowVector retval; | |
1244 | |
1245 Matrix m = matrix_value (force_string_conv); | |
1246 | |
1247 if (error_state) | |
1248 return retval; | |
1249 | |
5275 | 1250 octave_idx_type nr = m.rows (); |
1251 octave_idx_type nc = m.columns (); | |
3419 | 1252 |
1253 if (nr == 1) | |
1254 { | |
1255 retval.resize (nc); | |
5275 | 1256 for (octave_idx_type i = 0; i < nc; i++) |
3419 | 1257 retval (i) = m (0, i); |
1258 } | |
1259 else | |
1260 { | |
3523 | 1261 std::string tn = type_name (); |
3419 | 1262 gripe_invalid_conversion (tn.c_str (), "real row vector"); |
1263 } | |
1264 | |
1265 return retval; | |
1266 } | |
1267 | |
1268 ComplexRowVector | |
1269 octave_value::complex_row_vector_value (bool force_string_conv, | |
4661 | 1270 bool /* frc_vec_conv */) const |
3419 | 1271 { |
1272 ComplexRowVector retval; | |
1273 | |
1274 ComplexMatrix m = complex_matrix_value (force_string_conv); | |
1275 | |
1276 if (error_state) | |
1277 return retval; | |
1278 | |
5275 | 1279 octave_idx_type nr = m.rows (); |
1280 octave_idx_type nc = m.columns (); | |
3419 | 1281 |
1282 if (nr == 1) | |
1283 { | |
1284 retval.resize (nc); | |
5275 | 1285 for (octave_idx_type i = 0; i < nc; i++) |
3419 | 1286 retval (i) = m (0, i); |
1287 } | |
1288 else | |
1289 { | |
3523 | 1290 std::string tn = type_name (); |
3419 | 1291 gripe_invalid_conversion (tn.c_str (), "complex row vector"); |
1292 } | |
1293 | |
1294 return retval; | |
1295 } | |
1296 | |
1297 // Sloppy... | |
1298 | |
1299 Array<double> | |
2376 | 1300 octave_value::vector_value (bool force_string_conv, |
1301 bool force_vector_conversion) const | |
1302 { | |
3419 | 1303 Array<double> retval; |
2376 | 1304 |
1305 Matrix m = matrix_value (force_string_conv); | |
1306 | |
1307 if (error_state) | |
1308 return retval; | |
1309 | |
5275 | 1310 octave_idx_type nr = m.rows (); |
1311 octave_idx_type nc = m.columns (); | |
2376 | 1312 |
1313 if (nr == 1) | |
1314 { | |
1315 retval.resize (nc); | |
5275 | 1316 for (octave_idx_type i = 0; i < nc; i++) |
2376 | 1317 retval (i) = m (0, i); |
1318 } | |
1319 else if (nc == 1) | |
1320 { | |
1321 retval.resize (nr); | |
5275 | 1322 for (octave_idx_type i = 0; i < nr; i++) |
2376 | 1323 retval (i) = m (i, 0); |
1324 } | |
4455 | 1325 else if (nr > 0 && nc > 0) |
2376 | 1326 { |
5781 | 1327 if (! force_vector_conversion) |
1328 gripe_implicit_conversion ("Octave:array-as-vector", | |
1329 type_name (), "real vector"); | |
4455 | 1330 |
2376 | 1331 retval.resize (nr * nc); |
5275 | 1332 octave_idx_type k = 0; |
1333 for (octave_idx_type j = 0; j < nc; j++) | |
1334 for (octave_idx_type i = 0; i < nr; i++) | |
4153 | 1335 { |
1336 OCTAVE_QUIT; | |
1337 | |
1338 retval (k++) = m (i, j); | |
1339 } | |
2376 | 1340 } |
1341 else | |
1342 { | |
3523 | 1343 std::string tn = type_name (); |
2376 | 1344 gripe_invalid_conversion (tn.c_str (), "real vector"); |
1345 } | |
1346 | |
1347 return retval; | |
1348 } | |
1349 | |
4044 | 1350 Array<int> |
1351 octave_value::int_vector_value (bool force_string_conv, bool require_int, | |
1352 bool force_vector_conversion) const | |
1353 { | |
1354 Array<int> retval; | |
1355 | |
1356 Matrix m = matrix_value (force_string_conv); | |
1357 | |
1358 if (error_state) | |
1359 return retval; | |
1360 | |
5275 | 1361 octave_idx_type nr = m.rows (); |
1362 octave_idx_type nc = m.columns (); | |
4044 | 1363 |
1364 if (nr == 1) | |
1365 { | |
1366 retval.resize (nc); | |
5275 | 1367 for (octave_idx_type i = 0; i < nc; i++) |
4044 | 1368 { |
4153 | 1369 OCTAVE_QUIT; |
1370 | |
4044 | 1371 double d = m (0, i); |
1372 | |
1373 if (require_int && D_NINT (d) != d) | |
1374 { | |
1375 error ("conversion to integer value failed"); | |
1376 return retval; | |
1377 } | |
1378 | |
1379 retval (i) = static_cast<int> (d); | |
1380 } | |
1381 } | |
1382 else if (nc == 1) | |
1383 { | |
1384 retval.resize (nr); | |
5275 | 1385 for (octave_idx_type i = 0; i < nr; i++) |
4044 | 1386 { |
4153 | 1387 OCTAVE_QUIT; |
1388 | |
4044 | 1389 double d = m (i, 0); |
1390 | |
1391 if (require_int && D_NINT (d) != d) | |
1392 { | |
1393 error ("conversion to integer value failed"); | |
1394 return retval; | |
1395 } | |
1396 | |
1397 retval (i) = static_cast<int> (d); | |
1398 } | |
1399 } | |
4455 | 1400 else if (nr > 0 && nc > 0) |
4044 | 1401 { |
5781 | 1402 if (! force_vector_conversion) |
1403 gripe_implicit_conversion ("Octave:array-as-vector", | |
1404 type_name (), "real vector"); | |
4455 | 1405 |
4044 | 1406 retval.resize (nr * nc); |
5275 | 1407 octave_idx_type k = 0; |
1408 for (octave_idx_type j = 0; j < nc; j++) | |
4044 | 1409 { |
5275 | 1410 for (octave_idx_type i = 0; i < nr; i++) |
4044 | 1411 { |
4153 | 1412 OCTAVE_QUIT; |
1413 | |
4044 | 1414 double d = m (i, j); |
1415 | |
1416 if (require_int && D_NINT (d) != d) | |
1417 { | |
1418 error ("conversion to integer value failed"); | |
1419 return retval; | |
1420 } | |
1421 | |
1422 retval (k++) = static_cast<int> (d); | |
1423 } | |
1424 } | |
1425 } | |
1426 else | |
1427 { | |
1428 std::string tn = type_name (); | |
1429 gripe_invalid_conversion (tn.c_str (), "real vector"); | |
1430 } | |
1431 | |
1432 return retval; | |
1433 } | |
1434 | |
3419 | 1435 Array<Complex> |
2376 | 1436 octave_value::complex_vector_value (bool force_string_conv, |
1437 bool force_vector_conversion) const | |
1438 { | |
3419 | 1439 Array<Complex> retval; |
2376 | 1440 |
1441 ComplexMatrix m = complex_matrix_value (force_string_conv); | |
1442 | |
1443 if (error_state) | |
1444 return retval; | |
1445 | |
5275 | 1446 octave_idx_type nr = m.rows (); |
1447 octave_idx_type nc = m.columns (); | |
2376 | 1448 |
1449 if (nr == 1) | |
1450 { | |
1451 retval.resize (nc); | |
5275 | 1452 for (octave_idx_type i = 0; i < nc; i++) |
4153 | 1453 { |
1454 OCTAVE_QUIT; | |
1455 retval (i) = m (0, i); | |
1456 } | |
2376 | 1457 } |
1458 else if (nc == 1) | |
1459 { | |
1460 retval.resize (nr); | |
5275 | 1461 for (octave_idx_type i = 0; i < nr; i++) |
4153 | 1462 { |
1463 OCTAVE_QUIT; | |
1464 retval (i) = m (i, 0); | |
1465 } | |
2376 | 1466 } |
4455 | 1467 else if (nr > 0 && nc > 0) |
2376 | 1468 { |
5781 | 1469 if (! force_vector_conversion) |
1470 gripe_implicit_conversion ("Octave:array-as-vector", | |
1471 type_name (), "complex vector"); | |
4455 | 1472 |
2376 | 1473 retval.resize (nr * nc); |
5275 | 1474 octave_idx_type k = 0; |
1475 for (octave_idx_type j = 0; j < nc; j++) | |
1476 for (octave_idx_type i = 0; i < nr; i++) | |
4153 | 1477 { |
1478 OCTAVE_QUIT; | |
1479 retval (k++) = m (i, j); | |
1480 } | |
2376 | 1481 } |
1482 else | |
1483 { | |
3523 | 1484 std::string tn = type_name (); |
2376 | 1485 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
1486 } | |
1487 | |
1488 return retval; | |
1489 } | |
1490 | |
4944 | 1491 int |
1492 octave_value::write (octave_stream& os, int block_size, | |
1493 oct_data_conv::data_type output_type, int skip, | |
1494 oct_mach_info::float_format flt_fmt) const | |
1495 { | |
1496 return rep->write (os, block_size, output_type, skip, flt_fmt); | |
1497 } | |
1498 | |
2413 | 1499 static void |
3933 | 1500 gripe_binary_op (const std::string& on, const std::string& tn1, |
1501 const std::string& tn2) | |
2376 | 1502 { |
2903 | 1503 error ("binary operator `%s' not implemented for `%s' by `%s' operations", |
2376 | 1504 on.c_str (), tn1.c_str (), tn2.c_str ()); |
1505 } | |
1506 | |
3203 | 1507 static void |
3523 | 1508 gripe_binary_op_conv (const std::string& on) |
3203 | 1509 { |
1510 error ("type conversion failed for binary operator `%s'", on.c_str ()); | |
1511 } | |
1512 | |
2376 | 1513 octave_value |
3933 | 1514 do_binary_op (octave_value::binary_op op, |
1515 const octave_value& v1, const octave_value& v2) | |
2376 | 1516 { |
1517 octave_value retval; | |
1518 | |
1519 int t1 = v1.type_id (); | |
1520 int t2 = v2.type_id (); | |
1521 | |
7336 | 1522 if (t1 == octave_class::static_type_id () |
1523 || t2 == octave_class::static_type_id ()) | |
1524 { | |
1525 octave_value_typeinfo::binary_class_op_fcn f | |
1526 = octave_value_typeinfo::lookup_binary_class_op (op); | |
2376 | 1527 |
7336 | 1528 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1529 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1530 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1531 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1532 retval = f (v1, v2); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1533 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1534 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1535 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1536 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1537 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1538 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1539 } |
7336 | 1540 else |
1541 gripe_binary_op (octave_value::binary_op_as_string (op), | |
1542 v1.class_name (), v2.class_name ()); | |
1543 } | |
2376 | 1544 else |
1545 { | |
7336 | 1546 // FIXME -- we need to handle overloading operators for built-in |
1547 // classes (double, char, int8, etc.) | |
2376 | 1548 |
7336 | 1549 octave_value_typeinfo::binary_op_fcn f |
1550 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); | |
1551 | |
1552 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1553 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1554 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1555 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1556 retval = f (*v1.rep, *v2.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1557 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1558 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1559 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1560 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1561 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1562 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1563 } |
7336 | 1564 else |
2376 | 1565 { |
7336 | 1566 octave_value tv1; |
1567 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); | |
1568 | |
1569 if (cf1) | |
1570 { | |
1571 octave_base_value *tmp = cf1 (*v1.rep); | |
3203 | 1572 |
7336 | 1573 if (tmp) |
1574 { | |
1575 tv1 = octave_value (tmp); | |
1576 t1 = tv1.type_id (); | |
1577 } | |
1578 else | |
1579 { | |
1580 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); | |
1581 return retval; | |
1582 } | |
3203 | 1583 } |
1584 else | |
7336 | 1585 tv1 = v1; |
1586 | |
1587 octave_value tv2; | |
1588 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); | |
1589 | |
1590 if (cf2) | |
3203 | 1591 { |
7336 | 1592 octave_base_value *tmp = cf2 (*v2.rep); |
2376 | 1593 |
7336 | 1594 if (tmp) |
1595 { | |
1596 tv2 = octave_value (tmp); | |
1597 t2 = tv2.type_id (); | |
1598 } | |
1599 else | |
1600 { | |
1601 gripe_binary_op_conv (octave_value::binary_op_as_string (op)); | |
1602 return retval; | |
1603 } | |
3203 | 1604 } |
1605 else | |
7336 | 1606 tv2 = v2; |
1607 | |
1608 if (cf1 || cf2) | |
3203 | 1609 { |
7336 | 1610 f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
1611 | |
1612 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1613 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1614 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1615 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1616 retval = f (*tv1.rep, *tv2.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1617 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1618 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1619 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1620 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1621 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1622 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1623 } |
7336 | 1624 else |
1625 gripe_binary_op (octave_value::binary_op_as_string (op), | |
1626 v1.type_name (), v2.type_name ()); | |
3203 | 1627 } |
2376 | 1628 else |
1629 gripe_binary_op (octave_value::binary_op_as_string (op), | |
1630 v1.type_name (), v2.type_name ()); | |
1631 } | |
1632 } | |
1633 | |
1634 return retval; | |
1635 } | |
1636 | |
4915 | 1637 static void |
1638 gripe_cat_op (const std::string& tn1, const std::string& tn2) | |
1639 { | |
1640 error ("concatenation operator not implemented for `%s' by `%s' operations", | |
1641 tn1.c_str (), tn2.c_str ()); | |
1642 } | |
1643 | |
1644 static void | |
1645 gripe_cat_op_conv (void) | |
1646 { | |
1647 error ("type conversion failed for concatenation operator"); | |
1648 } | |
1649 | |
1650 octave_value | |
1651 do_cat_op (const octave_value& v1, const octave_value& v2, | |
6867 | 1652 const Array<octave_idx_type>& ra_idx) |
4915 | 1653 { |
1654 octave_value retval; | |
1655 | |
5164 | 1656 // Rapid return for concatenation with an empty object. Dimension |
1657 // checking handled elsewhere. | |
1658 if (v1.all_zero_dims ()) | |
1659 return v2; | |
1660 if (v2.all_zero_dims ()) | |
1661 return v1; | |
1662 | |
4915 | 1663 int t1 = v1.type_id (); |
1664 int t2 = v2.type_id (); | |
1665 | |
5759 | 1666 octave_value_typeinfo::cat_op_fcn f |
1667 = octave_value_typeinfo::lookup_cat_op (t1, t2); | |
4915 | 1668 |
1669 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1670 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1671 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1672 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1673 retval = f (*v1.rep, *v2.rep, ra_idx); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1674 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1675 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1676 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1677 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1678 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1679 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1680 } |
4915 | 1681 else |
1682 { | |
1683 octave_value tv1; | |
5759 | 1684 octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); |
4915 | 1685 |
1686 if (cf1) | |
1687 { | |
5759 | 1688 octave_base_value *tmp = cf1 (*v1.rep); |
4915 | 1689 |
1690 if (tmp) | |
1691 { | |
1692 tv1 = octave_value (tmp); | |
1693 t1 = tv1.type_id (); | |
1694 } | |
1695 else | |
1696 { | |
1697 gripe_cat_op_conv (); | |
1698 return retval; | |
1699 } | |
1700 } | |
1701 else | |
1702 tv1 = v1; | |
1703 | |
1704 octave_value tv2; | |
5759 | 1705 octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); |
4915 | 1706 |
1707 if (cf2) | |
1708 { | |
5759 | 1709 octave_base_value *tmp = cf2 (*v2.rep); |
4915 | 1710 |
1711 if (tmp) | |
1712 { | |
1713 tv2 = octave_value (tmp); | |
1714 t2 = tv2.type_id (); | |
1715 } | |
1716 else | |
1717 { | |
1718 gripe_cat_op_conv (); | |
1719 return retval; | |
1720 } | |
1721 } | |
1722 else | |
1723 tv2 = v2; | |
1724 | |
1725 if (cf1 || cf2) | |
1726 { | |
1727 f = octave_value_typeinfo::lookup_cat_op (t1, t2); | |
1728 | |
1729 if (f) | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1730 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1731 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1732 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1733 retval = f (*tv1.rep, *tv2.rep, ra_idx); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1734 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1735 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1736 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1737 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1738 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1739 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1740 } |
4915 | 1741 else |
1742 gripe_cat_op (v1.type_name (), v2.type_name ()); | |
1743 } | |
1744 else | |
1745 gripe_cat_op (v1.type_name (), v2.type_name ()); | |
1746 } | |
1747 | |
1748 return retval; | |
1749 } | |
1750 | |
3933 | 1751 void |
1752 octave_value::print_info (std::ostream& os, const std::string& prefix) const | |
1753 { | |
1754 os << prefix << "type_name: " << type_name () << "\n" | |
1755 << prefix << "count: " << get_count () << "\n" | |
1756 << prefix << "rep info: "; | |
1757 | |
1758 rep->print_info (os, prefix + " "); | |
1759 } | |
1760 | |
3203 | 1761 static void |
3523 | 1762 gripe_unary_op (const std::string& on, const std::string& tn) |
3203 | 1763 { |
1764 error ("unary operator `%s' not implemented for `%s' operands", | |
1765 on.c_str (), tn.c_str ()); | |
1766 } | |
1767 | |
1768 static void | |
3523 | 1769 gripe_unary_op_conv (const std::string& on) |
3203 | 1770 { |
1771 error ("type conversion failed for unary operator `%s'", on.c_str ()); | |
1772 } | |
1773 | |
1774 octave_value | |
1775 do_unary_op (octave_value::unary_op op, const octave_value& v) | |
1776 { | |
1777 octave_value retval; | |
1778 | |
1779 int t = v.type_id (); | |
1780 | |
7336 | 1781 if (t == octave_class::static_type_id ()) |
1782 { | |
1783 octave_value_typeinfo::unary_class_op_fcn f | |
1784 = octave_value_typeinfo::lookup_unary_class_op (op); | |
3203 | 1785 |
7336 | 1786 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1787 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1788 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1789 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1790 retval = f (v); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1791 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1792 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1793 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1794 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1795 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1796 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1797 } |
7336 | 1798 else |
1799 gripe_unary_op (octave_value::unary_op_as_string (op), | |
1800 v.class_name ()); | |
1801 } | |
3203 | 1802 else |
1803 { | |
7336 | 1804 // FIXME -- we need to handle overloading operators for built-in |
1805 // classes (double, char, int8, etc.) | |
1806 | |
1807 octave_value_typeinfo::unary_op_fcn f | |
1808 = octave_value_typeinfo::lookup_unary_op (op, t); | |
3203 | 1809 |
7336 | 1810 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1811 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1812 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1813 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1814 retval = f (*v.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1815 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1816 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1817 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1818 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1819 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1820 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1821 } |
7336 | 1822 else |
3203 | 1823 { |
7336 | 1824 octave_value tv; |
1825 octave_base_value::type_conv_fcn cf | |
1826 = v.numeric_conversion_function (); | |
3203 | 1827 |
7336 | 1828 if (cf) |
3203 | 1829 { |
7336 | 1830 octave_base_value *tmp = cf (*v.rep); |
3203 | 1831 |
7336 | 1832 if (tmp) |
1833 { | |
1834 tv = octave_value (tmp); | |
1835 t = tv.type_id (); | |
1836 | |
1837 f = octave_value_typeinfo::lookup_unary_op (op, t); | |
3203 | 1838 |
7336 | 1839 if (f) |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1840 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1841 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1842 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1843 retval = f (*tv.rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1844 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1845 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1846 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1847 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1848 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1849 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1850 } |
7336 | 1851 else |
1852 gripe_unary_op (octave_value::unary_op_as_string (op), | |
1853 v.type_name ()); | |
1854 } | |
3203 | 1855 else |
7336 | 1856 gripe_unary_op_conv (octave_value::unary_op_as_string (op)); |
3203 | 1857 } |
1858 else | |
7336 | 1859 gripe_unary_op (octave_value::unary_op_as_string (op), |
1860 v.type_name ()); | |
3203 | 1861 } |
1862 } | |
1863 | |
1864 return retval; | |
1865 } | |
1866 | |
1867 static void | |
3933 | 1868 gripe_unary_op_conversion_failed (const std::string& op, |
1869 const std::string& tn) | |
3203 | 1870 { |
1871 error ("operator %s: type conversion for `%s' failed", | |
1872 op.c_str (), tn.c_str ()); | |
1873 } | |
1874 | |
3933 | 1875 const octave_value& |
1876 octave_value::do_non_const_unary_op (unary_op op) | |
3203 | 1877 { |
1878 octave_value retval; | |
1879 | |
1880 int t = type_id (); | |
1881 | |
5759 | 1882 octave_value_typeinfo::non_const_unary_op_fcn f |
3203 | 1883 = octave_value_typeinfo::lookup_non_const_unary_op (op, t); |
1884 | |
1885 if (f) | |
1886 { | |
1887 make_unique (); | |
1888 | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1889 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1890 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1891 f (*rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1892 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1893 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1894 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1895 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1896 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1897 } |
3203 | 1898 } |
1899 else | |
1900 { | |
5759 | 1901 octave_base_value::type_conv_fcn cf = numeric_conversion_function (); |
3203 | 1902 |
1903 if (cf) | |
1904 { | |
5759 | 1905 octave_base_value *tmp = cf (*rep); |
3203 | 1906 |
1907 if (tmp) | |
1908 { | |
5759 | 1909 octave_base_value *old_rep = rep; |
3203 | 1910 rep = tmp; |
1911 | |
1912 t = type_id (); | |
1913 | |
1914 f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); | |
1915 | |
1916 if (f) | |
1917 { | |
7487
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1918 try |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1919 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1920 f (*rep); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1921 } |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1922 catch (octave_execution_exception) |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1923 { |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1924 octave_exception_state = octave_no_exception; |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1925 error ("caught execution error in library function"); |
1e01db14700b
catch octave_execution_exception for built-in and mex functions
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
1926 } |
3203 | 1927 |
1928 if (old_rep && --old_rep->count == 0) | |
1929 delete old_rep; | |
1930 } | |
1931 else | |
1932 { | |
1933 if (old_rep) | |
1934 { | |
1935 if (--rep->count == 0) | |
1936 delete rep; | |
1937 | |
1938 rep = old_rep; | |
1939 } | |
1940 | |
1941 gripe_unary_op (octave_value::unary_op_as_string (op), | |
1942 type_name ()); | |
1943 } | |
1944 } | |
1945 else | |
1946 gripe_unary_op_conversion_failed | |
1947 (octave_value::unary_op_as_string (op), type_name ()); | |
1948 } | |
1949 else | |
1950 gripe_unary_op (octave_value::unary_op_as_string (op), type_name ()); | |
1951 } | |
3933 | 1952 |
1953 return *this; | |
3203 | 1954 } |
1955 | |
3933 | 1956 #if 0 |
3205 | 1957 static void |
3933 | 1958 gripe_unary_op_failed_or_no_method (const std::string& on, |
1959 const std::string& tn) | |
3205 | 1960 { |
1961 error ("operator %s: no method, or unable to evaluate for %s operand", | |
1962 on.c_str (), tn.c_str ()); | |
1963 } | |
3933 | 1964 #endif |
3205 | 1965 |
1966 void | |
4661 | 1967 octave_value::do_non_const_unary_op (unary_op, const octave_value_list&) |
3205 | 1968 { |
3933 | 1969 abort (); |
1970 } | |
1971 | |
1972 octave_value | |
4247 | 1973 octave_value::do_non_const_unary_op (unary_op op, const std::string& type, |
4219 | 1974 const std::list<octave_value_list>& idx) |
3933 | 1975 { |
1976 octave_value retval; | |
1977 | |
1978 if (idx.empty ()) | |
1979 { | |
1980 do_non_const_unary_op (op); | |
3205 | 1981 |
3933 | 1982 retval = *this; |
1983 } | |
1984 else | |
1985 { | |
5775 | 1986 // FIXME -- only do the following stuff if we can't find a |
3933 | 1987 // specific function to call to handle the op= operation for the |
1988 // types we have. | |
3205 | 1989 |
3933 | 1990 assign_op assop = unary_op_to_assign_op (op); |
1991 | |
1992 retval = assign (assop, type, idx, 1.0); | |
1993 } | |
1994 | |
1995 return retval; | |
3205 | 1996 } |
1997 | |
1998 octave_value::assign_op | |
1999 octave_value::unary_op_to_assign_op (unary_op op) | |
2000 { | |
2001 assign_op binop = unknown_assign_op; | |
2002 | |
2003 switch (op) | |
2004 { | |
3533 | 2005 case op_incr: |
2006 binop = op_add_eq; | |
3205 | 2007 break; |
2008 | |
3533 | 2009 case op_decr: |
2010 binop = op_sub_eq; | |
3205 | 2011 break; |
2012 | |
2013 default: | |
2014 { | |
3523 | 2015 std::string on = unary_op_as_string (op); |
3205 | 2016 error ("operator %s: no assign operator found", on.c_str ()); |
2017 } | |
2018 } | |
2019 | |
2020 return binop; | |
2021 } | |
2022 | |
3204 | 2023 octave_value::binary_op |
2024 octave_value::op_eq_to_binary_op (assign_op op) | |
2025 { | |
2026 binary_op binop = unknown_binary_op; | |
2027 | |
2028 switch (op) | |
2029 { | |
3533 | 2030 case op_add_eq: |
2031 binop = op_add; | |
3204 | 2032 break; |
2033 | |
3533 | 2034 case op_sub_eq: |
2035 binop = op_sub; | |
3204 | 2036 break; |
2037 | |
3533 | 2038 case op_mul_eq: |
2039 binop = op_mul; | |
3204 | 2040 break; |
2041 | |
3533 | 2042 case op_div_eq: |
2043 binop = op_div; | |
3204 | 2044 break; |
2045 | |
3533 | 2046 case op_ldiv_eq: |
2047 binop = op_ldiv; | |
3204 | 2048 break; |
2049 | |
4018 | 2050 case op_pow_eq: |
2051 binop = op_pow; | |
2052 break; | |
2053 | |
3533 | 2054 case op_lshift_eq: |
2055 binop = op_lshift; | |
3204 | 2056 break; |
2057 | |
3533 | 2058 case op_rshift_eq: |
2059 binop = op_rshift; | |
3204 | 2060 break; |
2061 | |
3533 | 2062 case op_el_mul_eq: |
2063 binop = op_el_mul; | |
3204 | 2064 break; |
2065 | |
3533 | 2066 case op_el_div_eq: |
2067 binop = op_el_div; | |
3204 | 2068 break; |
2069 | |
3533 | 2070 case op_el_ldiv_eq: |
2071 binop = op_el_ldiv; | |
3204 | 2072 break; |
2073 | |
4018 | 2074 case op_el_pow_eq: |
2075 binop = op_el_pow; | |
2076 break; | |
2077 | |
3533 | 2078 case op_el_and_eq: |
2079 binop = op_el_and; | |
3204 | 2080 break; |
2081 | |
3533 | 2082 case op_el_or_eq: |
2083 binop = op_el_or; | |
3204 | 2084 break; |
2085 | |
2086 default: | |
2087 { | |
3523 | 2088 std::string on = assign_op_as_string (op); |
3204 | 2089 error ("operator %s: no binary operator found", on.c_str ()); |
2090 } | |
2091 } | |
2092 | |
2093 return binop; | |
2094 } | |
2095 | |
3933 | 2096 octave_value |
2097 octave_value::empty_conv (const std::string& type, const octave_value& rhs) | |
2098 { | |
2099 octave_value retval; | |
2100 | |
2101 if (type.length () > 0) | |
2102 { | |
2103 switch (type[0]) | |
2104 { | |
2105 case '(': | |
2106 { | |
2107 if (type.length () > 1 && type[1] == '.') | |
2108 retval = Octave_map (); | |
2109 else | |
2110 retval = octave_value (rhs.empty_clone ()); | |
2111 } | |
2112 break; | |
2113 | |
2114 case '{': | |
2115 retval = Cell (); | |
2116 break; | |
2117 | |
2118 case '.': | |
2119 retval = Octave_map (); | |
2120 break; | |
2121 | |
2122 default: | |
2123 panic_impossible (); | |
2124 } | |
2125 } | |
2126 else | |
2127 retval = octave_value (rhs.empty_clone ()); | |
2128 | |
2129 return retval; | |
2130 } | |
2131 | |
2376 | 2132 void |
2133 install_types (void) | |
2134 { | |
2135 octave_base_value::register_type (); | |
3928 | 2136 octave_cell::register_type (); |
2376 | 2137 octave_scalar::register_type (); |
2138 octave_complex::register_type (); | |
2139 octave_matrix::register_type (); | |
2140 octave_complex_matrix::register_type (); | |
2141 octave_range::register_type (); | |
2825 | 2142 octave_bool::register_type (); |
2143 octave_bool_matrix::register_type (); | |
2376 | 2144 octave_char_matrix::register_type (); |
2145 octave_char_matrix_str::register_type (); | |
5279 | 2146 octave_char_matrix_sq_str::register_type (); |
4901 | 2147 octave_int8_scalar::register_type (); |
2148 octave_int16_scalar::register_type (); | |
2149 octave_int32_scalar::register_type (); | |
2150 octave_int64_scalar::register_type (); | |
2151 octave_uint8_scalar::register_type (); | |
2152 octave_uint16_scalar::register_type (); | |
2153 octave_uint32_scalar::register_type (); | |
2154 octave_uint64_scalar::register_type (); | |
2155 octave_int8_matrix::register_type (); | |
2156 octave_int16_matrix::register_type (); | |
2157 octave_int32_matrix::register_type (); | |
2158 octave_int64_matrix::register_type (); | |
2159 octave_uint8_matrix::register_type (); | |
2160 octave_uint16_matrix::register_type (); | |
2161 octave_uint32_matrix::register_type (); | |
2162 octave_uint64_matrix::register_type (); | |
5164 | 2163 octave_sparse_bool_matrix::register_type (); |
2164 octave_sparse_matrix::register_type (); | |
2165 octave_sparse_complex_matrix::register_type (); | |
2376 | 2166 octave_struct::register_type (); |
7336 | 2167 octave_class::register_type (); |
2880 | 2168 octave_list::register_type (); |
3977 | 2169 octave_cs_list::register_type (); |
2376 | 2170 octave_magic_colon::register_type (); |
2974 | 2171 octave_builtin::register_type (); |
2172 octave_mapper::register_type (); | |
2173 octave_user_function::register_type (); | |
4649 | 2174 octave_dld_function::register_type (); |
4643 | 2175 octave_fcn_handle::register_type (); |
4966 | 2176 octave_fcn_inline::register_type (); |
4643 | 2177 octave_streamoff::register_type (); |
2376 | 2178 } |
2179 | |
4970 | 2180 #if 0 |
2181 DEFUN (cast, args, , | |
2182 "-*- texinfo -*-\n\ | |
2183 @deftypefn {Built-in Function} {} cast (@var{val}, @var{type})\n\ | |
2184 Convert @var{val} to the new data type @var{type}.\n\ | |
5642 | 2185 @seealso{class, typeinfo}\n\ |
2186 @end deftypefn") | |
4970 | 2187 { |
2188 octave_value retval; | |
2189 | |
2190 if (args.length () == 2) | |
2191 error ("cast: not implemented"); | |
2192 else | |
5823 | 2193 print_usage (); |
4970 | 2194 |
2195 return retval; | |
2196 } | |
2197 #endif | |
2198 | |
4791 | 2199 DEFUN (sizeof, args, , |
2200 "-*- texinfo -*-\n\ | |
2201 @deftypefn {Built-in Function} {} sizeof (@var{val})\n\ | |
2202 Return the size of @var{val} in bytes\n\ | |
2203 @end deftypefn") | |
2204 { | |
2205 octave_value retval; | |
2206 | |
2207 if (args.length () == 1) | |
2208 retval = args(0).byte_size (); | |
2209 else | |
5823 | 2210 print_usage (); |
4791 | 2211 |
2212 return retval; | |
2213 } | |
2214 | |
6153 | 2215 static void |
2216 decode_subscripts (const char* name, const octave_value& arg, | |
2217 std::string& type_string, | |
2218 std::list<octave_value_list>& idx) | |
2219 { | |
2220 Octave_map m = arg.map_value (); | |
2221 | |
2222 if (! error_state | |
6639 | 2223 && m.nfields () == 2 && m.contains ("type") && m.contains ("subs")) |
6153 | 2224 { |
2225 Cell& type = m.contents ("type"); | |
2226 Cell& subs = m.contents ("subs"); | |
2227 | |
2228 type_string = std::string (type.length(), '\0'); | |
2229 | |
2230 for (int k = 0; k < type.length (); k++) | |
2231 { | |
2232 std::string item = type(k).string_value (); | |
2233 | |
2234 if (! error_state) | |
2235 { | |
2236 if (item == "{}") | |
2237 type_string[k] = '{'; | |
2238 else if (item == "()") | |
2239 type_string[k] = '('; | |
2240 else if (item == ".") | |
2241 type_string[k] = '.'; | |
2242 else | |
2243 { | |
2244 error("%s: invalid indexing type `%s'", name, item.c_str ()); | |
2245 return; | |
2246 } | |
2247 } | |
2248 else | |
2249 { | |
2250 error ("%s: expecting type(%d) to be a character string", | |
2251 name, k+1); | |
2252 return; | |
2253 } | |
2254 | |
2255 octave_value_list idx_item; | |
2256 | |
2257 if (subs(k).is_string ()) | |
2258 idx_item(0) = subs(k); | |
2259 else if (subs(k).is_cell ()) | |
2260 { | |
2261 Cell subs_cell = subs(k).cell_value (); | |
2262 | |
2263 for (int n = 0; n < subs_cell.length (); n++) | |
2264 { | |
2265 if (subs_cell(n).is_string () | |
2266 && subs_cell(n).string_value () == ":") | |
2267 idx_item(n) = octave_value(octave_value::magic_colon_t); | |
2268 else | |
2269 idx_item(n) = subs_cell(n); | |
2270 } | |
2271 } | |
2272 else | |
2273 { | |
2274 error ("%s: expecting subs(%d) to be a character string or cell array", | |
2275 name, k+1); | |
2276 return; | |
2277 } | |
2278 | |
2279 idx.push_back (idx_item); | |
2280 } | |
2281 } | |
2282 else | |
2283 error ("%s: second argument must be a structure with fields `type' and `subs'", name); | |
2284 } | |
2285 | |
2286 DEFUN (subsref, args, nargout, | |
2287 "-*- texinfo -*-\n\ | |
2288 @deftypefn {Built-in Function} {} subsref (@var{val}, @var{idx})\n\ | |
2289 Perform the subscripted element selection operation according to\n\ | |
2290 the subscript specified by @var{idx}.\n\ | |
2291 \n\ | |
2292 The subscript @var{idx} is expected to be a structure array with\n\ | |
2293 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\ | |
6550 | 2294 are @samp{\"()\"}, @samp{\"@{@}\"}, and @samp{\".\"}.\n\ |
6153 | 2295 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\ |
2296 of index values.\n\ | |
6631 | 2297 \n\ |
2298 The following example shows how to extract the two first columns of\n\ | |
2299 a matrix\n\ | |
2300 \n\ | |
2301 @example\n\ | |
2302 val = magic(3)\n\ | |
2303 @result{} val = [ 8 1 6\n\ | |
2304 3 5 7\n\ | |
2305 4 9 2 ]\n\ | |
2306 idx.type = \"()\";\n\ | |
2307 idx.subs = @{\":\", 1:2@};\n\ | |
2308 subsref(val, idx)\n\ | |
2309 @result{} [ 8 1 \n\ | |
2310 3 5 \n\ | |
2311 4 9 ]\n\ | |
2312 @end example\n\ | |
2313 \n\ | |
2314 @noindent\n\ | |
2315 Note that this is the same as writing @code{val(:,1:2)}.\n\ | |
6157 | 2316 @seealso{subsasgn, substruct}\n\ |
6153 | 2317 @end deftypefn") |
2318 { | |
2319 octave_value_list retval; | |
2320 | |
2321 if (args.length () == 2) | |
2322 { | |
2323 std::string type; | |
2324 std::list<octave_value_list> idx; | |
2325 | |
2326 decode_subscripts ("subsref", args(1), type, idx); | |
2327 | |
2328 if (! error_state) | |
2329 retval = args(0).subsref (type, idx, nargout); | |
2330 } | |
2331 else | |
2332 print_usage (); | |
2333 | |
2334 return retval; | |
2335 } | |
2336 | |
2337 DEFUN (subsasgn, args, , | |
2338 "-*- texinfo -*-\n\ | |
2339 @deftypefn {Built-in Function} {} subsasgn (@var{val}, @var{idx}, @var{rhs})\n\ | |
2340 Perform the subscripted assignment operation according to\n\ | |
2341 the subscript specified by @var{idx}.\n\ | |
2342 \n\ | |
2343 The subscript @var{idx} is expected to be a structure array with\n\ | |
2344 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\ | |
6248 | 2345 are @samp{\"()\"}, @samp{\"@{@}\"}, and @samp{\".\"}.\n\ |
6153 | 2346 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\ |
2347 of index values.\n\ | |
6631 | 2348 \n\ |
2349 The following example shows how to set the two first columns of a\n\ | |
2350 3-by-3 matrix to zero.\n\ | |
2351 \n\ | |
2352 @example\n\ | |
2353 val = magic(3);\n\ | |
2354 idx.type = \"()\";\n\ | |
2355 idx.subs = @{\":\", 1:2@};\n\ | |
2356 subsasgn (val, idx, 0)\n\ | |
2357 @result{} [ 0 0 6\n\ | |
2358 0 0 7\n\ | |
2359 0 0 2 ]\n\ | |
2360 @end example\n\ | |
2361 \n\ | |
2362 Note that this is the same as writing @code{val(:,1:2) = 0}.\n\ | |
6157 | 2363 @seealso{subsref, substruct}\n\ |
6153 | 2364 @end deftypefn") |
2365 { | |
2366 octave_value retval; | |
2367 | |
2368 if (args.length () == 3) | |
2369 { | |
2370 std::string type; | |
2371 std::list<octave_value_list> idx; | |
2372 | |
2373 decode_subscripts ("subsasgn", args(1), type, idx); | |
2374 | |
2375 if (! error_state) | |
2376 retval = args(0).subsasgn (type, idx, args(2)); | |
2377 } | |
2378 else | |
2379 print_usage (); | |
2380 | |
2381 return retval; | |
2382 } | |
2383 | |
2376 | 2384 /* |
2385 ;;; Local Variables: *** | |
2386 ;;; mode: C++ *** | |
2387 ;;; End: *** | |
2388 */ |