Pairs.h 4.0 KB

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