ContextDocumentsProxy.cpp 4.5 KB

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