// - LuaValue::asBoolean ---------------------------------------------------- bool LuaValue::asBoolean() const { if (dataType_ == LUA_TBOOLEAN) return *reinterpret_cast<const bool*>(&data_); else throw TypeMismatchError ("boolean", typeName()); }
LuaUserData& LuaValue::asUserData() { if (dataType_ == LUA_TUSERDATA) return *reinterpret_cast<LuaUserData*>(&data_); else throw TypeMismatchError ("userdata", typeName()); }
// - LuaValue::asTable ------------------------------------------------------ LuaValueMap LuaValue::asTable() const { if (dataType_ == LUA_TTABLE) return *reinterpret_cast<const LuaValueMap*>(&data_); else throw TypeMismatchError ("table", typeName()); }
// - LuaValue::asString ----------------------------------------------------- const std::string& LuaValue::asString() const { if (dataType_ == LUA_TSTRING) return *reinterpret_cast<const std::string*>(&data_); else throw TypeMismatchError ("string", typeName()); }
// - LuaValue::asNumber() --------------------------------------------------- lua_Number LuaValue::asNumber() const { if (dataType_ == LUA_TNUMBER) return *reinterpret_cast<const lua_Number*>(&data_); else throw TypeMismatchError ("number", typeName()); }
// - LuaValue::operator[] --------------------------------------------------- LuaValue& LuaValue::operator[] (const LuaValue& key) { if (type() != LUA_TTABLE) throw TypeMismatchError ("table", typeName()); LuaValueMap& table = *reinterpret_cast<LuaValueMap*>(data_); return table[key]; }
// - LuaValue::asFunction --------------------------------------------------- const LuaFunction& LuaValue::asFunction() const { if (dataType_ == LUA_TFUNCTION) { return *reinterpret_cast<const LuaFunction*>(&data_); } else throw TypeMismatchError ("function", typeName()); }
const LuaValue& LuaValue::operator[] (const LuaValue& key) const { if (type() != LUA_TTABLE) throw TypeMismatchError ("table", typeName()); const LuaValueMap& table = *reinterpret_cast<const LuaValueMap*>(data_); LuaValueMap::const_iterator it = table.find(key); if (it == table.end()) return Nil; return it->second; }
// - LuaValue::asInteger() -------------------------------------------------- lua_Integer LuaValue::asInteger() const { if (dataType_ == LUA_TNUMBER) { lua_Number num = (*reinterpret_cast<const lua_Number*>(&data_)); lua_Integer res; lua_number2integer (res, num); return res; } else { throw TypeMismatchError ("number", typeName()); } }
// - LuaVariable::pushTheReferencedValue ------------------------------------ void LuaVariable::pushTheReferencedValue() const { assert (keys_.size() > 0 && "There should be at least one key here."); lua_rawgeti (state_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); typedef std::vector<LuaValue>::const_iterator iter_t; for (iter_t p = keys_.begin(); p != keys_.end(); ++p) { PushLuaValue (state_, *p); lua_gettable (state_, -2); if (keys_.size() > 1 && p != keys_.end()-1 && !lua_istable(state_, -1)) throw TypeMismatchError ("table", p->typeName()); lua_remove (state_, -2); } }
// - LuaVariable::pushLastTable --------------------------------------------- void LuaVariable::pushLastTable() { // Push the globals table onto the stack lua_pushglobaltable (state_); // Reach the "final" table (and leave it at the stack top) typedef KeyList::const_iterator iter_t; assert (keys_.size() > 0 && "At least one key should be present here."); iter_t end = keys_.end(); --end; for (iter_t p = keys_.begin(); p != end; ++p) { PushLuaValue (state_, *p); lua_gettable (state_, -2); if (!lua_istable (state_, -1)) throw TypeMismatchError ("table", luaL_typename (state_, -1)); lua_remove (state_, -2); } }