1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#ifndef LUA_HASH_STRING_H
#define LUA_HASH_STRING_H
// LuaHashString.h: lua utility routines
//
//////////////////////////////////////////////////////////////////////
#include <string>
using std::string;
#include "LuaInclude.h"
struct LuaHashString {
public:
LuaHashString(const string& s)
: str(s), hash(lua_calchash(s.c_str(), s.size())) {}
LuaHashString(const LuaHashString& hs)
: str(hs.str), hash(hs.hash) {}
LuaHashString& operator=(const LuaHashString& hs) {
str = hs.str;
hash = hs.hash;
return (*this);
}
inline unsigned int GetHash() const { return hash; }
inline const string& GetString() const { return str; }
void SetString(const string& s) {
str = s;
hash = lua_calchash(s.c_str(), s.size());
}
public:
inline void Push(lua_State* L) const {
lua_pushhstring(L, hash, str.c_str(), str.size());
}
inline void GetGlobal(lua_State* L) const {
Push(L);
lua_gettable(L, LUA_GLOBALSINDEX);
}
inline bool GetGlobalFunc(lua_State* L) const {
GetGlobal(L);
if (lua_isfunction(L, -1)) {
return true;
}
lua_pop(L, 1);
return false;
}
inline void GetRegistry(lua_State* L) const {
Push(L);
lua_gettable(L, LUA_REGISTRYINDEX);
}
inline bool GetRegistryFunc(lua_State* L) const {
GetRegistry(L);
if (lua_isfunction(L, -1)) {
return true;
}
lua_pop(L, 1);
return false;
}
inline void PushBool(lua_State* L, bool value) const {
Push(L);
lua_pushboolean(L, value);
lua_rawset(L, -3);
}
inline void PushNumber(lua_State* L, lua_Number value) const {
Push(L);
lua_pushnumber(L, value);
lua_rawset(L, -3);
}
inline void PushString(lua_State* L, const char* value) const {
Push(L);
lua_pushstring(L, value);
lua_rawset(L, -3);
}
inline void PushString(lua_State* L, const string& value) const {
Push(L);
lua_pushlstring(L, value.c_str(), value.size());
lua_rawset(L, -3);
}
inline void PushHashString(lua_State* L, const LuaHashString& hs) const {
Push(L);
hs.Push(L);
lua_rawset(L, -3);
}
inline void PushCFunc(lua_State* L, int (*func)(lua_State*)) const {
Push(L);
lua_pushcfunction(L, func);
lua_rawset(L, -3);
}
private:
string str;
unsigned int hash;
};
// NOTE: scoped to avoid name conflicts
#define HSTR_PUSH(L, name) \
{ static const LuaHashString hsPriv(name); hsPriv.Push(L); }
#define HSTR_PUSH_BOOL(L, name, val) \
{ static const LuaHashString hsPriv(name); hsPriv.PushBool(L, val); }
#define HSTR_PUSH_NUMBER(L, name, val) \
{ static const LuaHashString hsPriv(name); hsPriv.PushNumber(L, val); }
#define HSTR_PUSH_STRING(L, name, val) \
{ static const LuaHashString hsPriv(name); hsPriv.PushString(L, val); }
#define HSTR_PUSH_HSTR(L, name, val) \
{ static const LuaHashString hsPriv(name); hsPriv.PushHashString(L, val); }
#define HSTR_PUSH_CFUNC(L, name, val) \
{ static const LuaHashString hsPriv(name); hsPriv.PushCFunc(L, val); }
#endif // LUA_HASH_STRING_H
|