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 : #ifndef LUA_STACK_H_
34 : #define LUA_STACK_H_
35 :
36 : #include "registry/registry.h"
37 : #include "lua_parsing.h"
38 : #include "bindings_lua.h"
39 : #include "common/util/smart_pointer.h"
40 :
41 : namespace ug
42 : {
43 : namespace bridge {
44 :
45 : /// copies parameter values from the lua-stack to a parameter-list.
46 : /** \returns The index of the first bad parameter starting from 1.
47 : * Returns 0 if everything went right.
48 : * Returns -1 if the number of parameters did not match.
49 : */
50 : int LuaStackToParams(ParameterStack& ps,
51 : const ParameterInfo& psInfo,
52 : lua_State* L,
53 : int offsetToFirstParam = 0);
54 :
55 : /// Pushes the parameter-values to the Lua-Stack.
56 : /**
57 : * \returns The number of items pushed to the stack.
58 : */
59 : int ParamsToLuaStack(const ParameterStack& ps, lua_State* L);
60 :
61 : template <typename T>
62 0 : static void ParamStackEntryToLuaStack(const ParameterStack& ps, lua_State* L,
63 : int index, bool bIsVector)
64 : {
65 0 : if(!bIsVector){
66 0 : lua::LuaParsing<T>::push(L, ps.to<T>(index));
67 : }
68 : else {
69 : const std::vector<T>& vec = ps.to<std::vector<T> >(index);
70 0 : lua_createtable(L, vec.size(), 0);
71 0 : int newTable = lua_gettop(L);
72 0 : for(int i=0; i < (int)vec.size(); i++) {
73 0 : lua::LuaParsing<T>::push(L, vec[i]);
74 0 : lua_rawseti(L, newTable, i + 1);
75 : }
76 0 : }
77 0 : }
78 :
79 : template <typename T>
80 0 : static void ParamStackPointerEntryToLuaStack(const ParameterStack& ps, lua_State* L,
81 : int index, bool bIsVector)
82 : {
83 0 : const char* className = ps.class_name(index);
84 0 : if(!bIsVector){
85 0 : lua::LuaParsing<T>::push(L, ps.to<T>(index), className);
86 : }
87 : else {
88 : SmartPtr<std::vector<std::pair<T, const ClassNameNode*> > > spVec = ps.to<SmartPtr<std::vector<std::pair<T, const ClassNameNode*> > > >(index);
89 0 : lua_createtable(L, spVec->size(), 0);
90 0 : int newTable = lua_gettop(L);
91 0 : for(int i=0; i < (int)spVec->size(); i++) {
92 0 : lua::LuaParsing<T>::push(L, (*spVec)[i].first, (*spVec)[i].second->name().c_str());
93 0 : lua_rawseti(L, newTable, i + 1);
94 : }
95 : }
96 0 : }
97 :
98 : template <typename T>
99 0 : static bool PushLuaStackEntryToParamStack(ParameterStack& ps, lua_State* L,
100 : int index, bool bIsVector)
101 : {
102 0 : if(!bIsVector){
103 0 : if(lua::LuaParsing<T>::check(L, index)){
104 0 : ps.push(lua::LuaParsing<T>::get(L, index));
105 : }
106 : else return false;
107 : }
108 : else {
109 0 : if (lua_istable(L, index)){
110 : SmartPtr<std::vector<T> > spVec
111 0 : = SmartPtr<std::vector<T> >(new std::vector<T>());
112 0 : lua_pushnil(L);
113 0 : while (lua_next(L, index) != 0) {
114 0 : if(!lua::LuaParsing<T>::check(L, -1)) {
115 0 : lua_pop(L, 1);
116 0 : while (lua_next(L, index) != 0) lua_pop(L, 1);
117 : return false;
118 : }
119 0 : spVec->push_back(lua::LuaParsing<T>::get(L, -1));
120 0 : lua_pop(L, 1);
121 : }
122 0 : ps.push(spVec);
123 : }
124 : else return false;
125 : }
126 : return true;
127 : }
128 :
129 : template <typename T>
130 0 : static bool PushLuaStackPointerEntryToParamStack(ParameterStack& ps, lua_State* L,
131 : int index, const char* baseClassName,
132 : bool bIsVector)
133 : {
134 : typedef std::pair<T, const ClassNameNode*> result_type;
135 :
136 0 : result_type res;
137 0 : if(!bIsVector){
138 0 : if(lua::LuaParsing<T>::checkAndGet(res, L, index, baseClassName)){
139 0 : ps.push(res.first, res.second);
140 : }
141 : else return false;
142 : }
143 : else {
144 0 : if (lua_istable(L, index)){
145 : SmartPtr<std::vector<result_type> > spVec
146 0 : = SmartPtr<std::vector<result_type> >(new std::vector<result_type>());
147 0 : lua_pushnil(L);
148 0 : while (lua_next(L, index) != 0) {
149 0 : if(!lua::LuaParsing<T>::checkAndGet(res, L, -1, baseClassName)) {
150 0 : lua_pop(L, 1);
151 0 : while (lua_next(L, index) != 0) lua_pop(L, 1);
152 : return false;
153 : }
154 0 : spVec->push_back(res);
155 0 : lua_pop(L, 1);
156 : }
157 0 : ps.push(spVec);
158 : }
159 : else return false;
160 : }
161 : return true;
162 : }
163 :
164 : }
165 : }
166 : #endif /* LUA_STACK_H_ */
|