Cafu Engine
ConsoleInterpreterImplT Class Reference

This class provides an implementation for the ConsoleInterpreterI interface. More...

#include "ConsoleInterpreterImpl.hpp"

Inheritance diagram for ConsoleInterpreterImplT:

Public Member Functions

void Register (ConVarT *ConVar)
 Registers a convar with the console interpreter. More...
 
void Register (ConFuncT *ConFunc)
 Registers a confunc with the console interpreter. More...
 
void Unregister (ConVarT *ConVar)
 Unregisters the given convar from the interpreter again. More...
 
void Unregister (ConFuncT *ConFunc)
 Unregisters the given confunc from the interpreter again. More...
 
ConFuncTFindFunc (const std::string &Name)
 Finds the confunc with the given name. More...
 
ConVarTFindVar (const std::string &Name)
 Finds the convar with the given name. More...
 
std::string LineCompletion (const std::string &LineBegin, ArrayT< std::string > &Completions)
 This method provides command-line completion for this interpreter. More...
 
bool RunCommand (const std::string &Input)
 Compiles and runs the given Lua statements. More...
 
- Public Member Functions inherited from ConsoleInterpreterI
virtual ~ConsoleInterpreterI ()
 Virtual dtor so that derived classes properly destroy. More...
 

Static Public Member Functions

static int Lua_get_Callback (lua_State *LuaState)
 
static int Lua_set_Callback (lua_State *LuaState)
 
static int ConFunc_Help_Callback (lua_State *LuaState)
 
static int ConFunc_List_Callback (lua_State *LuaState)
 If no parameter is given, this function lists all known console functions and variables. More...
 
- Static Public Member Functions inherited from ConsoleInterpreterI
static void RegisterLua (lua_State *LuaState)
 Registers the methods of this interface with LuaState as a Lua module as described in the PiL2 book, chapter 26.2. More...
 

Detailed Description

This class provides an implementation for the ConsoleInterpreterI interface.

The key idea is that the console program state is represented by a Lua program / Lua state.

  • Each ConFuncT that is registered with the interpreter is made available as a Lua function. All these functions are grouped in a common table (used as a "namespace"). The name of that table is defined by the implementation, it's normally either "c" (Cafu- specific functions), or "_G" (the table of global variables, this is more convenient). When a ConFuncT is unregistered, it is of course removed again from the table.
  • The ConVarTs also have a dedicated table that contains them, this is the same table as for the functions. However, the situation is a bit more complicated here: a) When a ConVarT that is not (yet) registered with the interpreter, the user can still use and work with a value of that name in our common table. The value is then a native Lua value. b) When a ConVarT is registered, the table value is set as the default value for the ConVarT, the table value is cleared to nil, and all subsequent variable accesses (reads and writes) the variable are (re-)routed to the ConVarT by the related metamethods. c) When a ConVarT is unregistered again, its value at that time is re-established in the table under the name of the ConVarT, so that users can transparently continue to use it. In summary, registered ConVarTs are thought to "overlay" the corresponding Lua values. This makes it possible to define default value before the ConVarT is registered for the first time, and to preserve their values when a ConVarT becomes temporarily unregistered.

Member Function Documentation

int ConsoleInterpreterImplT::ConFunc_List_Callback ( lua_State *  LuaState)
static

If no parameter is given, this function lists all known console functions and variables.

Otherwise, the output is limited to matches with the given prefix.

ConFuncT * ConsoleInterpreterImplT::FindFunc ( const std::string &  Name)
virtual

Finds the confunc with the given name.

Parameters
NameThe name of the confunc to find.
Returns
the pointer to the confunc, or NULL if a confunc with that name does not exist.

Implements ConsoleInterpreterI.

ConVarT * ConsoleInterpreterImplT::FindVar ( const std::string &  Name)
virtual

Finds the convar with the given name.

Parameters
NameThe name of the convar to find.
Returns
the pointer to the convar, or NULL if a convar with that name does not exist.

Implements ConsoleInterpreterI.

std::string ConsoleInterpreterImplT::LineCompletion ( const std::string &  LineBegin,
ArrayT< std::string > &  Completions 
)
virtual

This method provides command-line completion for this interpreter.

It returns all available completions for the last token in the given string LineBegin in the Completions array. Note that the completions not only cover the registered ConFuncTs and ConVarTs, but also any other user- and implementation-defined symbols.

Parameters
LineBeginThe incomplete command-line string for whose last token the method is supposed to provide completions for. For example, if LineBegin is "a=GetValueX()*Get", the method is supposed to look for completions for "Get".
CompletionsThe found completions are returned here as complete tokens. For example, if LineBegin is "a=GetValueX()*Get", Completions may contain the strings "GetValueX" and "GetValueY". The caller can therefore not do much (i.e. concat to input string) with the returned strings but print them out.
Returns
the longest expansion substring for LineBegin that works with all completions. The caller can simply concatenate this string to LineBegin to implement incremental completions. In the context of the above examples, "Value" would be the expected return string.

Implements ConsoleInterpreterI.

void ConsoleInterpreterImplT::Register ( ConVarT ConVar)
virtual

Registers a convar with the console interpreter.

This method is normally not called by user code, but by the implementation of the ConVarT ctors and ConVarT::RegisterStaticList().

Parameters
ConVarThe console variable to register with this interpreter.

Implements ConsoleInterpreterI.

void ConsoleInterpreterImplT::Register ( ConFuncT ConFunc)
virtual

Registers a confunc with the console interpreter.

This method is normally not called by user code, but by the implementation of the ConFuncT ctors and ConFuncT::RegisterStaticList().

Parameters
ConFuncThe console function to register with this interpreter.

Implements ConsoleInterpreterI.

bool ConsoleInterpreterImplT::RunCommand ( const std::string &  Input)
virtual

Compiles and runs the given Lua statements.

Parameters
InputThe string with the Lua statements that is to be compiled and run within this console state.
Returns
true if the command has been successfully loaded (compiled) and run, false otherwise.

Implements ConsoleInterpreterI.

void ConsoleInterpreterImplT::Unregister ( ConVarT ConVar)
virtual

Unregisters the given convar from the interpreter again.

Parameters
ConVarThe console variable to unregister from this interpreter.

Implements ConsoleInterpreterI.

void ConsoleInterpreterImplT::Unregister ( ConFuncT ConFunc)
virtual

Unregisters the given confunc from the interpreter again.

Parameters
ConFuncThe console function to unregister from this interpreter.

Implements ConsoleInterpreterI.


The documentation for this class was generated from the following files: