comparison libinterp/octave-value/ov-fcn-inline.cc @ 20939:b17fda023ca6

maint: Use new C++ archetype in more files. Place input validation first in files. Move declaration of retval down in function to be closer to point of usage. Eliminate else clause after if () error. Use "return ovl()" where it makes sense. * find.cc, gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, givens.cc, graphics.cc, help.cc, hess.cc, hex2num.cc, input.cc, kron.cc, load-path.cc, load-save.cc, lookup.cc, mappers.cc, matrix_type.cc, mgorth.cc, nproc.cc, ordschur.cc, pager.cc, pinv.cc, pr-output.cc, profiler.cc, psi.cc, quad.cc, rcond.cc, regexp.cc, schur.cc, sighandlers.cc, sparse.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, urlwrite.cc, utils.cc, variables.cc, __delaunayn__.cc, __eigs__.cc, __glpk__.cc, __magick_read__.cc, __osmesa_print__.cc, __voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, chol.cc, colamd.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc, ov-bool-mat.cc, ov-cell.cc, ov-class.cc, ov-classdef.cc, ov-fcn-handle.cc, ov-fcn-inline.cc, ov-flt-re-mat.cc, ov-java.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-re-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-usr-fcn.cc, ov.cc, octave.cc: Use new C++ archetype in more files.
author Rik <rik@octave.org>
date Fri, 18 Dec 2015 15:37:22 -0800
parents 384ff5aa9437
children 77f5591878bf
comparison
equal deleted inserted replaced
20938:aac911d8847b 20939:b17fda023ca6
673 functions from strings is through the use of anonymous functions\n\ 673 functions from strings is through the use of anonymous functions\n\
674 (@pxref{Anonymous Functions}) or @code{str2func}.\n\ 674 (@pxref{Anonymous Functions}) or @code{str2func}.\n\
675 @seealso{argnames, formula, vectorize, str2func}\n\ 675 @seealso{argnames, formula, vectorize, str2func}\n\
676 @end deftypefn") 676 @end deftypefn")
677 { 677 {
678 octave_value retval;
679
680 int nargin = args.length (); 678 int nargin = args.length ();
681 679
682 if (nargin == 0) 680 if (nargin == 0)
683 print_usage (); 681 print_usage ();
684 682
733 else if (! is_arg) 731 else if (! is_arg)
734 { 732 {
735 if (c == 'e' || c == 'E') 733 if (c == 'e' || c == 'E')
736 { 734 {
737 // possible number in exponent form, not arg 735 // possible number in exponent form, not arg
738 if (isdigit (fun[i]) 736 if (isdigit (fun[i]) || fun[i] == '-' || fun[i] == '+')
739 || fun[i] == '-' || fun[i] == '+')
740 continue; 737 continue;
741 } 738 }
742 is_arg = true; 739 is_arg = true;
743 tmp_arg.append (1, c); 740 tmp_arg.append (1, c);
744 } 741 }
807 std::string s = args(i).xstring_value ("inline: additional arguments must be strings"); 804 std::string s = args(i).xstring_value ("inline: additional arguments must be strings");
808 fargs(i-1) = s; 805 fargs(i-1) = s;
809 } 806 }
810 } 807 }
811 808
812 return octave_value (new octave_fcn_inline (fun, fargs)); 809 return ovl (new octave_fcn_inline (fun, fargs));
813 } 810 }
814 811
815 /* 812 /*
816 %!shared fn 813 %!shared fn
817 %! fn = inline ("x.^2 + 1"); 814 %! fn = inline ("x.^2 + 1");
837 Note that @code{char (@var{fun})} is equivalent to\n\ 834 Note that @code{char (@var{fun})} is equivalent to\n\
838 @code{formula (@var{fun})}.\n\ 835 @code{formula (@var{fun})}.\n\
839 @seealso{char, argnames, inline, vectorize}\n\ 836 @seealso{char, argnames, inline, vectorize}\n\
840 @end deftypefn") 837 @end deftypefn")
841 { 838 {
842 octave_value retval;
843
844 if (args.length () != 1) 839 if (args.length () != 1)
845 print_usage (); 840 print_usage ();
846 841
847 octave_fcn_inline* fn = args(0).fcn_inline_value (true); 842 octave_fcn_inline* fn = args(0).fcn_inline_value (true);
848 843
849 if (fn) 844 if (! fn)
850 retval = octave_value (fn->fcn_text ());
851 else
852 error ("formula: FUN must be an inline function"); 845 error ("formula: FUN must be an inline function");
853 846
854 return retval; 847 return ovl (fn->fcn_text ());
855 } 848 }
856 849
857 /* 850 /*
858 %!assert (formula (fn), "x.^2 + 1") 851 %!assert (formula (fn), "x.^2 + 1")
859 %!assert (formula (fn), char (fn)) 852 %!assert (formula (fn), char (fn))
870 Return a cell array of character strings containing the names of the\n\ 863 Return a cell array of character strings containing the names of the\n\
871 arguments of the inline function @var{fun}.\n\ 864 arguments of the inline function @var{fun}.\n\
872 @seealso{inline, formula, vectorize}\n\ 865 @seealso{inline, formula, vectorize}\n\
873 @end deftypefn") 866 @end deftypefn")
874 { 867 {
875 octave_value retval;
876
877 if (args.length () != 1) 868 if (args.length () != 1)
878 print_usage (); 869 print_usage ();
879 870
880 octave_fcn_inline *fn = args(0).fcn_inline_value (true); 871 octave_fcn_inline *fn = args(0).fcn_inline_value (true);
881 872
882 if (fn) 873 if (! fn)
883 {
884 string_vector t1 = fn->fcn_arg_names ();
885
886 Cell t2 (dim_vector (t1.numel (), 1));
887
888 for (int i = 0; i < t1.numel (); i++)
889 t2(i) = t1(i);
890
891 retval = t2;
892 }
893 else
894 error ("argnames: FUN must be an inline function"); 874 error ("argnames: FUN must be an inline function");
895 875
896 return retval; 876 string_vector t1 = fn->fcn_arg_names ();
877
878 Cell t2 (dim_vector (t1.numel (), 1));
879
880 for (int i = 0; i < t1.numel (); i++)
881 t2(i) = t1(i);
882
883 return ovl (t2);
897 } 884 }
898 885
899 /* 886 /*
900 %!assert (argnames (fn), {"x"}) 887 %!assert (argnames (fn), {"x"})
901 %!assert (argnames (inline ("1e-3*y + 2e4*z")), {"y"; "z"}) 888 %!assert (argnames (inline ("1e-3*y + 2e4*z")), {"y"; "z"})
925 @end group\n\ 912 @end group\n\
926 @end example\n\ 913 @end example\n\
927 @seealso{inline, formula, argnames}\n\ 914 @seealso{inline, formula, argnames}\n\
928 @end deftypefn") 915 @end deftypefn")
929 { 916 {
930 octave_value retval;
931
932 if (args.length () != 1) 917 if (args.length () != 1)
933 print_usage (); 918 print_usage ();
934 919
935 std::string old_func; 920 std::string old_func;
936 octave_fcn_inline* old = 0; 921 octave_fcn_inline* old = 0;
972 new_func.append (t1); 957 new_func.append (t1);
973 i++; 958 i++;
974 } 959 }
975 960
976 if (func_is_string) 961 if (func_is_string)
977 retval = octave_value (new_func); 962 return ovl (new_func);
978 else 963 else
979 retval = octave_value (new octave_fcn_inline 964 return ovl (new octave_fcn_inline (new_func, old->fcn_arg_names ()));
980 (new_func, old->fcn_arg_names ()));
981
982 return retval;
983 } 965 }
984 966
985 /* 967 /*
986 %!assert (char (vectorize (fn)), "x.^2 + 1") 968 %!assert (char (vectorize (fn)), "x.^2 + 1")
987 %!assert (char (vectorize (inline ("1e-3*y + 2e4*z"))), "1e-3.*y + 2e4.*z") 969 %!assert (char (vectorize (inline ("1e-3*y + 2e4*z"))), "1e-3.*y + 2e4.*z")