Line data Source code
1 : /*
2 : * Copyright (c) 2010-2014: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
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 __H__UG__UG_SCRIPT__
34 : #define __H__UG__UG_SCRIPT__
35 : #include <vector>
36 : #include <string>
37 :
38 : #ifndef USE_LUAJIT
39 : // default lua
40 : extern "C" {
41 : #include "externals/lua/lua.h"
42 : #include "externals/lua/lauxlib.h"
43 : #include "externals/lua/lualib.h"
44 : }
45 : #else
46 : // luajit
47 : #include <lua.hpp>
48 : #endif
49 :
50 : #include "common/common.h"
51 : #include "common/util/path_provider.h"
52 : #include "registry/registry.h"
53 :
54 :
55 :
56 :
57 : namespace ug
58 : {
59 :
60 : namespace script
61 : {
62 :
63 : /// Error class thrown if an error occurs during parsing.
64 : class LuaError : public UGError
65 : {
66 : public:
67 0 : LuaError(const char* msg) : UGError(msg), bShowMsg(true) {}
68 0 : LuaError() : UGError(""), bShowMsg(false) {}
69 :
70 : bool show_msg() const {return bShowMsg;}
71 :
72 : protected:
73 : bool bShowMsg;
74 : };
75 :
76 :
77 :
78 : /// loads and parses a file. Several paths are tried if the file is not found.
79 : /** Throws an instance of LuaError, if a parse error occurs.
80 : * This method first tries to load the file specified with filename relative
81 : * to the path of the currently parsed file (if LoadUGScript is called from
82 : * within a load-script). If this failed, the file is tried to be loaded
83 : * with the raw specified filename. If this fails too, the method tries to
84 : * load the file from ugs scripting directory.
85 : *
86 : * Note that this method pushes the path of the currently parsed script to
87 : * PathProvider when parsing starts, and pops it when parsing is done.
88 : * \param filename The filename for the script to be loaded. may be relative to ug4/apps/
89 : * \param bDistributed if true, loads the script on core 0 and distributes
90 : * the script to all other cores via pcl::broadcast . Use this whenever
91 : * possible when doing parallel work otherwise this routine can take a long time
92 : * (ignored if UG_PARALLEL not defined)
93 : */
94 : UG_API bool LoadUGScript(const char* filename, bool bDistributed);
95 : /// calls LoadUGScript with bDistributed=true
96 : UG_API bool LoadUGScript_Parallel(const char* filename);
97 : /// calls LoadUGScript with bDistributed=false . Avoid. (\sa LoadUGScript)
98 : UG_API bool LoadUGScript_Single(const char* filename);
99 :
100 : /// registers lua only functionality at the registry
101 : UG_API void RegisterDefaultLuaBridge(ug::bridge::Registry* reg, std::string grp = "/ug4");
102 :
103 : /// returns the default lua state
104 : /** When called for the first time, or after ReleaseDefaultLuaState,
105 : * a new state is created and the methods and classes in ugs default registry
106 : * (ug::bridge::GetUGRegistry) are registered. Furthermore a callback
107 : * is registered, which registers new methods whenever
108 : * Registry::registry_changed() is called on the default registry.*/
109 : UG_API lua_State* GetDefaultLuaState();
110 :
111 : /// Releases the lua-state returned by GetDefaultLuaState().
112 : /** This method is useful, if you want to restart scripting from scratch.*/
113 : UG_API void ReleaseDefaultLuaState();
114 :
115 : /**
116 : * Parses the content of buffer and executes it in the default lua state
117 : * @param buffer the buffer to be executed
118 : * @param bufferName name of the buffer (for error messages)
119 : * @return true on success, otherwise throw(LuaError)
120 : * Throws an instance of LuaError, if a parse error occurs.
121 : */
122 : UG_API bool ParseAndExecuteBuffer(const char* buffer, const char *bufferName="buffer");
123 :
124 : /// UGLuaPrint. Redirects LUA prints to UG_LOG
125 : UG_API int UGLuaPrint(lua_State *L);
126 :
127 : /// UGLuaPrintAllProcs. Redirects LUA prints to UG_LOG_ALL_PROCS
128 : int UGLuaPrintAllProcs(lua_State *L);
129 :
130 : /// UGLuaWrite. prints LUA output to UG_LOG without adding std::endl automatically
131 : UG_API int UGLuaWrite(lua_State *L);
132 :
133 : /// Returns the metatable for the given class
134 : UG_API int UGGetMetatable(lua_State *L);
135 :
136 : /// Returns if a class contains a base class
137 : UG_API int UGIsBaseClass(lua_State *L);
138 :
139 : /// Returns if dimension is compiled into binary
140 : UG_API int UGDimCompiled(lua_State *L);
141 :
142 : /// Returns if dimension is compiled into binary
143 : UG_API int UGAlgebraCompiled(lua_State *L);
144 :
145 : /// Returns type of a userdata as string
146 : UG_API int UGGetClassName(lua_State *L);
147 :
148 : /// Returns classgroup of a userdata as string
149 : UG_API int UGGetClassGroup(lua_State *L);
150 :
151 : /**
152 : * create ugargc and ugargv in lua
153 : * we want to forward argc and argv to the lua-environment.
154 : * we'll create a table for that.
155 : * @param L the LUA state
156 : * @param argc the commandline argc
157 : * @param argv the commandline argv
158 : */
159 : UG_API void SetLuaUGArgs(lua_State* L, int argc, char* argv[]);
160 :
161 : /// register functions like print and write directly to LUA (not using the ug registry)
162 : UG_API void RegisterStdLUAFunctions(lua_State *L);
163 :
164 : /**
165 : * searches for the filename
166 : * - relative to current script
167 : * - as absolute filename
168 : * - in PathProvider::get_path(SCRIPT_PATH) (ug4/scripts)
169 : * - in PathProvider::get_path(APPS_PATH) (ug4/apps)
170 : * - in PathProvider::get_path(ROOT_PATH) (ug4)
171 : * \param filename in: relative filename to paths above. out: absolute filename (if found)
172 : * \return true if found, else false
173 : */
174 : UG_API bool GetAbsoluteUGScriptFilename(const std::string &filename, std::string &absoluteFilename);
175 :
176 : }// end of namespace
177 : }// end of namespace
178 :
179 :
180 : #endif
|