From 972ff3d010e424a88b5bf8fef4719db7e5aa4137 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Tue, 5 Jan 2016 14:57:29 +0200 Subject: [PATCH] Some performance and warning fixes --- BindingMap.h | 56 ++++++++++++++++++++++++++++++++++--------------- ElunaTemplate.h | 17 ++++++++++----- LuaEngine.cpp | 32 ++++++++-------------------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/BindingMap.h b/BindingMap.h index 313d489..5549d4d 100644 --- a/BindingMap.h +++ b/BindingMap.h @@ -260,6 +260,39 @@ struct UniqueObjectKey { } }; +class hash_helper +{ +public: + typedef std::size_t result_type; + + template + static inline result_type hash(T const &... t) + { + result_type seed = 0; + _hash_combine(seed, t...); + return seed; + } + + template + static inline result_type hash(T const & t) + { + return std::hash()(t); + } + +private: + template + static inline void _hash_combine(result_type& seed, T const & v) + { + seed ^= std::hash()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } + + template + static inline void _hash_combine(result_type& seed, H const & h, T const &... t) + { + _hash_combine(seed, h); + _hash_combine(seed, t...); + } +}; /* * Implementations of various std functions on the above key types, @@ -301,12 +334,10 @@ namespace std struct hash < EventKey > { typedef EventKey argument_type; - typedef std::size_t result_type; - result_type operator()(argument_type const& k) const + hash_helper::result_type operator()(argument_type const& k) const { - result_type const h1(std::hash()(k.event_id)); - return h1; + return hash_helper::hash(k.event_id); } }; @@ -314,14 +345,10 @@ namespace std struct hash < EntryKey > { typedef EntryKey argument_type; - typedef std::size_t result_type; - result_type operator()(argument_type const& k) const + hash_helper::result_type operator()(argument_type const& k) const { - result_type const h1(std::hash()(k.event_id)); - result_type const h2(std::hash()(k.entry)); - - return h1 ^ (h2 << 8); // `event_id` probably won't exceed 2^8. + return hash_helper::hash(k.event_id, k.entry); } }; @@ -329,15 +356,10 @@ namespace std struct hash < UniqueObjectKey > { typedef UniqueObjectKey argument_type; - typedef std::size_t result_type; - result_type operator()(argument_type const& k) const + hash_helper::result_type operator()(argument_type const& k) const { - result_type const h1(std::hash()(k.event_id)); - result_type const h2(std::hash()(k.instance_id)); - result_type const h3(std::hash()(k.guid)); - - return h1 ^ (h2 << 8) ^ (h3 << 24); // `instance_id` probably won't exceed 2^16. + return hash_helper::hash(k.event_id, k.instance_id, k.guid); } }; } diff --git a/ElunaTemplate.h b/ElunaTemplate.h index 73cf2c0..057e67d 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -66,10 +66,8 @@ public: class ElunaObject { public: - ElunaObject(void* obj, bool manageMemory) : _isvalid(false), _invalidate(!manageMemory), object(obj) - { - SetValid(true); - } + template + ElunaObject::ElunaObject(T * obj, bool manageMemory); ~ElunaObject() { @@ -81,6 +79,8 @@ public: bool IsValid() const { return _isvalid; } // Returns whether the object can be invalidated or not bool CanInvalidate() const { return _invalidate; } + // Returns pointer to the wrapped object's type name + const char* GetTypeName() const { return type_name; } // Sets the object pointer that is wrapped void SetObj(void* obj) @@ -111,6 +111,7 @@ private: bool _isvalid; bool _invalidate; void* object; + const char* type_name; }; template @@ -288,7 +289,7 @@ public: lua_pushnil(L); return 1; } - *ptrHold = new ElunaObject(obj_voidptr, manageMemory); + *ptrHold = new ElunaObject(const_cast(obj), manageMemory); // Set metatable for it lua_getglobal(L, tname); @@ -404,4 +405,10 @@ public: static int Call(lua_State* L) { return luaL_error(L, "attempt to call a %s value", tname); } }; +template +ElunaObject::ElunaObject(T * obj, bool manageMemory) : _isvalid(false), _invalidate(!manageMemory), object(obj), type_name(ElunaTemplate::tname) +{ + SetValid(true); +} + #endif diff --git a/LuaEngine.cpp b/LuaEngine.cpp index 990ea36..b4f49d5 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -810,7 +810,7 @@ template<> bool Eluna::CHECKVAL(lua_State* luastate, int narg) } template<> float Eluna::CHECKVAL(lua_State* luastate, int narg) { - return luaL_checknumber(luastate, narg); + return static_cast(luaL_checknumber(luastate, narg)); } template<> double Eluna::CHECKVAL(lua_State* luastate, int narg) { @@ -906,35 +906,21 @@ template<> ElunaObject* Eluna::CHECKOBJ(lua_State* luastate, int na ElunaObject* Eluna::CHECKTYPE(lua_State* luastate, int narg, const char* tname, bool error) { - bool valid = false; - ElunaObject** ptrHold = NULL; - - if (!tname) + if (lua_islightuserdata(luastate, narg)) { - valid = true; - ptrHold = static_cast(lua_touserdata(luastate, narg)); - } - else - { - if (lua_getmetatable(luastate, narg)) - { - lua_getglobal(luastate, tname); - bool equal = lua_rawequal(luastate, -1, -2) == 1; - lua_pop(luastate, 2); - if (equal) - { - valid = true; - ptrHold = static_cast(lua_touserdata(luastate, narg)); - } - } + if (error) + luaL_argerror(luastate, narg, "bad argument : userdata expected, got lightuserdata"); + return NULL; } - if (!valid || !ptrHold) + ElunaObject** ptrHold = static_cast(lua_touserdata(luastate, narg)); + + if (!ptrHold || (tname && (*ptrHold)->GetTypeName() != tname)) { if (error) { char buff[256]; - snprintf(buff, 256, "bad argument : %s expected, got %s", tname ? tname : "ElunaObject", luaL_typename(luastate, narg)); + snprintf(buff, 256, "bad argument : %s expected, got %s", tname ? tname : "ElunaObject", ptrHold ? (*ptrHold)->GetTypeName() : luaL_typename(luastate, narg)); luaL_argerror(luastate, narg, buff); } return NULL;