📄 interpreter.hpp
字号:
/** \file Define the Interpreter class. \copyright \endcopyright*/#ifndef LUACPP_INTERPRETER_H_#define LUACPP_INTERPRETER_H_extern "C" { #include <lauxlib.h> #include <lualib.h> #include <lua.h>}#include <string>#include <stdexcept>/* The following allows for the "library" to require no compilation whatsoever. If LUACPP_LIB_IS_HEADER_ONLY is defined at compilation time, then the .cpp file is inlined and its contents declared inline. */#ifdef LUACPP_LIB_IS_HEADER_ONLY#define OPT_INLINE inline#else#define OPT_INLINE#endifnamespace lua{/** Represent an instance of a lua interpreter. It holds its own lua_State. \todo add the newChunk() methods */class Interpreter{public: enum LuaLib { LIB_NONE = 0, LIB_BASE = 1 << 0, LIB_IO = 1 << 1, LIB_STRING = 1 << 2, LIB_MATH = 1 << 3, LIB_TABLE = 1 << 4, LIB_DEBUG = 1 << 5, LIB_PACKAGE= 1 << 6, LIB_ALL = 0xFFFF, LIB_DEFAULT = LIB_ALL & ~LIB_DEBUG // default is all minus debug }; enum ErrTypes { ERR_NONE = 0, ERR_SYNTAX = LUA_ERRSYNTAX, ERR_RUN = LUA_ERRRUN, ERR_MEM = LUA_ERRMEM, ERR_ERR = LUA_ERRERR, ERR_FILE = LUA_ERRFILE }; OPT_INLINE Interpreter(LuaLib whichLibs = LIB_DEFAULT); ~Interpreter() {lua_close(m_vm);} lua_State* state() {return m_vm;} operator lua_State* () {return m_vm;} /// Always contains the last error message generated by interpreter const std::string& errMsg() const {return m_errMsg;} // basic libraries; // TODO: determine if calling more than once leads to undefined behavior. void openLibBase() {luaopen_base(m_vm); } #if HAVE_LUA51 void openLibPackage(){luaopen_package(m_vm);} #endif void openLibIO() {luaopen_io(m_vm); } void openLibString() {luaopen_string(m_vm);} void openLibMath() {luaopen_math(m_vm); } void openLibTable() {luaopen_table(m_vm); } void openLibDebug() {luaopen_debug(m_vm); } OPT_INLINE void doFile(const std::string& filename); OPT_INLINE void doString(const std::string& cmd); private: // methods OPT_INLINE void checkOk(int status, const char* msg);private: lua_State* m_vm; std::string m_errMsg;};} // namespace#ifdef LUACPP_LIB_IS_HEADER_ONLY#include "Interpreter.cpp"#endif#endif // LUACPP_INTERPRETER_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -