File: LuaTable.cpp

package info (click to toggle)
arkrpg 0.1.4b-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,104 kB
  • ctags: 5,445
  • sloc: cpp: 28,145; sh: 9,006; ansic: 3,259; makefile: 344
file content (210 lines) | stat: -rw-r--r-- 6,859 bytes parent folder | download | duplicates (3)
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
199
200
201
202
203
204
205
206
207
208
209
210
/* $$
**
** Ark - Libraries, Tools & Programs for MMORPG developpements.
** Copyright (C) 1999-2002 The Contributors of the Ark Project
** Please see the file "AUTHORS" for a list of contributors
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <Modules/Lua/LuaTable.h>
#include <iostream>

namespace Ark
{

/******************************************************************************
 * Construct a LuaTable, the table being at the top of the stack
 ******************************************************************************/
LuaTable::LuaTable(lua_State* L, int index)
  : m_L(L), m_Index(index)
{
  if(m_Index == -1)
  {
    m_Index = lua_gettop(m_L);
  }
  else if(m_Index < -1)
  {
    std::cerr
      << "Warning: A LuaTable has a relative stack index, "
      << "which may cause some bugs..."
      << std::endl;
  }
}

/******************************************************************************
 * Destructor
 ******************************************************************************/
LuaTable::~LuaTable()
{
  lua_pop(m_L, m_Index);
}

/******************************************************************************
 * Return the table's index on the stack
 ******************************************************************************/
int LuaTable::index() const
{
  return m_Index;
}

/******************************************************************************
 * The following functions get a value of the table on the stack, given
 * the key of the value in the table.
 *
 * TODO: add the CFunction type.
 ******************************************************************************/

/******************************************************************************
 * Read a number in the table.
 * 
 * \param key The key of the element to get in the table.
 * \return The value at the given key or 0 if the value is not a number
 * or is not a string convertible to a number.
 ******************************************************************************/
double LuaTable::getNumber(double key)
{
  lua_pushnumber(m_L, key);
  lua_gettable(m_L, m_Index);
  
  int result = static_cast<int>(lua_tonumber(m_L, -1));
  lua_pop(m_L, 1);

  return result;
}

/******************************************************************************
 * Read a string in the table.
 *
 * \param key The key of the element to get in the table.
 * \return The value (a string or a number) at the given key or 0 if the
 * value isn't a string or a number.
 *
 * \bug In fact, it is not a bug but... Well, here is it ;-) Lua strings
 * may have embeddend \0 insided them, whereas the real string continues.
 * This function doesn't handle the problem. There should be a subclass
 * LuaStringTable that handles a table of strings, or a LuaString class
 * that return a correct string given the index of the string in the
 * stack. Feel free to write these functions if they don't exist and you
 * need them... ;-)
 ******************************************************************************/
const char* LuaTable::getString(double key)
{
  lua_pushnumber(m_L, key);
  lua_gettable(m_L, m_Index);
  
  const char* result = lua_tostring(m_L, -1);
  lua_pop(m_L, 1);

  return result;
}

/******************************************************************************
 * Read a user data in the table.
 *
 * \param key The key of the element to get in the table.
 * \return A pointer to some userdata, or 0 if the value in the table is
 * not of luatype \e userdata.
 ******************************************************************************/
void* LuaTable::getUserData(double key)
{
  lua_pushnumber(m_L, key);
  lua_gettable(m_L, m_Index);
  
  void* result = lua_touserdata(m_L, -1);
  lua_pop(m_L, 1);

  return result;
}

/******************************************************************************
 * Read a table in the table.
 *
 * \param key The key of the element to get in the table.
 * \return A pointer on a LuaTable or 0 if there's no table at the given
 * key.
 ******************************************************************************/
LuaTable* LuaTable::getTable(double key)
{
  lua_pushnumber(m_L, key);
  lua_gettable(m_L, m_Index);

  LuaTable* newTable = 0;
  
  if( lua_istable(m_L, -1) )
  {
    newTable = new LuaTable(m_L, -1);
  }

  return newTable;
}

/******************************************************************************
 * The following functions add element to the table at a given key.
 * TODO: add more types...
 ******************************************************************************/

void LuaTable::addElement(const char* key, double value)
{
  lua_pushstring( m_L, key );
  lua_pushnumber( m_L, value );
  lua_settable( m_L, m_Index );
}

/******************************************************************************
 * Use this function to test if the value at the given key is nil.
 *
 * \param key The value's key in the table.
 * \return True if the value at the given key is nil.
 ******************************************************************************/
bool LuaTable::isNil(double key)
{
  lua_pushnumber(m_L, key);
  lua_gettable(m_L, m_Index);

  return lua_isnil(m_L, -1);
}

/******************************************************************************
 * Use this function if you think that the get*() functions of this class
 * are not efficient for your purpose. It pushes the value at the given
 * table's key on the stack. You may then use the classical lua
 * functions...
 *
 * \param key The key of the element to get in the table.
 ******************************************************************************/
void LuaTable::pushElementAt(double key)
{
  lua_pushnumber(m_L, key);
  lua_gettable(m_L, m_Index);
}

/******************************************************************************
 * Constructs a LuaTable from scratch. This creates a new table on the
 * stack.
 *
 * \param L The lua state where we are going to create the table.
 ******************************************************************************/
LuaTable* LuaTable::createTable(lua_State* L)
{
  lua_newtable(L);
  return new LuaTable(L, -1);
}

}