DataFormatter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "DataFormatter.h"
  29. #include <RmlUi/Core/Log.h>
  30. namespace Rml {
  31. namespace Lua {
  32. //method
  33. int DataFormatternew(lua_State* L)
  34. {
  35. DataFormatter* df = nullptr;
  36. int ref = LUA_NOREF;
  37. int top = lua_gettop(L);
  38. if(top == 0)
  39. df = new DataFormatter();
  40. else if (top > 0) //at least one element means at least a name
  41. {
  42. if(top > 1) //if a function as well
  43. {
  44. if(lua_type(L,2) != LUA_TFUNCTION)
  45. {
  46. Log::Message(Log::LT_ERROR, "Lua: In DataFormatter.new, the second argument MUST be a function (or not exist). You passed in a %s.", lua_typename(L,lua_type(L,2)));
  47. }
  48. else //if it is a function
  49. {
  50. LuaDataFormatter::PushDataFormatterFunctionTable(L);
  51. lua_pushvalue(L,2); //re-push the function so it is on the top of the stack
  52. ref = luaL_ref(L,-2);
  53. lua_pop(L,1); //pop the function table
  54. }
  55. }
  56. df = new DataFormatter(luaL_checkstring(L,1));
  57. df->ref_FormatData = ref; //will either be valid or LUA_NOREF
  58. }
  59. LuaType<DataFormatter>::push(L,df,true);
  60. return 1;
  61. }
  62. //setter
  63. int DataFormatterSetAttrFormatData(lua_State* L)
  64. {
  65. DataFormatter* obj = LuaType<DataFormatter>::check(L,1);
  66. RMLUI_CHECK_OBJ(obj);
  67. int ref = LUA_NOREF;
  68. if(lua_type(L,2) != LUA_TFUNCTION)
  69. {
  70. Log::Message(Log::LT_ERROR, "Lua: Setting DataFormatter.FormatData, the value must be a function. You passed in a %s.", lua_typename(L,lua_type(L,2)));
  71. }
  72. else //if it is a function
  73. {
  74. LuaDataFormatter::PushDataFormatterFunctionTable(L);
  75. lua_pushvalue(L,2); //re-push the function so it is on the top of the stack
  76. ref = luaL_ref(L,-2);
  77. lua_pop(L,1); //pop the function table
  78. }
  79. obj->ref_FormatData = ref;
  80. return 0;
  81. }
  82. RegType<DataFormatter> DataFormatterMethods[] =
  83. {
  84. { nullptr, nullptr },
  85. };
  86. luaL_Reg DataFormatterGetters[] =
  87. {
  88. { nullptr, nullptr },
  89. };
  90. luaL_Reg DataFormatterSetters[] =
  91. {
  92. RMLUI_LUASETTER(DataFormatter,FormatData)
  93. { nullptr, nullptr },
  94. };
  95. template<> void ExtraInit<DataFormatter>(lua_State* L, int metatable_index)
  96. {
  97. lua_pushcfunction(L,DataFormatternew);
  98. lua_setfield(L,metatable_index-1,"new");
  99. return;
  100. }
  101. RMLUI_LUATYPE_DEFINE(DataFormatter)
  102. } // namespace Lua
  103. } // namespace Rml