Mercurial > code
changeset 194:9fc5f917872b
Update Luae bits and LuaSocket
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 27 Nov 2013 17:07:50 +0100 |
parents | 258087829c66 |
children | 42b77e0161d0 |
files | C++/Lua/LuaSocket.cpp C++/Luae.cpp C++/Luae.h C++/SocketListener.cpp C++/SocketListener.h |
diffstat | 5 files changed, 152 insertions(+), 113 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/Lua/LuaSocket.cpp Wed Nov 27 11:34:02 2013 +0100 +++ b/C++/Lua/LuaSocket.cpp Wed Nov 27 17:07:50 2013 +0100 @@ -33,15 +33,13 @@ #define ADDRESS_TYPE "SocketAddress" #define LISTENER_TYPE "SocketListener" -namespace irccd { - namespace { /* --------------------------------------------------------- * Enumerations * --------------------------------------------------------- */ -typedef std::unordered_map<std::string, int> EnumMap; +using EnumMap = std::unordered_map<std::string, int>; EnumMap createSockFamilies() { @@ -94,25 +92,23 @@ */ #if defined(_WIN32) -typedef bool OptionBool; -typedef int OptionInteger; +using OptionBool = bool; +using OptionInteger = int; #else -typedef int OptionBool; -typedef int OptionInteger; +using OptionBool = int; +using OptionInteger = int; #endif -enum class ArgType -{ +enum class ArgType { Invalid, Boolean, Integer }; -struct Option -{ +struct Option { int m_level; int m_optname; ArgType m_argtype; @@ -132,9 +128,9 @@ } }; -typedef std::unordered_map<std::string, - std::unordered_map<std::string, Option> -> OptionMap; +using OptionMap = std::unordered_map<std::string, + std::unordered_map<std::string, Option> + >; /* * Map here the socket options from the C side to Lua. It's very @@ -590,9 +586,9 @@ { luaL_checktype(L, 1, LUA_TTABLE); - std::string host = Luae::requireField<std::string>(L, 1, "host"); - int port = Luae::requireField<int>(L, 1, "port"); - int family = Luae::requireField<int>(L, 1, "family"); + std::string host = LuaeTable::require<std::string>(L, 1, "host"); + int port = LuaeTable::require<int>(L, 1, "port"); + int family = LuaeTable::require<int>(L, 1, "family"); try { new (L, ADDRESS_TYPE) ConnectAddressIP(host, port, family); @@ -614,12 +610,12 @@ luaL_checktype(L, 1, LUA_TTABLE); // Mandatory fields - port = Luae::requireField<int>(L, 1, "port"); - family = Luae::requireField<int>(L, 1, "family"); + port = LuaeTable::require<int>(L, 1, "port"); + family = LuaeTable::require<int>(L, 1, "family"); // Optional fields - if (Luae::typeField(L, 1, "address") == LUA_TSTRING) - address = Luae::requireField<std::string>(L, 1, "address"); + if (LuaeTable::type(L, 1, "address") == LUA_TSTRING) + address = LuaeTable::require<std::string>(L, 1, "address"); try { new (L, ADDRESS_TYPE) BindAddressIP(address, port, family); @@ -831,5 +827,3 @@ return 1; } - -} // !irccd
--- a/C++/Luae.cpp Wed Nov 27 11:34:02 2013 +0100 +++ b/C++/Luae.cpp Wed Nov 27 17:07:50 2013 +0100 @@ -18,38 +18,44 @@ #include "Luae.h" -namespace irccd { +/* -------------------------------------------------------- + * LuaeState + * -------------------------------------------------------- */ -LuaState::LuaState() +LuaeState::LuaeState() { m_state = Ptr(luaL_newstate()); } -LuaState::LuaState(lua_State *L) +LuaeState::LuaeState(lua_State *L) { m_state = Ptr(L); } -LuaState::LuaState(LuaState &&state) +LuaeState::LuaeState(LuaeState &&state) { m_state = std::move(state.m_state); } -LuaState &LuaState::operator=(LuaState &&state) +LuaeState &LuaeState::operator=(LuaeState &&state) { m_state = std::move(state.m_state); return *this; } -LuaState::operator lua_State*() +LuaeState::operator lua_State*() { return m_state.get(); } -LuaValue LuaValue::copy(lua_State *L, int index) +/* -------------------------------------------------------- + * LuaeValue + * -------------------------------------------------------- */ + +LuaeValue LuaeValue::copy(lua_State *L, int index) { - LuaValue v; + LuaeValue v; v.type = lua_type(L, index); @@ -65,7 +71,7 @@ break; case LUA_TTABLE: { - LuaValue k; + LuaeValue k; if (index < 0) -- index; @@ -86,7 +92,7 @@ return v; } -void LuaValue::push(lua_State *L, const LuaValue &value) +void LuaeValue::push(lua_State *L, const LuaeValue &value) { switch (value.type) { case LUA_TBOOLEAN: @@ -103,8 +109,8 @@ lua_createtable(L, 0, 0); for (auto p : value.table) { - LuaValue::push(L, p.first); - LuaValue::push(L, p.second); + push(L, p.first); + push(L, p.second); lua_settable(L, -3); } @@ -116,13 +122,17 @@ } } -LuaValue::LuaValue() +LuaeValue::LuaeValue() : type(LUA_TNIL) { } +/* -------------------------------------------------------- + * LuaeTable + * -------------------------------------------------------- */ + template <> -bool Luae::getField(lua_State *L, int idx, const std::string &name) +bool LuaeTable::get(lua_State *L, int idx, const std::string &name) { bool value = false; @@ -135,7 +145,7 @@ } template <> -double Luae::getField(lua_State *L, int idx, const std::string &name) +double LuaeTable::get(lua_State *L, int idx, const std::string &name) { double value = 0; @@ -148,7 +158,7 @@ } template <> -int Luae::getField(lua_State *L, int idx, const std::string &name) +int LuaeTable::get(lua_State *L, int idx, const std::string &name) { int value = 0; @@ -161,7 +171,7 @@ } template <> -std::string Luae::getField(lua_State *L, int idx, const std::string &name) +std::string LuaeTable::get(lua_State *L, int idx, const std::string &name) { std::string value; @@ -173,37 +183,24 @@ return value; } -int Luae::typeField(lua_State *L, int idx, const std::string &name) +int LuaeTable::type(lua_State *L, int idx, const std::string &name) { int type; - LUA_STACK_CHECKBEGIN(L); + LUAE_STACK_CHECKBEGIN(L); lua_getfield(L, idx, name.c_str()); type = lua_type(L, -1); lua_pop(L, 1); - LUA_STACK_CHECKEQUALS(L); + LUAE_STACK_CHECKEQUALS(L); return type; } -void Luae::preload(lua_State *L, const std::string &name, lua_CFunction func) +void LuaeTable::read(lua_State *L, int idx, ReadFunction func) { - LUA_STACK_CHECKBEGIN(L); - - lua_getglobal(L, "package"); - lua_getfield(L, -1, "preload"); - lua_pushcfunction(L, func); - lua_setfield(L, -2, name.c_str()); - lua_pop(L, 2); - - LUA_STACK_CHECKEQUALS(L); -} - -void Luae::readTable(lua_State *L, int idx, ReadFunction func) -{ - LUA_STACK_CHECKBEGIN(L); + LUAE_STACK_CHECKBEGIN(L); lua_pushnil(L); @@ -215,10 +212,10 @@ lua_pop(L, 1); } - LUA_STACK_CHECKEQUALS(L); + LUAE_STACK_CHECKEQUALS(L); } -int Luae::referenceField(lua_State *L, int idx, int type, const std::string &name) +int LuaeTable::ref(lua_State *L, int idx, int type, const std::string &name) { int ref = LUA_REFNIL; @@ -234,14 +231,31 @@ return ref; } +/* -------------------------------------------------------- + * Luae + * -------------------------------------------------------- */ + +void Luae::preload(lua_State *L, const std::string &name, lua_CFunction func) +{ + LUAE_STACK_CHECKBEGIN(L); + + lua_getglobal(L, "package"); + lua_getfield(L, -1, "preload"); + lua_pushcfunction(L, func); + lua_setfield(L, -2, name.c_str()); + lua_pop(L, 2); + + LUAE_STACK_CHECKEQUALS(L); +} + void Luae::require(lua_State *L, const std::string &name, lua_CFunction func, bool global) { - LUA_STACK_CHECKBEGIN(L); + LUAE_STACK_CHECKBEGIN(L); luaL_requiref(L, name.c_str(), func, global); lua_pop(L, 1); - LUA_STACK_CHECKEQUALS(L); + LUAE_STACK_CHECKEQUALS(L); } void Luae::initRegistry(lua_State *L) @@ -254,14 +268,12 @@ lua_setfield(L, LUA_REGISTRYINDEX, "refs"); } -} // !irccd - -void * operator new(size_t size, lua_State *L) +void *operator new(size_t size, lua_State *L) { return lua_newuserdata(L, size); } -void * operator new(size_t size, lua_State *L, const char *metaname) +void *operator new(size_t size, lua_State *L, const char *metaname) { void *object;
--- a/C++/Luae.h Wed Nov 27 11:34:02 2013 +0100 +++ b/C++/Luae.h Wed Nov 27 17:07:50 2013 +0100 @@ -27,16 +27,16 @@ #include <lua.hpp> -namespace irccd { +/* {{{ LuaeState */ /** - * @class LuaState + * @class LuaeState * @brief Wrapper for lua_State * * This class automatically create a new Lua state and add implicit * cast operator plus RAII destruction. */ -class LuaState { +class LuaeState { private: struct Deleter { void operator()(lua_State *L) @@ -50,34 +50,34 @@ Ptr m_state; public: - LuaState(const LuaState &) = delete; - LuaState &operator=(const LuaState &) = delete; + LuaeState(const LuaeState &) = delete; + LuaeState &operator=(const LuaeState &) = delete; /** * Default constructor. Create a new state. */ - LuaState(); + LuaeState(); /** * Use the already created state. * * @param L the state to use */ - LuaState(lua_State *L); + LuaeState(lua_State *L); /** * Move constructor. * * @param state the Lua state to move */ - LuaState(LuaState &&state); + LuaeState(LuaeState &&state); /** * Move assignment operator. * * @param state the Lua state to move */ - LuaState &operator=(LuaState &&state); + LuaeState &operator=(LuaeState &&state); /** * Implicit cast operator for convenient usage to C Lua API. @@ -87,14 +87,18 @@ operator lua_State*(); }; +/* }}} */ + +/* {{{ LuaeValue */ + /** - * @class LuaValue + * @class LuaeValue * @brief A fake variant for Lua values * * This class is primarly used for copying Lua values without checking * the types, useful to pass data. */ -class LuaValue { +class LuaeValue { private: union { lua_Number number; @@ -103,7 +107,7 @@ int type; std::string str; - std::vector<std::pair<LuaValue, LuaValue>> table; + std::vector<std::pair<LuaeValue, LuaeValue>> table; public: /** @@ -113,7 +117,7 @@ * @param index the value * @return a tree of values */ - static LuaValue copy(lua_State *L, int index); + static LuaeValue copy(lua_State *L, int index); /** * Push a value to a state. @@ -121,22 +125,25 @@ * @param L the Lua state * @param value the value to push */ - static void push(lua_State *L, const LuaValue &value); + static void push(lua_State *L, const LuaeValue &value); /** * Default constructor (type nil) */ - LuaValue(); + LuaeValue(); }; +/* }}} */ + +/* {{{ LuaeTable */ + /** - * @class Luae - * @brief Add lot of convenience for Lua + * @class LuaeTable + * @brief Some function for table manipulation * - * This class adds lot of functions for Lua and C++. + * Read, reference and get fields from tables. */ -class Luae -{ +class LuaeTable { public: using ReadFunction = std::function<void(lua_State *L, int tkey, int tvalue)>; @@ -150,7 +157,7 @@ * @return the converted type. */ template <typename T> - static T getField(lua_State *L, int idx, const std::string &name); + static T get(lua_State *L, int idx, const std::string &name); /** * Require a field from a table. @@ -161,7 +168,7 @@ * @return the value or call luaL_error */ template <typename T> - static T requireField(lua_State *L, int idx, const std::string &name) + static T require(lua_State *L, int idx, const std::string &name) { lua_getfield(L, idx, name.c_str()); @@ -171,7 +178,7 @@ lua_pop(L, 1); - return getField<T>(L, idx, name); + return get<T>(L, idx, name); } /** @@ -182,7 +189,7 @@ * @param name the field name * @return the type */ - static int typeField(lua_State *L, int idx, const std::string &name); + static int type(lua_State *L, int idx, const std::string &name); /** * Read a table, the function func is called for each element in the @@ -196,9 +203,37 @@ * @param idx the table index * @param func the function to call */ - static void readTable(lua_State *L, int idx, ReadFunction func); + static void read(lua_State *L, int idx, ReadFunction func); /** + * Reference a field from a table at the index. The reference is created in + * the registry only if type matches. + * + * @param L the Lua state + * @param idx the table index + * @param type the type requested + * @param name the field name + * @return the reference or LUA_REFNIL on problem + */ + static int ref(lua_State *L, + int idx, + int type, + const std::string &name); +}; + +/* }}} */ + +/* {{{ Luae */ + +/** + * @class Luae + * @brief Add lot of convenience for Lua + * + * This class adds lot of functions for Lua and C++. + */ +class Luae { +public: + /** * Preload a library, it will be added to package.preload so the * user can successfully call require "name". In order to work, you need * to open luaopen_package and luaopen_base first. @@ -212,20 +247,6 @@ const std::string &name, lua_CFunction func); - /** - * Reference a field from a table at the index. The reference is created in - * the registry only if type matches. - * - * @param L the Lua state - * @param idx the table index - * @param type the type requested - * @param name the field name - * @return the reference or LUA_REFNIL on problem - */ - static int referenceField(lua_State *L, - int idx, - int type, - const std::string &name); /** * Load a library just like it was loaded with require. @@ -329,27 +350,27 @@ } }; +/* }}} */ + #if !defined(NDEBUG) -#define LUA_STACK_CHECKBEGIN(L) \ +#define LUAE_STACK_CHECKBEGIN(L) \ int __topstack = lua_gettop((L)) -#define LUA_STACK_CHECKEQUALS(L) \ +#define LUAE_STACK_CHECKEQUALS(L) \ assert(lua_gettop((L)) == __topstack) -#define LUA_STACK_CHECKEND(L, cond) \ +#define LUAE_STACK_CHECKEND(L, cond) \ assert(lua_gettop((L)) cond == __topstack) #else -#define LUA_STACK_CHECKBEGIN(L) -#define LUA_STACK_CHECKEQUALS(L) -#define LUA_STACK_CHECKEND(L, cond) +#define LUAE_STACK_CHECKBEGIN(L) +#define LUAE_STACK_CHECKEQUALS(L) +#define LUAE_STACK_CHECKEND(L, cond) #endif -} // !irccd - void *operator new(size_t size, lua_State *L); void *operator new(size_t size, lua_State *L, const char *metaname);
--- a/C++/SocketListener.cpp Wed Nov 27 11:34:02 2013 +0100 +++ b/C++/SocketListener.cpp Wed Nov 27 17:07:50 2013 +0100 @@ -40,6 +40,11 @@ m_clients.clear(); } +unsigned SocketListener::size() +{ + return m_clients.size(); +} + Socket &SocketListener::select(int s, int us) { fd_set fds;
--- a/C++/SocketListener.h Wed Nov 27 11:34:02 2013 +0100 +++ b/C++/SocketListener.h Wed Nov 27 17:07:50 2013 +0100 @@ -63,6 +63,13 @@ void clear(); /** + * Get the number of clients in listener. + * + * @return the number of clients in the listener. + */ + unsigned size(); + + /** * Wait for an event in the socket list. If both s and us are set to 0 then * it waits indefinitely. *