GemaLuaCoreLib
The GeMA Lua Core library
gmLuaUtils.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMALUA_UTILS_H_
25 #define _GEMALUA_UTILS_H_
26 
27 #include "gmLuaEnv.h"
28 #include <luaEnv.h>
29 #include <luaTable.h>
30 
31 #include <gmVector.h>
32 #include <gmModelData.h>
33 #include <gmTaskManager.h>
34 
35 #include <gmCellGroupSet.h>
36 #include <gmLuaCellGroupSet.h>
37 
38 #include <gmTrace.h>
39 #include <assert.h>
40 #include <luaProxy.h>
41 
42 class GmGroupDumpItem;
43 
47 namespace GmLuaUtils
48 {
49  GML_API_EXPORT void fillVectorFromLua(lua_State* L, int stackPos, QString fname, GmVector& v);
50 
51  GML_API_EXPORT void checkAndLoadIntOption (LuaTable& optionsTable, const char* fieldName, int* option);
52  GML_API_EXPORT void checkAndLoadUnsignedOption(LuaTable& optionsTable, const char* fieldName, unsigned* option);
53  GML_API_EXPORT void checkAndLoadDoubleOption (LuaTable& optionsTable, const char* fieldName, double* option);
54 
55  GML_API_EXPORT void checkAndLoadTaskOptions(LuaTable& optionsTable, int* nworkers, int* ntasks,
58 
59  GML_API_EXPORT int stringOptionIndex(QString value, const char** optionNames, lua_State* L, const char* fieldName);
60 
69  template<class T, class M>
70  void checkAndLoadStringOption(LuaTable& optionsTable, const char* fieldName, const char** optionNames, T* optionsObj, M method)
71  {
72  QString v = optionsTable.getField(fieldName).toString();
73  if(v.isEmpty()) // If the field is missing from the table, keep the default
74  return;
75 
76  // Set the value using the provided setter function.
77  // stringOptionIndex() calls luaL_error() if v is not found in optionNames
78  (optionsObj->*method)(stringOptionIndex(v, optionNames, optionsTable.env()->state(), fieldName));
79  }
80 
81 
93  template<class T, class P> T* parseMesh(lua_State* L, int index, const GmModelData* mdata, QString fname)
94  {
95  S_TRACE();
96  assert(mdata);
97 
98  // String parameter
99  if(lua_type(L, index) == LUA_TSTRING)
100  {
101  QString meshName = lua_tostring(L, index);
102  GmMesh* mesh = mdata->meshes().value(meshName);
103 
104  if(!mesh)
105  luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' not found in call to %2").arg(meshName, fname)));
106 
107  T* tmesh = dynamic_cast<T*>(mesh);
108  if(!tmesh)
109  luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' has an invalid type in call to %2").arg(meshName, fname)));
110 
111  return tmesh;
112  }
113 
114  // Object parameter
115  P* proxy = LuaProxy::toObjectOfClass<P>(L, index);
116  if(!proxy)
117  luaL_error(L, "%s", luaPrintable(QObject::tr("Error: Parameter %1 in call to %2 should be a valid mesh object (or a mesh name). Check mesh type.")
118  .arg(index).arg(fname)));
119 
120  return dynamic_cast<T*>(proxy->mesh());
121  }
122 
137  template<class T, class P> void parseMeshOrGroup(lua_State* L, int index, const GmModelData* mdata, QString fname,
138  T** mesh, GmCellGroupSet** group)
139  {
140  S_TRACE();
141  assert(mdata);
142  assert(mesh && group);
143 
144  *group = NULL;
145 
146  // String parameter: must be a mesh name
147  if(lua_type(L, index) == LUA_TSTRING)
148  {
149  QString meshName = lua_tostring(L, index);
150  GmMesh* m = mdata->meshes().value(meshName);
151 
152  if(!m)
153  luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' not found in call to %2").arg(meshName, fname)));
154 
155  T* tmesh = dynamic_cast<T*>(m);
156  if(!tmesh)
157  luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' has an invalid type in call to %2").arg(meshName, fname)));
158 
159  *mesh = tmesh;
160  return;
161  }
162 
163  // Object parameter. Try a mesh
164  P* proxy = LuaProxy::toObjectOfClass<P>(L, index);
165  if(proxy)
166  {
167  *mesh = dynamic_cast<T*>(proxy->mesh());
168  return;
169  }
170 
171  // Not a mesh. Can still be a Cell group set
172  GmLuaCellGroupSet* gproxy = LuaProxy::toObjectOfClass<GmLuaCellGroupSet>(L, index);
173  if(!gproxy)
174  luaL_error(L, "%s", luaPrintable(QObject::tr("Error: Parameter %1 in call to %2 should be a valid mesh object, a mesh name or a cell group set object. Check parameter type.")
175  .arg(index).arg(fname)));
176 
177  *group = gproxy->cellGroupSet();
178  *mesh = dynamic_cast<T*>((*group)->mesh());
179  if(!*mesh)
180  luaL_error(L, "%s", luaPrintable(QObject::tr("Cell group mesh has an invalid type in call to %1").arg(fname)));
181  }
182 
199  template<const char* typeName> void pushGroupDumpItemProxy(lua_State* L, GmGroupDumpItem* ptr)
200  {
201  assert(L && ptr);
203  }
204 
212  template <class T> T* getGroupDumpItemPtr(lua_State* L, int pos, QString tmsg, QString fmsg)
213  {
214  LuaSimplePtrProxy<GmGroupDumpItem>* proxy = LuaProxy::toObjectOfClass<LuaSimplePtrProxy<GmGroupDumpItem> >(L, pos);
215  T* ptr = proxy ? dynamic_cast<T*>(proxy->get()) : NULL;
216 
217  if(!ptr)
218  return luaL_error(L, "%s", luaPrintable(QObject::tr("Invalid %1 parameter in call to %2()").arg(tmsg, fmsg))), NULL;
219  return ptr;
220  }
221 
222 }
223 
224 #endif
void checkAndLoadStringOption(LuaTable &optionsTable, const char *fieldName, const char **optionNames, T *optionsObj, M method)
An auxiliary function to check the existance and validity of a string option. If the field given by "...
Definition: gmLuaUtils.h:70
void parseMeshOrGroup(lua_State *L, int index, const GmModelData *mdata, QString fname, T **mesh, GmCellGroupSet **group)
Auxiliary routine to help processes that can accept from the Lua stack either a mesh object,...
Definition: gmLuaUtils.h:137
T * getGroupDumpItemPtr(lua_State *L, int pos, QString tmsg, QString fmsg)
Returns the parameter at Lua stack position 'pos' as a T*.
Definition: gmLuaUtils.h:212
void checkAndLoadDoubleOption(LuaTable &optionsTable, const char *fieldName, double *option)
An auxiliary function used to check the existance and validity of a double option....
Definition: gmLuaUtils.cpp:108
lua_State * state()
#define S_TRACE()
void checkAndLoadIntOption(LuaTable &optionsTable, const char *fieldName, int *option)
An auxiliary function used to check the existance and validity of an integer option....
Definition: gmLuaUtils.cpp:72
Declaration of the GmLuaCellGroupCell class.
QString tr(const char *sourceText, const char *disambiguation, int n)
Declaration of the function used to prepare an environment with the core Lua functions.
GmCellGroupSet * cellGroupSet() const
Returns the wrapped cell.
Definition: gmLuaCellGroupSet.h:51
void checkAndLoadTaskOptions(LuaTable &optionsTable, int *nworkers, int *ntasks, GmTaskManager::NodePartitionStrategy *nodeStrategy, GmTaskManager::CellPartitionStrategy *cellStrategy)
An auxiliary function used to load task manager options from optionsTable, filling nworkers,...
Definition: gmLuaUtils.cpp:130
A proxy class to export GmCellGroupSet methods to the Lua environment.
Definition: gmLuaCellGroupSet.h:33
bool isEmpty() const const
LuaEnv * env() const
T * parseMesh(lua_State *L, int index, const GmModelData *mdata, QString fname)
Auxiliary routine to help processes that can accept from the Lua stack either a mesh object or a mesh...
Definition: gmLuaUtils.h:93
Groups utilitary routines for working with Lua.
Definition: gmLuaUtils.cpp:32
static void pushObject(lua_State *L, Base *obj)
void checkAndLoadUnsignedOption(LuaTable &optionsTable, const char *fieldName, unsigned *option)
An auxiliary function used to check the existance and validity of a unsigned integer option....
Definition: gmLuaUtils.cpp:90
QVariant getField(const char *name, LuaEnv::StackOption opt=LuaEnv::STACK_AUTO)
int stringOptionIndex(QString value, const char **optionNames, lua_State *L, const char *fieldName)
Looks for value in optionNames. If found returns the index. If not found raises a lua error for field...
Definition: gmLuaUtils.cpp:188
arma::vec GmVector
void fillVectorFromLua(lua_State *L, int stackPos, QString fname, GmVector &v)
Resizes and fills the vector v to receive the data stored in a lua table.
Definition: gmLuaUtils.cpp:36
QString toString() const const
void pushGroupDumpItemProxy(lua_State *L, GmGroupDumpItem *ptr)
Pushes a pointer inheriting from GmGroupDumpItem as a Lua proxy. The pointer will be deleted when the...
Definition: gmLuaUtils.h:199
const QMap< QString, GmMesh * > & meshes() const
const T value(const Key &key, const T &defaultValue) const const