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,
@@ -301,12 +334,10 @@ namespace std
struct hash < EventKey<T> >
{
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 h1;
return hash_helper::hash(k.event_id);
}
};
@@ -314,14 +345,10 @@ namespace std
struct hash < EntryKey<T> >
{
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));
result_type const h2(std::hash<uint32>()(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<T> >
{
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));
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.
return hash_helper::hash(k.event_id, k.instance_id, k.guid);
}
};
}

View File

@@ -66,10 +66,8 @@ public:
class ElunaObject
{
public:
ElunaObject(void* obj, bool manageMemory) : _isvalid(false), _invalidate(!manageMemory), object(obj)
{
SetValid(true);
}
template<typename T>
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<typename T>
@@ -288,7 +289,7 @@ public:
lua_pushnil(L);
return 1;
}
*ptrHold = new ElunaObject(obj_voidptr, manageMemory);
*ptrHold = new ElunaObject(const_cast<T*>(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<typename T>
ElunaObject::ElunaObject(T * obj, bool manageMemory) : _isvalid(false), _invalidate(!manageMemory), object(obj), type_name(ElunaTemplate<T>::tname)
{
SetValid(true);
}
#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)
{
return luaL_checknumber(luastate, narg);
return static_cast<float>(luaL_checknumber(luastate, 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)
{
bool valid = false;
ElunaObject** ptrHold = NULL;
if (!tname)
if (lua_islightuserdata(luastate, narg))
{
valid = true;
ptrHold = static_cast<ElunaObject**>(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<ElunaObject**>(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<ElunaObject**>(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;