Skip to content

Commit

Permalink
Fix build for gcc 10 and earlier, add indirection to break circular d…
Browse files Browse the repository at this point in the history
…ependency on LuaVal->LuaVal::v
  • Loading branch information
Rochet2 committed Jan 14, 2024
1 parent 91041a1 commit a6595d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
30 changes: 14 additions & 16 deletions LuaValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include "LuaValue.h"
#include <stdio.h> // snprintf

Expand All @@ -37,7 +35,7 @@ extern "C"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
}

LuaVal* LuaVal::GetLuaVal(lua_State* L, int index) {
if (LuaVal** ptr = static_cast<LuaVal**>(luaL_testudata(L, index, LUAVAL_MT_NAME)))
Expand Down Expand Up @@ -83,13 +81,13 @@ void LuaVal::Register(lua_State* L) {
int LuaVal::lua_get(lua_State* L) {
LuaVal* self = GetCheckLuaVal(L, 1);
int arguments = std::max(2, lua_gettop(L));
WrappedMap const* p = std::get_if<WrappedMap>(&self->v);
WrappedMap const* p = std::get_if<WrappedMap>(&*self->v);
if (!p)
luaL_argerror(L, 1, "trying to index a non-table LuaVal");
for (int i = 2; i <= arguments; ++i) {
auto& map = (*p);
auto klv = AsLuaVal(L, i);
if (std::holds_alternative<NIL>(klv.v))
if (std::holds_alternative<NIL>(*klv.v))
luaL_argerror(L, i, "trying to use nil as key");
auto it = map->find(klv);
if (it == map->end()) {
Expand All @@ -102,7 +100,7 @@ int LuaVal::lua_get(lua_State* L) {
auto& val = it->second;
return val.asObject(L);
}
p = std::get_if<WrappedMap>(&it->second.v);
p = std::get_if<WrappedMap>(&*it->second.v);
if (!p)
luaL_argerror(L, i, "trying to index a non-table LuaVal");
}
Expand All @@ -113,13 +111,13 @@ int LuaVal::lua_get(lua_State* L) {
int LuaVal::lua_set(lua_State* L) {
LuaVal* self = GetCheckLuaVal(L, 1);
int arguments = std::max(3, lua_gettop(L));
WrappedMap const* p = std::get_if<WrappedMap>(&self->v);
WrappedMap const* p = std::get_if<WrappedMap>(&*self->v);
if (!p)
luaL_argerror(L, 1, "trying to index a non-table LuaVal");
for (int i = 2; i <= arguments - 2; ++i) {
auto& map = (*p);
auto klv = AsLuaVal(L, i);
if (std::holds_alternative<NIL>(klv.v))
if (std::holds_alternative<NIL>(*klv.v))
luaL_argerror(L, i, "trying to use nil as key");
auto it = map->find(klv);
if (it == map->end()) {
Expand All @@ -128,15 +126,15 @@ int LuaVal::lua_set(lua_State* L) {
}
break;
}
p = std::get_if<WrappedMap>(&it->second.v);
p = std::get_if<WrappedMap>(&*it->second.v);
if (!p)
luaL_argerror(L, i, "trying to index a non-table LuaVal");
}
auto kk = AsLuaVal(L, arguments - 1);
auto vv = AsLuaVal(L, arguments);
if (std::holds_alternative<NIL>(kk.v))
if (std::holds_alternative<NIL>(*kk.v))
luaL_argerror(L, arguments - 1, "trying to use nil as key");
if (std::holds_alternative<NIL>(vv.v)) {
if (std::holds_alternative<NIL>(*vv.v)) {
(**p).erase(kk);
}
else {
Expand Down Expand Up @@ -193,12 +191,12 @@ int LuaVal::asObject(lua_State* L) const
else {
static_assert(always_false<T>::value, "non-exhaustive visitor!");
}
}, v);
}, *v);
}

int LuaVal::asLua(lua_State* L, unsigned int depth) const
{
WrappedMap const* p = std::get_if<WrappedMap>(&v);
WrappedMap const* p = std::get_if<WrappedMap>(&*v);
if (p)
{
lua_newtable(L);
Expand Down Expand Up @@ -267,7 +265,7 @@ LuaVal LuaVal::FromTable(lua_State* L, int index)
{
// Assumed we know index is a table already
LuaVal m({});
auto& t = std::get<WrappedMap>(m.v);
auto& t = std::get<WrappedMap>(*m.v);
int top = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, index) != 0) {
Expand All @@ -279,9 +277,9 @@ LuaVal LuaVal::FromTable(lua_State* L, int index)

size_t LuaValHash(LuaVal const& k)
{
const std::unique_ptr<std::string>* pval = std::get_if<std::unique_ptr<std::string>>(&k.v);
const std::unique_ptr<std::string>* pval = std::get_if<std::unique_ptr<std::string>>(&*k.v);
if (pval) {
return std::hash<std::string>{}(**pval);
}
return std::hash<decltype(LuaVal::v)>{}(k.v);
return std::hash<LuaVal::LuaValVariant>{}(*k.v);
}
44 changes: 22 additions & 22 deletions LuaValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ class LuaVal

bool operator<(LuaVal const& b) const
{
if (v.index() == b.v.index()) {
const std::unique_ptr<std::string>* pval = std::get_if<std::unique_ptr<std::string>>(&v);
if (v->index() == b.v->index()) {
const std::unique_ptr<std::string>* pval = std::get_if<std::unique_ptr<std::string>>(&*v);
if (pval) {
return **pval < **std::get_if<std::unique_ptr<std::string>>(&b.v);
return **pval < **std::get_if<std::unique_ptr<std::string>>(&*b.v);
}
}
return v < b.v;
return *v < *b.v;
}

bool operator==(LuaVal const& b) const
{
if (v.index() == b.v.index()) {
const std::unique_ptr<std::string>* pval = std::get_if<std::unique_ptr<std::string>>(&v);
if (v->index() == b.v->index()) {
const std::unique_ptr<std::string>* pval = std::get_if<std::unique_ptr<std::string>>(&*v);
if (pval) {
return **pval == **std::get_if<std::unique_ptr<std::string>>(&b.v);
return **pval == **std::get_if<std::unique_ptr<std::string>>(&*b.v);
}
}
return v == b.v;
return *v == *b.v;
}

std::string to_string() const
Expand All @@ -120,27 +120,27 @@ class LuaVal
return std::to_string(arg);
else
static_assert(always_false<T>::value, "non-exhaustive visitor!");
}, v);
}, *v);
}

LuaVal() {}
LuaVal(std::string const& s) : v(std::make_unique<std::string>(s)) {}
LuaVal(bool b) : v(b) {}
LuaVal(double d) : v(d) {}
LuaVal(MapType const& t) : v(std::make_shared<MapType>(t)) {}
LuaVal(std::initializer_list<MapType::value_type> const& l) : v(std::make_shared<MapType>(l)) {}
LuaVal() : v(std::make_unique<LuaValVariant>()) {}
LuaVal(std::string const& s) : v(std::make_unique<LuaValVariant>(std::make_unique<std::string>(s))) {}
LuaVal(bool b) : v(std::make_unique<LuaValVariant>(b)) {}
LuaVal(double d) : v(std::make_unique<LuaValVariant>(d)) {}
LuaVal(MapType const& t) : v(std::make_unique<LuaValVariant>(std::make_shared<MapType>(t))) {}
LuaVal(std::initializer_list<std::pair<const LuaVal, LuaVal> /* MapType::value_type */> const& l) : v(std::make_unique<LuaValVariant>(std::make_shared<MapType>(l))) {}

LuaVal(LuaVal&& b) : v(std::move(b.v)) {
}
LuaVal(const LuaVal& b) : v(std::visit([&](auto&& arg) -> LuaValVariant {
LuaVal(const LuaVal& b) : v(std::make_unique<LuaValVariant>(std::visit([&](auto&& arg) -> LuaValVariant {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::unique_ptr<std::string>>)
return std::make_unique<std::string>(*arg);
else if constexpr (std::is_same_v<T, WrappedMap>)
return std::make_shared<MapType>(*arg);
else
return arg;
}, b.v)) {
}, *b.v))) {
}
~LuaVal() {
}
Expand All @@ -149,31 +149,31 @@ class LuaVal
return *this;
}
LuaVal& operator=(const LuaVal& b) {
v = std::visit([&](auto&& arg) -> LuaValVariant {
v = std::make_unique<LuaValVariant>(std::visit([&](auto&& arg) -> LuaValVariant {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::unique_ptr<std::string>>)
return std::make_unique<std::string>(*arg);
else if constexpr (std::is_same_v<T, WrappedMap>)
return std::make_shared<MapType>(*arg);
else
return arg;
}, b.v);
}, *b.v));
return *this;
}
LuaVal clone() const {
return *this;
}
LuaVal reference() const {
LuaVal lv;
lv.v = std::visit([&](auto&& arg) -> LuaValVariant {
lv.v = std::make_unique<LuaValVariant>(std::visit([&](auto&& arg) -> LuaValVariant {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::unique_ptr<std::string>>)
return std::make_unique<std::string>(*arg);
else
return arg;
}, v);
}, *v));
return lv;
}

LuaValVariant v;
std::unique_ptr<LuaValVariant> v;
};

0 comments on commit a6595d2

Please sign in to comment.