Skip to content

Commit

Permalink
Use -globals . to load global variables in module namespace (from Kar…
Browse files Browse the repository at this point in the history
…l Wette)

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12908 626c5289-ae23-0410-ae9c-e8d60b6d4f22
  • Loading branch information
xavier98 committed Feb 27, 2012
1 parent 320a836 commit 6cd79f4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Doc/Manual/Octave.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ <H3><a name="Octave_nn4"></a>30.2.1 Command-line options</H3>
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>

<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>

Expand Down
8 changes: 8 additions & 0 deletions Examples/octave/module_load/runme_args.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@
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 #####
24 changes: 17 additions & 7 deletions Lib/octave/octruntime.swg
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
Expand All @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions Source/Modules/octave.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 6cd79f4

Please sign in to comment.