ug4
Loading...
Searching...
No Matches
lua_traits.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013-2014: G-CSC, Goethe University Frankfurt
3 * Author: Martin Rupp
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef LUA_TRAITS_H
34#define LUA_TRAITS_H
35
37
38namespace ug{
40// Lua Traits
42
43
45
46inline number ReturnValueToNumber(lua_State* L, int index){
47 if(!lua_isnumber(L, index))
48 UG_THROW("ReturnValueToNumber: Data passed from Lua: "
49 "Can't convert return value to number!");
50 number a = lua_tonumber(L, index);
51 UG_COND_THROW(IsFiniteAndNotTooBig(a) == false, a << " not finite");
52 return a;
53
54}
55
57
58inline number ReturnValueToBool(lua_State* L, int index){
59 if(!lua_isboolean(L, index))
60 UG_THROW("ReturnValueToBool: Data passed from Lua: "
61 "Can't convert return value to boolean!");
62
63 return lua_toboolean(L, index);
64}
65
67
68inline int ReturnValueToInteger(lua_State* L, int index){
69 if(!lua_isnumber(L, index))
70 UG_THROW("ReturnValueToBool: Data passed from Lua: "
71 "Can't convert return value to integer!");
72
73 return lua_tointeger(L, index);
74}
75
76
78template <typename TData>
80
81template <>
82struct lua_traits<void>
83{
84 static const int size = 0;
85
86 static void push(lua_State* L, const bool&){}
87
88 static void read(lua_State* L, bool&, int index = -1){}
89
90 static void do_return(const bool&) {return;}
91
92 static bool check(lua_State* L, int index = -1){return true;}
93
94 static std::string signature()
95 {
96 return "void";
97 }
98
99 static std::string name()
100 {
101 return "void";
102 }
103};
104
105template <>
106struct lua_traits<bool>
107{
108 static const int size = 1;
109
110 static void push(lua_State* L, const bool& b)
111 {
112 lua_pushboolean(L, b);
113 }
114
115 static void read(lua_State* L, bool& b, int index = -1)
116 {
117 b = ReturnValueToBool(L, index);
118 }
119
120 static bool check(lua_State* L, int index = -1)
121 {
122 return lua_isboolean(L, index);
123 }
124
125 static bool do_return(const bool& b) {return b;}
126
127 static std::string signature()
128 {
129 return "bool";
130 }
131
132 static std::string name()
133 {
134 return "Bool";
135 }
136};
137
138template <>
139struct lua_traits<int>
140{
141 static const int size = 1;
142
143 static void push(lua_State* L, const int& c)
144 {
145 lua_pushinteger(L, c);
146 }
147
148 static void read(lua_State* L, int& c, int index = -1)
149 {
150 c = ReturnValueToInteger(L, index);
151 }
152
153 static bool check(lua_State* L, int index = -1)
154 {
155 return lua_isnumber(L, index);
156 }
157
158 static std::string signature()
159 {
160 return "integer";
161 }
162
163 static std::string name()
164 {
165 return "Integer";
166 }
167};
168
169template <>
171{
172 static const int size = 1;
173
174 static void push(lua_State* L, const number& c)
175 {
176 lua_pushnumber(L, c);
177 }
178
179 static void read(lua_State* L, number& c, int index = -1)
180 {
181 c = ReturnValueToNumber(L, index);
182 }
183
184 static void read(number &c, double ret[1], bool *dummy)
185 {
186 c = ret[1];
187 }
188 static void read(number &c, double ret[1], void *dummy)
189 {
190 c = ret[0];
191 }
192
193 static bool check(lua_State* L, int index = -1)
194 {
195 return lua_isnumber(L, index);
196 }
197
198 static std::string signature()
199 {
200 return "number";
201 }
202
203 static std::string name()
204 {
205 return "Number";
206 }
207};
208
209template <std::size_t dim>
210struct lua_traits< ug::MathVector<dim> >
211{
212 static const int size = dim;
213
214 static void push(lua_State* L, const MathVector<dim>& x)
215 {
216 for(size_t i = 0; i < dim; ++i)
217 lua_pushnumber(L, x[i]);
218 }
219
220 static void read(lua_State* L, MathVector<dim>& x, int index = -1)
221 {
222 for(size_t i = 0; i < dim; ++i){
223 x[dim-1-i] = ReturnValueToNumber(L, index--);
224 }
225 }
226
227 static void read(MathVector<dim>& x, const double ret[dim], bool *dummy)
228 {
229 for(size_t i=0; i<dim; i++)
230 x[i] = ret[i+1];
231 }
232
233 static void read(MathVector<dim>& x, const double ret[dim], void *dummy)
234 {
235 for(size_t i=0; i<dim; i++)
236 x[i] = ret[i];
237 }
238
239 static bool check(lua_State* L, int index = -1)
240 {
241 for(size_t i = 0; i < dim; ++i){
242 if(!lua_isnumber(L, index--)) return false;
243 }
244 return true;
245 }
246
247 static std::string signature()
248 {
249 static char cmp[] = {'x', 'y', 'z'};
250 std::stringstream ss;
251 for(size_t i = 0; i < dim; ++i){
252 if(i != 0) ss << ", ";
253 ss << "v" << cmp[i];
254 }
255 return ss.str();
256 }
257
258 static std::string name()
259 {
260 std::stringstream ss;
261 ss << "Vector";
262 return ss.str();
263 }
264};
265
266
267template <std::size_t dim>
268struct lua_traits< MathMatrix<dim, dim> >
269{
270 static const int size = dim*dim;
271
272 static void push(lua_State* L, const MathMatrix<dim, dim>& D)
273 {
274 for(size_t i = 0; i < dim; ++i){
275 for(size_t j = 0; j < dim; ++j){
276 lua_pushnumber(L, D[i][j]);
277 }
278 }
279
280 }
281
282 static void read(lua_State* L, MathMatrix<dim, dim>& D, int index = -1)
283 {
284 for(size_t i = 0; i < dim; ++i){
285 for(size_t j = 0; j < dim; ++j){
286 D[dim-1-i][dim-1-j] = ReturnValueToNumber(L, index--);
287 }
288 }
289 }
290
291 static void read(MathMatrix<dim, dim>& D, const double ret[size], bool *dummy)
292 {
293 for(size_t i = 0; i < dim; ++i){
294 for(size_t j = 0; j < dim; ++j){
295 D[i][j] = ret[i*dim+j+1];
296 }
297 }
298 }
299 static void read(MathMatrix<dim, dim>& D, const double ret[size], void *dummy)
300 {
301 for(size_t i = 0; i < dim; ++i){
302 for(size_t j = 0; j < dim; ++j){
303 D[i][j] = ret[i*dim+j];
304 }
305 }
306 }
307
308 static bool check(lua_State* L, int index = -1)
309 {
310 for(size_t i = 0; i < dim; ++i){
311 for(size_t j = 0; j < dim; ++j){
312 if(!lua_isnumber(L, index--)) return false;
313 }
314 }
315 return true;
316 }
317
318 static std::string signature()
319 {
320 static char cmp[] = {'x', 'y', 'z'};
321 std::stringstream ss;
322 for(size_t i = 0; i < dim; ++i){
323 for(size_t j = 0; j < dim; ++j){
324 if(i != 0 || j != 0) ss << ", ";
325 ss << "D" << cmp[i] << cmp[j];
326 }
327 }
328 return ss.str();
329 }
330
331 static std::string name()
332 {
333 std::stringstream ss;
334 ss << "Matrix";
335 return ss.str();
336 }
337};
338
339/* for debugging callbacks
340 *
341 if(compare(out, out2)==false)
342 {
343 UG_LOG("\neval_value------------\n" << m_luaC.name << "\n" << out << "\n--\n" << out2 << "\n------------\n");
344 for(int i=0; i<lua_traits<TData>::size+1; i++)
345 UG_LOG(ret[i] << "\n");
346 }
347
348inline bool compare(double &a, double &b)
349{
350 return a == b;
351}
352
353inline bool compare(MathVector<3, double> &x, MathVector<3, double> &y)
354{
355 for(int i=0; i<3; i++)
356 if(x[i] != y[i]) return false;
357 return true;
358}
359
360inline bool compare(MathVector<2, double> &x, MathVector<2, double> &y)
361{
362 for(int i=0; i<2; i++)
363 if(x[i] != y[i]) return false;
364 return true;
365}
366
367inline bool compare(MathMatrix<3, 3, double> &A, MathMatrix<3, 3, double> &B)
368{
369 for(int j=0; j<3; j++)
370 for(int i=0; i<3; i++)
371 if(A[i][j] != B[i][j])
372 return false;
373 return true;
374
375}
376inline bool compare(MathMatrix<2, 2, double> &A, MathMatrix<2, 2, double> &B)
377{
378 for(int j=0; j<2; j++)
379 for(int i=0; i<2; i++)
380 if(A[i][j] != B[i][j])
381 return false;
382 return true;
383
384}*/
385
386}
387#endif /* LUA_TRAITS_H */
388
389
A class for fixed size, dense matrices.
Definition math_matrix.h:63
a mathematical Vector with N entries.
Definition math_vector.h:97
#define UG_THROW(msg)
Definition error.h:57
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition error.h:61
double number
Definition types.h:124
struct lua_State lua_State
Definition lua_table_handle.h:40
the ug namespace
int ReturnValueToInteger(lua_State *L, int index)
Helper to access a return value on the stack.
Definition lua_traits.h:68
number ReturnValueToBool(lua_State *L, int index)
Helper to access a return value on the stack.
Definition lua_traits.h:58
bool IsFiniteAndNotTooBig(double d)
Definition number_util.h:39
number ReturnValueToNumber(lua_State *L, int index)
Helper to access a return value on the stack.
Definition lua_traits.h:46
static void read(MathMatrix< dim, dim > &D, const double ret[size], bool *dummy)
Definition lua_traits.h:291
static bool check(lua_State *L, int index=-1)
Definition lua_traits.h:308
static void push(lua_State *L, const MathMatrix< dim, dim > &D)
Definition lua_traits.h:272
static std::string signature()
Definition lua_traits.h:318
static std::string name()
Definition lua_traits.h:331
static void read(MathMatrix< dim, dim > &D, const double ret[size], void *dummy)
Definition lua_traits.h:299
static void read(lua_State *L, MathMatrix< dim, dim > &D, int index=-1)
Definition lua_traits.h:282
static std::string signature()
Definition lua_traits.h:127
static bool check(lua_State *L, int index=-1)
Definition lua_traits.h:120
static std::string name()
Definition lua_traits.h:132
static void push(lua_State *L, const bool &b)
Definition lua_traits.h:110
static void read(lua_State *L, bool &b, int index=-1)
Definition lua_traits.h:115
static bool do_return(const bool &b)
Definition lua_traits.h:125
static bool check(lua_State *L, int index=-1)
Definition lua_traits.h:153
static std::string signature()
Definition lua_traits.h:158
static void read(lua_State *L, int &c, int index=-1)
Definition lua_traits.h:148
static std::string name()
Definition lua_traits.h:163
static void push(lua_State *L, const int &c)
Definition lua_traits.h:143
static void read(number &c, double ret[1], bool *dummy)
Definition lua_traits.h:184
static void push(lua_State *L, const number &c)
Definition lua_traits.h:174
static bool check(lua_State *L, int index=-1)
Definition lua_traits.h:193
static void read(number &c, double ret[1], void *dummy)
Definition lua_traits.h:188
static std::string name()
Definition lua_traits.h:203
static std::string signature()
Definition lua_traits.h:198
static void read(lua_State *L, number &c, int index=-1)
Definition lua_traits.h:179
static void read(MathVector< dim > &x, const double ret[dim], void *dummy)
Definition lua_traits.h:233
static void push(lua_State *L, const MathVector< dim > &x)
Definition lua_traits.h:214
static void read(lua_State *L, MathVector< dim > &x, int index=-1)
Definition lua_traits.h:220
static void read(MathVector< dim > &x, const double ret[dim], bool *dummy)
Definition lua_traits.h:227
static std::string name()
Definition lua_traits.h:258
static std::string signature()
Definition lua_traits.h:247
static bool check(lua_State *L, int index=-1)
Definition lua_traits.h:239
static std::string signature()
Definition lua_traits.h:94
static bool check(lua_State *L, int index=-1)
Definition lua_traits.h:92
static void push(lua_State *L, const bool &)
Definition lua_traits.h:86
static void do_return(const bool &)
Definition lua_traits.h:90
static void read(lua_State *L, bool &, int index=-1)
Definition lua_traits.h:88
static std::string name()
Definition lua_traits.h:99
Lua Traits to push/pop on lua stack.
Definition lua_traits.h:79