summaryrefslogtreecommitdiff
path: root/Lib/octave/octruntime.swg
blob: dd68ca4c1ac3d5cc9101c3b9cf42dfcaec309a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
%insert(runtime) %{
#include <octave/oct.h>
#include <octave/parse.h>
#include <octave/ov-fcn-handle.h>
#include <octave/Cell.h>
%}

%insert(runtime) "swigrun.swg";
%insert(runtime) "swigerrors.swg";
%insert(runtime) "octrun.swg";

%insert(initbeforefunc) "swiginit.swg"

%insert(initbeforefunc) %{

static void SWIG_init_user(octave_swig_type* module_ns);

DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) {
  static bool already_init=false;
  if (already_init)
    return octave_value_list();
  already_init=true;

  octave_swig_ref::register_type();
  octave_swig_packed::register_type();
  SWIG_InitializeModule(0);
  SWIG_PropagateClientData();
  
  install_builtin_function(swig_type,"swig_type",std::string());
  install_builtin_function(swig_typequery,"swig_typequery",std::string());
  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));
  for (int j=0;swig_globals[j].name;++j)
    if (swig_globals[j].method)
      module_ns->assign(swig_globals[j].name,&swig_globals[j]);

  // * need better solution here; swig_type -> octave_class mapping is 
  // * really n-to-1, in some cases such as template partial spec, etc. 
  // * see failing tests.
  for (int j=0;swig_types[j];++j)
    if (swig_types[j]->clientdata) {
      swig_octave_class* c=(swig_octave_class*)swig_types[j]->clientdata;
      module_ns->assign(c->name,
			Swig::swig_value_ref
			(new octave_swig_type(0,swig_types[j])));
    }

  SWIG_init_user(module_ns);

  SWIG_InstallOps(octave_swig_ref::static_type_id());

  // 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)
    module_ns->install_global();
  module_ns->decref();

  link_to_global_variable(curr_sym_tab->lookup(SWIG_name_d,true));
  set_global_value(SWIG_name_d,Swig::swig_value_ref(module_ns));

  return octave_value_list();
}

%}