ParameterSetParser.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <lua/ParameterSetParser.h>
20 
21 #include <momemta/Logging.h>
22 
23 #include <lua/LazyTable.h>
24 #include <lua/utils.h>
25 
26 void ParameterSetParser::parse(ParameterSet& p, lua_State* L, int index) {
27 
28  LOG(trace) << "[parse] >> stack size = " << lua_gettop(L);
29  size_t absolute_index = lua::get_index(L, index);
30 
31  lua_pushnil(L);
32  while (lua_next(L, absolute_index) != 0) {
33 
34  std::string key = lua_tostring(L, -2);
35 
36  LOG(trace) << "[parse] >> key = " << key;
37 
38  try {
39  momemta::any value;
40  bool lazy = p.lazy();
41 
42  if (lazy) {
43  value = lua::LazyTableField(L, p.getModuleName(), key);
44  } else {
45  std::tie(value, lazy) = lua::to_any(L, -1);
46  }
47 
48  p.m_set.emplace(key, ParameterSet::Element(value, lazy));
49  } catch(...) {
50  LOG(fatal) << "Exception while trying to parse parameter " << p.getModuleType() << "." << p.getModuleName() << "::" << key;
51  lua_pop(L, 1);
52  std::rethrow_exception(std::current_exception());
53  }
54 
55  lua_pop(L, 1);
56  }
57 
58  LOG(trace) << "[parse] << stack size = " << lua_gettop(L);
59 }
size_t get_index(lua_State *L, int index)
Convert a negative lua stack index to an absolute index.
Definition: utils.cc:116
Lazy table field in lua (delayed table access)
Definition: LazyTable.h:35
A small wrapper around a momemta::any value.
Definition: ParameterSet.h:259
static void parse(ParameterSet &p, lua_State *L, int index)
Convert a lua table to a ParameterSet.
A class encapsulating a lua table.
Definition: ParameterSet.h:82
virtual bool lazy() const
A flag indicating if this ParameterSet lazy loads its fields or not.
Definition: ParameterSet.cc:39
std::pair< momemta::any, bool > to_any(lua_State *L, int index)
Convert a lua type to momemta::any.
Definition: utils.cc:191