annotate examples/code/oct_demo.cc @ 20595:c1a6c31ac29a

eliminate more simple uses of error_state * ov-classdef.cc: Eliminate simple uses of error_state.
author John W. Eaton <jwe@octave.org>
date Tue, 06 Oct 2015 00:20:02 -0400
parents c8240a60dd01
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
1 // oct_demo.cc -- example of a dynamically linked function for Octave.
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
2
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
3 // To use this file, your version of Octave must support dynamic
2153
dc829883f13a [project @ 1996-05-13 08:09:05 by jwe]
jwe
parents: 2152
diff changeset
4 // linking. To find out if it does, type the command
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
5 //
4128
919b2f6573ee [project @ 2002-10-25 20:36:14 by jwe]
jwe
parents: 3242
diff changeset
6 // octave_config_info ("ENABLE_DYNAMIC_LINKING")
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
7 //
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
8 // at the Octave prompt. Support for dynamic linking is included if
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
9 // this expression returns the string "yes".
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
10 //
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
11 // To compile this file, type the command
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
12 //
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
13 // mkoctfile oct_demo.cc
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
14 //
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
15 // from within Octave or from the shell prompt. This will create a file
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
16 // called oct_demo.oct that can be loaded by Octave. To test the
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
17 // oct_demo.oct file, start Octave and type the command
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
18 //
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
19 // oct_demo ("easy as", 1, 2, 3)
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
20 //
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
21 // at the Octave prompt. Octave should respond by printing
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
22 //
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
23 // Hello, world!
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
24 // easy as
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
25 // 1
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
26 // 2
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
27 // 3
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
28 // ans = 3
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
29
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
30 // Additional samples of real dynamically loaded functions are available in
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
31 // the files of the libinterp/dldfcn directory of the Octave distribution.
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
32 // See also the chapter External Code Interface in the documentation.
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
33
4399
286a3345aa8e [project @ 2003-05-01 03:00:28 by jwe]
jwe
parents: 4128
diff changeset
34 #include <iostream>
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
35
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
36 #include <octave/oct.h>
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
37
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
38 // Every user function should include <octave/oct.h> which imports the
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
39 // basic set of Octave header files required. In particular this will define
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
40 // the DEFUN_DLD macro (defun-dld.h) which is used for every user function
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
41 // that is visible to Octave.
2152
e9ee6418f62d [project @ 1996-05-13 08:03:26 by jwe]
jwe
parents: 2142
diff changeset
42
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
43 // The four arguments to the DEFUN_DLD macro are:
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
44 // 1) The function name as seen in Octave.
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
45 // 2) The variable to hold any inputs (of type octave_value_list)
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
46 // 3) The number of output arguments
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
47 // 4) A string to use as help text if 'help <function_name>' is entered.
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
48 //
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
49 // Note below that the third parameter (nargout) of DEFUN_DLD is not used,
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
50 // so it is omitted from the list of arguments in order to avoid a warning
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
51 // from gcc about an unused function parameter.
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
52
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
53 DEFUN_DLD (oct_demo, args, ,
17791
224e76250443 Use GNU style coding conventions for code in examples/
Rik <rik@octave.org>
parents: 16867
diff changeset
54 "[...] = oct_demo (...)\n\
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
55 \n\
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
56 Print a greeting followed by the values of all the arguments passed.\n\
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
57 Return all arguments in reverse order.")
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
58 {
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
59 // The list of values to return. See the declaration in oct-obj.h
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
60
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
61 octave_value_list retval;
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
62
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
63 // This stream is normally connected to the pager.
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
64
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
65 octave_stdout << "Hello, world!\n";
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
66
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
67 // The inputs to this function are available in args.
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
68
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
69 int nargin = args.length ();
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
70
16867
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
71 // The octave_value_list class is a zero-based array of octave_value objects.
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
72 // The declaration for the octave_value class is in the file ov.h.
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
73 // The print() method will send its output to octave_stdout,
be41c30bcb44 Re-write documentation and all examples of dynamically linked functions.
Rik <rik@octave.org>
parents: 14844
diff changeset
74 // so it will also end up going through the pager.
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
75
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
76 for (int i = 0; i < nargin; i++)
2142
3dc97364d58e [project @ 1996-05-13 04:34:11 by jwe]
jwe
parents: 2137
diff changeset
77 {
14844
5bc9b9cb4362 maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents: 12174
diff changeset
78 octave_value tmp = args(i);
3242
8c5ad0b49742 [project @ 1999-04-09 00:20:06 by jwe]
jwe
parents: 3045
diff changeset
79 tmp.print (octave_stdout);
14844
5bc9b9cb4362 maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents: 12174
diff changeset
80 retval(nargin-i-1) = tmp;
2142
3dc97364d58e [project @ 1996-05-13 04:34:11 by jwe]
jwe
parents: 2137
diff changeset
81 }
2134
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
82
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
83 return retval;
f44d96f6ca9f [project @ 1996-05-13 03:23:50 by jwe]
jwe
parents:
diff changeset
84 }