· 6 years ago · Mar 25, 2020, 07:22 AM
1// === USER FACING API =====
2template<typename K, typename V>
3inline void ThreadView::setTable(int index, const K& key, const V& value) {
4 internal::ThreadViewTableHelper<K>::set(this, index, key, value);
5}
6
7// == WHAT ITS ACTUALLY DOING
8namespace internal {
9
10 template<typename K>
11 struct ThreadViewTableHelperSetBase {
12 template<typename V>
13 static void set(ThreadView* pView, int index, const K& key, const V& value) {
14 const int absIndex = pView->toAbsStackIndex(index);
15 pView->push(key);
16 pView->push(value);
17 lua_settable(pView->getState(), absIndex);
18 }
19 };
20
21 template<typename K>
22 struct ThreadViewTableHelperSetRawBase {
23 template<typename V>
24 static void setRaw(ThreadView* pView, int index, const K& key, const V& value) {
25 const int absIndex = pView->toAbsStackIndex(index);
26 pView->push(key);
27 pView->push(value);
28 lua_rawset(pView->getState(), absIndex);
29 }
30 };
31
32 template<typename K>
33 struct ThreadViewTableHelperGetBase {
34 template<typename V>
35 static int get(ThreadView* pView, int index, const K& key) {
36 const int absIndex = pView->toAbsStackIndex(index);
37 pView->push(key);
38 return lua_gettable(pView->getState(), absIndex);
39 }
40 };
41
42 template<typename K>
43 struct ThreadViewTableHelperGetRawBase {
44 template<typename V>
45 static int getRaw(ThreadView* pView, int index, const K& key) {
46 const int absIndex = pView->toAbsStackIndex(index);
47 pView->push(key);
48 return lua_rawget(pView->getState(), absIndex);
49 }
50 };
51
52 template<typename K>
53 struct ThreadViewTableHelper:
54 public ThreadViewTableHelperGetBase<K>,
55 public ThreadViewTableHelperGetRawBase<K>,
56 public ThreadViewTableHelperSetBase<K>,
57 public ThreadViewTableHelperSetRawBase<K> {};
58
59 template<>
60 struct ThreadViewTableHelper<int> {
61
62 static int get(ThreadView* pView, int index, int key) {
63 return lua_geti(pView->getState(), index, key);
64 }
65
66 static int getRaw(ThreadView* pView, int index, int key) {
67 return lua_rawgeti(pView->getState(), index, key);
68 }
69
70 template<typename V>
71 static void set(ThreadView* pView, int index, int key, const V& value) {
72 const int absIndex = pView->toAbsStackIndex(index);
73 pView->push(value);
74 lua_seti(pView->getState(), absIndex, key);
75 }
76
77 template<typename V>
78 static void setRaw(ThreadView* pView, int index, int key, const V& value) {
79 const int absIndex = pView->toAbsStackIndex(index);
80 pView->push(value);
81 lua_rawseti(pView->getState(), absIndex, key);
82 }
83 };
84
85 template<>
86 struct ThreadViewTableHelper<const char*>:
87 public ThreadViewTableHelperGetRawBase<const char*>,
88 public ThreadViewTableHelperSetRawBase<const char*>
89 {
90 static int get(ThreadView* pView, int index, const char* pKey) {
91 return lua_getfield(pView->getState(), index, pKey);
92 }
93
94 template<typename V>
95 static void set(ThreadView* pView, int index, const char* pKey, const V& value) {
96 const int absIndex = pView->toAbsStackIndex(index);
97 pView->push(value);
98 lua_setfield(pView->getState(), absIndex, pKey);
99 }
100 };
101
102 template<>
103 struct ThreadViewTableHelper<const void*>:
104 public ThreadViewTableHelperGetBase<const void*>,
105 public ThreadViewTableHelperSetBase<const void*>
106 {
107 static int getRaw(ThreadView* pView, int index, const void* pKey) {
108 return lua_rawgetp(pView->getState(), index, pKey);
109 }
110
111 template<typename V>
112 static void setRaw(ThreadView* pView, int index, const void* pKey, const V& value) {
113 const int absIndex = pView->toAbsStackIndex(index);
114 pView->push(value);
115 lua_setrawsetp(pView->getState(), absIndex, pKey);
116 }
117 };
118}