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