Octave changes by Karl Wette:
It's currently not possible to tell SWIG whether or not symbols should be loaded into the global namespace by default. In fact, it's not even possible to make the generated .oct module *not* load all symbols globally, due to a bug in Lib/octave/octruntime.swg (line 37: "noglobal" should be "global"). It's also not possible to change the name of the symbol used to access global variables/constants from the default "cvar". This patch adds 3 Octave-specific command-line options: * -global/-noglobal tell SWIG whether the generated .oct module should load symbols into the global namespace by default. The default option is -global to preserve existing behaviour. * -globals <name> sets the name of the symbol used to access global variables/constants. It is set to "cvar" by default. These options are parsed in Source/Modules/octave.cxx, and written to the wrapping code as the macros SWIG_global_load and SWIG_global_name. In Lib/octave/octruntime.swg, the Octave entry point DEFUN_DLD function now contains a expanded input argument parser, which uses the same command-line arguments (-global/-noglobal, -globals) as can be passed to SWIG itself; this allows the module user to change the default loading behaviour of the module user at load time. The parser checks for non-string and unrecognised arguments, and also checks that the argument to -globals is a valid Octave identifier. A -help option prints a short usage message. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12670 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
cb0a975206
commit
8848b16e01
2 changed files with 72 additions and 12 deletions
|
|
@ -1,4 +1,5 @@
|
|||
%insert(runtime) %{
|
||||
#include <iostream>
|
||||
#include <octave/oct.h>
|
||||
#include <octave/parse.h>
|
||||
#include <octave/ov-fcn-handle.h>
|
||||
|
|
@ -20,6 +21,45 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
|
|||
static bool already_init=false;
|
||||
if (already_init)
|
||||
return octave_value_list();
|
||||
|
||||
// parse command line
|
||||
const char* usage="usage: " SWIG_name_d " [-global|-noglobal] [-globals <name>]";
|
||||
bool global_load=SWIG_global_load;
|
||||
std::string global_name=SWIG_global_name;
|
||||
for (int j=0;j<args.length();++j)
|
||||
if (args(j).is_string()) {
|
||||
if (args(j).string_value()=="-help") {
|
||||
std::cout << usage << std::endl;
|
||||
return octave_value_list();
|
||||
} else if (args(j).string_value()=="-global") {
|
||||
global_load = true;
|
||||
} else if (args(j).string_value()=="-noglobal") {
|
||||
global_load = false;
|
||||
} else if (args(j).string_value()=="-globals") {
|
||||
if (j+1<args.length()&&args(j+1).is_string()) {
|
||||
global_name = args(j+1).string_value();
|
||||
++j;
|
||||
} else {
|
||||
std::cerr << "error: " SWIG_name_d ": option '-globals' requires an argument." << std::endl;
|
||||
std::cerr << usage << std::endl;
|
||||
return octave_value_list();
|
||||
}
|
||||
} else {
|
||||
std::cerr << "error: " SWIG_name_d ": unrecognised argument '" << args(j).string_value() << "'." << std::endl;
|
||||
std::cerr << usage << std::endl;
|
||||
return octave_value_list();
|
||||
}
|
||||
} else {
|
||||
std::cerr << "error: " SWIG_name_d ": unrecognised non-string argument." << std::endl;
|
||||
std::cerr << usage << std::endl;
|
||||
return octave_value_list();
|
||||
}
|
||||
|
||||
if (!valid_identifier(global_name)) {
|
||||
std::cerr << "error: " SWIG_name_d ": '" << global_name << "' is not a valid Octave identifier." << std::endl;
|
||||
return octave_value_list();
|
||||
}
|
||||
|
||||
already_init=true;
|
||||
|
||||
octave_swig_ref::register_type();
|
||||
|
|
@ -32,20 +72,13 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
|
|||
install_builtin_function(swig_this,"swig_this",std::string());
|
||||
install_builtin_function(swig_subclass,"subclass",std::string());
|
||||
|
||||
bool global_option=true; // * swig cli option should control this default
|
||||
for (int j=0;j<args.length();++j)
|
||||
if (args(j).is_string()&&args(j).string_value()=="noglobal")
|
||||
global_option=true;
|
||||
else if (args(j).is_string()&&args(j).string_value()=="noglobal")
|
||||
global_option=false;
|
||||
|
||||
octave_swig_type* cvar_ns=new octave_swig_type;
|
||||
for (int j=0;swig_globals[j].name;++j)
|
||||
if (swig_globals[j].get_method)
|
||||
cvar_ns->assign(swig_globals[j].name,&swig_globals[j]);
|
||||
|
||||
octave_swig_type* module_ns=new octave_swig_type(0, 0, 0, true);
|
||||
module_ns->assign("cvar",Swig::swig_value_ref(cvar_ns));
|
||||
module_ns->assign(global_name,Swig::swig_value_ref(cvar_ns));
|
||||
for (int j=0;swig_globals[j].name;++j)
|
||||
if (swig_globals[j].method)
|
||||
module_ns->assign(swig_globals[j].name,&swig_globals[j]);
|
||||
|
|
@ -68,7 +101,7 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
|
|||
// the incref is necessary so install_global doesn't destroy module_ns,
|
||||
// as it would if it installed something with the same name as the module.
|
||||
module_ns->incref();
|
||||
if (global_option)
|
||||
if (global_load)
|
||||
module_ns->install_global();
|
||||
module_ns->decref();
|
||||
|
||||
|
|
|
|||
|
|
@ -15,9 +15,14 @@ char cvsroot_octave_cxx[] = "$Id$";
|
|||
|
||||
#include "swigmod.h"
|
||||
|
||||
static bool global_load = true;
|
||||
static String *global_name = 0;
|
||||
|
||||
static const char *usage = (char *) "\
|
||||
Octave Options (available with -octave)\n\
|
||||
[no additional options]\n\
|
||||
-global - Load all symbols into the global namespace [default]\n\
|
||||
-globals <name> - Set <name> used to access C global variables [default: 'cvar']\n\
|
||||
-noglobal - Do not load all symbols into the global namespace\n\
|
||||
\n";
|
||||
|
||||
|
||||
|
|
@ -64,11 +69,29 @@ public:
|
|||
for (int i = 1; i < argc; i++) {
|
||||
if (argv[i]) {
|
||||
if (strcmp(argv[i], "-help") == 0) {
|
||||
fputs(usage, stdout);
|
||||
}
|
||||
fputs(usage, stderr);
|
||||
} else if (strcmp(argv[i], "-global") == 0) {
|
||||
global_load = true;
|
||||
Swig_mark_arg(i);
|
||||
} else if (strcmp(argv[i], "-noglobal") == 0) {
|
||||
global_load = false;
|
||||
Swig_mark_arg(i);
|
||||
} else if (strcmp(argv[i], "-globals") == 0) {
|
||||
if (argv[i + 1]) {
|
||||
global_name = NewString(argv[i + 1]);
|
||||
Swig_mark_arg(i);
|
||||
Swig_mark_arg(i + 1);
|
||||
i++;
|
||||
} else {
|
||||
Swig_arg_error();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!global_name)
|
||||
global_name = NewString("cvar");
|
||||
|
||||
SWIG_library_directory("octave");
|
||||
Preprocessor_define("SWIGOCTAVE 1", 0);
|
||||
SWIG_config_file("octave.swg");
|
||||
|
|
@ -131,6 +154,10 @@ public:
|
|||
Printf(f_runtime, "#define SWIG_name_d \"%s\"\n", module);
|
||||
Printf(f_runtime, "#define SWIG_name %s\n", module);
|
||||
|
||||
Printf(f_runtime, "\n");
|
||||
Printf(f_runtime, "#define SWIG_global_load %s\n", global_load ? "true" : "false");
|
||||
Printf(f_runtime, "#define SWIG_global_name \"%s\"\n", global_name);
|
||||
|
||||
if (directorsEnabled()) {
|
||||
Printf(f_runtime, "#define SWIG_DIRECTORS\n");
|
||||
Swig_banner(f_directors_h);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue