ContextDocumentsProxy.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ContextDocumentsProxy.h"
  29. #include <Rocket/Core/ElementDocument.h>
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Lua {
  33. typedef Rocket::Core::ElementDocument Document;
  34. template<> void ExtraInit<ContextDocumentsProxy>(lua_State* L, int metatable_index)
  35. {
  36. lua_pushcfunction(L,ContextDocumentsProxy__index);
  37. lua_setfield(L,metatable_index,"__index");
  38. lua_pushcfunction(L,ContextDocumentsProxy__pairs);
  39. lua_setfield(L,metatable_index,"__pairs");
  40. lua_pushcfunction(L,ContextDocumentsProxy__ipairs);
  41. lua_setfield(L,metatable_index,"__ipairs");
  42. }
  43. int ContextDocumentsProxy__index(lua_State* L)
  44. {
  45. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  46. int type = lua_type(L,2);
  47. if(type == LUA_TNUMBER || type == LUA_TSTRING) //only valid key types
  48. {
  49. ContextDocumentsProxy* proxy = LuaType<ContextDocumentsProxy>::check(L,1);
  50. Document* ret = NULL;
  51. if(type == LUA_TSTRING)
  52. ret = proxy->owner->GetDocument(luaL_checkstring(L,2));
  53. else
  54. ret = proxy->owner->GetDocument(luaL_checkint(L,2));
  55. LuaType<Document>::push(L,ret,false);
  56. return 1;
  57. }
  58. else
  59. return LuaType<ContextDocumentsProxy>::index(L);
  60. }
  61. //[1] is the object, [2] is the last used key, [3] is the userdata
  62. int ContextDocumentsProxy__pairs(lua_State* L)
  63. {
  64. Document* doc = NULL;
  65. ContextDocumentsProxy* obj = LuaType<ContextDocumentsProxy>::check(L,1);
  66. LUACHECKOBJ(obj);
  67. int* pindex = (int*)lua_touserdata(L,3);
  68. if((*pindex) == -1)
  69. *pindex = 0;
  70. int num_docs = obj->owner->GetNumDocuments();
  71. //because there can be missing indexes, make sure to continue until there
  72. //is actually a document at the index
  73. while((*pindex) < num_docs)
  74. {
  75. doc = obj->owner->GetDocument((*pindex)++);
  76. if(doc != NULL)
  77. break;
  78. }
  79. //If we found a document
  80. if(doc != NULL)
  81. {
  82. lua_pushstring(L,doc->GetId().CString());
  83. LuaType<Document>::push(L,doc);
  84. }
  85. else //if we were at the end and didn't find a document
  86. {
  87. lua_pushnil(L);
  88. lua_pushnil(L);
  89. }
  90. return 2;
  91. }
  92. //same as __pairs, but putting an integer key instead of a string key
  93. int ContextDocumentsProxy__ipairs(lua_State* L)
  94. {
  95. Document* doc = NULL;
  96. ContextDocumentsProxy* obj = LuaType<ContextDocumentsProxy>::check(L,1);
  97. LUACHECKOBJ(obj);
  98. int* pindex = (int*)lua_touserdata(L,3);
  99. if((*pindex) == -1)
  100. *pindex = 0;
  101. int num_docs = obj->owner->GetNumDocuments();
  102. //because there can be missing indexes, make sure to continue until there
  103. //is actually a document at the index
  104. while((*pindex) < num_docs)
  105. {
  106. doc = obj->owner->GetDocument((*pindex)++);
  107. if(doc != NULL)
  108. break;
  109. }
  110. //we found a document
  111. if(doc != NULL)
  112. {
  113. lua_pushinteger(L,(*pindex)-1);
  114. LuaType<Document>::push(L,doc);
  115. }
  116. else //we got to the end and didn't find another document
  117. {
  118. lua_pushnil(L);
  119. lua_pushnil(L);
  120. }
  121. return 2;
  122. }
  123. RegType<ContextDocumentsProxy> ContextDocumentsProxyMethods[] =
  124. {
  125. { NULL, NULL },
  126. };
  127. luaL_reg ContextDocumentsProxyGetters[] =
  128. {
  129. { NULL, NULL },
  130. };
  131. luaL_reg ContextDocumentsProxySetters[] =
  132. {
  133. { NULL, NULL },
  134. };
  135. LUATYPEDEFINE(ContextDocumentsProxy,false)
  136. }
  137. }
  138. }