Mercurial > hg > octave-terminal
changeset 13329:80f7564a3849
handle class objects in num2cell
* cellfun.cc (Fnum2cell): Also handle class objects.
* test_classes.m: New tests.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 12 Oct 2011 12:18:27 -0400 |
parents | a2144dbc2f3a |
children | b68d95054947 |
files | src/DLD-FUNCTIONS/cellfun.cc test/classes/test_classes.m |
diffstat | 2 files changed, 30 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/cellfun.cc +++ b/src/DLD-FUNCTIONS/cellfun.cc @@ -45,6 +45,7 @@ #include "gripes.h" #include "utils.h" +#include "ov-class.h" #include "ov-scalar.h" #include "ov-float.h" #include "ov-complex.h" @@ -1800,10 +1801,26 @@ retval = do_num2cell (array.array_value (), dimv); } } - else if (array.is_map ()) - retval = do_num2cell (array.map_value (), dimv); + else if (array.is_map () || array.is_object ()) + { + Cell tmp = do_num2cell (array.map_value (), dimv); + + if (array.is_object ()) + { + std::string cname = array.class_name (); + std::list<std::string> parents = array.parent_class_name_list (); + + for (octave_idx_type i = 0; i < tmp.numel (); i++) + tmp(i) = octave_value (new octave_class (tmp(i).map_value (), + cname, parents)); + } + + retval = tmp; + } else if (array.is_cell ()) retval = do_num2cell (array.cell_value (), dimv); + else if (array.is_object ()) + retval = do_num2cell (array.cell_value (), dimv); else gripe_wrong_type_arg ("num2cell", array); }
--- a/test/classes/test_classes.m +++ b/test/classes/test_classes.m @@ -199,3 +199,14 @@ %! assert(isa(grk,'Blork')) %! assert(isa(grk,'Snork')) %! assert(isa(grk,'Spork')) + +%!test +%! d = Dork (); +%! x = [d,d]; +%! assert (size (x), [1, 2]) +%! assert (class (x), "Dork") + +%!test +%! d = Dork (); +%! x = [d,d]; +%! assert (num2cell (x), {d, d});