GemaLuaCoreLib
The GeMA Lua Core library
Loading...
Searching...
No Matches
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
42class GmGroupDumpItem;
43
47namespace 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
61
70 template<class T, class M>
71 void checkAndLoadStringOption(LuaTable& optionsTable, const char* fieldName, const char** optionNames, T* optionsObj, M method)
72 {
73 QString v = optionsTable.getField(fieldName).toString();
74 if(v.isEmpty()) // If the field is missing from the table, keep the default
75 return;
76
77 // Set the value using the provided setter function.
78 // stringOptionIndex() calls luaL_error() if v is not found in optionNames
79 (optionsObj->*method)(stringOptionIndex(v, optionNames, optionsTable.env()->state(), fieldName));
80 }
81
82
94 template<class T, class P> T* parseMesh(lua_State* L, int index, const GmModelData* mdata, QString fname)
95 {
96 S_TRACE();
97 assert(mdata);
98
99 // String parameter
100 if(lua_type(L, index) == LUA_TSTRING)
101 {
102 QString meshName = lua_tostring(L, index);
103 GmMesh* mesh = mdata->meshes().value(meshName);
104
105 if(!mesh)
106 luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' not found in call to %2").arg(meshName, fname)));
107
108 T* tmesh = dynamic_cast<T*>(mesh);
109 if(!tmesh)
110 luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' has an invalid type in call to %2").arg(meshName, fname)));
111
112 return tmesh;
113 }
114
115 // Object parameter
116 P* proxy = LuaProxy::toObjectOfClass<P>(L, index);
117 if(!proxy)
118 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.")
119 .arg(index).arg(fname)));
120
121 return dynamic_cast<T*>(proxy->mesh());
122 }
123
138 template<class T, class P> void parseMeshOrGroup(lua_State* L, int index, const GmModelData* mdata, QString fname,
139 T** mesh, const GmCellGroupSet** group)
140 {
141 S_TRACE();
142 assert(mdata);
143 assert(mesh && group);
144
145 *group = NULL;
146
147 // String parameter: must be a mesh name
148 if(lua_type(L, index) == LUA_TSTRING)
149 {
150 QString meshName = lua_tostring(L, index);
151 GmMesh* m = mdata->meshes().value(meshName);
152
153 if(!m)
154 luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' not found in call to %2").arg(meshName, fname)));
155
156 T* tmesh = dynamic_cast<T*>(m);
157 if(!tmesh)
158 luaL_error(L, "%s", luaPrintable(QObject::tr("Mesh '%1' has an invalid type in call to %2").arg(meshName, fname)));
159
160 *mesh = tmesh;
161 return;
162 }
163
164 // Object parameter. Try a mesh
165 P* proxy = LuaProxy::toObjectOfClass<P>(L, index);
166 if(proxy)
167 {
168 *mesh = dynamic_cast<T*>(proxy->mesh());
169 return;
170 }
171
172 // Not a mesh. Can still be a Cell group set
173 GmLuaCellGroupSet* gproxy = LuaProxy::toObjectOfClass<GmLuaCellGroupSet>(L, index);
174 if(!gproxy)
175 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.")
176 .arg(index).arg(fname)));
177
178 *group = gproxy->cellGroupSet();
179 *mesh = dynamic_cast<T*>((*group)->mesh());
180 if(!*mesh)
181 luaL_error(L, "%s", luaPrintable(QObject::tr("Cell group mesh has an invalid type in call to %1").arg(fname)));
182 }
183
200 template<const char* typeName> void pushGroupDumpItemProxy(lua_State* L, GmGroupDumpItem* ptr)
201 {
202 assert(L && ptr);
203 LuaProxy::pushObject(L, new LuaSimpleTypedPtrProxy<GmGroupDumpItem, typeName>(ptr));
204 }
205
213 template <class T> T* getGroupDumpItemPtr(lua_State* L, int pos, QString tmsg, QString fmsg)
214 {
215 LuaSimplePtrProxy<GmGroupDumpItem>* proxy = LuaProxy::toObjectOfClass<LuaSimplePtrProxy<GmGroupDumpItem> >(L, pos);
216 T* ptr = proxy ? dynamic_cast<T*>(proxy->get()) : NULL;
217
218 if(!ptr) {
219 luaL_error(L, "%s", luaPrintable(QObject::tr("Invalid %1 parameter in call to %2()").arg(tmsg, fmsg)));
220 return NULL;
221 }
222 return ptr;
223 }
224
225}
226
227#endif
A proxy class to export GmCellGroupSet methods to the Lua environment.
Definition gmLuaCellGroupSet.h:34
const GmCellGroupSet * cellGroupSet() const
Returns the wrapped cell.
Definition gmLuaCellGroupSet.h:51
const QMap< QString, GmMesh * > & meshes() const
lua_State * state()
LuaEnv * env() const
QVariant getField(const char *name, LuaEnv::StackOption opt=LuaEnv::STACK_AUTO)
Declaration of the GmLuaCellGroupCell class.
Declaration of the function used to prepare an environment with the core Lua functions.
#define S_TRACE()
arma::vec GmVector
Groups utilitary routines for working with Lua.
Definition gmLuaUtils.cpp:34
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:37
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:94
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:131
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:213
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:91
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:71
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
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:200
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:109
void parseMeshOrGroup(lua_State *L, int index, const GmModelData *mdata, QString fname, T **mesh, const GmCellGroupSet **group)
Auxiliary routine to help processes that can accept from the Lua stack either a mesh object,...
Definition gmLuaUtils.h:138
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:73
const T value(const Key &key, const T &defaultValue) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
QString toString() const const