Some performance and warning fixes

This commit is contained in:
Rochet2
2016-01-05 14:57:29 +02:00
parent 3f3de007ef
commit 972ff3d010
3 changed files with 60 additions and 45 deletions

View File

@@ -260,6 +260,39 @@ struct UniqueObjectKey
{ } { }
}; };
class hash_helper
{
public:
typedef std::size_t result_type;
template <typename... T>
static inline result_type hash(T const &... t)
{
result_type seed = 0;
_hash_combine(seed, t...);
return seed;
}
template <typename T>
static inline result_type hash(T const & t)
{
return std::hash<T>()(t);
}
private:
template <typename T>
static inline void _hash_combine(result_type& seed, T const & v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <typename H, typename... T>
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, * Implementations of various std functions on the above key types,
@@ -301,12 +334,10 @@ namespace std
struct hash < EventKey<T> > struct hash < EventKey<T> >
{ {
typedef EventKey<T> argument_type; typedef EventKey<T> 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<uint32>()(k.event_id)); return hash_helper::hash(k.event_id);
return h1;
} }
}; };
@@ -314,14 +345,10 @@ namespace std
struct hash < EntryKey<T> > struct hash < EntryKey<T> >
{ {
typedef EntryKey<T> argument_type; typedef EntryKey<T> 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<uint32>()(k.event_id)); return hash_helper::hash(k.event_id, k.entry);
result_type const h2(std::hash<uint32>()(k.entry));
return h1 ^ (h2 << 8); // `event_id` probably won't exceed 2^8.
} }
}; };
@@ -329,15 +356,10 @@ namespace std
struct hash < UniqueObjectKey<T> > struct hash < UniqueObjectKey<T> >
{ {
typedef UniqueObjectKey<T> argument_type; typedef UniqueObjectKey<T> 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<uint32>()(k.event_id)); return hash_helper::hash(k.event_id, k.instance_id, k.guid);
result_type const h2(std::hash<uint32>()(k.instance_id));
result_type const h3(std::hash<uint64>()(k.guid));
return h1 ^ (h2 << 8) ^ (h3 << 24); // `instance_id` probably won't exceed 2^16.
} }
}; };
} }

View File

@@ -66,10 +66,8 @@ public:
class ElunaObject class ElunaObject
{ {
public: public:
ElunaObject(void* obj, bool manageMemory) : _isvalid(false), _invalidate(!manageMemory), object(obj) template<typename T>
{ ElunaObject::ElunaObject(T * obj, bool manageMemory);
SetValid(true);
}
~ElunaObject() ~ElunaObject()
{ {
@@ -81,6 +79,8 @@ public:
bool IsValid() const { return _isvalid; } bool IsValid() const { return _isvalid; }
// Returns whether the object can be invalidated or not // Returns whether the object can be invalidated or not
bool CanInvalidate() const { return _invalidate; } 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 // Sets the object pointer that is wrapped
void SetObj(void* obj) void SetObj(void* obj)
@@ -111,6 +111,7 @@ private:
bool _isvalid; bool _isvalid;
bool _invalidate; bool _invalidate;
void* object; void* object;
const char* type_name;
}; };
template<typename T> template<typename T>
@@ -288,7 +289,7 @@ public:
lua_pushnil(L); lua_pushnil(L);
return 1; return 1;
} }
*ptrHold = new ElunaObject(obj_voidptr, manageMemory); *ptrHold = new ElunaObject(const_cast<T*>(obj), manageMemory);
// Set metatable for it // Set metatable for it
lua_getglobal(L, tname); 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); } static int Call(lua_State* L) { return luaL_error(L, "attempt to call a %s value", tname); }
}; };
template<typename T>
ElunaObject::ElunaObject(T * obj, bool manageMemory) : _isvalid(false), _invalidate(!manageMemory), object(obj), type_name(ElunaTemplate<T>::tname)
{
SetValid(true);
}
#endif #endif

View File

@@ -810,7 +810,7 @@ template<> bool Eluna::CHECKVAL<bool>(lua_State* luastate, int narg)
} }
template<> float Eluna::CHECKVAL<float>(lua_State* luastate, int narg) template<> float Eluna::CHECKVAL<float>(lua_State* luastate, int narg)
{ {
return luaL_checknumber(luastate, narg); return static_cast<float>(luaL_checknumber(luastate, narg));
} }
template<> double Eluna::CHECKVAL<double>(lua_State* luastate, int narg) template<> double Eluna::CHECKVAL<double>(lua_State* luastate, int narg)
{ {
@@ -906,35 +906,21 @@ template<> ElunaObject* Eluna::CHECKOBJ<ElunaObject>(lua_State* luastate, int na
ElunaObject* Eluna::CHECKTYPE(lua_State* luastate, int narg, const char* tname, bool error) ElunaObject* Eluna::CHECKTYPE(lua_State* luastate, int narg, const char* tname, bool error)
{ {
bool valid = false; if (lua_islightuserdata(luastate, narg))
ElunaObject** ptrHold = NULL;
if (!tname)
{ {
valid = true; if (error)
ptrHold = static_cast<ElunaObject**>(lua_touserdata(luastate, narg)); luaL_argerror(luastate, narg, "bad argument : userdata expected, got lightuserdata");
} return NULL;
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<ElunaObject**>(lua_touserdata(luastate, narg));
}
}
} }
if (!valid || !ptrHold) ElunaObject** ptrHold = static_cast<ElunaObject**>(lua_touserdata(luastate, narg));
if (!ptrHold || (tname && (*ptrHold)->GetTypeName() != tname))
{ {
if (error) if (error)
{ {
char buff[256]; 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); luaL_argerror(luastate, narg, buff);
} }
return NULL; return NULL;