|
Lutok 0.7.0
|
A RAII model for the Lua state. More...
#include <state.hpp>
Classes | |
| struct | impl |
| Internal implementation for lutok::state. More... | |
Public Member Functions | |
| state (void) | |
| Initializes the Lua state. | |
| ~state (void) | |
| Destructor for the Lua state. | |
| void | close (void) const |
| Terminates this Lua session. | |
| void | get_global (const std::string &) |
| Wrapper around lua_getglobal. | |
| void | get_global_table (void) const |
| Pushes a reference to the global table onto the stack. | |
| bool | get_metafield (int, const std::string &) const |
| Wrapper around luaL_getmetafield. | |
| bool | get_metatable (int) const |
| Wrapper around lua_getmetatable. | |
| void | get_table (int) |
| Wrapper around lua_gettable. | |
| int | get_top (void) const |
| Wrapper around lua_gettop. | |
| void | insert (int) const |
| Wrapper around lua_insert. | |
| bool | is_boolean (int) const |
| Wrapper around lua_isboolean. | |
| bool | is_function (int) const |
| Wrapper around lua_isfunction. | |
| bool | is_nil (int) const |
| Wrapper around lua_isnil. | |
| bool | is_number (int) const |
| Wrapper around lua_isnumber. | |
| bool | is_string (int) const |
| Wrapper around lua_isstring. | |
| bool | is_table (int) const |
| Wrapper around lua_istable. | |
| bool | is_userdata (int) const |
| Wrapper around lua_isuserdata. | |
| void | load_file (const std::string &) |
| Wrapper around luaL_loadfile. | |
| void | load_string (const std::string &) |
| Wrapper around luaL_loadstring. | |
| void | new_table (void) const |
| Wrapper around lua_newtable. | |
| template<typename Type> | |
| Type * | new_userdata (void) |
| Wrapper around lua_newuserdata. | |
| bool | next (int) |
| Wrapper around lua_next. | |
| void | open_all (void) const |
| Wrapper around luaL_openlibs. | |
| void | open_base (void) |
| Wrapper around luaopen_base. | |
| void | open_string (void) const |
| Wrapper around luaopen_string. | |
| void | open_table (void) const |
| Wrapper around luaopen_table. | |
| void | pcall (int, int, int) |
| Wrapper around lua_pcall. | |
| void | pop (int) const |
| Wrapper around lua_pop. | |
| void | push_boolean (bool) const |
| Wrapper around lua_pushboolean. | |
| void | push_cxx_closure (cxx_function, int) const |
| Wrapper around lua_pushcclosure. | |
| void | push_cxx_function (cxx_function) const |
| Wrapper around lua_pushcfunction. | |
| void | push_integer (int) const |
| Wrapper around lua_pushinteger. | |
| void | push_nil (void) const |
| Wrapper around lua_pushnil. | |
| void | push_string (const std::string &) const |
| Wrapper around lua_pushstring. | |
| void | push_value (int) const |
| Wrapper around lua_pushvalue. | |
| void | raw_get (int) const |
| Wrapper around lua_rawget. | |
| void | raw_set (int) const |
| Wrapper around lua_rawset. | |
| void | set_global (const std::string &) |
| Wrapper around lua_setglobal. | |
| void | set_metatable (int) const |
| Wrapper around lua_setmetatable. | |
| void | set_table (int) |
| Wrapper around lua_settable. | |
| bool | to_boolean (int) const |
| Wrapper around lua_toboolean. | |
| lua_Integer | to_integer (int) const |
| Wrapper around lua_tointeger. | |
| template<typename Type> | |
| Type * | to_userdata (int) |
| Wrapper around lua_touserdata. | |
| std::string | to_string (int) const |
| Wrapper around lua_tostring. | |
Static Public Member Functions | |
| static int | upvalue_index (int) |
| Wrapper around lua_upvalueindex. | |
Private Member Functions | |
| void * | new_userdata_voidp (size_t) const |
| Wrapper around lua_newuserdata. | |
| void * | to_userdata_voidp (int) const |
| Wrapper around lua_touserdata. | |
| state (void *) | |
| Initializes the Lua state from an existing raw state. | |
| void * | raw_state (void) const |
| Gets the internal lua_State object. | |
Private Attributes | |
| std::shared_ptr< impl > | _pimpl |
| Pointer to the shared internal implementation. | |
Friends | |
| class | state_c_gate |
A RAII model for the Lua state.
This class holds the state of the Lua interpreter during its existence and provides wrappers around several Lua library functions that operate on such state.
These wrapper functions differ from the C versions in that they use the implicit state hold by the class, they use C++ types where appropriate and they use exceptions to report errors.
The wrappers intend to be as lightweight as possible but, in some situations, they are pretty complex because they need to do extra work to capture the errors reported by the Lua C API. We prefer having fine-grained error control rather than efficiency, so this is OK.
|
explicitprivate |
Initializes the Lua state from an existing raw state.
Instances constructed using this method do NOT own the raw state. This means that, on exit, the state will not be destroyed.
| raw_state_ | The raw Lua state to wrap. |
| lutok::state::state | ( | void | ) |
Initializes the Lua state.
You must share the same state object alongside the lifetime of your Lua session. As soon as the object is destroyed, the session is terminated.
| lutok::state::~state | ( | void | ) |
Destructor for the Lua state.
Closes the session unless it has already been closed by calling the close() method. It is recommended to explicitly close the session in the code.
| void lutok::state::close | ( | void | ) | const |
Terminates this Lua session.
It is recommended to call this instead of relying on the destructor to do the cleanup, but it is not a requirement to use close().
| void lutok::state::get_global | ( | const std::string & | name | ) |
Wrapper around lua_getglobal.
| name | The second parameter to lua_getglobal. |
| api_error | If lua_getglobal fails. |
| void lutok::state::get_global_table | ( | void | ) | const |
Pushes a reference to the global table onto the stack.
This is a wrapper around the incompatible differences between Lua 5.1 and 5.2 to access to the globals table.
|
nodiscard |
Wrapper around luaL_getmetafield.
| index | The second parameter to luaL_getmetafield. |
| name | The third parameter to luaL_getmetafield. |
|
nodiscard |
Wrapper around lua_getmetatable.
| index | The second parameter to lua_getmetatable. |
| void lutok::state::get_table | ( | int | index | ) |
Wrapper around lua_gettable.
| index | The second parameter to lua_gettable. |
| api_error | If lua_gettable fails. |
|
nodiscard |
Wrapper around lua_gettop.
| void lutok::state::insert | ( | int | index | ) | const |
Wrapper around lua_insert.
| index | The second parameter to lua_insert. |
|
nodiscard |
Wrapper around lua_isboolean.
| index | The second parameter to lua_isboolean. |
|
nodiscard |
Wrapper around lua_isfunction.
| index | The second parameter to lua_isfunction. |
|
nodiscard |
Wrapper around lua_isnil.
| index | The second parameter to lua_isnil. |
|
nodiscard |
Wrapper around lua_isnumber.
| index | The second parameter to lua_isnumber. |
|
nodiscard |
Wrapper around lua_isstring.
| index | The second parameter to lua_isstring. |
|
nodiscard |
Wrapper around lua_istable.
| index | The second parameter to lua_istable. |
|
nodiscard |
Wrapper around lua_isuserdata.
| index | The second parameter to lua_isuserdata. |
| void lutok::state::load_file | ( | const std::string & | file | ) |
Wrapper around luaL_loadfile.
| file | The second parameter to luaL_loadfile. |
| api_error | If luaL_loadfile returns an error. |
| file_not_found_error | If the file cannot be accessed. |
| void lutok::state::load_string | ( | const std::string & | str | ) |
Wrapper around luaL_loadstring.
| str | The second parameter to luaL_loadstring. |
| api_error | If luaL_loadstring returns an error. |
| void lutok::state::new_table | ( | void | ) | const |
Wrapper around lua_newtable.
| Type * lutok::state::new_userdata | ( | void | ) |
Wrapper around lua_newuserdata.
This allocates an object as big as the size of the provided Type.
|
nodiscardprivate |
Wrapper around lua_newuserdata.
This is internal. The public type-safe interface of this method should be used instead.
| size | The second parameter to lua_newuserdata. |
| bool lutok::state::next | ( | int | index | ) |
Wrapper around lua_next.
| index | The second parameter to lua_next. |
| void lutok::state::open_all | ( | void | ) | const |
Wrapper around luaL_openlibs.
| api_error | If luaL_openlibs fails. |
| void lutok::state::open_base | ( | void | ) |
Wrapper around luaopen_base.
| api_error | If luaopen_base fails. |
| void lutok::state::open_string | ( | void | ) | const |
Wrapper around luaopen_string.
| api_error | If luaopen_string fails. |
| void lutok::state::open_table | ( | void | ) | const |
Wrapper around luaopen_table.
| api_error | If luaopen_table fails. |
| void lutok::state::pcall | ( | int | nargs, |
| int | nresults, | ||
| int | errfunc ) |
Wrapper around lua_pcall.
| nargs | The second parameter to lua_pcall. |
| nresults | The third parameter to lua_pcall. |
| errfunc | The fourth parameter to lua_pcall. |
| api_error | If lua_pcall returns an error. |
| void lutok::state::pop | ( | int | count | ) | const |
Wrapper around lua_pop.
| count | The second parameter to lua_pop. |
| void lutok::state::push_boolean | ( | bool | value | ) | const |
Wrapper around lua_pushboolean.
| value | The second parameter to lua_pushboolean. |
| void lutok::state::push_cxx_closure | ( | cxx_function | function, |
| int | nvalues ) const |
Wrapper around lua_pushcclosure.
This is not a pure wrapper around lua_pushcclosure because this has to do extra magic to allow passing C++ functions instead of plain C functions.
| function | The C++ function to be pushed as a closure. |
| nvalues | The number of upvalues that the function receives. |
| void lutok::state::push_cxx_function | ( | cxx_function | function | ) | const |
Wrapper around lua_pushcfunction.
This is not a pure wrapper around lua_pushcfunction because this has to do extra magic to allow passing C++ functions instead of plain C functions.
| function | The C++ function to be pushed. |
| void lutok::state::push_integer | ( | int | value | ) | const |
Wrapper around lua_pushinteger.
| value | The second parameter to lua_pushinteger. |
| void lutok::state::push_string | ( | const std::string & | str | ) | const |
Wrapper around lua_pushstring.
| str | The second parameter to lua_pushstring. |
| void lutok::state::push_value | ( | int | index | ) | const |
Wrapper around lua_pushvalue.
| index | The second parameter to lua_pushvalue. |
| void lutok::state::raw_get | ( | int | index | ) | const |
Wrapper around lua_rawget.
| index | The second parameter to lua_rawget. |
| void lutok::state::raw_set | ( | int | index | ) | const |
Wrapper around lua_rawset.
| index | The second parameter to lua_rawset. |
|
nodiscardprivate |
Gets the internal lua_State object.
| void lutok::state::set_global | ( | const std::string & | name | ) |
Wrapper around lua_setglobal.
| name | The second parameter to lua_setglobal. |
| api_error | If lua_setglobal fails. |
| void lutok::state::set_metatable | ( | int | index | ) | const |
Wrapper around lua_setmetatable.
| index | The second parameter to lua_setmetatable. |
| void lutok::state::set_table | ( | int | index | ) |
Wrapper around lua_settable.
| index | The second parameter to lua_settable. |
| api_error | If lua_settable fails. |
|
nodiscard |
Wrapper around lua_toboolean.
| index | The second parameter to lua_toboolean. |
|
nodiscard |
Wrapper around lua_tointeger.
| index | The second parameter to lua_tointeger. |
|
nodiscard |
Wrapper around lua_tostring.
| index | The second parameter to lua_tostring. |
| Type * lutok::state::to_userdata | ( | int | index | ) |
Wrapper around lua_touserdata.
| index | The second parameter to lua_touserdata. |
|
nodiscardprivate |
Wrapper around lua_touserdata.
This is internal. The public type-safe interface of this method should be used instead.
| index | The second parameter to lua_touserdata. |
|
static |
Wrapper around lua_upvalueindex.
| index | The first parameter to lua_upvalueindex. |