2 #include <octave/oct.h>
3 #include <octave/parse.h>
4 #include <octave/ov-fcn-handle.h>
5 #include <octave/Cell.h>
6 #include <octave/oct-map.h>
9 %insert(runtime) "swigrun.swg";
10 %insert(runtime) "swigerrors.swg";
11 %insert(runtime) "octrun.swg";
13 %insert(initbeforefunc) "swiginit.swg"
15 %insert(initbeforefunc) %{
17 static void SWIG_init_user(octave_swig_type* module_ns);
19 DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
20 static bool already_init=false;
22 return octave_value_list();
25 octave_swig_ref::register_type();
26 octave_swig_packed::register_type();
27 SWIG_InitializeModule(0);
28 SWIG_PropagateClientData();
30 install_builtin_function(swig_type,"swig_type",std::string());
31 install_builtin_function(swig_typequery,"swig_typequery",std::string());
32 install_builtin_function(swig_this,"swig_this",std::string());
33 install_builtin_function(swig_subclass,"subclass",std::string());
35 bool global_option=true; // * swig cli option should control this default
36 for (int j=0;j<args.length();++j)
37 if (args(j).is_string()&&args(j).string_value()=="noglobal")
39 else if (args(j).is_string()&&args(j).string_value()=="noglobal")
42 octave_swig_type* cvar_ns=new octave_swig_type;
43 for (int j=0;swig_globals[j].name;++j)
44 if (swig_globals[j].get_method)
45 cvar_ns->assign(swig_globals[j].name,&swig_globals[j]);
47 octave_swig_type* module_ns=new octave_swig_type(0, 0, 0, true);
48 module_ns->assign("cvar",Swig::swig_value_ref(cvar_ns));
49 for (int j=0;swig_globals[j].name;++j)
50 if (swig_globals[j].method)
51 module_ns->assign(swig_globals[j].name,&swig_globals[j]);
53 // * need better solution here; swig_type -> octave_class mapping is
54 // * really n-to-1, in some cases such as template partial spec, etc.
55 // * see failing tests.
56 for (int j=0;swig_types[j];++j)
57 if (swig_types[j]->clientdata) {
58 swig_octave_class* c=(swig_octave_class*)swig_types[j]->clientdata;
59 module_ns->assign(c->name,
61 (new octave_swig_type(0,swig_types[j])));
64 SWIG_init_user(module_ns);
66 SWIG_InstallOps(octave_swig_ref::static_type_id());
68 // the incref is necessary so install_global doesn't destroy module_ns,
69 // as it would if it installed something with the same name as the module.
72 module_ns->install_global();
75 #if USE_OCTAVE_API_VERSION<37
76 link_to_global_variable(curr_sym_tab->lookup(SWIG_name_d,true));
78 symbol_table::varref(SWIG_name_d);
79 symbol_table::mark_global(SWIG_name_d);
81 set_global_value(SWIG_name_d,Swig::swig_value_ref(module_ns));
83 #if USE_OCTAVE_API_VERSION>=37
87 return octave_value_list();
90 // workaround bug in octave where installing global variable of custom type and then
91 // exiting without explicitly clearing the variable causes octave to segfault.
92 #if USE_OCTAVE_API_VERSION>=37
93 struct oct_file_unload {
95 string_vector vars = symbol_table::global_variable_names();
96 for (int i = 0; i < vars.length(); i++)
97 symbol_table::clear_global(vars[i]);
100 static oct_file_unload __unload;