Pairs.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-2023 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. #ifndef RMLUI_LUA_PAIRS_H
  29. #define RMLUI_LUA_PAIRS_H
  30. #include <RmlUi/Lua/IncludeLua.h>
  31. #include <RmlUi/Lua/Utilities.h>
  32. #include <utility>
  33. namespace Rml {
  34. namespace Lua {
  35. template <class T>
  36. int PairsConvertTolua(lua_State* L, const T& v);
  37. template <class F, class S>
  38. int PairsConvertTolua(lua_State* L, const std::pair<F, S>& v)
  39. {
  40. int nresult = 0;
  41. nresult += PairsConvertTolua(L, v.first);
  42. nresult += PairsConvertTolua(L, v.second);
  43. return nresult;
  44. }
  45. template <>
  46. inline int PairsConvertTolua<String>(lua_State* L, const String& s)
  47. {
  48. lua_pushlstring(L, s.c_str(), s.size());
  49. return 1;
  50. }
  51. template <>
  52. inline int PairsConvertTolua<Variant>(lua_State* L, const Variant& v)
  53. {
  54. PushVariant(L, &v);
  55. return 1;
  56. }
  57. template <typename T>
  58. struct PairsHelper {
  59. static int next(lua_State* L)
  60. {
  61. PairsHelper* self = static_cast<PairsHelper*>(lua_touserdata(L, lua_upvalueindex(1)));
  62. if (self->m_first == self->m_last)
  63. {
  64. return 0;
  65. }
  66. int nreslut = PairsConvertTolua(L, *self->m_first);
  67. ++(self->m_first);
  68. return nreslut;
  69. }
  70. static int destroy(lua_State* L)
  71. {
  72. static_cast<PairsHelper*>(lua_touserdata(L, 1))->~PairsHelper();
  73. return 0;
  74. }
  75. static int constructor(lua_State* L, const T& first, const T& last)
  76. {
  77. void* storage = lua_newuserdata(L, sizeof(PairsHelper<T>));
  78. if (luaL_newmetatable(L, "RmlUi::Lua::PairsHelper"))
  79. {
  80. static luaL_Reg mt[] = {
  81. {"__gc", destroy},
  82. {NULL, NULL},
  83. };
  84. luaL_setfuncs(L, mt, 0);
  85. }
  86. lua_setmetatable(L, -2);
  87. lua_pushcclosure(L, next, 1);
  88. new (storage) PairsHelper<T>(first, last);
  89. return 1;
  90. }
  91. PairsHelper(const T& first, const T& last) : m_first(first), m_last(last) {}
  92. T m_first;
  93. T m_last;
  94. };
  95. template <class Iterator>
  96. int MakePairs(lua_State* L, const Iterator& first, const Iterator& last)
  97. {
  98. return PairsHelper<Iterator>::constructor(L, first, last);
  99. }
  100. template <class Container>
  101. int MakePairs(lua_State* L, const Container& container)
  102. {
  103. return MakePairs(L, std::begin(container), std::end(container));
  104. }
  105. inline int ipairsaux(lua_State* L)
  106. {
  107. lua_Integer i = luaL_checkinteger(L, 2) + 1;
  108. lua_pushinteger(L, i);
  109. #if LUA_VERSION_NUM >= 503
  110. return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
  111. #else
  112. lua_pushinteger(L, i);
  113. lua_gettable(L, 1);
  114. return (lua_isnil(L, -1)) ? 1 : 2;
  115. #endif
  116. }
  117. inline int MakeIntPairs(lua_State* L)
  118. {
  119. luaL_checkany(L, 1);
  120. lua_pushcfunction(L, ipairsaux);
  121. lua_pushvalue(L, 1);
  122. lua_pushinteger(L, 0);
  123. return 3;
  124. }
  125. } // namespace Lua
  126. } // namespace Rml
  127. #endif