Mercurial > octave-nkf
annotate src/ov-class.cc @ 8782:45524925bed9
ov-class.cc (octave_class::subsref): return class object, not struct when indexing directly
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 17 Feb 2009 14:24:02 -0500 |
parents | 5dd06f19e9be |
children | 70f5a0375afd |
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" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8219
diff
changeset
|
31 #include "oct-locbuf.h" |
7338 | 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 | |
8782
45524925bed9
ov-class.cc (octave_class::subsref): return class object, not struct when indexing directly
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
238 retval(0) = octave_value (map.index (idx.front ()), |
45524925bed9
ov-class.cc (octave_class::subsref): return class object, not struct when indexing directly
John W. Eaton <jwe@octave.org>
parents:
8746
diff
changeset
|
239 class_name ()); |
7338 | 240 } |
241 break; | |
242 | |
243 case '.': | |
244 { | |
245 if (map.numel() > 0) | |
246 { | |
247 Cell t = dotref (idx.front ()); | |
248 | |
249 retval(0) = (t.length () == 1) ? t(0) : octave_value (t, true); | |
250 } | |
251 } | |
252 break; | |
253 | |
254 case '{': | |
255 gripe_invalid_index_type (type_name (), type[0]); | |
256 break; | |
257 | |
258 default: | |
259 panic_impossible (); | |
260 } | |
261 | |
262 // FIXME -- perhaps there should be an | |
263 // octave_value_list::next_subsref member function? See also | |
264 // octave_user_function::subsref. | |
265 | |
266 if (idx.size () > 1) | |
267 retval = retval(0).next_subsref (nargout, type, idx, skip); | |
268 } | |
269 else | |
270 { | |
271 octave_value meth = symbol_table::find_method ("subsref", class_name ()); | |
272 | |
273 if (meth.is_defined ()) | |
274 { | |
275 octave_value_list args; | |
276 | |
277 args(1) = make_idx_args (type, idx, "subsref"); | |
278 | |
279 if (error_state) | |
280 return octave_value_list (); | |
281 | |
282 count++; | |
283 args(0) = octave_value (this); | |
284 | |
285 return feval (meth.function_value (), args, nargout); | |
286 } | |
287 else | |
288 { | |
289 if (type.length () == 1 && type[0] == '(') | |
290 retval(0) = octave_value (map.index (idx.front ()), class_name ()); | |
291 else | |
292 gripe_invalid_index (); | |
293 } | |
294 } | |
295 | |
296 return retval; | |
297 } | |
298 | |
299 octave_value | |
300 octave_class::numeric_conv (const Cell& val, const std::string& type) | |
301 { | |
302 octave_value retval; | |
303 | |
304 if (val.length () == 1) | |
305 { | |
306 retval = val(0); | |
307 | |
308 if (type.length () > 0 && type[0] == '.' && ! retval.is_map ()) | |
309 retval = Octave_map (); | |
310 } | |
311 else | |
312 gripe_invalid_index_for_assignment (); | |
313 | |
314 return retval; | |
315 } | |
316 | |
317 octave_value | |
318 octave_class::subsasgn (const std::string& type, | |
319 const std::list<octave_value_list>& idx, | |
320 const octave_value& rhs) | |
321 { | |
322 octave_value retval; | |
323 | |
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
|
324 if (in_class_method () && ! rhs.is_object ()) |
7338 | 325 { |
326 // FIXME -- this block of code is the same as the body of | |
327 // octave_struct::subsasgn. Maybe it could be shared instead of | |
328 // duplicated. | |
329 | |
330 int n = type.length (); | |
331 | |
332 octave_value t_rhs = rhs; | |
333 | |
334 if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.')) | |
335 { | |
336 switch (type[0]) | |
337 { | |
338 case '(': | |
339 { | |
340 if (type.length () > 1 && type[1] == '.') | |
341 { | |
342 std::list<octave_value_list>::const_iterator p = idx.begin (); | |
343 octave_value_list t_idx = *p; | |
344 | |
345 octave_value_list key_idx = *++p; | |
346 | |
347 assert (key_idx.length () == 1); | |
348 | |
349 std::string key = key_idx(0).string_value (); | |
350 | |
351 octave_value u; | |
352 | |
353 if (! map.contains (key)) | |
354 u = octave_value::empty_conv (type.substr (2), rhs); | |
355 else | |
356 { | |
357 Cell map_val = map.contents (key); | |
358 | |
359 Cell map_elt = map_val.index (idx.front (), true); | |
360 | |
361 u = numeric_conv (map_elt, type.substr (2)); | |
362 } | |
363 | |
364 if (! error_state) | |
365 { | |
366 std::list<octave_value_list> next_idx (idx); | |
367 | |
368 // We handled two index elements, so subsasgn to | |
369 // needs to skip both of them. | |
370 | |
371 next_idx.erase (next_idx.begin ()); | |
372 next_idx.erase (next_idx.begin ()); | |
373 | |
374 u.make_unique (); | |
375 | |
376 t_rhs = u.subsasgn (type.substr (2), next_idx, rhs); | |
377 } | |
378 } | |
379 else | |
380 gripe_invalid_index_for_assignment (); | |
381 } | |
382 break; | |
383 | |
384 case '.': | |
385 { | |
386 octave_value_list key_idx = idx.front (); | |
387 | |
388 assert (key_idx.length () == 1); | |
389 | |
390 std::string key = key_idx(0).string_value (); | |
391 | |
392 octave_value u; | |
393 | |
394 if (! map.contains (key)) | |
395 u = octave_value::empty_conv (type.substr (1), rhs); | |
396 else | |
397 { | |
398 Cell map_val = map.contents (key); | |
399 | |
400 u = numeric_conv (map_val, type.substr (1)); | |
401 } | |
402 | |
403 if (! error_state) | |
404 { | |
405 std::list<octave_value_list> next_idx (idx); | |
406 | |
407 next_idx.erase (next_idx.begin ()); | |
408 | |
409 u.make_unique (); | |
410 | |
411 t_rhs = u.subsasgn (type.substr (1), next_idx, rhs); | |
412 } | |
413 } | |
414 break; | |
415 | |
416 case '{': | |
417 gripe_invalid_index_type (type_name (), type[0]); | |
418 break; | |
419 | |
420 default: | |
421 panic_impossible (); | |
422 } | |
423 } | |
424 | |
425 if (! error_state) | |
426 { | |
427 switch (type[0]) | |
428 { | |
429 case '(': | |
430 { | |
431 if (n > 1 && type[1] == '.') | |
432 { | |
433 std::list<octave_value_list>::const_iterator p = idx.begin (); | |
434 octave_value_list key_idx = *++p; | |
435 | |
436 assert (key_idx.length () == 1); | |
437 | |
438 std::string key = key_idx(0).string_value (); | |
439 | |
440 if (! error_state) | |
441 { | |
442 map.assign (idx.front (), key, t_rhs); | |
443 | |
444 if (! error_state) | |
445 { | |
446 count++; | |
447 retval = octave_value (this); | |
448 } | |
449 else | |
450 gripe_failed_assignment (); | |
451 } | |
452 else | |
453 gripe_failed_assignment (); | |
454 } | |
455 else | |
456 { | |
457 if (t_rhs.is_map()) | |
458 { | |
459 Octave_map rhs_map = t_rhs.map_value (); | |
460 | |
461 if (! error_state) | |
462 { | |
463 map.assign (idx.front (), rhs_map); | |
464 | |
465 if (! error_state) | |
466 { | |
467 count++; | |
468 retval = octave_value (this); | |
469 } | |
470 else | |
471 gripe_failed_assignment (); | |
472 } | |
473 else | |
474 error ("invalid class assignment"); | |
475 } | |
476 else | |
477 { | |
478 if (t_rhs.is_empty()) | |
479 { | |
480 map.maybe_delete_elements (idx.front()); | |
481 | |
482 if (! error_state) | |
483 { | |
484 count++; | |
485 retval = octave_value (this); | |
486 } | |
487 else | |
488 gripe_failed_assignment (); | |
489 } | |
490 else | |
491 error ("invalid class assignment"); | |
492 } | |
493 } | |
494 } | |
495 break; | |
496 | |
497 case '.': | |
498 { | |
499 octave_value_list key_idx = idx.front (); | |
500 | |
501 assert (key_idx.length () == 1); | |
502 | |
503 std::string key = key_idx(0).string_value (); | |
504 | |
505 map.assign (key, t_rhs); | |
506 | |
507 if (! error_state) | |
508 { | |
509 count++; | |
510 retval = octave_value (this); | |
511 } | |
512 else | |
513 gripe_failed_assignment (); | |
514 } | |
515 break; | |
516 | |
517 case '{': | |
518 gripe_invalid_index_type (type_name (), type[0]); | |
519 break; | |
520 | |
521 default: | |
522 panic_impossible (); | |
523 } | |
524 } | |
525 else | |
526 gripe_failed_assignment (); | |
527 } | |
528 else | |
529 { | |
530 octave_value meth = symbol_table::find_method ("subsasgn", class_name ()); | |
531 | |
532 if (meth.is_defined ()) | |
533 { | |
534 octave_value_list args; | |
535 | |
536 args(2) = rhs; | |
537 | |
538 args(1) = make_idx_args (type, idx, "subsasgn"); | |
539 | |
540 if (error_state) | |
541 return octave_value_list (); | |
542 | |
543 count++; | |
544 args(0) = octave_value (this); | |
545 | |
546 octave_value_list tmp = feval (meth.function_value (), args); | |
547 | |
548 // FIXME -- should the subsasgn method be able to return | |
549 // more than one value? | |
550 | |
551 if (tmp.length () > 1) | |
552 error ("expecting single return value from @%s/subsasgn", | |
553 class_name().c_str ()); | |
554 else | |
555 retval = tmp(0); | |
556 | |
557 } | |
558 else | |
559 error ("no subsasgn method defined for class %s", | |
560 class_name().c_str ()); | |
561 } | |
562 | |
563 return retval; | |
564 } | |
565 | |
8154
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
566 idx_vector |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
567 octave_class::index_vector (void) const |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
568 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
569 idx_vector retval; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
570 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
571 octave_value meth = symbol_table::find_method ("subsindex", class_name ()); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
572 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
573 if (meth.is_defined ()) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
574 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
575 octave_value_list args; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
576 args(0) = octave_value (new octave_class (map, c_name)); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
577 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
578 octave_value_list tmp = feval (meth.function_value (), args, 1); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
579 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
580 if (!error_state && tmp.length () >= 1) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
581 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
582 if (tmp(0).is_object()) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
583 error ("subsindex function must return a valid index vector"); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
584 else |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
585 // Index vector returned by subsindex is zero based |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
586 // (why this inconsistency Mathworks?), and so we must |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
587 // add one to the value returned as the index_vector method |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
588 // expects it to be one based. |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
589 retval = do_binary_op (octave_value::op_add, tmp (0), |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
590 octave_value (1.0)).index_vector (); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
591 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
592 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
593 else |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
594 error ("no subsindex method defined for class %s", |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
595 class_name().c_str ()); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
596 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
597 return retval; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
598 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
599 |
7338 | 600 size_t |
601 octave_class::byte_size (void) const | |
602 { | |
603 // Neglect the size of the fieldnames. | |
604 | |
605 size_t retval = 0; | |
606 | |
607 for (Octave_map::const_iterator p = map.begin (); p != map.end (); p++) | |
608 { | |
609 std::string key = map.key (p); | |
610 | |
611 octave_value val = octave_value (map.contents (p)); | |
612 | |
613 retval += val.byte_size (); | |
614 } | |
615 | |
616 return retval; | |
617 } | |
618 | |
619 string_vector | |
620 octave_class::map_keys (void) const | |
621 { | |
622 string_vector retval; | |
623 gripe_wrong_type_arg ("octave_class::map_keys()", type_name ()); | |
624 return retval; | |
625 } | |
626 | |
627 void | |
628 octave_class::print (std::ostream& os, bool) const | |
629 { | |
630 print_raw (os); | |
631 } | |
632 | |
633 void | |
634 octave_class::print_raw (std::ostream& os, bool) const | |
635 { | |
636 unwind_protect::begin_frame ("octave_class_print"); | |
637 | |
638 unwind_protect_int (Vstruct_levels_to_print); | |
639 | |
640 indent (os); | |
641 os << " <class " << class_name () << ">"; | |
642 newline (os); | |
643 | |
644 unwind_protect::run_frame ("octave_class_print"); | |
645 } | |
646 | |
647 bool | |
648 octave_class::print_name_tag (std::ostream& os, const std::string& name) const | |
649 { | |
650 bool retval = false; | |
651 | |
652 indent (os); | |
653 os << name << " ="; | |
654 newline (os); | |
655 newline (os); | |
656 | |
657 return retval; | |
658 } | |
659 | |
660 void | |
661 octave_class::print_with_name (std::ostream&, const std::string& name, | |
662 bool) const | |
663 { | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
664 octave_value fcn = symbol_table::find_method ("display", class_name ()); |
7338 | 665 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
666 if (fcn.is_defined ()) |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
667 { |
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
668 octave_value_list args; |
7338 | 669 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
670 args(0) = octave_value (clone (), 1); |
7338 | 671 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
672 string_vector arg_names (1); |
7338 | 673 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
674 arg_names[0] = name; |
7338 | 675 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
676 args.stash_name_tags (arg_names); |
7338 | 677 |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
678 feval (fcn.function_value (), args); |
7338 | 679 } |
680 } | |
681 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
682 bool |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
683 octave_class::save_ascii (std::ostream& os) |
7338 | 684 { |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
685 os << "# classname: " << class_name () << "\n"; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
686 Octave_map m; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
687 if (load_path::find_method (class_name (), "saveobj") != std::string()) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
688 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
689 octave_value in = new octave_class (*this); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
690 octave_value_list tmp = feval ("saveobj", in, 1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
691 if (! error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
692 m = tmp(0).map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
693 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
694 return false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
695 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
696 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
697 m = map_value (); |
7338 | 698 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
699 os << "# length: " << m.nfields () << "\n"; |
7338 | 700 |
701 Octave_map::iterator i = m.begin (); | |
702 while (i != m.end ()) | |
703 { | |
704 octave_value val = map.contents (i); | |
705 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
706 bool b = save_ascii_data (os, val, m.key (i), false, 0); |
7338 | 707 |
708 if (! b) | |
709 return os; | |
710 | |
711 i++; | |
712 } | |
713 | |
714 return true; | |
715 } | |
716 | |
717 bool | |
718 octave_class::load_ascii (std::istream& is) | |
719 { | |
720 octave_idx_type len = 0; | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
721 std::string classname; |
7338 | 722 bool success = true; |
723 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
724 if (extract_keyword (is, "classname", classname) && classname != "") |
7338 | 725 { |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
726 if (extract_keyword (is, "length", len) && len >= 0) |
7338 | 727 { |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
728 if (len > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
729 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
730 Octave_map m (map); |
7338 | 731 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
732 for (octave_idx_type j = 0; j < len; j++) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
733 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
734 octave_value t2; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
735 bool dummy; |
7338 | 736 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
737 // recurse to read cell elements |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
738 std::string nm |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
739 = read_ascii_data (is, std::string (), dummy, t2, j); |
7338 | 740 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
741 if (!is) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
742 break; |
7338 | 743 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
744 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); |
7338 | 745 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
746 if (error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
747 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
748 error ("load: internal error loading class elements"); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
749 return false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
750 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
751 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
752 m.assign (nm, tcell); |
7338 | 753 } |
754 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
755 if (is) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
756 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
757 map = m; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
758 c_name = classname; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
759 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
760 if (load_path::find_method (classname, "loadobj") != |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
761 std::string()) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
762 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
763 octave_value in = new octave_class (*this); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
764 octave_value_list tmp = feval ("loadobj", in, 1); |
7338 | 765 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
766 if (! error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
767 map = tmp(0).map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
768 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
769 success = false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
770 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
771 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
772 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
773 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
774 error ("load: failed to load class"); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
775 success = false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
776 } |
7338 | 777 } |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
778 else if (len == 0 ) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
779 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
780 map = Octave_map (dim_vector (1, 1)); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
781 c_name = classname; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
782 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
783 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
784 panic_impossible (); |
7338 | 785 } |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
786 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
787 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
788 error ("load: failed to extract number of elements in class"); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
789 success = false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
790 } |
7338 | 791 } |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
792 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
793 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
794 error ("load: failed to extract name of class"); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
795 success = false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
796 } |
7338 | 797 |
798 return success; | |
799 } | |
800 | |
801 bool | |
802 octave_class::save_binary (std::ostream& os, bool& save_as_floats) | |
803 { | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
804 int32_t classname_len = class_name().length (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
805 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
806 os.write (reinterpret_cast<char *> (&classname_len), 4); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
807 os << class_name (); |
7338 | 808 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
809 Octave_map m; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
810 if (load_path::find_method (class_name (), "saveobj") != std::string()) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
811 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
812 octave_value in = new octave_class (*this); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
813 octave_value_list tmp = feval ("saveobj", in, 1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
814 if (! error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
815 m = tmp(0).map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
816 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
817 return false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
818 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
819 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
820 m = map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
821 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
822 int32_t len = m.nfields(); |
7338 | 823 os.write (reinterpret_cast<char *> (&len), 4); |
824 | |
825 Octave_map::iterator i = m.begin (); | |
826 while (i != m.end ()) | |
827 { | |
828 octave_value val = map.contents (i); | |
829 | |
830 bool b = save_binary_data (os, val, m.key (i), "", 0, save_as_floats); | |
831 | |
832 if (! b) | |
833 return os; | |
834 | |
835 i++; | |
836 } | |
837 | |
838 return true; | |
839 } | |
840 | |
841 bool | |
842 octave_class::load_binary (std::istream& is, bool swap, | |
843 oct_mach_info::float_format fmt) | |
844 { | |
845 bool success = true; | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
846 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
847 int32_t classname_len; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
848 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
849 is.read (reinterpret_cast<char *> (&classname_len), 4); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
850 if (! is) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
851 return false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
852 else if (swap) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
853 swap_bytes<4> (&classname_len); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
854 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
855 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
856 OCTAVE_LOCAL_BUFFER (char, classname, classname_len+1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
857 classname[classname_len] = '\0'; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
858 if (! is.read (reinterpret_cast<char *> (classname), classname_len)) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
859 return false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
860 c_name = classname; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
861 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
862 |
7338 | 863 int32_t len; |
864 if (! is.read (reinterpret_cast<char *> (&len), 4)) | |
865 return false; | |
866 if (swap) | |
867 swap_bytes<4> (&len); | |
868 | |
869 if (len > 0) | |
870 { | |
871 Octave_map m (map); | |
872 | |
873 for (octave_idx_type j = 0; j < len; j++) | |
874 { | |
875 octave_value t2; | |
876 bool dummy; | |
877 std::string doc; | |
878 | |
879 // recurse to read cell elements | |
880 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
881 dummy, t2, doc); | |
882 | |
883 if (!is) | |
884 break; | |
885 | |
886 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); | |
887 | |
888 if (error_state) | |
889 { | |
890 error ("load: internal error loading class elements"); | |
891 return false; | |
892 } | |
893 | |
894 m.assign (nm, tcell); | |
895 } | |
896 | |
897 if (is) | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
898 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
899 map = m; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
900 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
901 if (load_path::find_method (class_name(), "loadobj") != std::string()) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
902 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
903 octave_value in = new octave_class (*this); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
904 octave_value_list tmp = feval ("loadobj", in, 1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
905 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
906 if (! error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
907 map = tmp(0).map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
908 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
909 success = false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
910 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
911 } |
7338 | 912 else |
913 { | |
914 error ("load: failed to load class"); | |
915 success = false; | |
916 } | |
917 } | |
918 else if (len == 0 ) | |
919 map = Octave_map (dim_vector (1, 1)); | |
920 else | |
921 panic_impossible (); | |
922 | |
923 return success; | |
924 } | |
925 | |
926 #if defined (HAVE_HDF5) | |
927 | |
928 bool | |
929 octave_class::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
930 { | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
931 hsize_t hdims[3]; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
932 hid_t group_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
933 hid_t type_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
934 hid_t space_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
935 hid_t class_hid = -1; |
7338 | 936 hid_t data_hid = -1; |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
937 Octave_map m; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
938 Octave_map::iterator i; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
939 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
940 group_hid = H5Gcreate (loc_id, name, 0); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
941 if (group_hid < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
942 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
943 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
944 // Add the class name to the group |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
945 type_hid = H5Tcopy (H5T_C_S1); H5Tset_size (type_hid, c_name.length () + 1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
946 if (type_hid < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
947 goto error_cleanup; |
7338 | 948 |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
949 hdims[0] = 0; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
950 space_hid = H5Screate_simple (0 , hdims, 0); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
951 if (space_hid < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
952 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
953 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
954 class_hid = H5Dcreate (group_hid, "classname", type_hid, space_hid, |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
955 H5P_DEFAULT); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
956 if (class_hid < 0 || H5Dwrite (class_hid, type_hid, H5S_ALL, H5S_ALL, |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
957 H5P_DEFAULT, c_name.c_str ()) < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
958 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
959 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
960 data_hid = H5Gcreate (group_hid, "value", 0); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
961 if (data_hid < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
962 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
963 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
964 if (load_path::find_method (class_name (), "saveobj") != std::string()) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
965 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
966 octave_value in = new octave_class (*this); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
967 octave_value_list tmp = feval ("saveobj", in, 1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
968 if (! error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
969 m = tmp(0).map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
970 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
971 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
972 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
973 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
974 m = map_value (); |
7338 | 975 |
976 // recursively add each element of the class to this group | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
977 i = m.begin (); |
7338 | 978 while (i != m.end ()) |
979 { | |
980 octave_value val = map.contents (i); | |
981 | |
982 bool retval2 = add_hdf5_data (data_hid, val, m.key (i), "", false, | |
983 save_as_floats); | |
984 | |
985 if (! retval2) | |
986 break; | |
987 | |
988 i++; | |
989 } | |
990 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
991 error_cleanup: |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
992 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
993 if (data_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
994 H5Gclose (data_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
995 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
996 if (class_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
997 H5Dclose (class_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
998 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
999 if (space_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1000 H5Sclose (space_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1001 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1002 if (type_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1003 H5Tclose (type_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1004 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1005 if (group_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1006 H5Gclose (group_hid); |
7338 | 1007 |
1008 return true; | |
1009 } | |
1010 | |
1011 bool | |
1012 octave_class::load_hdf5 (hid_t loc_id, const char *name, | |
1013 bool have_h5giterate_bug) | |
1014 { | |
1015 bool retval = false; | |
1016 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1017 hid_t group_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1018 hid_t data_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1019 hid_t type_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1020 hid_t type_class_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1021 hid_t space_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1022 hid_t subgroup_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1023 hid_t st_id = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1024 |
7338 | 1025 hdf5_callback_data dsub; |
1026 | |
1027 herr_t retval2 = 0; | |
1028 Octave_map m (dim_vector (1, 1)); | |
1029 int current_item = 0; | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1030 hsize_t num_obj = 0; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1031 int slen = 0; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1032 hsize_t rank = 0; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1033 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1034 group_hid = H5Gopen (loc_id, name); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1035 if (group_hid < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1036 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1037 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1038 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1039 data_hid = H5Dopen (group_hid, "classname"); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1040 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1041 if (data_hid < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1042 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1043 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1044 type_hid = H5Dget_type (data_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1045 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1046 type_class_hid = H5Tget_class (type_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1047 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1048 if (type_class_hid != H5T_STRING) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1049 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1050 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1051 space_hid = H5Dget_space (data_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1052 rank = H5Sget_simple_extent_ndims (space_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1053 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1054 if (rank != 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1055 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1056 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1057 slen = H5Tget_size (type_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1058 if (slen < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1059 goto error_cleanup; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1060 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1061 // do-while loop here to prevent goto crossing initialization of classname |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1062 do |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1063 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1064 OCTAVE_LOCAL_BUFFER (char, classname, slen); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1065 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1066 // create datatype for (null-terminated) string to read into: |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1067 st_id = H5Tcopy (H5T_C_S1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1068 H5Tset_size (st_id, slen); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1069 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1070 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1071 classname) < 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1072 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1073 H5Tclose (st_id); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1074 H5Dclose (data_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1075 H5Gclose (group_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1076 return false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1077 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1078 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1079 H5Tclose (st_id); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1080 H5Dclose (data_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1081 data_hid = -1; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1082 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1083 c_name = classname; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1084 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1085 while (0); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1086 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1087 |
7338 | 1088 #ifdef HAVE_H5GGET_NUM_OBJS |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1089 subgroup_hid = H5Gopen (group_hid, name); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1090 H5Gget_num_objs (subgroup_hid, &num_obj); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1091 H5Gclose (subgroup_hid); |
7338 | 1092 |
1093 while (current_item < static_cast<int> (num_obj) | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1094 && (retval2 = H5Giterate (group_hid, name, ¤t_item, |
7338 | 1095 hdf5_read_next_data, &dsub)) > 0) |
1096 #else | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1097 while ((retval2 = H5Giterate (group_hid, name, ¤t_item, |
7338 | 1098 hdf5_read_next_data, &dsub)) > 0) |
1099 #endif | |
1100 { | |
1101 octave_value t2 = dsub.tc; | |
1102 | |
1103 Cell tcell = t2.is_cell () ? t2.cell_value () : Cell (t2); | |
1104 | |
1105 if (error_state) | |
1106 { | |
1107 error ("load: internal error loading class elements"); | |
1108 return false; | |
1109 } | |
1110 | |
1111 m.assign (dsub.name, tcell); | |
1112 | |
1113 if (have_h5giterate_bug) | |
1114 current_item++; // H5Giterate returned the last index processed | |
1115 } | |
1116 | |
1117 if (retval2 >= 0) | |
1118 { | |
1119 map = m; | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1120 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1121 if (load_path::find_method (class_name(), "loadobj") != std::string()) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1122 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1123 octave_value in = new octave_class (*this); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1124 octave_value_list tmp = feval ("loadobj", in, 1); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1125 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1126 if (! error_state) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1127 { |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1128 map = tmp(0).map_value (); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1129 retval = true; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1130 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1131 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1132 retval = false; |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1133 } |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1134 else |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1135 retval = true; |
7338 | 1136 } |
1137 | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1138 error_cleanup: |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1139 if (data_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1140 H5Dclose (data_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1141 |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1142 if (data_hid > 0) |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1143 H5Gclose (group_hid); |
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8154
diff
changeset
|
1144 |
7338 | 1145 return retval; |
1146 } | |
1147 | |
1148 #endif | |
1149 | |
1150 mxArray * | |
1151 octave_class::as_mxArray (void) const | |
1152 { | |
1153 gripe_wrong_type_arg ("octave_class::as_mxArray ()", type_name ()); | |
1154 | |
1155 return 0; | |
1156 } | |
1157 | |
1158 bool | |
1159 octave_class::in_class_method (void) const | |
1160 { | |
1161 octave_function *fcn = octave_call_stack::current (); | |
1162 | |
1163 return (fcn | |
1164 && (fcn->is_class_method () || fcn->is_class_constructor ()) | |
1165 && fcn->dispatch_class () == class_name ()); | |
1166 } | |
1167 | |
1168 DEFUN (class, args, , | |
1169 "-*- texinfo -*-\n\ | |
1170 @deftypefn {Built-in Function} {} class (@var{expr})\n\ | |
1171 @deftypefnx {Built-in Function} {} class (@var{s}, @var{id})\n\ | |
1172 \n\ | |
1173 Return the class of the expression @var{expr}, as a string or\n\ | |
1174 create a class object from the structure @var{s} with name @var{id}.\n\ | |
1175 @end deftypefn") | |
1176 { | |
1177 octave_value retval; | |
1178 | |
1179 int nargin = args.length (); | |
1180 | |
1181 if (nargin == 1) | |
1182 retval = args(0).class_name (); | |
1183 else if (nargin == 2) | |
1184 { | |
1185 Octave_map m = args(0).map_value (); | |
1186 | |
1187 if (! error_state) | |
1188 { | |
1189 std::string id = args(1).string_value (); | |
1190 | |
1191 if (! error_state) | |
1192 { | |
1193 octave_function *fcn = octave_call_stack::caller (); | |
1194 | |
1195 if (fcn && fcn->is_class_constructor ()) | |
1196 retval = octave_value (new octave_class (m, id)); | |
1197 else | |
1198 error ("class: invalid call from outside class constructor"); | |
1199 } | |
1200 else | |
1201 error ("class: expecting character string as second argument"); | |
1202 } | |
1203 else | |
1204 error ("class: expecting structure as first argument"); | |
1205 } | |
1206 else | |
1207 print_usage (); | |
1208 | |
1209 return retval; | |
1210 } | |
1211 | |
1212 | |
1213 DEFUN (isobject, args, , | |
1214 "-*- texinfo -*-\n\ | |
1215 @deftypefn {Built-in Function} {} isobject (@var{x})\n\ | |
1216 Return true if @var{x} is a class object.\n\ | |
1217 @end deftypefn") | |
1218 { | |
1219 octave_value retval; | |
1220 | |
1221 if (args.length () == 1) | |
1222 retval = args(0).is_object (); | |
1223 else | |
1224 print_usage (); | |
1225 | |
1226 return retval; | |
1227 } | |
1228 | |
8154
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1229 DEFUN (ismethod, args, , |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1230 "-*- texinfo -*-\n\ |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1231 @deftypefn {Built-in Function} {} ismethod (@var{x}, @var{method})\n\ |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1232 Return true if @var{x} is a class object and the string @var{method}\n\ |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1233 is a method of this class.\n\ |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1234 @end deftypefn") |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1235 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1236 octave_value retval; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1237 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1238 if (args.length () == 2) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1239 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1240 octave_value arg = args(0); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1241 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1242 std::string class_name; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1243 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1244 if (arg.is_object ()) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1245 class_name = arg.class_name (); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1246 else if (arg.is_string ()) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1247 class_name = arg.string_value (); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1248 else |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1249 error ("ismethod: expecting object or class name as first argument"); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1250 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1251 if (! error_state) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1252 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1253 std::string method = args(1).string_value (); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1254 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1255 if (! error_state) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1256 { |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1257 if (load_path::find_method (class_name, method) != std::string ()) |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1258 retval = true; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1259 else |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1260 retval = false; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1261 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1262 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1263 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1264 else |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1265 print_usage (); |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1266 |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1267 return retval; |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1268 } |
265a821f6555
Add subsindex and ismethod functions
David Bateman <dbateman@free.fr>
parents:
7972
diff
changeset
|
1269 |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8658
diff
changeset
|
1270 DEFUN (methods, args, nargout, |
7338 | 1271 "-*- texinfo -*-\n\ |
1272 @deftypefn {Built-in Function} {} methods (@var{x})\n\ | |
1273 @deftypefnx {Built-in Function} {} methods (\"classname\")\n\ | |
1274 Return a cell array containing the names of the methods for the\n\ | |
1275 object @var{x} or the named class.\n\ | |
1276 @end deftypefn") | |
1277 { | |
1278 octave_value retval; | |
1279 | |
1280 if (args.length () == 1) | |
1281 { | |
1282 octave_value arg = args(0); | |
1283 | |
1284 std::string class_name; | |
1285 | |
1286 if (arg.is_object ()) | |
1287 class_name = arg.class_name (); | |
1288 else if (arg.is_string ()) | |
1289 class_name = arg.string_value (); | |
1290 else | |
1291 error ("methods: expecting object or class name as argument"); | |
1292 | |
1293 if (! error_state) | |
1294 { | |
1295 string_vector sv = load_path::methods (class_name); | |
1296 | |
1297 if (nargout == 0) | |
1298 { | |
1299 octave_stdout << "Methods for class " << class_name << ":\n\n"; | |
1300 | |
1301 sv.list_in_columns (octave_stdout); | |
1302 | |
1303 octave_stdout << std::endl; | |
1304 } | |
1305 else | |
1306 retval = sv; | |
1307 } | |
1308 } | |
1309 else | |
1310 print_usage (); | |
1311 | |
1312 return retval; | |
1313 } | |
1314 | |
1315 static bool | |
1316 is_built_in_class (const std::string& cn) | |
1317 { | |
1318 static std::set<std::string> built_in_class_names; | |
1319 | |
1320 if (built_in_class_names.empty ()) | |
1321 { | |
1322 built_in_class_names.insert ("double"); | |
1323 built_in_class_names.insert ("single"); | |
1324 built_in_class_names.insert ("cell"); | |
1325 built_in_class_names.insert ("struct"); | |
1326 built_in_class_names.insert ("logical"); | |
1327 built_in_class_names.insert ("char"); | |
1328 built_in_class_names.insert ("function handle"); | |
1329 built_in_class_names.insert ("int8"); | |
1330 built_in_class_names.insert ("uint8"); | |
1331 built_in_class_names.insert ("int16"); | |
1332 built_in_class_names.insert ("uint16"); | |
1333 built_in_class_names.insert ("int32"); | |
1334 built_in_class_names.insert ("uint32"); | |
1335 built_in_class_names.insert ("int64"); | |
1336 built_in_class_names.insert ("uint64"); | |
1337 } | |
1338 | |
1339 return built_in_class_names.find (cn) != built_in_class_names.end (); | |
1340 } | |
1341 | |
1342 DEFUN (superiorto, args, , | |
1343 "-*- texinfo -*-\n\ | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1344 @deftypefn {Built-in Function} {} superiorto (@var{class_name}, @dots{})\n\ |
7338 | 1345 When called from a class constructor, mark the object currently\n\ |
1346 constructed as having a higher precedence than @var{class_name}.\n\ | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1347 More that one such class can be specified in a single call.\n\ |
7338 | 1348 This function may only be called from a class constructor.\n\ |
1349 @end deftypefn") | |
1350 { | |
1351 octave_value retval; | |
1352 | |
1353 octave_function *fcn = octave_call_stack::caller (); | |
1354 | |
1355 if (fcn && fcn->is_class_constructor ()) | |
1356 { | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1357 for (int i = 0; i < args.length(); i++) |
7338 | 1358 { |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1359 std::string class_name = args(i).string_value (); |
7338 | 1360 |
1361 if (! error_state) | |
1362 { | |
1363 if (! is_built_in_class (class_name)) | |
1364 { | |
1365 std::string this_class_name = fcn->name (); | |
1366 | |
7972
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1367 if (! symbol_table::set_class_relationship (this_class_name, |
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1368 class_name)) |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1369 { |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1370 error ("superiorto: precedence already set for %s and %s", |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1371 this_class_name.c_str (), class_name.c_str ()); |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1372 break; |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1373 } |
7338 | 1374 } |
1375 else | |
1376 { | |
1377 // User defined classes always have higher precedence | |
1378 // than built-in classes. | |
1379 } | |
1380 } | |
1381 else | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1382 { |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1383 error ("superiorto: expecting argument to be class name"); |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1384 break; |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1385 } |
7338 | 1386 } |
1387 } | |
1388 else | |
1389 error ("superiorto: invalid call from outside class constructor"); | |
1390 | |
1391 return retval; | |
1392 } | |
1393 | |
1394 DEFUN (inferiorto, args, , | |
1395 "-*- texinfo -*-\n\ | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1396 @deftypefn {Built-in Function} {} inferiorto (@var{class_name}, @dots{})\n\ |
7338 | 1397 When called from a class constructor, mark the object currently\n\ |
1398 constructed as having a lower precedence than @var{class_name}.\n\ | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1399 More that one such class can be specified in a single call.\n\ |
7338 | 1400 This function may only be called from a class constructor.\n\ |
1401 @end deftypefn") | |
1402 { | |
1403 octave_value retval; | |
1404 | |
1405 octave_function *fcn = octave_call_stack::caller (); | |
1406 | |
1407 if (fcn && fcn->is_class_constructor ()) | |
1408 { | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1409 for (int i = 0; i < args.length(); i++) |
7338 | 1410 { |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1411 std::string class_name = args(i).string_value (); |
7338 | 1412 |
1413 if (! error_state) | |
1414 { | |
1415 if (! is_built_in_class (class_name)) | |
1416 { | |
1417 std::string this_class_name = fcn->name (); | |
1418 | |
7972
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1419 symbol_table::set_class_relationship (class_name, |
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1420 this_class_name); |
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1421 |
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1422 if (! symbol_table::set_class_relationship (this_class_name, |
5bf4e2c13ed8
make superiorto and inferiorto work
John W. Eaton <jwe@octave.org>
parents:
7960
diff
changeset
|
1423 class_name)) |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1424 { |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1425 error ("inferiorto: precedence already set for %s and %s", |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1426 this_class_name.c_str (), class_name.c_str ()); |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1427 break; |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1428 } |
7338 | 1429 } |
1430 else | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1431 { |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1432 error ("inferiorto: cannot give user-defined class lower precedence than built-in class"); |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1433 break; |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1434 } |
7338 | 1435 } |
1436 else | |
8219
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1437 { |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1438 error ("inferiorto: expecting argument to be class name"); |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1439 break; |
f8a885ccd5b4
allow multiple args for inferiorto and superiorto
David Bateman <dbateman@free.fr>
parents:
8212
diff
changeset
|
1440 } |
7338 | 1441 } |
1442 } | |
1443 else | |
1444 error ("inferiorto: invalid call from outside class constructor"); | |
1445 | |
1446 return retval; | |
1447 } | |
1448 | |
1449 /* | |
1450 ;;; Local Variables: *** | |
1451 ;;; mode: C++ *** | |
1452 ;;; End: *** | |
1453 */ |