luaxx.cc

Go to the documentation of this file.
00001 /* vim: set et sw=3 tw=0 fo=croqlaw cino=t0:
00002  *
00003  * Luaxx, the C++ Lua wrapper library.
00004  * Copyright (c) 2006-2008 Matthew Nicholson
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy 
00007  * of this software and associated documentation files (the "Software"), to 
00008  * deal in the Software without restriction, including without limitation the 
00009  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
00010  * sell copies of the Software, and to permit persons to whom the Software is 
00011  * furnished to do so, subject to the following conditions:
00012  * 
00013  * The above copyright notice and this permission notice shall be included in 
00014  * all copies or substantial portions of the Software.
00015  * 
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
00021  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
00022  * IN THE SOFTWARE. 
00023  */
00024 
00025 #include <luaxx/luaxx.hpp>
00026 
00031 namespace lua
00032 {
00034    state::state() : L(luaL_newstate()), managed(true) {
00035       if (L == NULL)
00036          throw bad_alloc("Error creating lua state");
00037    }
00038 
00046    state::state(lua_State* L) :
00047       L(L), managed(false) {
00048    }
00049 
00051    state::~state() {
00052       if (managed)
00053          lua_close(L);
00054    }
00055 
00059    state& state::push() {
00060       lua_pushnil(L);
00061       return *this;
00062    }
00063    
00067    state& state::push(nil) {
00068       lua_pushnil(L);
00069       return *this;
00070    }
00071 
00076    state& state::push(bool boolean) {
00077       lua_pushboolean(L, boolean);
00078       return *this;
00079    }
00080    
00086    state& state::push(const char* s, size_t length) {
00087       lua_pushlstring(L, s, length);
00088       return *this;
00089    }
00090 
00096    state& state::push(const char* s) {
00097       lua_pushstring(L, s);
00098       return *this;
00099    }
00100 
00105    state& state::push(const std::string& s) {
00106       lua_pushlstring(L, s.c_str(), s.size());
00107       return *this;
00108    }
00109    
00114    state& state::push(cfunction f) {
00115       lua_pushcfunction(L, f);
00116       return *this;
00117    }
00118    
00123    state& state::push(table) {
00124       lua_newtable(L);
00125       return *this;
00126    }
00127 
00132    state& state::push(void* p) {
00133       lua_pushlightuserdata(L, p);
00134       return *this;
00135    }
00136 
00149    template<>
00150    std::string state::as(std::string default_value, int index) {
00151       if (lua_isstring(L, index))
00152          return std::string(lua_tostring(L, index), lua_strlen(L, index));
00153       else
00154          return default_value;
00155    }
00156    
00168    state& state::check(int narg) {
00169       luaL_checkany(L, narg);
00170       return *this;
00171    }
00172    
00173 #if lua_Integer != int
00174 
00185    state& state::check(int& i, int narg) {
00186       i = luaL_checkint(L, narg);
00187       return *this;
00188    }
00189 #endif
00190 
00205    state& state::check(integer& i, int narg) {
00206       i = luaL_checkinteger(L, narg);
00207       return *this;
00208    }
00209 
00210 #if lua_Integer != long
00211 
00222    state& state::check(long& l, int narg) {
00223       l = luaL_checklong(L, narg);
00224       return *this;
00225    }
00226 #endif
00227 
00239    state& state::check(std::string& s, int narg) {
00240       const char* c;
00241       size_t l;
00242       c = luaL_checklstring(L, narg, &l);
00243 
00244       s.assign(c, l);
00245 
00246       return *this;
00247    }
00248 
00261    state& state::check(number& n, int narg) {
00262       n = luaL_checknumber(L, narg);
00263       return *this;
00264    }
00265 
00266 #if 0
00267 
00269    template<>
00270    void state::error(const std::string& message) {
00271       push(message);
00272       lua_error(L);
00273    }
00274 #endif
00275 
00285    state& state::pcall(int nargs, int nresults, int on_error) {
00286       throw_error(lua_pcall(L, nargs, nresults, on_error));
00287       return *this;
00288    }
00289    
00297    state& state::call(int nargs, int nresults) {
00298       lua_call(L, nargs, nresults);
00299       return *this;
00300    }
00301    
00311    state& state::checkstack(int size) {
00312       if (!lua_checkstack(L, size))
00313          throw lua::exception("Error growing the stack");
00314       return *this;
00315    }
00316 
00324    state& state::settop(int index) {
00325       lua_settop(L, index);
00326       return *this;
00327    }
00328 
00333    int state::gettop() {
00334       return lua_gettop(L);
00335    }
00336 
00341    int state::size() {
00342       return lua_gettop(L);
00343    }
00344    
00348    bool state::empty() {
00349       return !lua_gettop(L);
00350    }
00351 
00358    state& state::insert(int index) {
00359       lua_insert(L, index);
00360       return *this;
00361    }
00362    
00367    state& state::replace(int index) {
00368       lua_replace(L, index);
00369       return *this;
00370    }
00371 
00377    state& state::remove(int index) {
00378       lua_remove(L, index);
00379       return *this;
00380    }
00381 
00386    state& state::pop(int elements) {
00387       lua_pop(L, elements);
00388       return *this;
00389    }
00390 
00396    state& state::pushvalue(int index) {
00397       lua_pushvalue(L, index);
00398       return *this;
00399    }
00400    
00404    state& state::newtable() {
00405       lua_newtable(L);
00406       return *this;
00407    }
00408 
00422    bool state::newmetatable(const std::string& tname) {
00423       return luaL_newmetatable(L, tname.c_str());
00424    }
00425 
00430    void* state::newuserdata(size_t nbytes) {
00431       return lua_newuserdata(L, nbytes);
00432    }
00433 
00445    state& state::gettable(int index) {
00446       lua_gettable(L, index);
00447       return *this;
00448    }
00449    
00459    state& state::getfield(const std::string& k, int index) {
00460       lua_getfield(L, index, k.c_str());
00461       return *this;
00462    }
00463    
00474    state& state::settable(int index) {
00475       lua_settable(L, index);
00476       return *this;
00477    }
00478 
00489    state& state::setfield(const std::string& k, int index) {
00490       lua_setfield(L, index, k.c_str());
00491       return *this;
00492    }
00493 
00504    state& state::getmetatable(const std::string& tname) {
00505       luaL_getmetatable(L, tname.c_str());
00506       return *this;
00507    }
00508 
00520    bool state::getmetatable(int index) {
00521       return lua_getmetatable(L, index);
00522    }
00523 
00555    bool state::next(int index) {
00556       return lua_next(L, index);
00557    }
00558 
00567    state& state::getglobal(const std::string& name) {
00568       lua_getglobal(L, name.c_str());
00569       return *this;
00570    }
00571 
00583    state& state::setglobal(const std::string& name) {
00584       lua_setglobal(L, name.c_str());
00585       return *this;
00586    }
00587 
00592    state& state::loadfile(const std::string& filename) {
00593       throw_error(luaL_loadfile(L, filename.c_str()));
00594       return *this;
00595    }
00596 
00601    state& state::loadstring(const std::string& s) {
00602       throw_error(luaL_loadstring(L, s.c_str()));
00603       return *this;
00604    }
00605 
00610    size_t state::objlen(int index) {
00611       return lua_objlen(L, index);
00612    }
00613 
00624    template<>
00625    state& state::to(bool& boolean, int index) {
00626       if (lua_isboolean(L, index))
00627          boolean = lua_toboolean(L, index);
00628       else
00629          throw bad_conversion("Cannot convert non 'boolean' value to bool");
00630 
00631       return *this;
00632    }
00633          
00649    template<>
00650    state& state::to(std::string& string, int index) {
00651       if (lua_isstring(L, index))
00652          string.replace(0, std::string::npos, lua_tostring(L, index), lua_strlen(L, index));
00653       else
00654          throw bad_conversion("Cannot convert value to string");
00655 
00656       return *this;
00657    }
00658 
00666    template<>
00667    bool state::as(bool default_value, int index) {
00668       if (lua_isboolean(L, index))
00669          return lua_toboolean(L, index);
00670       else
00671          return default_value;
00672    }
00673    
00676    template<>
00677    bool state::as(int index) {
00678       if (lua_isboolean(L, index))
00679          return lua_toboolean(L, index);
00680       else
00681          throw bad_conversion("Cannot convert non 'boolean' value to bool");
00682    }
00683          
00689    template<>
00690    std::string state::as(int index) {
00691       if (lua_isstring(L, index))
00692          return std::string(lua_tostring(L, index), lua_strlen(L, index));
00693       else
00694          throw bad_conversion("Cannot convert value to string");
00695    }
00696          
00699    template<>
00700    bool state::is<nil>(int index) {
00701       return lua_isnil(L, index);
00702    }
00703    
00706    template<>
00707    bool state::is<bool>(int index) {
00708       return lua_isboolean(L, index);
00709    }
00710 
00713    template<>
00714    bool state::is<std::string>(int index) {
00715       return lua_isstring(L, index);
00716    }
00717    
00720    template<>
00721    bool state::is<table>(int index) {
00722       return lua_istable(L, index);
00723    }
00724    
00728    template<>
00729    bool state::is<cfunction>(int index) {
00730       return lua_iscfunction(L, index);
00731    }
00732    
00736    template<>
00737    bool state::is<function>(int index) {
00738       return lua_isfunction(L, index);
00739    }
00740 
00744    template<>
00745    bool state::is<userdata>(int index) {
00746       return lua_isuserdata(L, index);
00747    }
00748 
00752    template<>
00753    bool state::is<lightuserdata>(int index) {
00754       return lua_islightuserdata(L, index);
00755    }
00756 
00757 }
00758 

Generated on Wed Apr 30 09:34:45 2008 for Luaxx by  doxygen 1.5.5