sqrat  0.9
sqrat
 All Classes Functions Variables Enumerations Enumerator Pages
sqratTable.h
1 //
2 // SqratTable: Table Binding
3 //
4 
5 //
6 // Copyright (c) 2009 Brandon Jones
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
27 
28 #if !defined(_SCRAT_TABLE_H_)
29 #define _SCRAT_TABLE_H_
30 
31 #include <squirrel.h>
32 #include <string.h>
33 
34 #include "sqratObject.h"
35 #include "sqratFunction.h"
36 #include "sqratGlobalMethods.h"
37 
38 namespace Sqrat {
39 
43 class TableBase : public Object {
44 public:
45 
52  TableBase(HSQUIRRELVM v = DefaultVM::Get()) : Object(v, true) {
53  }
54 
61  TableBase(const Object& obj) : Object(obj) {
62  }
63 
71  TableBase(HSQOBJECT o, HSQUIRRELVM v = DefaultVM::Get()) : Object(o, v) {
72  }
73 
84  void Bind(const SQChar* name, Object& obj) {
85  sq_pushobject(vm, GetObject());
86  sq_pushstring(vm, name, -1);
87  sq_pushobject(vm, obj.GetObject());
88  sq_newslot(vm, -3, false);
89  sq_pop(vm,1); // pop table
90  }
91 
101  TableBase& SquirrelFunc(const SQChar* name, SQFUNCTION func) {
102  sq_pushobject(vm, GetObject());
103  sq_pushstring(vm, name, -1);
104  sq_newclosure(vm, func, 0);
105  sq_newslot(vm, -3, false);
106  sq_pop(vm,1); // pop table
107  return *this;
108  }
109 
121  template<class V>
122  TableBase& SetValue(const SQChar* name, const V& val) {
123  BindValue<V>(name, val, false);
124  return *this;
125  }
126 
138  template<class V>
139  TableBase& SetValue(const SQInteger index, const V& val) {
140  BindValue<V>(index, val, false);
141  return *this;
142  }
143 
155  template<class V>
156  TableBase& SetInstance(const SQChar* name, V* val) {
157  BindInstance<V>(name, val, false);
158  return *this;
159  }
160 
172  template<class V>
173  TableBase& SetInstance(const SQInteger index, V* val) {
174  BindInstance<V>(index, val, false);
175  return *this;
176  }
177 
189  template<class F>
190  TableBase& Func(const SQChar* name, F method) {
191  BindFunc(name, &method, sizeof(method), SqGlobalFunc(method));
192  return *this;
193  }
194 
209  template<class F>
210  TableBase& Overload(const SQChar* name, F method) {
211  BindOverload(name, &method, sizeof(method), SqGlobalOverloadedFunc(method), SqOverloadFunc(method), SqGetArgCount(method));
212  return *this;
213  }
214 
228  template <typename T>
229  SharedPtr<T> GetValue(const SQChar* name)
230  {
231  sq_pushobject(vm, obj);
232  sq_pushstring(vm, name, -1);
233 #if !defined (SCRAT_NO_ERROR_CHECKING)
234  if (SQ_FAILED(sq_get(vm, -2))) {
235  sq_pop(vm, 1);
236  Error::Instance().Throw(vm, _SC("illegal index"));
237  return SharedPtr<T>();
238  }
239 #else
240  sq_get(vm, -2);
241 #endif
242  Var<SharedPtr<T> > entry(vm, -1);
243 #if !defined (SCRAT_NO_ERROR_CHECKING)
244  if (Error::Instance().Occurred(vm)) {
245  sq_pop(vm, 2);
246  return SharedPtr<T>();
247  }
248 #endif
249  sq_pop(vm, 2);
250  return entry.value;
251  }
252 
266  template <typename T>
268  {
269  sq_pushobject(vm, obj);
270  sq_pushinteger(vm, index);
271 #if !defined (SCRAT_NO_ERROR_CHECKING)
272  if (SQ_FAILED(sq_get(vm, -2))) {
273  sq_pop(vm, 1);
274  Error::Instance().Throw(vm, _SC("illegal index"));
275  return SharedPtr<T>();
276  }
277 #else
278  sq_get(vm, -2);
279 #endif
280  Var<SharedPtr<T> > entry(vm, -1);
281 #if !defined (SCRAT_NO_ERROR_CHECKING)
282  if (Error::Instance().Occurred(vm)) {
283  sq_pop(vm, 2);
284  return SharedPtr<T>();
285  }
286 #endif
287  sq_pop(vm, 2);
288  return entry.value;
289  }
290 
299  Function GetFunction(const SQChar* name) {
300  HSQOBJECT funcObj;
301  sq_pushobject(vm, GetObject());
302  sq_pushstring(vm, name, -1);
303 #if !defined (SCRAT_NO_ERROR_CHECKING)
304  if(SQ_FAILED(sq_get(vm, -2))) {
305  sq_pop(vm, 1);
306  return Function();
307  }
308  SQObjectType value_type = sq_gettype(vm, -1);
309  if (value_type != OT_CLOSURE && value_type != OT_NATIVECLOSURE) {
310  sq_pop(vm, 2);
311  return Function();
312  }
313 #else
314  sq_get(vm, -2);
315 #endif
316  sq_getstackobj(vm, -1, &funcObj);
317  Function ret(vm, GetObject(), funcObj); // must addref before the pop!
318  sq_pop(vm, 2);
319  return ret;
320  }
321 
330  Function GetFunction(const SQInteger index) {
331  HSQOBJECT funcObj;
332  sq_pushobject(vm, GetObject());
333  sq_pushinteger(vm, index);
334 #if !defined (SCRAT_NO_ERROR_CHECKING)
335  if(SQ_FAILED(sq_get(vm, -2))) {
336  sq_pop(vm, 1);
337  return Function();
338  }
339  SQObjectType value_type = sq_gettype(vm, -1);
340  if (value_type != OT_CLOSURE && value_type != OT_NATIVECLOSURE) {
341  sq_pop(vm, 2);
342  return Function();
343  }
344 #else
345  sq_get(vm, -2);
346 #endif
347  sq_getstackobj(vm, -1, &funcObj);
348  Function ret(vm, GetObject(), funcObj); // must addref before the pop!
349  sq_pop(vm, 2);
350  return ret;
351  }
352 };
353 
357 class Table : public TableBase {
358 public:
359 
367  Table() {
368  }
369 
376  Table(HSQUIRRELVM v) : TableBase(v) {
377  sq_newtable(vm);
378  sq_getstackobj(vm,-1,&obj);
379  sq_addref(vm, &obj);
380  sq_pop(vm,1);
381  }
382 
389  Table(const Object& obj) : TableBase(obj) {
390  }
391 
399  Table(HSQOBJECT o, HSQUIRRELVM v = DefaultVM::Get()) : TableBase(o, v) {
400  }
401 };
402 
407 class RootTable : public TableBase {
408 public:
409 
416  RootTable(HSQUIRRELVM v = DefaultVM::Get()) : TableBase(v) {
417  sq_pushroottable(vm);
418  sq_getstackobj(vm,-1,&obj);
419  sq_addref(vm, &obj);
420  sq_pop(v,1); // pop root table
421  }
422 };
423 
428 class RegistryTable : public TableBase {
429 public:
430 
437  RegistryTable(HSQUIRRELVM v = DefaultVM::Get()) : TableBase(v) {
438  sq_pushregistrytable(v);
439  sq_getstackobj(vm,-1,&obj);
440  sq_addref(vm, &obj);
441  sq_pop(v,1); // pop the registry table
442  }
443 };
444 
448 template<>
449 struct Var<Table> {
450 
452 
463  Var(HSQUIRRELVM vm, SQInteger idx) {
464  HSQOBJECT obj;
465  sq_resetobject(&obj);
466  sq_getstackobj(vm,idx,&obj);
467  value = Table(obj, vm);
468 #if !defined (SCRAT_NO_ERROR_CHECKING)
469  SQObjectType value_type = sq_gettype(vm, idx);
470  if (value_type != OT_TABLE) {
471  Error::Instance().Throw(vm, Sqrat::Error::FormatTypeError(vm, idx, _SC("table")));
472  }
473 #endif
474  }
475 
483  static void push(HSQUIRRELVM vm, const Table & value) {
484  HSQOBJECT obj;
485  sq_resetobject(&obj);
486  obj = value.GetObject();
487  sq_pushobject(vm,obj);
488  }
489 };
490 
491 
495 template<>
496 struct Var<Table&> {
498 
509  Var(HSQUIRRELVM vm, SQInteger idx) {
510  HSQOBJECT obj;
511  sq_resetobject(&obj);
512  sq_getstackobj(vm,idx,&obj);
513  value = Table(obj, vm);
514 #if !defined (SCRAT_NO_ERROR_CHECKING)
515  SQObjectType value_type = sq_gettype(vm, idx);
516  if (value_type != OT_TABLE) {
517  Error::Instance().Throw(vm, Sqrat::Error::FormatTypeError(vm, idx, _SC("table")));
518  }
519 #endif
520  }
521 
522 
530  static void push(HSQUIRRELVM vm, Table value) {
531  HSQOBJECT obj;
532  sq_resetobject(&obj);
533  obj = value.GetObject();
534  sq_pushobject(vm,obj);
535  }
536 };
537 
538 }
539 
540 #endif
static HSQUIRRELVM Get()
Definition: sqratUtil.h:92
Table value
The actual value of get operations.
Definition: sqratTable.h:451
TableBase & SetValue(const SQChar *name, const V &val)
Definition: sqratTable.h:122
void Bind(const SQChar *name, Object &obj)
Definition: sqratTable.h:84
RootTable(HSQUIRRELVM v=DefaultVM::Get())
Definition: sqratTable.h:416
T value
The actual value of get operations.
Definition: sqratTypes.h:152
Table value
The actual value of get operations.
Definition: sqratTable.h:497
Definition: sqratTypes.h:150
static string FormatTypeError(HSQUIRRELVM vm, SQInteger idx, const string &expectedType)
Definition: sqratUtil.h:149
SharedPtr< T > GetValue(const SQChar *name)
Definition: sqratTable.h:229
TableBase(const Object &obj)
Definition: sqratTable.h:61
Definition: sqratTable.h:407
Table()
Definition: sqratTable.h:367
TableBase & SquirrelFunc(const SQChar *name, SQFUNCTION func)
Definition: sqratTable.h:101
Represents a function in Squirrel.
Definition: sqratFunction.h:40
TableBase & Overload(const SQChar *name, F method)
Definition: sqratTable.h:210
Table(HSQOBJECT o, HSQUIRRELVM v=DefaultVM::Get())
Definition: sqratTable.h:399
Definition: sqratUtil.h:291
TableBase(HSQOBJECT o, HSQUIRRELVM v=DefaultVM::Get())
Definition: sqratTable.h:71
virtual HSQOBJECT GetObject() const
Definition: sqratObject.h:182
TableBase & SetValue(const SQInteger index, const V &val)
Definition: sqratTable.h:139
Function GetFunction(const SQChar *name)
Definition: sqratTable.h:299
RegistryTable(HSQUIRRELVM v=DefaultVM::Get())
Definition: sqratTable.h:437
Definition: sqratObject.h:48
static Error & Instance()
Definition: sqratUtil.h:134
void Throw(HSQUIRRELVM vm, const string &err)
Definition: sqratUtil.h:210
The base class for Table that implements almost all of its functionality.
Definition: sqratTable.h:43
TableBase & SetInstance(const SQInteger index, V *val)
Definition: sqratTable.h:173
Var(HSQUIRRELVM vm, SQInteger idx)
Definition: sqratTable.h:509
Represents a table in Squirrel.
Definition: sqratTable.h:357
Var(HSQUIRRELVM vm, SQInteger idx)
Definition: sqratTable.h:463
Definition: sqratTable.h:428
static void push(HSQUIRRELVM vm, Table value)
Definition: sqratTable.h:530
Function GetFunction(const SQInteger index)
Definition: sqratTable.h:330
static void push(HSQUIRRELVM vm, const Table &value)
Definition: sqratTable.h:483
TableBase & SetInstance(const SQChar *name, V *val)
Definition: sqratTable.h:156
Table(const Object &obj)
Definition: sqratTable.h:389
TableBase(HSQUIRRELVM v=DefaultVM::Get())
Definition: sqratTable.h:52
Table(HSQUIRRELVM v)
Definition: sqratTable.h:376
SharedPtr< T > GetValue(int index)
Definition: sqratTable.h:267
TableBase & Func(const SQChar *name, F method)
Definition: sqratTable.h:190