ug4
lua_stack.h
Go to the documentation of this file.
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"
40 
41 namespace ug
42 {
43 namespace bridge {
44 
46 
50 int LuaStackToParams(ParameterStack& ps,
51  const ParameterInfo& psInfo,
52  lua_State* L,
53  int offsetToFirstParam = 0);
54 
56 
59 int ParamsToLuaStack(const ParameterStack& ps, lua_State* L);
60 
61 template <typename T>
63  int index, bool bIsVector)
64 {
65  if(!bIsVector){
66  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  lua_createtable(L, vec.size(), 0);
71  int newTable = lua_gettop(L);
72  for(int i=0; i < (int)vec.size(); i++) {
73  lua::LuaParsing<T>::push(L, vec[i]);
74  lua_rawseti(L, newTable, i + 1);
75  }
76  }
77 }
78 
79 template <typename T>
81  int index, bool bIsVector)
82 {
83  const char* className = ps.class_name(index);
84  if(!bIsVector){
85  lua::LuaParsing<T>::push(L, ps.to<T>(index), className);
86  }
87  else {
89  lua_createtable(L, spVec->size(), 0);
90  int newTable = lua_gettop(L);
91  for(int i=0; i < (int)spVec->size(); i++) {
92  lua::LuaParsing<T>::push(L, (*spVec)[i].first, (*spVec)[i].second->name().c_str());
93  lua_rawseti(L, newTable, i + 1);
94  }
95  }
96 }
97 
98 template <typename T>
100  int index, bool bIsVector)
101 {
102  if(!bIsVector){
103  if(lua::LuaParsing<T>::check(L, index)){
104  ps.push(lua::LuaParsing<T>::get(L, index));
105  }
106  else return false;
107  }
108  else {
109  if (lua_istable(L, index)){
111  = SmartPtr<std::vector<T> >(new std::vector<T>());
112  lua_pushnil(L);
113  while (lua_next(L, index) != 0) {
114  if(!lua::LuaParsing<T>::check(L, -1)) {
115  lua_pop(L, 1);
116  while (lua_next(L, index) != 0) lua_pop(L, 1);
117  return false;
118  }
119  spVec->push_back(lua::LuaParsing<T>::get(L, -1));
120  lua_pop(L, 1);
121  }
122  ps.push(spVec);
123  }
124  else return false;
125  }
126  return true;
127 }
128 
129 template <typename T>
131  int index, const char* baseClassName,
132  bool bIsVector)
133 {
134  typedef std::pair<T, const ClassNameNode*> result_type;
135 
136  result_type res;
137  if(!bIsVector){
138  if(lua::LuaParsing<T>::checkAndGet(res, L, index, baseClassName)){
139  ps.push(res.first, res.second);
140  }
141  else return false;
142  }
143  else {
144  if (lua_istable(L, index)){
146  = SmartPtr<std::vector<result_type> >(new std::vector<result_type>());
147  lua_pushnil(L);
148  while (lua_next(L, index) != 0) {
149  if(!lua::LuaParsing<T>::checkAndGet(res, L, -1, baseClassName)) {
150  lua_pop(L, 1);
151  while (lua_next(L, index) != 0) lua_pop(L, 1);
152  return false;
153  }
154  spVec->push_back(res);
155  lua_pop(L, 1);
156  }
157  ps.push(spVec);
158  }
159  else return false;
160  }
161  return true;
162 }
163 
164 }
165 }
166 #endif /* LUA_STACK_H_ */
Definition: smart_pointer.h:108
const char * class_name(int index) const
returns the class name for an element in the param stack
Definition: parameter_stack.h:123
A stack that can hold values together with their type-id.
Definition: parameter_stack.h:270
T to(int index) const
return element in param stack casted to type
Definition: parameter_stack.h:481
void push(void *ptr, const ClassNameNode *classNameNode)
Definition: parameter_stack.h:367
struct lua_State lua_State
Definition: lua_table_handle.h:40
int ParamsToLuaStack(const ParameterStack &ps, lua_State *L)
Pushes the parameter-values to the Lua-Stack.
Definition: lua_stack.cpp:169
static bool PushLuaStackPointerEntryToParamStack(ParameterStack &ps, lua_State *L, int index, const char *baseClassName, bool bIsVector)
Definition: lua_stack.h:130
int LuaStackToParams(ParameterStack &ps, const ParameterInfo &psInfo, lua_State *L, int offsetToFirstParam)
copies parameter values from the lua-stack to a parameter-list.
Definition: lua_stack.cpp:53
static void ParamStackPointerEntryToLuaStack(const ParameterStack &ps, lua_State *L, int index, bool bIsVector)
Definition: lua_stack.h:80
static void ParamStackEntryToLuaStack(const ParameterStack &ps, lua_State *L, int index, bool bIsVector)
Definition: lua_stack.h:62
static bool PushLuaStackEntryToParamStack(ParameterStack &ps, lua_State *L, int index, bool bIsVector)
Definition: lua_stack.h:99
the ug namespace
Definition: lua_parsing.h:51