00001 /******************************************************************************\ 00002 * LuaValue.hpp * 00003 * A class that somewhat mimics a Lua value. * 00004 * * 00005 * * 00006 * Copyright (C) 2005-2007 by Leandro Motta Barros. * 00007 * * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy * 00009 * of this software and associated documentation files (the "Software"), to * 00010 * deal in the Software without restriction, including without limitation the * 00011 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * 00012 * sell copies of the Software, and to permit persons to whom the Software is * 00013 * furnished to do so, subject to the following conditions: * 00014 * * 00015 * The above copyright notice and this permission notice shall be included in * 00016 * all copies or substantial portions of the Software. * 00017 * * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * 00023 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * 00024 * IN THE SOFTWARE. * 00025 \******************************************************************************/ 00026 00027 #ifndef _DILUCULUM_LUA_VALUE_HPP_ 00028 #define _DILUCULUM_LUA_VALUE_HPP_ 00029 00030 #include <lua.hpp> 00031 #include <map> 00032 #include <stdexcept> 00033 #include <string> 00034 #include <boost/variant.hpp> 00035 #include <Diluculum/LuaUserData.hpp> 00036 #include <Diluculum/Types.hpp> 00037 00038 00039 namespace Diluculum 00040 { 00041 // Some forward declarations. 00042 class LuaValue; 00043 00044 00054 class LuaValue 00055 { 00056 public: 00058 LuaValue() { }; 00059 00061 LuaValue (bool b) 00062 : value_(b) 00063 { } 00064 00066 LuaValue (float n) 00067 : value_(static_cast<lua_Number>(n)) 00068 { } 00069 00071 LuaValue (double n) 00072 : value_(static_cast<lua_Number>(n)) 00073 { } 00074 00076 LuaValue (long double n) 00077 : value_(static_cast<lua_Number>(n)) 00078 { } 00079 00081 LuaValue (short n) 00082 : value_(static_cast<lua_Number>(n)) 00083 { } 00084 00086 LuaValue (unsigned short n) 00087 : value_(static_cast<lua_Number>(n)) 00088 { } 00089 00091 LuaValue (int n) 00092 : value_(static_cast<lua_Number>(n)) 00093 { } 00094 00096 LuaValue (unsigned n) 00097 : value_(static_cast<lua_Number>(n)) 00098 { } 00099 00101 LuaValue (long n) 00102 : value_(static_cast<lua_Number>(n)) 00103 { } 00104 00106 LuaValue (unsigned long n) 00107 : value_(static_cast<lua_Number>(n)) 00108 { } 00109 00111 LuaValue (const std::string& s) 00112 : value_(s) 00113 { } 00114 00116 LuaValue (const char* s) 00117 : value_(std::string(s)) 00118 { } 00119 00121 LuaValue (const LuaValueMap& t) 00122 : value_(t) 00123 { } 00124 00126 LuaValue (lua_CFunction f) 00127 : value_(f) 00128 { } 00129 00131 LuaValue (const LuaUserData& ud) 00132 : value_(ud) 00133 { } 00134 00140 LuaValue (const LuaValueList& v); 00141 00146 const LuaValueList& operator= (const LuaValueList& rhs); 00147 00151 int type() const; 00152 00159 std::string typeName() const; 00160 00165 lua_Number asNumber() const; 00166 00171 const std::string& asString() const; 00172 00177 bool asBoolean() const; 00178 00187 LuaValueMap asTable() const; 00188 00193 lua_CFunction asFunction() const; 00194 00199 const LuaUserData& asUserData() const; 00200 00208 LuaUserData& asUserData(); 00209 00235 bool operator< (const LuaValue& rhs) const; 00236 00241 bool operator> (const LuaValue& rhs) const; 00242 00247 bool operator== (const LuaValue& rhs) const; 00248 00253 bool operator!= (const LuaValue& rhs) const 00254 { return !(*this == rhs); } 00255 00262 LuaValue& operator[] (const LuaValue& key); 00263 00269 const LuaValue& operator[] (const LuaValue& key) const; 00270 00271 private: 00273 class NilType { }; 00274 00276 boost::variant <NilType, lua_Number, std::string, bool, LuaValueMap, 00277 lua_CFunction, LuaUserData> value_; 00278 }; 00279 00280 00281 00283 const LuaValue Nil; 00284 00286 const LuaValueMap EmptyLuaValueMap; 00287 00289 const LuaValue EmptyTable (EmptyLuaValueMap); 00290 00291 } // namespace Diluculum 00292 00293 00294 #endif // _DILUCULUM_LUA_VALUE_HPP_
1.4.6