Mercurial > hg > octave-nkf
changeset 20753:2f8500ca91d3
eliminate error_state from example files
* addtwomatrices.cc, celldemo.cc, embedded.cc, fortrandemo.cc,
funcdemo.cc, globaldemo.cc, helloworld.cc, make_int.cc, paramdemo.cc,
stringdemo.cc, structdemo.cc, unwinddemo.cc:
Eliminate use of global error_state variable.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 03 Oct 2015 16:05:27 -0400 |
parents | c547458dc10e |
children | 52ce821a52fd |
files | examples/code/addtwomatrices.cc examples/code/celldemo.cc examples/code/embedded.cc examples/code/fortrandemo.cc examples/code/funcdemo.cc examples/code/globaldemo.cc examples/code/helloworld.cc examples/code/make_int.cc examples/code/paramdemo.cc examples/code/stringdemo.cc examples/code/structdemo.cc examples/code/unwinddemo.cc |
diffstat | 12 files changed, 141 insertions(+), 178 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/code/addtwomatrices.cc +++ b/examples/code/addtwomatrices.cc @@ -2,17 +2,11 @@ DEFUN_DLD (addtwomatrices, args, , "Add A to B") { - int nargin = args.length (); - - if (nargin != 2) + if (args.length () != 2) print_usage (); - else - { - NDArray A = args(0).array_value (); - NDArray B = args(1).array_value (); - if (! error_state) - return octave_value (A + B); - } - return octave_value_list (); + NDArray A = args(0).array_value (); + NDArray B = args(1).array_value (); + + return octave_value (A + B); }
--- a/examples/code/celldemo.cc +++ b/examples/code/celldemo.cc @@ -4,19 +4,16 @@ DEFUN_DLD (celldemo, args, , "Cell Demo") { octave_value_list retval; - int nargin = args.length (); + + if (args.length () != 1) + print_usage (); - if (nargin != 1) - print_usage (); - else + Cell c = args(0).cell_value (); + + for (octave_idx_type i = 0; i < c.numel (); i++) { - Cell c = args(0).cell_value (); - if (! error_state) - for (octave_idx_type i = 0; i < c.numel (); i++) - { - retval(i) = c(i); // using operator syntax - //retval(i) = c.elem (i); // using method syntax - } + retval(i) = c(i); // using operator syntax + //retval(i) = c.elem (i); // using method syntax } return retval;
--- a/examples/code/embedded.cc +++ b/examples/code/embedded.cc @@ -21,7 +21,7 @@ octave_value_list out = feval ("gcd", in, 1); - if (! error_state && out.length () > 0) + if (out.length () > 0) std::cout << "GCD of [" << in(0).int_value () << ", "
--- a/examples/code/fortrandemo.cc +++ b/examples/code/fortrandemo.cc @@ -11,25 +11,22 @@ DEFUN_DLD (fortrandemo, args, , "Fortran Demo") { octave_value_list retval; - int nargin = args.length (); - if (nargin != 1) + if (args.length () != 1) print_usage (); - else - { - NDArray a = args(0).array_value (); - if (! error_state) - { - double *av = a.fortran_vec (); - octave_idx_type na = a.numel (); - OCTAVE_LOCAL_BUFFER (char, ctmp, 128); + + NDArray a = args(0).array_value (); + + double *av = a.fortran_vec (); + octave_idx_type na = a.numel (); - F77_XFCN (fortransub, FORTSUB, - (na, av, ctmp F77_CHAR_ARG_LEN (128))); + OCTAVE_LOCAL_BUFFER (char, ctmp, 128); - retval(1) = std::string (ctmp); - retval(0) = a; - } - } + F77_XFCN (fortransub, FORTSUB, + (na, av, ctmp F77_CHAR_ARG_LEN (128))); + + retval(1) = std::string (ctmp); + retval(0) = a; + return retval; }
--- a/examples/code/funcdemo.cc +++ b/examples/code/funcdemo.cc @@ -4,29 +4,31 @@ DEFUN_DLD (funcdemo, args, nargout, "Function Demo") { octave_value_list retval; + int nargin = args.length (); if (nargin < 2) print_usage (); - else + + octave_value_list newargs; + + for (octave_idx_type i = nargin - 1; i > 0; i--) + newargs(i-1) = args(i); + + if (args(0).is_function_handle () || args(0).is_inline_function ()) { - octave_value_list newargs; - for (octave_idx_type i = nargin - 1; i > 0; i--) - newargs(i-1) = args(i); - if (args(0).is_function_handle () || args(0).is_inline_function ()) - { - octave_function *fcn = args(0).function_value (); - if (! error_state) - retval = feval (fcn, newargs, nargout); - } - else if (args(0).is_string ()) - { - std::string fcn = args(0).string_value (); - if (! error_state) - retval = feval (fcn, newargs, nargout); - } - else - error ("funcdemo: INPUT must be string, inline, or function handle"); + octave_function *fcn = args(0).function_value (); + + retval = feval (fcn, newargs, nargout); } + else if (args(0).is_string ()) + { + std::string fcn = args(0).string_value (); + + retval = feval (fcn, newargs, nargout); + } + else + error ("funcdemo: INPUT must be string, inline, or function handle"); + return retval; }
--- a/examples/code/globaldemo.cc +++ b/examples/code/globaldemo.cc @@ -3,23 +3,20 @@ DEFUN_DLD (globaldemo, args, , "Global Demo") { octave_value retval; - int nargin = args.length (); + + if (args.length () != 1) + print_usage (); - if (nargin != 1) - print_usage (); + std::string s = args(0).string_value (); + + octave_value tmp = get_global_value (s, true); + + if (tmp.is_defined ()) + retval = tmp; else - { - std::string s = args(0).string_value (); - if (! error_state) - { - octave_value tmp = get_global_value (s, true); - if (tmp.is_defined ()) - retval = tmp; - else - retval = "Global variable not found"; + retval = "Global variable not found"; - set_global_value ("a", 42.0); - } - } + set_global_value ("a", 42.0); + return retval; }
--- a/examples/code/helloworld.cc +++ b/examples/code/helloworld.cc @@ -3,10 +3,8 @@ DEFUN_DLD (helloworld, args, nargout, "Hello World Help String") { - int nargin = args.length (); - octave_stdout << "Hello World has " - << nargin << " input arguments and " + << args.length () << " input arguments and " << nargout << " output arguments.\n"; return octave_value_list ();
--- a/examples/code/make_int.cc +++ b/examples/code/make_int.cc @@ -278,8 +278,7 @@ { double d = args(0).double_value (); - if (! error_state) - retval = octave_value (new octave_integer (NINT (d))); + retval = octave_value (new octave_integer (NINT (d))); } else usage ("make_int");
--- a/examples/code/paramdemo.cc +++ b/examples/code/paramdemo.cc @@ -2,29 +2,33 @@ DEFUN_DLD (paramdemo, args, nargout, "Parameter Check Demo") { - octave_value retval; - int nargin = args.length (); + if (args.length () != 1) + print_usage (); - if (nargin != 1) - print_usage (); - else if (nargout != 0) + if (nargout != 0) error ("paramdemo: OUTPUT argument required"); - else - { - NDArray m = args(0).array_value (); - double min_val = -10.0; - double max_val = 10.0; - octave_stdout << "Properties of input array:\n"; - if (m.any_element_is_negative ()) - octave_stdout << " includes negative values\n"; - if (m.any_element_is_inf_or_nan ()) - octave_stdout << " includes Inf or NaN values\n"; - if (m.any_element_not_one_or_zero ()) - octave_stdout << " includes other values than 1 and 0\n"; - if (m.all_elements_are_int_or_inf_or_nan ()) - octave_stdout << " includes only int, Inf or NaN values\n"; - if (m.all_integers (min_val, max_val)) - octave_stdout << " includes only integers in [-10,10]\n"; - } - return retval; + + NDArray m = args(0).array_value (); + + double min_val = -10.0; + double max_val = 10.0; + + octave_stdout << "Properties of input array:\n"; + + if (m.any_element_is_negative ()) + octave_stdout << " includes negative values\n"; + + if (m.any_element_is_inf_or_nan ()) + octave_stdout << " includes Inf or NaN values\n"; + + if (m.any_element_not_one_or_zero ()) + octave_stdout << " includes other values than 1 and 0\n"; + + if (m.all_elements_are_int_or_inf_or_nan ()) + octave_stdout << " includes only int, Inf or NaN values\n"; + + if (m.all_integers (min_val, max_val)) + octave_stdout << " includes only integers in [-10,10]\n"; + + return octave_value (); }
--- a/examples/code/stringdemo.cc +++ b/examples/code/stringdemo.cc @@ -3,27 +3,25 @@ DEFUN_DLD (stringdemo, args, , "String Demo") { octave_value_list retval; - int nargin = args.length (); - if (nargin != 1) + if (args.length () != 1) print_usage (); - else - { - charMatrix ch = args(0).char_matrix_value (); + + charMatrix ch = args(0).char_matrix_value (); + + retval(1) = octave_value (ch, '\''); // Single Quote String + + octave_idx_type nr = ch.rows (); - if (! error_state) - { - retval(1) = octave_value (ch, '\''); // Single Quote String + for (octave_idx_type i = 0; i < nr / 2; i++) + { + std::string tmp = ch.row_as_string (i); - octave_idx_type nr = ch.rows (); - for (octave_idx_type i = 0; i < nr / 2; i++) - { - std::string tmp = ch.row_as_string (i); - ch.insert (ch.row_as_string (nr-i-1).c_str (), i, 0); - ch.insert (tmp.c_str (), nr-i-1, 0); - } - retval(0) = octave_value (ch, '"'); // Double Quote String - } + ch.insert (ch.row_as_string (nr-i-1).c_str (), i, 0); + ch.insert (tmp.c_str (), nr-i-1, 0); } + + retval(0) = octave_value (ch, '"'); // Double Quote String + return retval; }
--- a/examples/code/structdemo.cc +++ b/examples/code/structdemo.cc @@ -3,43 +3,30 @@ DEFUN_DLD (structdemo, args, , "Struct Demo") { - octave_value retval; - int nargin = args.length (); - - if (args.length () == 2) - { - octave_scalar_map arg0 = args(0).scalar_map_value (); - //octave_map arg0 = args(0).map_value (); - - if (! error_state) - { - std::string arg1 = args(1).string_value (); - - if (! error_state) - { - octave_value tmp = arg0.contents (arg1); - //octave_value tmp = arg0.contents (arg1)(0); - - if (tmp.is_defined ()) - { - octave_scalar_map st; - - st.assign ("selected", tmp); - - retval = octave_value (st); - } - else - error ("structdemo: struct does not have a field named '%s'\n", - arg1.c_str ()); - } - else - error ("structdemo: ARG2 must be a character string"); - } - else - error ("structdemo: ARG1 must be a struct"); - } - else + if (args.length () != 2) print_usage (); - return retval; + if (! args(0).is_map ()) + error ("structdemo: ARG1 must be a struct"); + + octave_scalar_map arg0 = args(0).scalar_map_value (); + //octave_map arg0 = args(0).map_value (); + + if (! args(1).is_string ()) + error ("structdemo: ARG2 must be a character string"); + + std::string arg1 = args(1).string_value (); + + octave_value tmp = arg0.contents (arg1); + //octave_value tmp = arg0.contents (arg1)(0); + + if (! tmp.is_defined ()) + error ("structdemo: struct does not have a field named '%s'\n", + arg1.c_str ()); + + octave_scalar_map st; + + st.assign ("selected", tmp); + + return octave_value (st); }
--- a/examples/code/unwinddemo.cc +++ b/examples/code/unwinddemo.cc @@ -15,33 +15,23 @@ DEFUN_DLD (unwinddemo, args, nargout, "Unwind Demo") { - octave_value retval; - int nargin = args.length (); - - if (nargin < 2) + if (args.length () < 2) print_usage (); - else - { - NDArray a = args(0).array_value (); - NDArray b = args(1).array_value (); + + NDArray a = args(0).array_value (); + NDArray b = args(1).array_value (); - if (! error_state) - { - // Declare unwind_protect frame which lasts as long as - // the variable frame has scope. - unwind_protect frame; - frame.add_fcn (set_liboctave_warning_handler, - current_liboctave_warning_handler); + // Declare unwind_protect frame which lasts as long as + // the variable frame has scope. + unwind_protect frame; + frame.add_fcn (set_liboctave_warning_handler, + current_liboctave_warning_handler); - frame.add_fcn (set_liboctave_warning_with_id_handler, - current_liboctave_warning_with_id_handler); - - set_liboctave_warning_handler (my_err_handler); - set_liboctave_warning_with_id_handler (my_err_with_id_handler); + frame.add_fcn (set_liboctave_warning_with_id_handler, + current_liboctave_warning_with_id_handler); - retval = octave_value (quotient (a, b)); - } - } + set_liboctave_warning_handler (my_err_handler); + set_liboctave_warning_with_id_handler (my_err_with_id_handler); - return retval; + return octave_value (quotient (a, b)); }