Skip to content

Commit a9924b7

Browse files
authored
Fix build
1 parent 358e5c5 commit a9924b7

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ int main()
244244

245245
std::forward_list<std::deque<std::string>> vec = { { "a", "b" },{ "a", "b" } };
246246
std::unordered_map<std::string, std::string> m;
247-
std::array<std::string, 2> ar = {"d", "v"};
248247
m["test"] = "asd";
249248
LuaVal t441 = vec;
250249
std::cout << t441.dumps() << std::endl;

smallfolk.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ std::string LuaVal::tostring() const
6161
}
6262

6363
size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const
64+
{
65+
return LuaValHash(v);
66+
}
67+
68+
size_t LuaValHash(LuaVal const & v)
6469
{
6570
switch (v.tag)
6671
{
@@ -73,7 +78,7 @@ size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const
7378
case TNUMBER:
7479
return std::hash<double>()(v.d);
7580
case TTABLE:
76-
return std::hash<TblPtr>()(v.tbl_ptr);
81+
return std::hash<LuaVal::TblPtr>()(v.tbl_ptr);
7782
}
7883
return std::hash<std::string>()(v.tostring());
7984
}

smallfolk.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ enum LuaTypeTag
3535
TBOOL,
3636
};
3737

38+
class LuaVal;
39+
size_t LuaValHash(LuaVal const & v);
40+
41+
namespace std {
42+
template <>
43+
struct hash<LuaVal> {
44+
public:
45+
size_t operator()(LuaVal const & v) const
46+
{
47+
return LuaValHash(v);
48+
};
49+
};
50+
}
51+
3852
class LuaVal
3953
{
4054
public:
@@ -52,7 +66,7 @@ class LuaVal
5266
size_t operator()(LuaVal const & v) const;
5367
};
5468

55-
typedef std::unordered_map<LuaVal, LuaVal, LuaValHasher> LuaTable;
69+
typedef std::unordered_map<LuaVal, LuaVal> LuaTable;
5670
typedef std::unique_ptr<LuaTable> TblPtr; // circular reference memleak if insert self to self
5771

5872
LuaVal(const LuaTypeTag tag) : tag(tag), tbl_ptr(tag == TTABLE ? new LuaTable() : nullptr), d(0), b(false) {}
@@ -63,7 +77,6 @@ class LuaVal
6377
LuaVal(const std::string & s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false) {}
6478
LuaVal(const char * s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false) {}
6579
LuaVal(const bool b) : tag(TBOOL), tbl_ptr(nullptr), d(0), b(b) {}
66-
LuaVal(LuaTable const & luatable) : tag(TTABLE), tbl_ptr(new LuaTable(luatable)), d(0), b(false) {}
6780
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) {}
6881
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)) {}
6982
LuaVal(std::initializer_list<LuaVal> const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
@@ -122,9 +135,8 @@ class LuaVal
122135
{
123136
InitializeMap(l);
124137
}
125-
LuaVal(std::unordered_map<LuaVal, LuaVal> const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
138+
LuaVal(std::unordered_map<LuaVal, LuaVal> const & l) : tag(TTABLE), tbl_ptr(new LuaTable(l)), d(0), b(false)
126139
{
127-
InitializeMap(l);
128140
}
129141
template<typename K, typename V> LuaVal(std::unordered_map<K, V> const & l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
130142
{
@@ -271,6 +283,8 @@ class LuaVal
271283
tbl[std::move(k)] = std::move(v);
272284
}
273285
}
286+
287+
friend size_t LuaValHash(LuaVal const & v);
274288

275289
LuaTypeTag tag;
276290
TblPtr tbl_ptr;

0 commit comments

Comments
 (0)