Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Rochet2 authored Jan 9, 2018
1 parent 358e5c5 commit a9924b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ int main()

std::forward_list<std::deque<std::string>> vec = { { "a", "b" },{ "a", "b" } };
std::unordered_map<std::string, std::string> m;
std::array<std::string, 2> ar = {"d", "v"};
m["test"] = "asd";
LuaVal t441 = vec;
std::cout << t441.dumps() << std::endl;
Expand Down
7 changes: 6 additions & 1 deletion smallfolk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ std::string LuaVal::tostring() const
}

size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const
{
return LuaValHash(v);
}

size_t LuaValHash(LuaVal const & v)
{
switch (v.tag)
{
Expand All @@ -73,7 +78,7 @@ size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const
case TNUMBER:
return std::hash<double>()(v.d);
case TTABLE:
return std::hash<TblPtr>()(v.tbl_ptr);
return std::hash<LuaVal::TblPtr>()(v.tbl_ptr);
}
return std::hash<std::string>()(v.tostring());
}
Expand Down
22 changes: 18 additions & 4 deletions smallfolk.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ enum LuaTypeTag
TBOOL,
};

class LuaVal;
size_t LuaValHash(LuaVal const & v);

namespace std {
template <>
struct hash<LuaVal> {
public:
size_t operator()(LuaVal const & v) const
{
return LuaValHash(v);
};
};
}

class LuaVal
{
public:
Expand All @@ -52,7 +66,7 @@ class LuaVal
size_t operator()(LuaVal const & v) const;
};

typedef std::unordered_map<LuaVal, LuaVal, LuaValHasher> LuaTable;
typedef std::unordered_map<LuaVal, LuaVal> LuaTable;
typedef std::unique_ptr<LuaTable> TblPtr; // circular reference memleak if insert self to self

LuaVal(const LuaTypeTag tag) : tag(tag), tbl_ptr(tag == TTABLE ? new LuaTable() : nullptr), d(0), b(false) {}
Expand All @@ -63,7 +77,6 @@ class LuaVal
LuaVal(const std::string & s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false) {}
LuaVal(const char * s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false) {}
LuaVal(const bool b) : tag(TBOOL), tbl_ptr(nullptr), d(0), b(b) {}
LuaVal(LuaTable const & luatable) : tag(TTABLE), tbl_ptr(new LuaTable(luatable)), d(0), b(false) {}
LuaVal(LuaVal const & val) : tag(val.tag), tbl_ptr(val.tag == TTABLE ? val.tbl_ptr ? new LuaTable(*val.tbl_ptr) : new LuaTable() : nullptr), s(val.s), d(val.d), b(val.b) {}
LuaVal(LuaVal && val) noexcept : tag(std::move(val.tag)), tbl_ptr(std::move(val.tbl_ptr)), s(std::move(val.s)), d(std::move(val.d)), b(std::move(val.b)) {}
LuaVal(std::initializer_list<LuaVal> const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
Expand Down Expand Up @@ -122,9 +135,8 @@ class LuaVal
{
InitializeMap(l);
}
LuaVal(std::unordered_map<LuaVal, LuaVal> const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
LuaVal(std::unordered_map<LuaVal, LuaVal> const & l) : tag(TTABLE), tbl_ptr(new LuaTable(l)), d(0), b(false)
{
InitializeMap(l);
}
template<typename K, typename V> LuaVal(std::unordered_map<K, V> const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
{
Expand Down Expand Up @@ -271,6 +283,8 @@ class LuaVal
tbl[std::move(k)] = std::move(v);
}
}

friend size_t LuaValHash(LuaVal const & v);

LuaTypeTag tag;
TblPtr tbl_ptr;
Expand Down

0 comments on commit a9924b7

Please sign in to comment.