Mercurial > octave-nkf
diff libinterp/octave-value/ov-classdef.cc @ 19218:e0a7718ac085
Implement calling "struct" on classdef objects.
* ov-classdef.h (cdef_object::map_value, octave_classdef::map_value):
New methods.
(cdef_class::get_properties,
cdef_class::cdef_class_rep::get_properties,
cdef_class::cdef_class_rep::find_properties): Change signature to include
an int argument "mode".
(cdef_class::get_property_map,
cdef_class::cdef_class_rep::get_property_map): New methods.
(class cdef_class): New enum to hold possible values of mode argument
for method get_properties/get_property_map.
(cdef_object_rep::map_keys): Code style.
* ov-classdef.cc (cdef_class::cdef_class_rep::find_properties): Change
signature to include an int argument "mode".
(cdef_class::cdef_class_rep::get_properties): Likewise. Use
get_property_map instead of find_properties.
(cdef_class::cdef_class_rep::get_property_map): New method.
(cdef_object::map_value): Likewise.
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Sun, 28 Sep 2014 21:43:51 -0400 |
parents | 56bc1464ec59 |
children | eb90f921bbc0 |
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.cc Sun Sep 28 12:55:03 2014 -0700 +++ b/libinterp/octave-value/ov-classdef.cc Sun Sep 28 21:43:51 2014 -0400 @@ -1330,6 +1330,63 @@ //---------------------------------------------------------------------------- +octave_map +cdef_object::map_value (void) const +{ + octave_map retval; + + warning_with_id ("Octave:classdef-to-struct", + "struct: converting a classdef object into a struct " + "overrides the access restrictions defined for properties. " + "All properties are returned, including private and " + "protected ones."); + + cdef_class cls = get_class (); + + if (cls.ok ()) + { + std::map<std::string, cdef_property> props; + + props = cls.get_property_map (cdef_class::property_all); + + for (std::map<std::string, cdef_property>::iterator it = props.begin (); + it != props.end (); ++it) + { + octave_value pvalue; + + if (is_array ()) + { + Array<cdef_object> a_obj = array_value (); + + Cell cvalue (a_obj.dims ()); + + for (octave_idx_type i = 0; i < a_obj.numel (); i++) + { + cvalue (i) = it->second.get_value (a_obj(i), false); + + if (error_state) + break; + } + + if (! error_state) + retval.setfield (it->first, cvalue); + } + else + { + Cell cvalue (dim_vector (1, 1), it->second.get_value (*this, false)); + + if (! error_state) + retval.setfield (it->first, cvalue); + } + + if (error_state) + break; + } + } + + return retval; +} + string_vector cdef_object_rep::map_keys (void) const { @@ -2218,11 +2275,11 @@ } Cell -cdef_class::cdef_class_rep::get_properties (void) +cdef_class::cdef_class_rep::get_properties (int mode) { std::map<std::string,cdef_property> props; - find_properties (props, false); + props = get_property_map (mode); if (! error_state) { @@ -2240,9 +2297,19 @@ return Cell (); } +std::map<std::string, cdef_property> +cdef_class::cdef_class_rep::get_property_map (int mode) +{ + std::map<std::string,cdef_property> props; + + find_properties (props, mode); + + return props; +} + void cdef_class::cdef_class_rep::find_properties (std::map<std::string,cdef_property>& props, - bool only_inherited) + int mode) { property_const_iterator it; @@ -2253,7 +2320,7 @@ if (props.find (nm) == props.end ()) { - if (only_inherited) + if (mode == property_inherited) { octave_value acc = it->second.get ("GetAccess"); @@ -2275,7 +2342,10 @@ cdef_class cls = lookup_class (super_classes(i)); if (! error_state) - cls.get_rep ()->find_properties (props, true); + cls.get_rep ()->find_properties (props, + (mode == property_all ? + property_all : + property_inherited)); else break; }