examples/hello.cc
Go to the documentation of this file.00001 /* 00002 00003 Copyright (C) 1996, 1997, 1999, 2002, 2003, 2007 John W. Eaton 00004 00005 This file is part of Octave. 00006 00007 Octave is free software; you can redistribute it and/or modify it 00008 under the terms of the GNU General Public License as published by the 00009 Free Software Foundation; either version 3 of the License, or (at your 00010 option) any later version. 00011 00012 Octave is distributed in the hope that it will be useful, but WITHOUT 00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00015 for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with Octave; see the file COPYING. If not, see 00019 <http://www.gnu.org/licenses/>. 00020 00021 */ 00022 00023 // hello.cc -- example of a dynamically linked function for Octave. 00024 00025 // To use this file, your version of Octave must support dynamic 00026 // linking. To find out if it does, type the command 00027 // 00028 // octave_config_info ("ENABLE_DYNAMIC_LINKING") 00029 // 00030 // at the Octave prompt. Support for dynamic linking is included if 00031 // this expression returns the string "true". 00032 // 00033 // To compile this file, type the command 00034 // 00035 // mkoctfile hello.cc 00036 // 00037 // at the shell prompt. The script mkoctfile should have been 00038 // installed along with Octave. Running it will create a file called 00039 // hello.oct that can be loaded by Octave. To test the hello.oct 00040 // file, start Octave and type the command 00041 // 00042 // hello ("easy as", 1, 2, 3) 00043 // 00044 // at the Octave prompt. Octave should respond by printing 00045 // 00046 // Hello, world! 00047 // easy as 00048 // 1 00049 // 2 00050 // 3 00051 // ans = 3 00052 00053 // Additional examples are available in the files in the src directory 00054 // of the Octave distribution that use the macro DEFUN_DLD_BUILTIN. 00055 // Currently, this includes the files 00056 // 00057 // balance.cc fft.cc ifft.cc minmax.cc sort.cc 00058 // chol.cc fft2.cc ifft2.cc pinv.cc svd.cc 00059 // colloc.cc filter.cc inv.cc qr.cc syl.cc 00060 // dassl.cc find.cc log.cc quad.cc 00061 // det.cc fsolve.cc lsode.cc qzval.cc 00062 // eig.cc givens.cc lu.cc rand.cc 00063 // expm.cc hess.cc minmax.cc schur.cc 00064 // 00065 // The difference between DEFUN_DLD and DEFUN_DLD_BUILTIN is that 00066 // DEFUN_DLD_BUILTIN can define a built-in function that is not 00067 // dynamically loaded if the operating system does not support dynamic 00068 // linking. To define your own dynamically linked functions you 00069 // should use DEFUN_DLD. 00070 00071 #include <octave/config.h> 00072 00073 #include <iostream> 00074 00075 #include <octave/defun-dld.h> 00076 #include <octave/error.h> 00077 #include <octave/oct-obj.h> 00078 #include <octave/pager.h> 00079 #include <octave/symtab.h> 00080 #include <octave/variables.h> 00081 00082 // DEFUN_DLD and the macros that it depends on are defined in the 00083 // files defun-dld.h, defun.h, and defun-int.h. 00084 00085 // Note that the third parameter (nargout) is not used, so it is 00086 // omitted from the list of arguments to DEFUN_DLD in order to avoid 00087 // the warning from gcc about an unused function parameter. 00088 00089 DEFUN_DLD (hello, args, , 00090 "[...] = hello (...)\n\ 00091 \n\ 00092 Print greeting followed by the values of all the arguments passed.\n\ 00093 Returns all arguments in reverse order.") 00094 { 00095 // The list of values to return. See the declaration in oct-obj.h 00096 00097 octave_value_list retval; 00098 00099 // This stream is normally connected to the pager. 00100 00101 octave_stdout << "Hello, world!\n"; 00102 00103 // The arguments to this function are available in args. 00104 00105 int nargin = args.length (); 00106 00107 // The octave_value_list class is a zero-based array of octave_value 00108 // objects. The declaration for the octave_value class is in the 00109 // file ov.h. The print() method will send its output to 00110 // octave_stdout, so it will also end up going through the pager. 00111 00112 for (int i = 0; i < nargin; i++) 00113 { 00114 octave_value tmp = args (i); 00115 tmp.print (octave_stdout); 00116 retval (nargin-i-1) = tmp; 00117 } 00118 00119 return retval; 00120 }