Cafu Engine
ConsoleInterpreter.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_HPP_INCLUDED
8 #define CAFU_CONSOLE_INTERPRETER_HPP_INCLUDED
9 
10 #include "Templates/Array.hpp"
11 #include <string>
12 
13 
14 class ConFuncT;
15 class ConVarT;
16 struct lua_State;
17 
18 
19 /// This class is an interface to the console interpreter.
20 /// User code can register its convars and confuncs with it so that they can be used in the context of the interpreter.
21 /// For each application, there is only one, global, application-wide, unique implementation of the console interpreter,
22 /// typically in the main exe, and the exe and all dlls each have a pointer to it.
24 {
25  public:
26 
27  /// Virtual dtor so that derived classes properly destroy.
28  virtual ~ConsoleInterpreterI() { }
29 
30  /// Registers a convar with the console interpreter.
31  /// This method is normally not called by user code, but by the implementation of the ConVarT ctors and ConVarT::RegisterStaticList().
32  /// @param ConVar The console variable to register with this interpreter.
33  virtual void Register(ConVarT* ConVar)=0;
34 
35  /// Registers a confunc with the console interpreter.
36  /// This method is normally not called by user code, but by the implementation of the ConFuncT ctors and ConFuncT::RegisterStaticList().
37  /// @param ConFunc The console function to register with this interpreter.
38  virtual void Register(ConFuncT* ConFunc)=0;
39 
40  /// Unregisters the given convar from the interpreter again.
41  /// @param ConVar The console variable to unregister from this interpreter.
42  virtual void Unregister(ConVarT* ConVar)=0;
43 
44  /// Unregisters the given confunc from the interpreter again.
45  /// @param ConFunc The console function to unregister from this interpreter.
46  virtual void Unregister(ConFuncT* ConFunc)=0;
47 
48  /// Finds the confunc with the given name.
49  /// @param Name The name of the confunc to find.
50  /// @returns the pointer to the confunc, or NULL if a confunc with that name does not exist.
51  virtual ConFuncT* FindFunc(const std::string& Name)=0;
52 
53  /// Finds the convar with the given name.
54  /// @param Name The name of the convar to find.
55  /// @returns the pointer to the convar, or NULL if a convar with that name does not exist.
56  virtual ConVarT* FindVar(const std::string& Name)=0;
57 
58  /// This method provides command-line completion for this interpreter.
59  /// It returns all available completions for the last token in the given string LineBegin in the Completions array.
60  /// Note that the completions not only cover the registered ConFuncTs and ConVarTs, but also any other user-
61  /// and implementation-defined symbols.
62  /// @param LineBegin The incomplete command-line string for whose last token the method is supposed to provide completions for.
63  /// For example, if LineBegin is "a=GetValueX()*Get", the method is supposed to look for completions for "Get".
64  /// @param Completions The found completions are returned here as complete tokens.
65  /// For example, if LineBegin is "a=GetValueX()*Get", Completions may contain the strings "GetValueX" and "GetValueY".
66  /// The caller can therefore not do much (i.e. concat to input string) with the returned strings but print them out.
67  /// @returns the longest expansion substring for LineBegin that works with all completions.
68  /// The caller can simply concatenate this string to LineBegin to implement incremental completions.
69  /// In the context of the above examples, "Value" would be the expected return string.
70  virtual std::string LineCompletion(const std::string& LineBegin, ArrayT<std::string>& Completions)=0;
71 
72  // TODO: Some commands can possibly not be executed immediately.
73  // For example, a "map" command for changing the level can probably not be executed in mid-frame from a Think()ing entity,
74  // but should only be executed between frames, e.g. after Think()ing returned.
75 
76  /// Compiles and runs the given Lua statements.
77  /// @param Input The string with the Lua statements that is to be compiled and run within this console state.
78  /// @returns true if the command has been successfully loaded (compiled) and run, false otherwise.
79  virtual bool RunCommand(const std::string& Input)=0;
80 
81 
82  /// Registers the methods of this interface with LuaState as a Lua module as described in the PiL2 book, chapter 26.2.
83  /// The key idea is that all methods are called via the global ConsoleInterpreter variable defined below,
84  /// and therefore we may consider them as a collection of C-style functions (no OO involved),
85  /// so that putting them in a Lua table as described in chapter 26 of the PiL2 book is straightforward.
86  static void RegisterLua(lua_State* LuaState);
87 };
88 
89 
90 /// A global pointer to an implementation of the ConsoleInterpreterI interface.
91 ///
92 /// Each module (exe or dll) that uses this pointer must somewhere provide exactly one definition for it (none is provided by the ConsoleInterpreter library).
93 /// That is, typically the main.cpp or similar file of each exe and dll must contain a line like
94 /// ConsoleInterpreterI* ConsoleInterpreter=NULL;
95 /// or else the module will not link successfully due to an undefined symbol.
96 ///
97 /// Exe files will then want to reset this pointer to an instance of a ConsoleInterpreterImplT during their initialization
98 /// e.g. by code like: ConsoleInterpreter=new ConsoleInterpreterImplT;
99 /// Note that the ConsoleInterpreterImplT ctor may require that other interfaces (e.g. the Console) have been inited first.
100 ///
101 /// Dlls typically get one of their init functions called immediately after they have been loaded.
102 /// By doing so, the exe passes a pointer to its above instance to the dll, which in turn copies it to its ConsoleInterpreter variable.
103 extern ConsoleInterpreterI* ConsoleInterpreter;
104 
105 #endif
virtual void Unregister(ConVarT *ConVar)=0
Unregisters the given convar from the interpreter again.
This class implements Console Variables ("convars").
Definition: ConVar.hpp:39
virtual ConVarT * FindVar(const std::string &Name)=0
Finds the convar with the given name.
static void RegisterLua(lua_State *LuaState)
Registers the methods of this interface with LuaState as a Lua module as described in the PiL2 book...
Definition: ConsoleInterpreter_LuaBinding.cpp:78
This class is an interface to the console interpreter.
Definition: ConsoleInterpreter.hpp:23
virtual bool RunCommand(const std::string &Input)=0
Compiles and runs the given Lua statements.
virtual ConFuncT * FindFunc(const std::string &Name)=0
Finds the confunc with the given name.
virtual ~ConsoleInterpreterI()
Virtual dtor so that derived classes properly destroy.
Definition: ConsoleInterpreter.hpp:28
virtual std::string LineCompletion(const std::string &LineBegin, ArrayT< std::string > &Completions)=0
This method provides command-line completion for this interpreter.
Definition: ConFunc.hpp:18
virtual void Register(ConVarT *ConVar)=0
Registers a convar with the console interpreter.