From octave-maintainers-request at bevo dot che dot wisc dot edu Fri Nov 21 22:02:13 2003 Subject: Re: restructuring load-save From: "John W. Eaton" To: David Bateman Cc: octave-maintainers mailing list Date: Fri, 21 Nov 2003 22:01:25 -0600 On 22-Nov-2003, David Bateman wrote: | However, for the load functions I've run into a problem of what is the best | way to convert from a string of the type name to an octave_value. I thought | I could use octave_value_typeinfo class, something like | | octave_value | octave_value_typeinfo::lookup_type (const std::string &nm) | { | for (int i = 0;i < num_types; i++) | if (nm == types(i)) | { | // FIXME: How do I initialize an octave_value of the right type? | return octave_value(); | } | | return octave_value(); | } | | I can use this to get the static_type_id of the type, but don't see an | obvious way of converting that to an octave_value. Does anyone have a | clue? I think we have to invent something. It looks like a reasonable thing to do would be to make the static function register_type that is defined for each octave_value type also pass an object of the type it is registering. Then octave_value_typeinfo can cache that value and return it in a function like you have above. I think the following change should do it (I haven't tested it completely, but I think it should do what you want). jwe Index: ov.h =================================================================== RCS file: /usr/local/cvsroot/octave/src/ov.h,v retrieving revision 1.90 diff -u -r1.90 ov.h --- ov.h 14 Nov 2003 19:49:56 -0000 1.90 +++ ov.h 22 Nov 2003 03:59:41 -0000 at @ -767,7 +774,13 @@ #define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c) \ int t::t_id (-1); \ const std::string t::t_name (n); \ - const std::string t::c_name (c) + const std::string t::c_name (c); \ + void t::register_type (void) \ + { \ + t_id = octave_value_typeinfo::register_type (t::t_name, \ + t::c_name, \ + octave_value (new t)); \ + } // If TRUE, print a warning for assignments like // Index: ov-typeinfo.h =================================================================== RCS file: /usr/local/cvsroot/octave/src/ov-typeinfo.h,v retrieving revision 1.14 diff -u -r1.14 ov-typeinfo.h --- ov-typeinfo.h 14 Nov 2003 19:49:56 -0000 1.14 +++ ov-typeinfo.h 22 Nov 2003 03:59:41 -0000 at @ -44,7 +44,8 @@ static bool instance_ok (void); - static int register_type (const std::string&, const std::string&); + static int register_type (const std::string&, const std::string&, + const octave_value&); static bool register_unary_op (octave_value::unary_op, int, unary_op_fcn); at @ -64,6 +65,12 @@ static bool register_widening_op (int, int, type_conv_fcn); + static octave_value + lookup_type (const std::string& nm) + { + return instance->do_lookup_type (nm); + } + static unary_op_fcn lookup_unary_op (octave_value::unary_op op, int t) { at @ -138,6 +145,8 @@ Array types; + Array vals; + Array2 unary_ops; Array2 non_const_unary_ops; at @ -152,7 +161,8 @@ Array2 widening_ops; - int do_register_type (const std::string&, const std::string&); + int do_register_type (const std::string&, const std::string&, + const octave_value&); bool do_register_unary_op (octave_value::unary_op, int, unary_op_fcn); at @ -172,6 +182,8 @@ bool do_register_widening_op (int, int, type_conv_fcn); + octave_value do_lookup_type (const std::string& nm); + unary_op_fcn do_lookup_unary_op (octave_value::unary_op, int); non_const_unary_op_fcn do_lookup_non_const_unary_op Index: ov-typeinfo.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/ov-typeinfo.cc,v retrieving revision 1.26 diff -u -r1.26 ov-typeinfo.cc --- ov-typeinfo.cc 15 Nov 2003 12:51:20 -0000 1.26 +++ ov-typeinfo.cc 22 Nov 2003 03:59:41 -0000 at @ -82,10 +82,11 @@ int octave_value_typeinfo::register_type (const std::string& t_name, - const std::string& c_name) + const std::string& c_name, + const octave_value& val) { return (instance_ok ()) - ? instance->do_register_type (t_name, c_name) : -1; + ? instance->do_register_type (t_name, c_name, val) : -1; } bool at @ -149,7 +150,8 @@ int octave_value_typeinfo::do_register_type (const std::string& t_name, - const std::string& c_name) + const std::string& c_name, + const octave_value& val) { int i = 0; at @ -165,6 +167,8 @@ types.resize (len, std::string ()); + vals.resize (len, octave_value ()); + unary_ops.resize (static_cast (octave_value::num_unary_ops), len, static_cast (0)); at @ -188,6 +192,8 @@ types (i) = t_name; + vals (i) = val; + num_types++; return i; at @ -323,6 +329,23 @@ return false; } +octave_value +octave_value_typeinfo::do_lookup_type (const std::string& nm) +{ + octave_value retval; + + for (int i = 0; i < num_types; i++) + { + if (nm == types(i)) + { + retval = vals(i); + break; + } + } + + return retval; +} + unary_op_fcn octave_value_typeinfo::do_lookup_unary_op (octave_value::unary_op op, int t) {