-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLuaTableIterator.cpp
More file actions
199 lines (162 loc) · 4.93 KB
/
Copy pathLuaTableIterator.cpp
File metadata and controls
199 lines (162 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
///////////////////////////////////////////////////////////////////////////////
// This source file is part of the LuaPlus source distribution and is Copyright
// 2001-2010 by Joshua C. Jensen (jjensen@workspacewhiz.com).
//
// The latest version may be obtained from http://luaplus.org/.
//
// The code presented in this file may be used in any environment it is
// acceptable to use Lua.
///////////////////////////////////////////////////////////////////////////////
#ifndef BUILDING_LUAPLUS
#define BUILDING_LUAPLUS
#endif
#include "LuaLink.h"
LUA_EXTERN_C_BEGIN
#include "src/lobject.h"
LUA_EXTERN_C_END
#include "LuaPlus.h"
#include "LuaCall.h"
#include <string.h>
#ifdef WIN32
#if defined(WIN32) && !defined(_XBOX) && !defined(_XBOX_VER) && !defined(_WIN32_WCE)
#include <windows.h>
#elif defined(_XBOX) || defined(_XBOX_VER)
#include <xtl.h>
#endif // WIN32
#undef GetObject
#endif // WIN32
#if defined(__GNUC__)
#include <new>
#else
#include <new.h>
#endif
#include <stdlib.h>
#include <wchar.h>
#include <assert.h>
#if LUAPLUS_EXTENSIONS
//-----------------------------------------------------------------------------
LUA_EXTERN_C_BEGIN
#include "src/ltable.h"
#include "src/lstate.h"
NAMESPACE_LUA_BEGIN
LUA_EXTERN_C int luaH_findindex (lua_State *L, Table *t, StkId key);
NAMESPACE_LUA_END
LUA_EXTERN_C_END
namespace LuaPlus {
LUAPLUS_API bool LuaPlusH_next(LuaState* state, LuaObject* table, LuaObject* key, LuaObject* value)
{
Table* t = hvalue(table->GetTObject());
int i = luaH_findindex(*state, t, key->GetTObject()); /* find original element */
for (i++; i < t->sizearray; i++) { /* try first array part */
if (!ttisnil(&t->array[i])) { /* a non-nil value? */
key->AssignInteger(state, i + 1);
value->AssignTObject(state, &t->array[i]);
return true;
}
}
for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */
if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */
key->AssignTObject(state, key2tval(gnode(t, i)));
value->AssignTObject(state, gval(gnode(t, i)));
return true;
}
}
return false; /* no more elements */
}
/**
\param tableObj The table to iterate the contents of.
\param doReset If true, the Reset() function is called at constructor
initialization time, allowing the iterator to be used immediately.
If false, then Reset() must be called before iterating.
**/
LuaTableIterator::LuaTableIterator( const LuaObject& tableObj, bool doReset ) :
m_keyObj(tableObj.GetState()),
m_valueObj(tableObj.GetState()),
m_tableObj(tableObj),
m_isDone(false) {
luaplus_assert(tableObj.IsTable());
// If the user requested it, perform the automatic reset.
if ( doReset )
Reset();
}
/**
The destructor.
**/
LuaTableIterator::~LuaTableIterator() {
}
/**
Start iteration at the beginning of the table.
**/
void LuaTableIterator::Reset() {
// Start afresh...
LuaState* state = m_tableObj.GetState();
// Start at the beginning.
m_keyObj.AssignNil(state);
// Start the iteration. If the return value is 0, then the iterator
// will be invalid.
m_isDone = !LuaPlusH_next(state, &m_tableObj, &m_keyObj, &m_valueObj);
}
/**
Invalidates the iterator. Call this function if you early abort from
an iteration loop (such as before it hits the end).
**/
void LuaTableIterator::Invalidate() {
// This is a local helper variable so we don't waste space in the class
// definition.
LuaState* state = m_tableObj.GetState();
m_keyObj.AssignNil(state);
m_valueObj.AssignNil(state);
}
/**
Go to the next entry in the table.
\return Returns true if the iteration is done.
**/
bool LuaTableIterator::Next() {
// This function is only active if Reset() has been called head.
luaplus_assert( IsValid() );
// This is a local helper variable so we don't waste space in the class
// definition.
LuaState* state = m_tableObj.GetState();
// Do the Lua table iteration.
m_isDone = !LuaPlusH_next(state, &m_tableObj, &m_keyObj, &m_valueObj);
return !m_isDone;
}
/**
\return Returns true if the iterator is valid (there is a current element).
**/
bool LuaTableIterator::IsValid() const {
return !m_isDone;
}
/**
We can easily allow a prefix operator++. Postfix would be a stack
management nightmare.
**/
LuaTableIterator& LuaTableIterator::operator++() {
Next();
return *this;
}
/**
\return Returns true if the iterator is valid (there is a current element).
**/
LuaTableIterator::operator bool() const {
// If the iterator is valid, then we're good.
return IsValid();
}
/**
\return Returns a LuaObject describing the current key.
**/
LuaObject& LuaTableIterator::GetKey() {
// This function is only active if Reset() has been called head.
luaplus_assert( IsValid() );
return m_keyObj;
}
/**
\return Returns a LuaObject describing the current value.
**/
LuaObject& LuaTableIterator::GetValue() {
// This function is only active if Reset() has been called head.
luaplus_assert( IsValid() );
return m_valueObj;
}
} // namespace LuaPlus
#endif // LUAPLUS_EXTENSIONS