-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoct_demo.cc
76 lines (64 loc) · 2.22 KB
/
oct_demo.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// oct_demo.cc -- example of a dynamically linked function for Octave.
//
// 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 code are in the examples directory of the Octave
// distribution. See also the Octave manual
//
// https://octave.org/doc/v5.2.0/External-Code-Interface.html
#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.
DEFUN_DLD (oct_demo, args, /* nargout */,
"[...] = oct_demo (...)\n\
\n\
Print a greeting followed by the values of all input arguments.\n\
\n\
Return all arguments in reverse order.")
{
// The inputs to this are available in the variable named args.
int nargin = args.length ();
// The list of values to return. See the declaration in ovl.h.
octave_value_list retval;
// This stream is normally connected to the pager.
octave_stdout << "Hello, world!\n";
// 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;
}