LuaValue::LuaValue (const LuaValue& other) : dataType_ (other.dataType_) { switch (dataType_) { case LUA_TSTRING: new(data_) std::string(other.asString()); break; case LUA_TTABLE: new(data_) LuaValueMap (other.asTable()); break; case LUA_TUSERDATA: new(data_) LuaUserData (other.asUserData()); break; case LUA_TFUNCTION: new(data_) LuaFunction (other.asFunction()); break; default: // no constructor needed. memcpy (data_, other.data_, sizeof(PossibleTypes)); break; } }
// - LuaValue::operator== --------------------------------------------------- bool LuaValue::operator== (const LuaValue& rhs) const { std::string lhsTypeName = typeName(); std::string rhsTypeName = rhs.typeName(); if (typeName() != rhs.typeName()) return false; else switch (type()) { case LUA_TNIL: return true; case LUA_TBOOLEAN: return asBoolean() == rhs.asBoolean(); case LUA_TNUMBER: return asNumber() == rhs.asNumber(); case LUA_TSTRING: return asString() == rhs.asString(); case LUA_TTABLE: return asTable() == rhs.asTable(); case LUA_TFUNCTION: return asFunction() == rhs.asFunction(); case LUA_TUSERDATA: return asUserData() == rhs.asUserData(); default: { assert( false && "Invalid type found in a call to 'LuaValue::operator==()'."); return 0; // make compilers happy } } }
// - LuaValue::operator> ---------------------------------------------------- bool LuaValue::operator> (const LuaValue& rhs) const { std::string lhsTypeName = typeName(); std::string rhsTypeName = rhs.typeName(); if (lhsTypeName > rhsTypeName) return true; else if (lhsTypeName < rhsTypeName) return false; else // lhsTypeName == rhsTypeName { if (lhsTypeName == "nil") return false; else if (lhsTypeName == "boolean") return asBoolean() > rhs.asBoolean(); else if (lhsTypeName == "number") return asNumber() > rhs.asNumber(); else if (lhsTypeName == "string") return asString() > rhs.asString(); else if (lhsTypeName == "function") return asFunction() > rhs.asFunction(); else if (lhsTypeName == "userdata") return asUserData() > rhs.asUserData(); else if (lhsTypeName == "table") { const LuaValueMap lhsMap = asTable(); const LuaValueMap rhsMap = rhs.asTable(); if (lhsMap.size() > rhsMap.size()) return true; else if (lhsMap.size() < rhsMap.size()) return false; else // lhsMap.size() == rhsMap.size() { typedef LuaValueMap::const_iterator iter_t; iter_t pLHS = lhsMap.begin(); iter_t pRHS = rhsMap.begin(); const iter_t end = lhsMap.end(); while (pLHS != end) { // check the key first if (pLHS->first > pRHS->first) return true; else if (pLHS->first < pRHS->first) return false; // then check the value if (pLHS->second > pRHS->second) return true; else if (pLHS->second < pRHS->second) return false; // make the iterators iterate ++pRHS; ++pLHS; } return false; } } else { assert (false && "Unsupported type found at a call " "to 'LuaValue::operator>()'"); return false; // make the compiler happy. } } }