Use -globals . to load global variables in module namespace (from Karl Wette)

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12908 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Xavier Delacour 2012-02-27 17:07:16 +00:00
commit 6cd79f4320
4 changed files with 28 additions and 7 deletions

View file

@ -112,6 +112,7 @@ Options specific to the Octave module are:
Octave Options (available with -octave)
-global - Load all symbols into the global namespace [default]
-globals <em>name</em> - Set <em>name</em> used to access C global variables [default: 'cvar']
- Use '.' to load C global variables into module namespace
-noglobal - Do not load all symbols into the global namespace
-opprefix <em>str</em> - Prefix <em>str</em> for global operator functions [default: 'op_']
</pre></div>
@ -119,6 +120,7 @@ Octave Options (available with -octave)
<p>
The <em>-global</em> and <em>-noglobal</em> options determine whether the Octave module will load all symbols into the global namespace in addition to the global namespace.
The <em>-globals</em> option sets the name of the variable which is the namespace for C global variables exported by the module.
The special name "." loads C global variables into the module namespace, i.e. alongside C functions and structs exported by the module.
The <em>-opprefix</em> options sets the prefix of the names of global/friend <a href="#Octave_nn18">operator</a> functions.
</p>

View file

@ -38,3 +38,11 @@ example -globals mycvar
assert(!isglobal("cvar"))
assert(mycvar.ivar == example.ifunc())
##### END TEST #####
clear all;
##### BEGIN TEST #####
# load module with root-level cvar
example -globals .
assert(example.ivar == example.ifunc())
##### END TEST #####

View file

@ -33,7 +33,7 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
if (!already_load) {
// parse command line
const char* usage="usage: " SWIG_name_d " [-global|-noglobal] [-globals <name>]";
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)
@ -65,7 +65,7 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
return octave_value_list();
}
if (!valid_identifier(global_name)) {
if (global_name != "." && !valid_identifier(global_name)) {
std::cerr << "error: " SWIG_name_d ": '" << global_name << "' is not a valid Octave identifier." << std::endl;
return octave_value_list();
}
@ -88,13 +88,23 @@ 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());
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* cvar_ns=0;
if (global_name != ".") {
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(global_name,Swig::swig_value_ref(cvar_ns));
if (global_name != ".") {
module_ns->assign(global_name,Swig::swig_value_ref(cvar_ns));
}
else {
for (int j=0;swig_globals[j].name;++j)
if (swig_globals[j].get_method)
module_ns->assign(swig_globals[j].name,&swig_globals[j]);
}
for (int j=0;swig_globals[j].name;++j)
if (swig_globals[j].method)
module_ns->assign(swig_globals[j].name,&swig_globals[j]);

View file

@ -23,6 +23,7 @@ static const char *usage = (char *) "\
Octave Options (available with -octave)\n\
-global - Load all symbols into the global namespace [default]\n\
-globals <name> - Set <name> used to access C global variables [default: 'cvar']\n\
- Use '.' to load C global variables into module namespace\n\
-noglobal - Do not load all symbols into the global namespace\n\
-opprefix <str> - Prefix <str> for global operator functions [default: 'op_']\n\
\n";