Line data Source code
1 : /*
2 : * Copyright (c) 2015: 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 : #include "bindings_lua.h"
34 : #include "registry/registry.h"
35 : #include "registry/class_helper.h"
36 : #include "common/common.h"
37 : #include "info_commands.h"
38 : #include "lua_util.h"
39 : #include "lua_parsing.h"
40 :
41 : #include "lua_stack.h"
42 :
43 : #ifdef UG_FOR_LUA
44 : #include "bindings/lua/lua_function_handle.h"
45 : #include "bindings/lua/lua_table_handle.h"
46 : #endif
47 :
48 : namespace ug
49 : {
50 : namespace bridge {
51 :
52 :
53 0 : int LuaStackToParams(ParameterStack& ps,
54 : const ParameterInfo& psInfo,
55 : lua_State* L,
56 : int offsetToFirstParam)
57 : {
58 : // make sure that we have the right amount of parameters
59 : // if the sizes do not match, return -1.
60 0 : if((lua_gettop(L)) - offsetToFirstParam != (int)psInfo.size())
61 : return -1;
62 :
63 : // initialize temporary variables
64 : int badParam = 0;
65 :
66 : // iterate through parameter list and copy the value in the associated stack entry.
67 0 : for(int i = 0; i < psInfo.size(); ++i){
68 : // get type and vectorMode (if bIsVector==true a lua table is expected)
69 : int type = psInfo.type(i);
70 : bool bIsVector = psInfo.is_vector(i);
71 :
72 : // compute stack index, stack-indices start with 1
73 0 : int index = (int)i + offsetToFirstParam + 1;
74 :
75 : // check for nil
76 0 : if(lua_type(L, index) == LUA_TNONE) return (int)i + 1;
77 :
78 : // try to read in expected type
79 0 : switch(type){
80 0 : case Variant::VT_BOOL:{
81 0 : if(!PushLuaStackEntryToParamStack<bool>(ps, L, index, bIsVector))
82 0 : badParam = (int)i + 1;
83 : }break;
84 0 : case Variant::VT_INT:{
85 0 : if(!PushLuaStackEntryToParamStack<int>(ps, L, index, bIsVector))
86 0 : badParam = (int)i + 1;
87 : }break;
88 0 : case Variant::VT_SIZE_T:{
89 0 : if(!PushLuaStackEntryToParamStack<size_t>(ps, L, index, bIsVector))
90 0 : badParam = (int)i + 1;
91 : }break;
92 0 : case Variant::VT_FLOAT:{
93 0 : if(!PushLuaStackEntryToParamStack<float>(ps, L, index, bIsVector))
94 0 : badParam = (int)i + 1;
95 : }break;
96 0 : case Variant::VT_DOUBLE:{
97 0 : if(!PushLuaStackEntryToParamStack<double>(ps, L, index, bIsVector))
98 0 : badParam = (int)i + 1;
99 : }break;
100 0 : case Variant::VT_CSTRING:{
101 0 : if(!PushLuaStackEntryToParamStack<const char*>(ps, L, index, bIsVector))
102 0 : badParam = (int)i + 1;
103 : }break;
104 0 : case Variant::VT_STDSTRING:{
105 0 : if(!PushLuaStackEntryToParamStack<std::string>(ps, L, index, bIsVector))
106 0 : badParam = (int)i + 1;
107 : }break;
108 0 : case Variant::VT_POINTER:{
109 : // NOTE that smart-ptrs are implicitly used as normal pointers here.
110 : // This can cause severe problems in regard to reference-counting.
111 : // This behaviour was introduced, since the registry does not
112 : // allow by-value arguments. (Small temporary objects profit from
113 : // this strategy).
114 0 : if(!PushLuaStackPointerEntryToParamStack<void*>
115 0 : (ps, L, index, psInfo.class_name(i), bIsVector))
116 0 : badParam = (int)i + 1;
117 : }break;
118 0 : case Variant::VT_CONST_POINTER:{
119 : // NOTE that smart-ptrs are implicitly used as normal pointers here.
120 : // This can cause severe problems in regard to reference-counting.
121 : // This behaviour was introduced, since the registry does not
122 : // allow by-value arguments. (Small temporary objects profit from
123 : // this strategy).
124 0 : if(!PushLuaStackPointerEntryToParamStack<const void*>
125 0 : (ps, L, index, psInfo.class_name(i), bIsVector))
126 0 : badParam = (int)i + 1;
127 : }break;
128 0 : case Variant::VT_SMART_POINTER:{
129 0 : if(!PushLuaStackPointerEntryToParamStack<SmartPtr<void> >
130 0 : (ps, L, index, psInfo.class_name(i), bIsVector))
131 0 : badParam = (int)i + 1;
132 : }break;
133 0 : case Variant::VT_CONST_SMART_POINTER:{
134 0 : if(!PushLuaStackPointerEntryToParamStack<ConstSmartPtr<void> >
135 0 : (ps, L, index, psInfo.class_name(i), bIsVector))
136 0 : badParam = (int)i + 1;
137 : }break;
138 :
139 : #ifdef UG_FOR_LUA
140 0 : case Variant::VT_LUA_FUNCTION_HANDLE:{
141 0 : if(!PushLuaStackEntryToParamStack<LuaFunctionHandle>(ps, L, index, bIsVector))
142 0 : badParam = (int)i + 1;
143 : }break;
144 0 : case Variant::VT_LUA_TABLE_HANDLE:{
145 0 : if(!PushLuaStackEntryToParamStack<ug::LuaTableHandle>(ps, L, index, bIsVector))
146 0 : badParam = (int)i + 1;
147 : }break;
148 : #endif
149 :
150 0 : default:{// unknown type
151 0 : badParam = (int)i + 1;
152 0 : }break;
153 : }
154 :
155 : // check if param has been read correctly
156 0 : if(badParam){
157 0 : return badParam;
158 : }else{
159 : }
160 : }
161 :
162 : // return result flag
163 : return badParam;
164 : }
165 :
166 :
167 :
168 :
169 0 : int ParamsToLuaStack(const ParameterStack& ps, lua_State* L)
170 : {
171 : // push output parameters to the lua stack
172 0 : for(int i = 0; i < ps.size(); ++i){
173 : const int type = ps.type(i);
174 : const bool bIsVector = ps.is_vector(i);
175 0 : switch(type){
176 0 : case Variant::VT_BOOL:{
177 0 : ParamStackEntryToLuaStack<bool>(ps, L, i, bIsVector);
178 0 : }break;
179 0 : case Variant::VT_INT:{
180 0 : ParamStackEntryToLuaStack<int>(ps, L, i, bIsVector);
181 0 : }break;
182 0 : case Variant::VT_SIZE_T:{
183 0 : ParamStackEntryToLuaStack<size_t>(ps, L, i, bIsVector);
184 0 : }break;
185 0 : case Variant::VT_FLOAT:{
186 0 : ParamStackEntryToLuaStack<float>(ps, L, i, bIsVector);
187 0 : }break;
188 0 : case Variant::VT_DOUBLE:{
189 0 : ParamStackEntryToLuaStack<double>(ps, L, i, bIsVector);
190 0 : }break;
191 0 : case Variant::VT_CSTRING:{
192 0 : ParamStackEntryToLuaStack<const char*>(ps, L, i, bIsVector);
193 0 : }break;
194 0 : case Variant::VT_STDSTRING:{
195 0 : ParamStackEntryToLuaStack<std::string>(ps, L, i, bIsVector);
196 0 : }break;
197 0 : case Variant::VT_POINTER:{
198 0 : ParamStackPointerEntryToLuaStack<void*>(ps, L, i, bIsVector);
199 0 : }break;
200 0 : case Variant::VT_CONST_POINTER:{
201 0 : ParamStackPointerEntryToLuaStack<const void*>(ps, L, i, bIsVector);
202 0 : }break;
203 0 : case Variant::VT_SMART_POINTER:{
204 0 : ParamStackPointerEntryToLuaStack<SmartPtr<void> >(ps, L, i, bIsVector);
205 0 : }break;
206 0 : case Variant::VT_CONST_SMART_POINTER:{
207 0 : ParamStackPointerEntryToLuaStack<ConstSmartPtr<void> >(ps, L, i, bIsVector);
208 0 : }break;
209 0 : default:{
210 0 : UG_THROW("ParamsToLuaStack: invalid type in ParamStack.")
211 : }break;
212 : }
213 : }
214 :
215 0 : return (int)ps.size();
216 : }
217 :
218 : }
219 : }
|