Mercurial > hg > octave-lyh
annotate src/ov-class.cc @ 7960:22a18f206121
octave_class::subsasgn: only do internal magic if RHS is not an octave_class object
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 21 Jul 2008 17:55:29 -0400 |
parents | 1e1e2608da7b |
children | 5bf4e2c13ed8 |
rev | line source |
---|---|
7338 | 1 /* |
2 | |
3 Copyright (C) 2007 John W. Eaton | |
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 | |
7444 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
7338 | 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 | |
7444 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
7338 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <iostream> | |
28 | |
29 #include "Array-util.h" | |
30 #include "byte-swap.h" | |
31 | |
32 #include "Cell.h" | |
33 #include "defun.h" | |
34 #include "error.h" | |
35 #include "gripes.h" | |
36 #include "load-path.h" | |
37 #include "ls-hdf5.h" | |
38 #include "ls-oct-ascii.h" | |
39 #include "ls-oct-binary.h" | |
40 #include "ls-utils.h" | |
41 #include "oct-lvalue.h" | |
42 #include "ov-class.h" | |
43 #include "ov-fcn.h" | |
44 #include "pager.h" | |
45 #include "parse.h" | |
46 #include "pr-output.h" | |
47 #include "toplev.h" | |
48 #include "unwind-prot.h" | |
49 #include "variables.h" | |
50 | |
51 DEFINE_OCTAVE_ALLOCATOR(octave_class); | |
52 | |
53 int octave_class::t_id (-1); | |
54 | |
55 const std::string octave_class::t_name ("class"); | |
56 | |
57 void | |
58 octave_class::register_type (void) | |
59 { | |
60 t_id = octave_value_typeinfo::register_type | |
61 (octave_class::t_name, "<unknown>", octave_value (new octave_class ())); | |
62 } | |
63 | |
64 Cell | |
65 octave_class::dotref (const octave_value_list& idx) | |
66 { | |
67 Cell retval; | |
68 | |
69 assert (idx.length () == 1); | |
70 | |
71 std::string nm = idx(0).string_value (); | |
72 | |
73 Octave_map::const_iterator p = map.seek (nm); | |
74 | |
75 if (p != map.end ()) | |
76 retval = map.contents (p); | |
77 else | |
78 error ("class has no member `%s'", nm.c_str ()); | |
79 | |
80 return retval; | |
81 } | |
82 | |
83 static void | |
84 gripe_invalid_index (void) | |
85 { | |
86 error ("invalid index for class"); | |
87 } | |
88 | |
89 static void | |
90 gripe_invalid_index_for_assignment (void) | |
91 { | |
92 error ("invalid index for class assignment"); | |
93 } | |
94 | |
95 static void | |
96 gripe_invalid_index_type (const std::string& nm, char t) | |
97 { | |
98 error ("%s cannot be indexed with %c", nm.c_str (), t); | |
99 } | |
100 | |
101 static void | |
102 gripe_failed_assignment (void) | |
103 { | |
104 error ("assignment to class element failed"); | |
105 } | |
106 | |
107 static inline octave_value_list | |
108 sanitize (const octave_value_list& ovl) | |
109 { | |
110 octave_value_list retval = ovl; | |
111 | |
112 for (octave_idx_type i = 0; i < ovl.length (); i++) | |
113 { | |
114 if (retval(i).is_magic_colon ()) | |
115 retval(i) = ":"; | |
116 } | |
117 | |
118 return retval; | |
119 } | |
120 | |
121 static inline octave_value | |
122 make_idx_args (const std::string& type, | |
123 const std::list<octave_value_list>& idx, | |
124 const std::string& who) | |
125 { | |
126 octave_value retval; | |
127 | |
128 size_t len = type.length (); | |
129 | |
130 if (len == idx.size ()) | |
131 { | |
132 Cell type_field (len, 1); | |
133 Cell subs_field (len, 1); | |
134 | |
135 std::list<octave_value_list>::const_iterator p = idx.begin (); | |
136 | |
137 for (size_t i = 0; i < len; i++) | |
138 { | |
139 char t = type[i]; | |
140 | |
141 switch (t) | |
142 { | |
143 case '(': | |
144 type_field(i) = "()"; | |
145 subs_field(i) = Cell (sanitize (*p++)); | |
146 break; | |
147 | |
148 case '{': | |
149 type_field(i) = "{}"; | |
150 subs_field(i) = Cell (sanitize (*p++)); | |
151 break; | |
152 | |
153 case '.': | |
154 { | |
155 type_field(i) = "."; | |
156 | |
157 octave_value_list vlist = *p++; | |
158 | |
159 if (vlist.length () == 1) | |
160 { | |
161 octave_value val = vlist(0); | |
162 | |
163 if (val.is_string ()) | |
164 subs_field(i) = val; | |
165 else | |
166 { | |
167 error ("expecting character string argument for `.' index"); | |
168 return retval; | |
169 } | |
170 } | |
171 else | |
172 { | |
173 error ("expecting single argument for `.' index"); | |
174 return retval; | |
175 } | |
176 } | |
177 break; | |
178 | |
179 default: | |
180 panic_impossible (); | |
181 break; | |
182 } | |
183 } | |
184 | |
185 Octave_map m; | |
186 | |
187 m.assign ("type", type_field); | |
188 m.assign ("subs", subs_field); | |
189 | |
190 retval = m; | |
191 } | |
192 else | |
193 error ("invalid index for %s", who.c_str ()); | |
194 | |
195 return retval; | |
196 } | |
197 | |
198 octave_value_list | |
199 octave_class::subsref (const std::string& type, | |
200 const std::list<octave_value_list>& idx, | |
201 int nargout) | |
202 { | |
203 octave_value_list retval; | |
204 | |
205 if (in_class_method ()) | |
206 { | |
207 // FIXME -- this block of code is the same as the body of | |
208 // octave_struct::subsref. Maybe it could be shared instead of | |
209 // duplicated. | |
210 | |
211 int skip = 1; | |
212 | |
213 switch (type[0]) | |
214 { | |
215 case '(': | |
216 { | |
217 if (type.length () > 1 && type[1] == '.') | |
218 { | |
219 std::list<octave_value_list>::const_iterator p = idx.begin (); | |
220 octave_value_list key_idx = *++p; | |
221 | |
222 Cell tmp = dotref (key_idx); | |
223 | |
224 if (! error_state) | |
225 { | |
226 Cell t = tmp.index (idx.front ()); | |
227 | |
228 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); | |
229 | |
230 // We handled two index elements, so tell | |
231 // next_subsref to skip both of them. | |
232 | |
233 skip++; | |
234 } | |
235 } | |
236 else | |
237 retval(0) = map.index (idx.front ()); | |
238 } | |
239 break; | |
240 | |
241 case '.': | |
242 { | |
243 if (map.numel() > 0) | |
244 { | |
245 Cell t = dotref (idx.front ()); | |
246 | |
247 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); | |
248 } | |
249 } | |
250 break; | |
251 | |
252 case '{': | |
253 gripe_invalid_index_type (type_name (), type[0]); | |
254 break; | |
255 | |
256 default: | |
257 panic_impossible (); | |
258 } | |
259 | |
260 // FIXME -- perhaps there should be an | |
261 // octave_value_list::next_subsref member function? See also | |
262 // octave_user_function::subsref. | |
263 | |
264 if (idx.size () > 1) | |
265 retval = retval(0).next_subsref (nargout, type, idx, skip); | |
266 } | |
267 else | |
268 { | |
269 octave_value meth = symbol_table::find_method ("subsref", class_name ()); | |
270 | |
271 if (meth.is_defined ()) | |
272 { | |
273 octave_value_list args; | |
274 | |
275 args(1) = make_idx_args (type, idx, "subsref"); | |
276 | |
277 if (error_state) | |
278 return octave_value_list (); | |
279 | |
280 count++; | |
281 args(0) = octave_value (this); | |
282 | |
283 return feval (meth.function_value (), args, nargout); | |
284 } | |
285 else | |
286 { | |
287 if (type.length () == 1 && type[0] == '(') | |
288 retval(0) = octave_value (map.index (idx.front ()), class_name ()); | |
289 else | |
290 gripe_invalid_index (); | |
291 } | |
292 } | |
293 | |
294 return retval; | |
295 } | |
296 | |
297 octave_value | |
298 octave_class::numeric_conv (const Cell& val, const std::string& type) | |
299 { | |
300 octave_value retval; | |
301 | |
302 if (val.length () == 1) | |
303 { | |
304 retval = val(0); | |
305 | |
306 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) | |
307 retval = Octave_map (); | |
308 } | |
309 else | |
310 gripe_invalid_index_for_assignment (); | |
311 | |
312 return retval; | |
313 } | |
314 | |
315 octave_value | |
316 octave_class::subsasgn (const std::string& type, | |
317 const std::list<octave_value_list>& idx, | |
318 const octave_value& rhs) | |
319 { | |
320 octave_value retval; | |
321 | |
7960
22a18f206121
octave_class::subsasgn: only do internal magic if RHS is not an octave_class object
John W. Eaton <jwe@octave.org>
parents:
7444
diff
changeset
|
322 if (in_class_method () && ! rhs.is_object ()) |
7338 | 323 { |
324 // FIXME -- this block of code is the same as the body of | |
325 // octave_struct::subsasgn. Maybe it could be shared instead of | |
326 // duplicated. | |
327 | |
328 int n = type.length (); | |
329 | |
330 octave_value t_rhs = rhs; | |
331 | |
332 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) | |
333 { | |
334 switch (type[0]) | |
335 { | |
336 case '(': | |
337 { | |
338 if (type.length () > 1 && type[1] == '.') | |
339 { | |
340 std::list<octave_value_list>::const_iterator p = idx.begin (); | |
341 octave_value_list t_idx = *p; | |
342 | |
343 octave_value_list key_idx = *++p; | |
344 | |
345 assert (key_idx.length () == 1); | |
346 | |
347 std::string key = key_idx(0).string_value (); | |
348 | |
349 octave_value u; | |
350 | |
351 if (! map.contains (key)) | |
352 u = octave_value::empty_conv (type.substr (2), rhs); | |
353 else | |
354 { | |
355 Cell map_val = map.contents (key); | |
356 | |
357 Cell map_elt = map_val.index (idx.front (), true); | |
358 | |
359 u = numeric_conv (map_elt, type.substr (2)); | |
360 } | |
361 | |
362 if (! error_state) | |
363 { | |
364 std::list<octave_value_list> next_idx (idx); | |
365 | |
366 // We handled two index elements, so subsasgn to | |
367 // needs to skip both of them. | |
368 | |
369 next_idx.erase (next_idx.begin ()); | |
370 next_idx.erase (next_idx.begin ()); | |
371 | |
372 u.make_unique (); | |
373 | |
374 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); | |
375 } | |
376 } | |
377 else | |
378 gripe_invalid_index_for_assignment (); | |
379 } | |
380 break; | |
381 | |
382 case '.': | |
383 { | |
384 octave_value_list key_idx = idx.front (); | |
385 | |
386 assert (key_idx.length () == 1); | |
387 | |
388 std::string key = key_idx(0).string_value (); | |
389 | |
390 octave_value u; | |
391 | |
392 if (! map.contains (key)) | |
393 u = octave_value::empty_conv (type.substr (1), rhs); | |
394 else | |
395 { | |
396 Cell map_val = map.contents (key); | |
397 | |
398 u = numeric_conv (map_val, type.substr (1)); | |
399 } | |
400 | |
401 if (! error_state) | |
402 { | |
403 std::list<octave_value_list> next_idx (idx); | |
404 | |
405 next_idx.erase (next_idx.begin ()); | |
406 | |
407 u.make_unique (); | |
408 | |
409 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); | |
410 } | |
411 } | |
412 break; | |
413 | |
414 case '{': | |
415 gripe_invalid_index_type (type_name (), type[0]); | |
416 break; | |
417 | |
418 default: | |
419 panic_impossible (); | |
420 } | |
421 } | |
422 | |
423 if (! error_state) | |
424 { | |
425 switch (type[0]) | |
426 { | |
427 case '(': | |
428 { | |
429 if (n > 1 && type[1] == '.') | |
430 { | |
431 std::list<octave_value_list>::const_iterator p = idx.begin (); | |
432 octave_value_list key_idx = *++p; | |
433 | |
434 assert (key_idx.length () == 1); | |
435 | |
436 std::string key = key_idx(0).string_value (); | |
437 | |
438 if (! error_state) | |
439 { | |
440 map.assign (idx.front (), key, t_rhs); | |
441 | |
442 if (! error_state) | |
443 { | |
444 count++; | |
445 retval = octave_value (this); | |
446 } | |
447 else | |
448 gripe_failed_assignment (); | |
449 } | |
450 else | |
451 gripe_failed_assignment (); | |
452 } | |
453 else | |
454 { | |
455 if (t_rhs.is_map()) | |
456 { | |
457 Octave_map rhs_map = t_rhs.map_value (); | |
458 | |
459 if (! error_state) | |
460 { | |
461 map.assign (idx.front (), rhs_map); | |
462 | |
463 if (! error_state) | |
464 { | |
465 count++; | |
466 retval = octave_value (this); | |
467 } | |
468 else | |
469 gripe_failed_assignment (); | |
470 } | |
471 else | |
472 error ("invalid class assignment"); | |
473 } | |
474 else | |
475 { | |
476 if (t_rhs.is_empty()) | |
477 { | |
478 map.maybe_delete_elements (idx.front()); | |
479 | |
480 if (! error_state) | |
481 { | |
482 count++; | |
483 retval = octave_value (this); | |
484 } | |
485 else | |
486 gripe_failed_assignment (); | |
487 } | |
488 else | |
489 error ("invalid class assignment"); | |
490 } | |
491 } | |
492 } | |
493 break; | |
494 | |
495 case '.': | |
496 { | |
497 octave_value_list key_idx = idx.front (); | |
498 | |
499 assert (key_idx.length () == 1); | |
500 | |
501 std::string key = key_idx(0).string_value (); | |
502 | |
503 map.assign (key, t_rhs); | |
504 | |
505 if (! error_state) | |
506 { | |
507 count++; | |
508 retval = octave_value (this); | |
509 } | |
510 else | |
511 gripe_failed_assignment (); | |
512 } | |
513 break; | |
514 | |
515 case '{': | |
516 gripe_invalid_index_type (type_name (), type[0]); | |
517 break; | |
518 | |
519 default: | |
520 panic_impossible (); | |
521 } | |
522 } | |
523 else | |
524 gripe_failed_assignment (); | |
525 } | |
526 else | |
527 { | |
528 octave_value meth = symbol_table::find_method ("subsasgn", class_name ()); | |
529 | |
530 if (meth.is_defined ()) | |
531 { | |
532 octave_value_list args; | |
533 | |
534 args(2) = rhs; | |
535 | |
536 args(1) = make_idx_args (type, idx, "subsasgn"); | |
537 | |
538 if (error_state) | |
539 return octave_value_list (); | |
540 | |
541 count++; | |
542 args(0) = octave_value (this); | |
543 | |
544 octave_value_list tmp = feval (meth.function_value (), args); | |
545 | |
546 // FIXME -- should the subsasgn method be able to return | |
547 // more than one value? | |
548 | |
549 if (tmp.length () > 1) | |
550 error ("expecting single return value from @%s/subsasgn", | |
551 class_name().c_str ()); | |
552 else | |
553 retval = tmp(0); | |
554 | |
555 } | |
556 else | |
557 error ("no subsasgn method defined for class %s", | |
558 class_name().c_str ()); | |
559 } | |
560 | |
561 return retval; | |
562 } | |
563 | |
564 size_t | |
565 octave_class::byte_size (void) const | |
566 { | |
567 // Neglect the size of the fieldnames. | |
568 | |
569 size_t retval = 0; | |
570 | |
571 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
572 { | |
573 std::string key = map.key (p); | |
574 | |
575 octave_value val = octave_value (map.contents (p)); | |
576 | |
577 retval += val.byte_size (); | |
578 } | |
579 | |
580 return retval; | |
581 } | |
582 | |
583 Octave_map | |
584 octave_class::map_value (void) const | |
585 { | |
586 Octave_map retval; | |
587 gripe_wrong_type_arg ("octave_class::map_value()", type_name ()); | |
588 return retval; | |
589 } | |
590 | |
591 string_vector | |
592 octave_class::map_keys (void) const | |
593 { | |
594 string_vector retval; | |
595 gripe_wrong_type_arg ("octave_class::map_keys()", type_name ()); | |
596 return retval; | |
597 } | |
598 | |
599 void | |
600 octave_class::print (std::ostream& os, bool) const | |
601 { | |
602 print_raw (os); | |
603 } | |
604 | |
605 void | |
606 octave_class::print_raw (std::ostream& os, bool) const | |
607 { | |
608 unwind_protect::begin_frame ("octave_class_print"); | |
609 | |
610 unwind_protect_int (Vstruct_levels_to_print); | |
611 | |
612 indent (os); | |
613 os << " <class " << class_name () << ">"; | |
614 newline (os); | |
615 | |
616 unwind_protect::run_frame ("octave_class_print"); | |
617 } | |
618 | |
619 bool | |
620 octave_class::print_name_tag (std::ostream& os, const std::string& name) const | |
621 { | |
622 bool retval = false; | |
623 | |
624 indent (os); | |
625 os << name << " ="; | |
626 newline (os); | |
627 newline (os); | |
628 | |
629 return retval; | |
630 } | |
631 | |
632 void | |
633 octave_class::print_with_name (std::ostream&, const std::string& name, | |
634 bool) const | |
635 { | |
636 if (! (evaluating_function_body && Vsilent_functions)) | |
637 { | |
638 octave_value fcn = symbol_table::find_method ("display", class_name ()); | |
639 | |
640 if (fcn.is_defined ()) | |
641 { | |
642 octave_value_list args; | |
643 | |
644 args(0) = octave_value (clone (), 1); | |
645 | |
646 string_vector arg_names (1); | |
647 | |
648 arg_names[0] = name; | |
649 | |
650 args.stash_name_tags (arg_names); | |
651 | |
652 feval (fcn.function_value (), args); | |
653 } | |
654 } | |
655 } | |
656 | |
657 bool | |
658 octave_class::save_ascii (std::ostream&, bool&) | |
659 { | |
660 gripe_wrong_type_arg ("octave_class::save_ascii()", type_name ()); | |
661 return false; | |
662 } | |
663 | |
664 bool | |
665 octave_class::load_ascii (std::istream&) | |
666 { | |
667 gripe_wrong_type_arg ("octave_class::load_ascii()", type_name ()); | |
668 return false; | |
669 } | |
670 | |
671 bool | |
672 octave_class::save_binary (std::ostream&, bool&) | |
673 { | |
674 gripe_wrong_type_arg ("octave_class::save_binary()", type_name ()); | |
675 return false; | |
676 } | |
677 | |
678 bool | |
679 octave_class::load_binary (std::istream&, bool, | |
680 oct_mach_info::float_format) | |
681 { | |
682 gripe_wrong_type_arg ("octave_class::load_binary()", type_name ()); | |
683 return false; | |
684 } | |
685 | |
686 #if defined (HAVE_HDF5) | |
687 | |
688 bool | |
689 octave_class::save_hdf5 (hid_t, const char *, bool) | |
690 { | |
691 gripe_wrong_type_arg ("octave_class::save_binary()", type_name ()); | |
692 | |
693 return false; | |
694 } | |
695 | |
696 bool | |
697 octave_class::load_hdf5 (hid_t, const char *, bool) | |
698 { | |
699 gripe_wrong_type_arg ("octave_class::load_binary()", type_name ()); | |
700 | |
701 return false; | |
702 } | |
703 | |
704 #endif | |
705 | |
706 #if 0 | |
707 bool | |
708 octave_class::save_ascii (std::ostream& os, bool& infnan_warned) | |
709 { | |
710 Octave_map m = map_value (); | |
711 os << "# length: " << m.length () << "\n"; | |
712 | |
713 Octave_map::iterator i = m.begin (); | |
714 while (i != m.end ()) | |
715 { | |
716 octave_value val = map.contents (i); | |
717 | |
718 bool b = save_ascii_data (os, val, m.key (i), infnan_warned, false, 0); | |
719 | |
720 if (! b) | |
721 return os; | |
722 | |
723 i++; | |
724 } | |
725 | |
726 return true; | |
727 } | |
728 | |
729 bool | |
730 octave_class::load_ascii (std::istream& is) | |
731 { | |
732 octave_idx_type len = 0; | |
733 bool success = true; | |
734 | |
735 if (extract_keyword (is, "length", len) && len >= 0) | |
736 { | |
737 if (len > 0) | |
738 { | |
739 Octave_map m (map); | |
740 | |
741 for (octave_idx_type j = 0; j < len; j++) | |
742 { | |
743 octave_value t2; | |
744 bool dummy; | |
745 | |
746 // recurse to read cell elements | |
747 std::string nm | |
748 = read_ascii_data (is, std::string (), dummy, t2, j); | |
749 | |
750 if (!is) | |
751 break; | |
752 | |
753 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); | |
754 | |
755 if (error_state) | |
756 { | |
757 error ("load: internal error loading class elements"); | |
758 return false; | |
759 } | |
760 | |
761 m.assign (nm, tcell); | |
762 } | |
763 | |
764 if (is) | |
765 map = m; | |
766 else | |
767 { | |
768 error ("load: failed to load class"); | |
769 success = false; | |
770 } | |
771 } | |
772 else if (len == 0 ) | |
773 map = Octave_map (dim_vector (1, 1)); | |
774 else | |
775 panic_impossible (); | |
776 } | |
777 else { | |
778 error ("load: failed to extract number of elements in class"); | |
779 success = false; | |
780 } | |
781 | |
782 return success; | |
783 } | |
784 | |
785 bool | |
786 octave_class::save_binary (std::ostream& os, bool& save_as_floats) | |
787 { | |
788 Octave_map m = map_value (); | |
789 | |
790 int32_t len = m.length(); | |
791 os.write (reinterpret_cast<char *> (&len), 4); | |
792 | |
793 Octave_map::iterator i = m.begin (); | |
794 while (i != m.end ()) | |
795 { | |
796 octave_value val = map.contents (i); | |
797 | |
798 bool b = save_binary_data (os, val, m.key (i), "", 0, save_as_floats); | |
799 | |
800 if (! b) | |
801 return os; | |
802 | |
803 i++; | |
804 } | |
805 | |
806 return true; | |
807 } | |
808 | |
809 bool | |
810 octave_class::load_binary (std::istream& is, bool swap, | |
811 oct_mach_info::float_format fmt) | |
812 { | |
813 bool success = true; | |
814 int32_t len; | |
815 if (! is.read (reinterpret_cast<char *> (&len), 4)) | |
816 return false; | |
817 if (swap) | |
818 swap_bytes<4> (&len); | |
819 | |
820 if (len > 0) | |
821 { | |
822 Octave_map m (map); | |
823 | |
824 for (octave_idx_type j = 0; j < len; j++) | |
825 { | |
826 octave_value t2; | |
827 bool dummy; | |
828 std::string doc; | |
829 | |
830 // recurse to read cell elements | |
831 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
832 dummy, t2, doc); | |
833 | |
834 if (!is) | |
835 break; | |
836 | |
837 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); | |
838 | |
839 if (error_state) | |
840 { | |
841 error ("load: internal error loading class elements"); | |
842 return false; | |
843 } | |
844 | |
845 m.assign (nm, tcell); | |
846 } | |
847 | |
848 if (is) | |
849 map = m; | |
850 else | |
851 { | |
852 error ("load: failed to load class"); | |
853 success = false; | |
854 } | |
855 } | |
856 else if (len == 0 ) | |
857 map = Octave_map (dim_vector (1, 1)); | |
858 else | |
859 panic_impossible (); | |
860 | |
861 return success; | |
862 } | |
863 | |
864 #if defined (HAVE_HDF5) | |
865 | |
866 bool | |
867 octave_class::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
868 { | |
869 hid_t data_hid = -1; | |
870 | |
871 data_hid = H5Gcreate (loc_id, name, 0); | |
872 if (data_hid < 0) return false; | |
873 | |
874 // recursively add each element of the class to this group | |
875 Octave_map m = map_value (); | |
876 Octave_map::iterator i = m.begin (); | |
877 while (i != m.end ()) | |
878 { | |
879 octave_value val = map.contents (i); | |
880 | |
881 bool retval2 = add_hdf5_data (data_hid, val, m.key (i), "", false, | |
882 save_as_floats); | |
883 | |
884 if (! retval2) | |
885 break; | |
886 | |
887 i++; | |
888 } | |
889 | |
890 H5Gclose (data_hid); | |
891 | |
892 return true; | |
893 } | |
894 | |
895 bool | |
896 octave_class::load_hdf5 (hid_t loc_id, const char *name, | |
897 bool have_h5giterate_bug) | |
898 { | |
899 bool retval = false; | |
900 | |
901 hdf5_callback_data dsub; | |
902 | |
903 herr_t retval2 = 0; | |
904 Octave_map m (dim_vector (1, 1)); | |
905 int current_item = 0; | |
906 #ifdef HAVE_H5GGET_NUM_OBJS | |
907 hsize_t num_obj = 0; | |
908 hid_t group_id = H5Gopen (loc_id, name); | |
909 H5Gget_num_objs (group_id, &num_obj); | |
910 H5Gclose (group_id); | |
911 | |
912 while (current_item < static_cast<int> (num_obj) | |
913 && (retval2 = H5Giterate (loc_id, name, ¤t_item, | |
914 hdf5_read_next_data, &dsub)) > 0) | |
915 #else | |
916 while ((retval2 = H5Giterate (loc_id, name, ¤t_item, | |
917 hdf5_read_next_data, &dsub)) > 0) | |
918 #endif | |
919 { | |
920 octave_value t2 = dsub.tc; | |
921 | |
922 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); | |
923 | |
924 if (error_state) | |
925 { | |
926 error ("load: internal error loading class elements"); | |
927 return false; | |
928 } | |
929 | |
930 m.assign (dsub.name, tcell); | |
931 | |
932 if (have_h5giterate_bug) | |
933 current_item++; // H5Giterate returned the last index processed | |
934 } | |
935 | |
936 if (retval2 >= 0) | |
937 { | |
938 map = m; | |
939 retval = true; | |
940 } | |
941 | |
942 return retval; | |
943 } | |
944 | |
945 #endif | |
946 | |
947 #endif | |
948 | |
949 mxArray * | |
950 octave_class::as_mxArray (void) const | |
951 { | |
952 gripe_wrong_type_arg ("octave_class::as_mxArray ()", type_name ()); | |
953 | |
954 return 0; | |
955 } | |
956 | |
957 bool | |
958 octave_class::in_class_method (void) const | |
959 { | |
960 octave_function *fcn = octave_call_stack::current (); | |
961 | |
962 return (fcn | |
963 && (fcn->is_class_method () || fcn->is_class_constructor ()) | |
964 && fcn->dispatch_class () == class_name ()); | |
965 } | |
966 | |
967 DEFUN (class, args, , | |
968 "-*- texinfo -*-\n\ | |
969 @deftypefn {Built-in Function} {} class (@var{expr})\n\ | |
970 @deftypefnx {Built-in Function} {} class (@var{s}, @var{id})\n\ | |
971 \n\ | |
972 Return the class of the expression @var{expr}, as a string or\n\ | |
973 create a class object from the structure @var{s} with name @var{id}.\n\ | |
974 @end deftypefn") | |
975 { | |
976 octave_value retval; | |
977 | |
978 int nargin = args.length (); | |
979 | |
980 if (nargin == 1) | |
981 retval = args(0).class_name (); | |
982 else if (nargin == 2) | |
983 { | |
984 Octave_map m = args(0).map_value (); | |
985 | |
986 if (! error_state) | |
987 { | |
988 std::string id = args(1).string_value (); | |
989 | |
990 if (! error_state) | |
991 { | |
992 octave_function *fcn = octave_call_stack::caller (); | |
993 | |
994 if (fcn && fcn->is_class_constructor ()) | |
995 retval = octave_value (new octave_class (m, id)); | |
996 else | |
997 error ("class: invalid call from outside class constructor"); | |
998 } | |
999 else | |
1000 error ("class: expecting character string as second argument"); | |
1001 } | |
1002 else | |
1003 error ("class: expecting structure as first argument"); | |
1004 } | |
1005 else | |
1006 print_usage (); | |
1007 | |
1008 return retval; | |
1009 } | |
1010 | |
1011 | |
1012 DEFUN (isobject, args, , | |
1013 "-*- texinfo -*-\n\ | |
1014 @deftypefn {Built-in Function} {} isobject (@var{x})\n\ | |
1015 Return true if @var{x} is a class object.\n\ | |
1016 @end deftypefn") | |
1017 { | |
1018 octave_value retval; | |
1019 | |
1020 if (args.length () == 1) | |
1021 retval = args(0).is_object (); | |
1022 else | |
1023 print_usage (); | |
1024 | |
1025 return retval; | |
1026 } | |
1027 | |
1028 DEFCMD (methods, args, nargout, | |
1029 "-*- texinfo -*-\n\ | |
1030 @deftypefn {Built-in Function} {} methods (@var{x})\n\ | |
1031 @deftypefnx {Built-in Function} {} methods (\"classname\")\n\ | |
1032 Return a cell array containing the names of the methods for the\n\ | |
1033 object @var{x} or the named class.\n\ | |
1034 @end deftypefn") | |
1035 { | |
1036 octave_value retval; | |
1037 | |
1038 if (args.length () == 1) | |
1039 { | |
1040 octave_value arg = args(0); | |
1041 | |
1042 std::string class_name; | |
1043 | |
1044 if (arg.is_object ()) | |
1045 class_name = arg.class_name (); | |
1046 else if (arg.is_string ()) | |
1047 class_name = arg.string_value (); | |
1048 else | |
1049 error ("methods: expecting object or class name as argument"); | |
1050 | |
1051 if (! error_state) | |
1052 { | |
1053 string_vector sv = load_path::methods (class_name); | |
1054 | |
1055 if (nargout == 0) | |
1056 { | |
1057 octave_stdout << "Methods for class " << class_name << ":\n\n"; | |
1058 | |
1059 sv.list_in_columns (octave_stdout); | |
1060 | |
1061 octave_stdout << std::endl; | |
1062 } | |
1063 else | |
1064 retval = sv; | |
1065 } | |
1066 } | |
1067 else | |
1068 print_usage (); | |
1069 | |
1070 return retval; | |
1071 } | |
1072 | |
1073 static bool | |
1074 is_built_in_class (const std::string& cn) | |
1075 { | |
1076 static std::set<std::string> built_in_class_names; | |
1077 | |
1078 if (built_in_class_names.empty ()) | |
1079 { | |
1080 built_in_class_names.insert ("double"); | |
1081 built_in_class_names.insert ("single"); | |
1082 built_in_class_names.insert ("cell"); | |
1083 built_in_class_names.insert ("struct"); | |
1084 built_in_class_names.insert ("logical"); | |
1085 built_in_class_names.insert ("char"); | |
1086 built_in_class_names.insert ("function handle"); | |
1087 built_in_class_names.insert ("int8"); | |
1088 built_in_class_names.insert ("uint8"); | |
1089 built_in_class_names.insert ("int16"); | |
1090 built_in_class_names.insert ("uint16"); | |
1091 built_in_class_names.insert ("int32"); | |
1092 built_in_class_names.insert ("uint32"); | |
1093 built_in_class_names.insert ("int64"); | |
1094 built_in_class_names.insert ("uint64"); | |
1095 } | |
1096 | |
1097 return built_in_class_names.find (cn) != built_in_class_names.end (); | |
1098 } | |
1099 | |
1100 static void | |
1101 set_class_relationship (const std::string& sup_class, | |
1102 const std::string& inf_class) | |
1103 { | |
1104 // FIXME | |
1105 | |
1106 warning ("class precedence not implemented"); | |
1107 } | |
1108 | |
1109 DEFUN (superiorto, args, , | |
1110 "-*- texinfo -*-\n\ | |
1111 @deftypefn {Built-in Function} {} superiorto (@var{class_name})\n\ | |
1112 When called from a class constructor, mark the object currently\n\ | |
1113 constructed as having a higher precedence than @var{class_name}.\n\ | |
1114 This function may only be called from a class constructor.\n\ | |
1115 @end deftypefn") | |
1116 { | |
1117 octave_value retval; | |
1118 | |
1119 octave_function *fcn = octave_call_stack::caller (); | |
1120 | |
1121 if (fcn && fcn->is_class_constructor ()) | |
1122 { | |
1123 if (args.length () == 1) | |
1124 { | |
1125 std::string class_name = args(0).string_value (); | |
1126 | |
1127 if (! error_state) | |
1128 { | |
1129 if (! is_built_in_class (class_name)) | |
1130 { | |
1131 std::string this_class_name = fcn->name (); | |
1132 | |
1133 set_class_relationship (this_class_name, class_name); | |
1134 } | |
1135 else | |
1136 { | |
1137 // User defined classes always have higher precedence | |
1138 // than built-in classes. | |
1139 } | |
1140 } | |
1141 else | |
1142 error ("superiorto: expecting argument to be class name"); | |
1143 } | |
1144 else | |
1145 print_usage (); | |
1146 } | |
1147 else | |
1148 error ("superiorto: invalid call from outside class constructor"); | |
1149 | |
1150 return retval; | |
1151 } | |
1152 | |
1153 DEFUN (inferiorto, args, , | |
1154 "-*- texinfo -*-\n\ | |
1155 @deftypefn {Built-in Function} {} inferiorto (@var{class_name})\n\ | |
1156 When called from a class constructor, mark the object currently\n\ | |
1157 constructed as having a lower precedence than @var{class_name}.\n\ | |
1158 This function may only be called from a class constructor.\n\ | |
1159 @end deftypefn") | |
1160 { | |
1161 octave_value retval; | |
1162 | |
1163 octave_function *fcn = octave_call_stack::caller (); | |
1164 | |
1165 if (fcn && fcn->is_class_constructor ()) | |
1166 { | |
1167 if (args.length () == 1) | |
1168 { | |
1169 std::string class_name = args(0).string_value (); | |
1170 | |
1171 if (! error_state) | |
1172 { | |
1173 if (! is_built_in_class (class_name)) | |
1174 { | |
1175 std::string this_class_name = fcn->name (); | |
1176 | |
1177 set_class_relationship (class_name, this_class_name); | |
1178 } | |
1179 else | |
1180 error ("inferiorto: cannot give user-defined class lower precedence than built-in class"); | |
1181 } | |
1182 else | |
1183 error ("inferiorto: expecting argument to be class name"); | |
1184 } | |
1185 else | |
1186 print_usage (); | |
1187 } | |
1188 else | |
1189 error ("inferiorto: invalid call from outside class constructor"); | |
1190 | |
1191 return retval; | |
1192 } | |
1193 | |
1194 /* | |
1195 ;;; Local Variables: *** | |
1196 ;;; mode: C++ *** | |
1197 ;;; End: *** | |
1198 */ |