Mercurial > code
view C++/LuaeVariant.cpp @ 231:2f1d820e6e33
Merge with OptionParser
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 22 Jun 2014 16:19:31 +0200 |
parents | e01ee0c72c43 |
children |
line wrap: on
line source
/* * LuaeVariant.cpp -- Lua variant helper * * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "LuaeVariant.h" LuaeVariant LuaeVariant::copy(lua_State *L, int index) { LuaeVariant v; v.type = lua_type(L, index); switch (v.type) { case LUA_TBOOLEAN: v.boolean = lua_toboolean(L, index); break; case LUA_TNUMBER: v.number = lua_tonumber(L, index); break; case LUA_TSTRING: v.str = lua_tostring(L, index); break; case LUA_TTABLE: { if (index < 0) -- index; lua_pushnil(L); while (lua_next(L, index)) { v.table.push_back(std::make_pair(copy(L, -2), copy(L, -1))); lua_pop(L, 1); } break; } default: v.type = LUA_TNIL; break; } return v; } void LuaeVariant::push(lua_State *L, const LuaeVariant &value) { switch (value.type) { case LUA_TBOOLEAN: lua_pushboolean(L, value.boolean); break; case LUA_TSTRING: lua_pushlstring(L, value.str.c_str(), value.str.size()); break; case LUA_TNUMBER: lua_pushnumber(L, value.number); break; case LUA_TTABLE: { lua_createtable(L, 0, 0); for (auto p : value.table) { push(L, p.first); push(L, p.second); lua_settable(L, -3); } break; } default: lua_pushnil(L); break; } } LuaeVariant::LuaeVariant() : type(LUA_TNIL) { }