Chi-Tech
chi_console.h
Go to the documentation of this file.
1#ifndef CHI_CONSOLE_H
2#define CHI_CONSOLE_H
3
4extern "C"
5{
6#include <lua.h>
7#include <lualib.h>
8#include <lauxlib.h>
9}
10#include "chi_console_structs.h"
13
14#include "chi_log_exceptions.h"
15
16#include <vector>
17#include <string>
18#include <map>
19#include <stack>
20
21class Chi;
22
23/**Small utility macro for joining two words.*/
24#define ConsoleJoinWordsA(x, y) x##y
25/**IDK why this is needed. Seems like counter doesnt work properly without it*/
26#define ConsoleJoinWordsB(x, y) ConsoleJoinWordsA(x, y)
27
28/**Macro for registering a lua_CFunction within the Console
29 * singleton, with the function being in the global namespace. Example:
30 * \code
31 * ConsoleRegisterLuaFunction(chiSolverInitialize);
32 * \endcode
33 *
34 * \note Remember to include the header "console/chi_console.h".
35 * The name supplied to this function cannot have scope resolution operators,
36 * i.e., "::".*/
37#define RegisterLuaFunctionAsIs(func_name) \
38 static char ConsoleJoinWordsB(unique_var_name_luacfunc_##func_name##_, \
39 __COUNTER__) = \
40 chi::Console::AddFunctionToRegistryGlobalNamespace(#func_name, \
41 func_name)
42
43/**Macro for registering a lua_CFunction within the Console
44* singleton.
45\param function LuaCFunction. The function to use.
46\param namespace_name NonQuotedString. May include scope resolution
47\param func_name NonQuotedString. The name of the function as it will appear in
48 the lua console.
49*/
50#define RegisterLuaFunction(function, namespace_name, func_name) \
51 static char ConsoleJoinWordsB(unique_var_name_luacfunc_##func_name##_, \
52 __COUNTER__) = \
53 chi::Console::AddFunctionToRegistryInNamespaceWithName( \
54 function, #namespace_name, #func_name)
55
56#define RegisterWrapperFunction( \
57 namespace_name, name_in_lua, syntax_function, actual_function) \
58 static char ConsoleJoinWordsB(unique_var_name_luacfunc_##name_in_lua##_, \
59 __COUNTER__) = \
60 chi::Console::AddWrapperToRegistryInNamespaceWithName( \
61 #namespace_name, #name_in_lua, syntax_function, actual_function)
62
63#define RegisterLuaConstant(namespace_name, name_in_lua, value) \
64 static char ConsoleJoinWordsB( \
65 unique_var_name_luaconst_##namespace_name##_##name_in_lua, __COUNTER__) = \
66 chi::Console::AddLuaConstantToRegistry( \
67 #namespace_name, #name_in_lua, value)
68
69#define RegisterLuaConstantAsIs(name_in_lua, value) \
70 static char ConsoleJoinWordsB( \
71 unique_var_name_luaconst_##name_in_lua, __COUNTER__) = \
72 chi::Console::AddLuaConstantToRegistry( \
73 "", #name_in_lua, value)
74
75namespace chi_physics
76{
77class Solver;
78}
79namespace chi
80{
81struct RegistryStatuses;
82}
83
84// #############################################################################
85// CLASS DEF
86namespace chi
87{
88
89/** Class for handling the console and scripting.*/
91{
92public:
95
96private:
98 {
99 lua_CFunction function_ptr;
100 std::string function_raw_name;
101 };
103 {
106 };
107
108private:
109 lua_State* console_state_; ///< Pointer to lua console state
110
111 std::vector<std::string> command_buffer_; ///< Buffer of commands to execute
113
114 std::map<std::string, LuaFunctionRegistryEntry> lua_function_registry_;
115
116 std::map<std::string, LuaFuncWrapperRegEntry> function_wrapper_registry_;
117
118 std::map<std::string, chi_data_types::Varying> lua_constants_registry_;
119
120 // 00
121 Console() noexcept;
122
123private:
124 friend class ::Chi;
126
127public:
128 static Console& GetInstance() noexcept;
129
130 lua_State*& GetConsoleState() { return console_state_; }
131 std::vector<std::string>& GetCommandBuffer() { return command_buffer_; }
132
133 const std::map<std::string, LuaFunctionRegistryEntry>&
135 {
137 }
138
139 const std::map<std::string, LuaFunctionRegistryEntry>&
141 {
143 }
144
145 // 01 Loop
146 void RunConsoleLoop(char* fileName = nullptr) const;
147 // 02 Utilities
148 int ExecuteFile(const std::string& fileName, int argc, char** argv) const;
149 void PostMPIInfo(int location_id, int number_of_processes) const;
150
151private:
152 /**Basic addition to registry. Used by the other public methods
153 * to registry a text-key to a lua function.*/
154 static void AddFunctionToRegistry(const std::string& name_in_lua,
155 lua_CFunction function_ptr);
156
157public:
158 /**\brief Adds a lua_CFunction to the registry.*/
159 static char
160 AddFunctionToRegistryGlobalNamespace(const std::string& raw_name_in_lua,
161 lua_CFunction function_ptr);
162
163 /**\brief Adds a lua_CFunction to the registry. With namespace-table
164 * analogy.*/
165 static char
166 AddFunctionToRegistryInNamespaceWithName(lua_CFunction function_ptr,
167 const std::string& namespace_name,
168 const std::string& function_name);
169
170 /**\brief Adds a constant to the lua state.*/
171 static char AddLuaConstantToRegistry(const std::string& namespace_name,
172 const std::string& constant_name,
173 const chi_data_types::Varying& value);
174
175 /**\brief A default function for returning empty input parameters. */
177
178 /**\brief Adds a function wrapper to the lua registry.*/
180 const std::string& namespace_name,
181 const std::string& name_in_lua,
182 WrapperGetInParamsFunc syntax_function,
183 WrapperCallFunc actual_function,
184 bool ignore_null_call_func = false);
185
186 /**\brief Formats a namespace structure as table.*/
187 static void
188 SetLuaFuncNamespaceTableStructure(const std::string& full_lua_name,
189 lua_CFunction function_ptr);
190
191 /**\brief Formats a namespace structure as a table, but the last entry
192 * is a function call.*/
193 static void
194 SetLuaFuncWrapperNamespaceTableStructure(const std::string& full_lua_name);
195
196 /**\brief Formats a namespace structure as a table, but the last entry
197 * contains a "Create" function and a type.*/
198 static void
199 SetObjectNamespaceTableStructure(const std::string& full_lua_name);
200
201 /**\brief Makes sure a table structure exists for the list of table names.*/
202 static void
203 FleshOutLuaTableStructure(const std::vector<std::string>& table_names);
204
205 /**Sets a lua constant in the lua state.*/
206 static void SetLuaConstant(const std::string& constant_name,
207 const chi_data_types::Varying& value);
208
209 // 03
210 /**\brief Flushes any commands in the command buffer.*/
211 void FlushConsole();
212 // 05 Memory
213 /**\brief Get current memory usage.*/
214 static CSTMemory GetMemoryUsage();
215 /**\brief Get current memory usage in Megabytes.*/
216 static double GetMemoryUsageInMB();
217
218 // fwrapper_call
219 /**\brief Generic entry point for wrapper calls.*/
220 static int LuaWrapperCall(lua_State* L);
221
222 /**\brief Dumps the object registry to stdout.*/
223 void DumpRegister() const;
224
225 /**Given an old status, will update the bindings for only newly registered
226 * items.*/
227 void UpdateConsoleBindings(const chi::RegistryStatuses& old_statuses);
228};
229} // namespace chi
230
231#endif
static void AddFunctionToRegistry(const std::string &name_in_lua, lua_CFunction function_ptr)
static double GetMemoryUsageInMB()
Get current memory usage in Megabytes.
chi::ParameterBlock(*)(const chi::InputParameters &) WrapperCallFunc
Definition: chi_console.h:94
std::map< std::string, chi_data_types::Varying > lua_constants_registry_
Definition: chi_console.h:118
void UpdateConsoleBindings(const chi::RegistryStatuses &old_statuses)
static void SetLuaFuncWrapperNamespaceTableStructure(const std::string &full_lua_name)
Formats a namespace structure as a table, but the last entry is a function call.
int ExecuteFile(const std::string &fileName, int argc, char **argv) const
std::map< std::string, LuaFuncWrapperRegEntry > function_wrapper_registry_
Definition: chi_console.h:116
chi::InputParameters(*)() WrapperGetInParamsFunc
Definition: chi_console.h:93
std::map< std::string, LuaFunctionRegistryEntry > lua_function_registry_
Definition: chi_console.h:114
lua_State *& GetConsoleState()
Definition: chi_console.h:130
static char AddLuaConstantToRegistry(const std::string &namespace_name, const std::string &constant_name, const chi_data_types::Varying &value)
Adds a constant to the lua state.
static CSTMemory GetMemoryUsage()
Get current memory usage.
static char AddFunctionToRegistryInNamespaceWithName(lua_CFunction function_ptr, const std::string &namespace_name, const std::string &function_name)
Adds a lua_CFunction to the registry. With namespace-table analogy.
static Console instance_
Definition: chi_console.h:112
std::vector< std::string > & GetCommandBuffer()
Definition: chi_console.h:131
static void SetLuaConstant(const std::string &constant_name, const chi_data_types::Varying &value)
void PostMPIInfo(int location_id, int number_of_processes) const
static int LuaWrapperCall(lua_State *L)
Generic entry point for wrapper calls.
static Console & GetInstance() noexcept
lua_State * console_state_
Pointer to lua console state.
Definition: chi_console.h:109
void FlushConsole()
Flushes any commands in the command buffer.
const std::map< std::string, LuaFunctionRegistryEntry > & GetLuaFunctionRegistry() const
Definition: chi_console.h:134
static void SetObjectNamespaceTableStructure(const std::string &full_lua_name)
Formats a namespace structure as a table, but the last entry contains a "Create" function and a type.
static InputParameters DefaultGetInParamsFunc()
A default function for returning empty input parameters.
void DumpRegister() const
Dumps the object registry to stdout.
void RunConsoleLoop(char *fileName=nullptr) const
std::vector< std::string > command_buffer_
Buffer of commands to execute.
Definition: chi_console.h:111
const std::map< std::string, LuaFunctionRegistryEntry > & GetFunctionWrapperRegistry() const
Definition: chi_console.h:140
static void FleshOutLuaTableStructure(const std::vector< std::string > &table_names)
Makes sure a table structure exists for the list of table names.
static char AddFunctionToRegistryGlobalNamespace(const std::string &raw_name_in_lua, lua_CFunction function_ptr)
Adds a lua_CFunction to the registry.
static char AddWrapperToRegistryInNamespaceWithName(const std::string &namespace_name, const std::string &name_in_lua, WrapperGetInParamsFunc syntax_function, WrapperCallFunc actual_function, bool ignore_null_call_func=false)
Adds a function wrapper to the lua registry.
static void SetLuaFuncNamespaceTableStructure(const std::string &full_lua_name, lua_CFunction function_ptr)
Formats a namespace structure as table.
Definition: chi_console.h:103
WrapperGetInParamsFunc get_in_params_func
Definition: chi_console.h:104
WrapperCallFunc call_func
Definition: chi_console.h:105
Definition: chi_console.h:98
lua_CFunction function_ptr
Definition: chi_console.h:99
std::string function_raw_name
Definition: chi_console.h:100