7019
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997, 1999, 2002, 2003, 2007 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
|
20 |
|
21 */ |
|
22 |
2134
|
23 // hello.cc -- example of a dynamically linked function for Octave. |
|
24 |
|
25 // To use this file, your version of Octave must support dynamic |
2153
|
26 // linking. To find out if it does, type the command |
2134
|
27 // |
4128
|
28 // octave_config_info ("ENABLE_DYNAMIC_LINKING") |
2134
|
29 // |
|
30 // at the Octave prompt. Support for dynamic linking is included if |
4128
|
31 // this expression returns the string "true". |
2134
|
32 // |
|
33 // To compile this file, type the command |
|
34 // |
2137
|
35 // mkoctfile hello.cc |
2134
|
36 // |
|
37 // at the shell prompt. The script mkoctfile should have been |
|
38 // installed along with Octave. Running it will create a file called |
|
39 // hello.oct that can be loaded by Octave. To test the hello.oct |
|
40 // file, start Octave and type the command |
|
41 // |
|
42 // hello ("easy as", 1, 2, 3) |
|
43 // |
|
44 // at the Octave prompt. Octave should respond by printing |
|
45 // |
|
46 // Hello, world! |
|
47 // easy as |
|
48 // 1 |
|
49 // 2 |
|
50 // 3 |
|
51 // ans = 3 |
|
52 |
|
53 // Additional examples are available in the files in the src directory |
|
54 // of the Octave distribution that use the macro DEFUN_DLD_BUILTIN. |
|
55 // Currently, this includes the files |
|
56 // |
3045
|
57 // balance.cc fft.cc ifft.cc minmax.cc sort.cc |
|
58 // chol.cc fft2.cc ifft2.cc pinv.cc svd.cc |
|
59 // colloc.cc filter.cc inv.cc qr.cc syl.cc |
|
60 // dassl.cc find.cc log.cc quad.cc |
|
61 // det.cc fsolve.cc lsode.cc qzval.cc |
|
62 // eig.cc givens.cc lu.cc rand.cc |
|
63 // expm.cc hess.cc minmax.cc schur.cc |
2134
|
64 // |
|
65 // The difference between DEFUN_DLD and DEFUN_DLD_BUILTIN is that |
|
66 // DEFUN_DLD_BUILTIN can define a built-in function that is not |
|
67 // dynamically loaded if the operating system does not support dynamic |
|
68 // linking. To define your own dynamically linked functions you |
|
69 // should use DEFUN_DLD. |
|
70 |
|
71 #include <octave/config.h> |
|
72 |
4399
|
73 #include <iostream> |
2134
|
74 |
|
75 #include <octave/defun-dld.h> |
|
76 #include <octave/error.h> |
|
77 #include <octave/oct-obj.h> |
|
78 #include <octave/pager.h> |
|
79 #include <octave/symtab.h> |
|
80 #include <octave/variables.h> |
|
81 |
2152
|
82 // DEFUN_DLD and the macros that it depends on are defined in the |
|
83 // files defun-dld.h, defun.h, and defun-int.h. |
|
84 |
|
85 // Note that the third parameter (nargout) is not used, so it is |
|
86 // omitted from the list of arguments to DEFUN_DLD in order to avoid |
|
87 // the warning from gcc about an unused function parameter. |
2134
|
88 |
|
89 DEFUN_DLD (hello, args, , |
|
90 "[...] = hello (...)\n\ |
|
91 \n\ |
|
92 Print greeting followed by the values of all the arguments passed.\n\ |
|
93 Returns all arguments in reverse order.") |
|
94 { |
|
95 // The list of values to return. See the declaration in oct-obj.h |
|
96 |
|
97 octave_value_list retval; |
|
98 |
|
99 // This stream is normally connected to the pager. |
|
100 |
|
101 octave_stdout << "Hello, world!\n"; |
|
102 |
|
103 // The arguments to this function are available in args. |
|
104 |
|
105 int nargin = args.length (); |
|
106 |
|
107 // The octave_value_list class is a zero-based array of octave_value |
|
108 // objects. The declaration for the octave_value class is in the |
3242
|
109 // file ov.h. The print() method will send its output to |
2134
|
110 // octave_stdout, so it will also end up going through the pager. |
|
111 |
|
112 for (int i = 0; i < nargin; i++) |
2142
|
113 { |
|
114 octave_value tmp = args (i); |
3242
|
115 tmp.print (octave_stdout); |
2142
|
116 retval (nargin-i-1) = tmp; |
|
117 } |
2134
|
118 |
|
119 return retval; |
|
120 } |