view examples/code/oct_demo.cc @ 20441:83792dd9bcc1

Use in-place operators in m-files where possible. * scripts/audio/@audioplayer/set.m, scripts/audio/@audiorecorder/set.m, scripts/audio/mu2lin.m, scripts/elfun/cosd.m, scripts/general/del2.m, scripts/general/profexplore.m, scripts/general/quadl.m, scripts/general/rat.m, scripts/general/rotdim.m, scripts/help/get_first_help_sentence.m, scripts/help/private/__strip_html_tags__.m, scripts/image/cubehelix.m, scripts/io/textread.m, scripts/linear-algebra/duplication_matrix.m, scripts/linear-algebra/housh.m, scripts/linear-algebra/krylov.m, scripts/linear-algebra/logm.m, scripts/linear-algebra/normest.m, scripts/linear-algebra/onenormest.m, scripts/optimization/fminsearch.m, scripts/optimization/lsqnonneg.m, scripts/optimization/qp.m, scripts/plot/appearance/annotation.m, scripts/plot/appearance/axis.m, scripts/plot/appearance/legend.m, scripts/plot/appearance/specular.m, scripts/plot/draw/colorbar.m, scripts/plot/draw/hist.m, scripts/plot/draw/plotmatrix.m, scripts/plot/draw/private/__stem__.m, scripts/plot/util/__actual_axis_position__.m, scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/findobj.m, scripts/plot/util/print.m, scripts/plot/util/private/__go_draw_axes__.m, scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/rotate.m, scripts/polynomial/pchip.m, scripts/polynomial/polyaffine.m, scripts/polynomial/polyder.m, scripts/polynomial/private/__splinefit__.m, scripts/polynomial/residue.m, scripts/signal/arch_fit.m, scripts/signal/arch_rnd.m, scripts/signal/bartlett.m, scripts/signal/blackman.m, scripts/signal/freqz.m, scripts/signal/hamming.m, scripts/signal/hanning.m, scripts/signal/spectral_adf.m, scripts/signal/spectral_xdf.m, scripts/signal/stft.m, scripts/sparse/bicgstab.m, scripts/sparse/cgs.m, scripts/sparse/private/__sprand_impl__.m, scripts/sparse/qmr.m, scripts/sparse/sprandsym.m, scripts/sparse/svds.m, scripts/specfun/legendre.m, scripts/special-matrix/gallery.m, scripts/statistics/base/gls.m, scripts/statistics/models/logistic_regression.m, scripts/statistics/tests/kruskal_wallis_test.m, scripts/statistics/tests/manova.m, scripts/statistics/tests/wilcoxon_test.m, scripts/time/datevec.m: Use in-place operators in m-files where possible.
author Rik <rik@octave.org>
date Tue, 26 May 2015 21:07:42 -0700
parents c8240a60dd01
children
line wrap: on
line source

// oct_demo.cc -- example of a dynamically linked function for Octave.

// To use this file, your version of Octave must support dynamic
// linking.  To find out if it does, type the command
//
//   octave_config_info ("ENABLE_DYNAMIC_LINKING")
//
// at the Octave prompt.  Support for dynamic linking is included if
// this expression returns the string "yes".
//
// To compile this file, type the command
//
//   mkoctfile oct_demo.cc
//
// from within Octave or from the shell prompt.  This will create a file
// called oct_demo.oct that can be loaded by Octave.  To test the
// oct_demo.oct file, start Octave and type the command
//
//   oct_demo ("easy as", 1, 2, 3)
//
// at the Octave prompt.  Octave should respond by printing
//
//   Hello, world!
//   easy as
//   1
//   2
//   3
//   ans = 3

// Additional samples of real dynamically loaded functions are available in
// the files of the libinterp/dldfcn directory of the Octave distribution.
// See also the chapter External Code Interface in the documentation.

#include <iostream>

#include <octave/oct.h>

// Every user function should include <octave/oct.h> which imports the
// basic set of Octave header files required.  In particular this will define
// the DEFUN_DLD macro (defun-dld.h) which is used for every user function
// that is visible to Octave.

// The four arguments to the DEFUN_DLD macro are:
// 1) The function name as seen in Octave.
// 2) The variable to hold any inputs (of type octave_value_list)
// 3) The number of output arguments
// 4) A string to use as help text if 'help <function_name>' is entered.
//
// Note below that the third parameter (nargout) of DEFUN_DLD is not used,
// so it is omitted from the list of arguments in order to avoid a warning
// from gcc about an unused function parameter.

DEFUN_DLD (oct_demo, args, ,
           "[...] = oct_demo (...)\n\
\n\
Print a greeting followed by the values of all the arguments passed.\n\
Return all arguments in reverse order.")
{
  // The list of values to return.  See the declaration in oct-obj.h

  octave_value_list retval;

  // This stream is normally connected to the pager.

  octave_stdout << "Hello, world!\n";

  // The inputs to this function are available in args.

  int nargin = args.length ();

  // The octave_value_list class is a zero-based array of octave_value objects.
  // The declaration for the octave_value class is in the file ov.h.
  // The print() method will send its output to octave_stdout,
  // so it will also end up going through the pager.

  for (int i = 0; i < nargin; i++)
    {
      octave_value tmp = args(i);
      tmp.print (octave_stdout);
      retval(nargin-i-1) = tmp;
    }

  return retval;
}