summaryrefslogtreecommitdiff
path: root/graphics/pgf/base/source/InterfaceFromC++.c++
blob: 11c1f99dee053330a02aa8c54f72a3fbadf8a961 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

// Own header:
#include <pgf/gd/interface/c/InterfaceFromC++.h>

extern "C" {
  
// Own C header:
#include <pgf/gd/interface/c/InterfaceFromC.h>
  
// Lua stuff:
#include <lauxlib.h>
  
}


namespace {

  void cpp_caller(pgfgd_SyntacticDigraph* g, void* f)
  {
    using namespace scripting;
    
    run_parameters p;
    p.syntactic_digraph = g;
    
    runner* algo = static_cast<runner*> (f);
    
    algo->prepare(&p);
    algo->bridge();
    algo->run();
    algo->unbridge();
  }

}


namespace scripting {

  // The key class
  
  key::key(const char* key) : d (pgfgd_new_key(key)) {}
  key::key(struct pgfgd_Declaration* dec) : d (dec) {}
  key::~key() {pgfgd_free_key(d); }
  
  const key& key::summary (const char* x) const {
    pgfgd_key_summary(d, x);
    return *this;
  }

  const key& key::initial (const char* x) const {
    pgfgd_key_initial(d, x);
    return *this;
  }

  const key& key::initial (factory_base* fac) const {
    pgfgd_key_initial_user(d, static_cast<void*>(fac));
    return *this;
  }

  const key& key::type (const char* x) const {
    pgfgd_key_type(d, x);
    return *this;
  }

  const key& key::module_type () const {
    pgfgd_key_type(d, "user value");
    return *this;
  }

  const key& key::default_value (const char* x) const {
    pgfgd_key_default(d, x);
    return *this;
  }

  const key& key::alias (const char* x) const {
    pgfgd_key_alias(d, x);
    return *this;
  }

  const key& key::alias_function (const char* x) const {
    pgfgd_key_alias_function(d, x);
    return *this;
  }

  const key& key::documentation (const char* x) const {
    pgfgd_key_documentation(d, x);
    return *this;
  }

  const key& key::documentation_in (const char* x) const {
    pgfgd_key_documentation_in(d, x);
    return *this;
  }

  const key& key::phase (const char* x) const {
    pgfgd_key_phase(d, x);
    return *this;
  }

  const key& key::set_key (const char* k, const char* v) const {
    pgfgd_key_add_use(d, k, v);
    return *this;
  }

  const key& key::set_module (const char* k, factory_base* fac) const {
    pgfgd_key_add_use_user(d, k, static_cast<void*>(fac));
    return *this;
  }

  const key& key::example (const char* x) const {
    pgfgd_key_add_example(d, x);
    return *this;
  }

  const key& key::precondition (const char* x) const {
    pgfgd_key_add_precondition(d, x);
    return *this;
  }

  const key& key::postcondition (const char* x) const {
    pgfgd_key_add_postcondition(d, x);
    return *this;
  }

  const key& key::algorithm (runner* a) const {
    pgfgd_key_algorithm(d, cpp_caller, static_cast<void*>(a));
    return *this;
  }

  
  // The script class
  
  script::script (struct lua_State* s) : state(s) {}
  
  void script::declare (const key& k) {
    pgfgd_declare(state, k.d);
  }
  
  void script::declare (declarations* d) {
    d->declare(*this);
  }
  
  void script::declare (declarations& d) {
    d.declare(*this);
  }
  

  // The run_parameters class

  namespace {
    
    // Helpers:
    template <class T>
    bool fromnumber (pgfgd_SyntacticDigraph* g, const char* k, T& t)
    {
      if (pgfgd_isnumber(g->options, k)) {
	t = static_cast<T>(pgfgd_tonumber(g->options, k));
	return true;
      }
      return false;
    }
    
  };
  
  template <> bool run_parameters::option<bool> (const char* k, bool& t)
  {
    if (pgfgd_isboolean(syntactic_digraph->options, k)) {
      t = static_cast<bool>(pgfgd_toboolean(syntactic_digraph->options, k));
      return true;
    }
    return false;
  }
  
  template <> bool run_parameters::option<char*> (const char* k, char*& t)
  {
    if (pgfgd_isstring(syntactic_digraph->options, k)) {
      t = pgfgd_tostring(syntactic_digraph->options, k);
      return true;
    }
    return false;
  }
  
  template <> bool run_parameters::option<short> (const char* k, short& t)
  { return fromnumber<short> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<unsigned short> (const char* k, unsigned short& t)
  { return fromnumber<unsigned short> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<int> (const char* k, int& t)
  { return fromnumber<int> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<unsigned int> (const char* k, unsigned int& t)
  { return fromnumber<unsigned int> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<long> (const char* k, long& t)
  { return fromnumber<long> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<unsigned long> (const char* k, unsigned long& t)
  { return fromnumber<unsigned long> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<float> (const char* k, float& t)
  { return fromnumber<float> (syntactic_digraph, k, t); }
  
  template <> bool run_parameters::option<double> (const char* k, double& t)
  { return fromnumber<double> (syntactic_digraph, k, t); }
  
  void* run_parameters::invoke_void_factory_for (const char* k)
  {
    if (pgfgd_isuser (syntactic_digraph->options, k)) {
      factory_base* user = static_cast<factory_base *>(pgfgd_touser (syntactic_digraph->options, k));
      return user->make_void(this);
    }
    return 0;
  }
  
}