Cafu Engine
ConsoleInterpreterImpl.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_CONSOLE_INTERPRETER_IMPL_HPP_INCLUDED
8 #define CAFU_CONSOLE_INTERPRETER_IMPL_HPP_INCLUDED
9 
10 #include "ConsoleInterpreter.hpp"
11 
12 
13 /// This class provides an implementation for the ConsoleInterpreterI interface.
14 ///
15 /// The key idea is that the console program state is represented by a Lua program / Lua state.
16 /// - Each ConFuncT that is registered with the interpreter is made available as a Lua function.
17 /// All these functions are grouped in a common table (used as a "namespace").
18 /// The name of that table is defined by the implementation, it's normally either "c" (Cafu-
19 /// specific functions), or "_G" (the table of global variables, this is more convenient).
20 /// When a ConFuncT is unregistered, it is of course removed again from the table.
21 /// - The ConVarTs also have a dedicated table that contains them, this is the same table as for
22 /// the functions. However, the situation is a bit more complicated here:
23 /// a) When a ConVarT that is not (yet) registered with the interpreter, the user can still use
24 /// and work with a value of that name in our common table. The value is then a native Lua value.
25 /// b) When a ConVarT is registered, the table value is set as the default value for the ConVarT,
26 /// the table value is cleared to nil, and all subsequent variable accesses (reads and writes)
27 /// the variable are (re-)routed to the ConVarT by the related metamethods.
28 /// c) When a ConVarT is unregistered again, its value at that time is re-established in the table
29 /// under the name of the ConVarT, so that users can transparently continue to use it.
30 /// In summary, registered ConVarTs are thought to "overlay" the corresponding Lua values.
31 /// This makes it possible to define default value before the ConVarT is registered for the first time,
32 /// and to preserve their values when a ConVarT becomes temporarily unregistered.
34 {
35  public:
36 
39 
40  // The methods from the ConsoleInterpreterI interface.
41  void Register(ConVarT* ConVar);
42  void Register(ConFuncT* ConFunc);
43  void Unregister(ConVarT* ConVar);
44  void Unregister(ConFuncT* ConFunc);
45  ConFuncT* FindFunc(const std::string& Name);
46  ConVarT* FindVar(const std::string& Name);
47  std::string LineCompletion(const std::string& LineBegin, ArrayT<std::string>& Completions);
48  bool RunCommand(const std::string& Input);
49 
50  static int Lua_get_Callback(lua_State* LuaState);
51  static int Lua_set_Callback(lua_State* LuaState);
52  static int ConFunc_Help_Callback(lua_State* LuaState);
53  static int ConFunc_List_Callback(lua_State* LuaState);
54 
55 
56  private:
57 
58  lua_State* LuaState; ///< The Lua environment with the actual console state.
59  ArrayT<ConVarT*> RegisteredConVars; ///< This is the global list of master convars.
60  ArrayT<ConFuncT*> RegisteredConFuncs; ///< This is the global list of master confuncs.
61 };
62 
63 #endif
This class implements Console Variables ("convars").
Definition: ConVar.hpp:39
static int ConFunc_List_Callback(lua_State *LuaState)
If no parameter is given, this function lists all known console functions and variables.
Definition: ConsoleInterpreterImpl.cpp:550
std::string LineCompletion(const std::string &LineBegin, ArrayT< std::string > &Completions)
This method provides command-line completion for this interpreter.
Definition: ConsoleInterpreterImpl.cpp:298
ConFuncT * FindFunc(const std::string &Name)
Finds the confunc with the given name.
Definition: ConsoleInterpreterImpl.cpp:278
This class is an interface to the console interpreter.
Definition: ConsoleInterpreter.hpp:23
void Register(ConVarT *ConVar)
Registers a convar with the console interpreter.
Definition: ConsoleInterpreterImpl.cpp:126
void Unregister(ConVarT *ConVar)
Unregisters the given convar from the interpreter again.
Definition: ConsoleInterpreterImpl.cpp:229
This class provides an implementation for the ConsoleInterpreterI interface.
Definition: ConsoleInterpreterImpl.hpp:33
bool RunCommand(const std::string &Input)
Compiles and runs the given Lua statements.
Definition: ConsoleInterpreterImpl.cpp:368
Definition: ConFunc.hpp:18
ConVarT * FindVar(const std::string &Name)
Finds the convar with the given name.
Definition: ConsoleInterpreterImpl.cpp:288