DataSource.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "DataSource.h"
  29. #include <RmlUi/Core/Log.h>
  30. namespace Rml {
  31. namespace Lua {
  32. typedef LuaDataSource DataSource;
  33. int DataSourcenew(lua_State* L)
  34. {
  35. const char* name = luaL_checkstring(L,1);
  36. LuaDataSource* ds = new LuaDataSource(name);
  37. LuaType<DataSource>::push(L,ds,true);
  38. return 1;
  39. }
  40. int DataSourceNotifyRowAdd(lua_State* L, DataSource* obj)
  41. {
  42. RMLUI_CHECK_OBJ(obj);
  43. const char* table_name = luaL_checkstring(L,1);
  44. int first_row_added = (int)luaL_checkinteger(L,2);
  45. int num_rows_added = (int)luaL_checkinteger(L,3);
  46. obj->NotifyRowAdd(table_name,first_row_added,num_rows_added);
  47. return 0;
  48. }
  49. int DataSourceNotifyRowRemove(lua_State* L, DataSource* obj)
  50. {
  51. RMLUI_CHECK_OBJ(obj);
  52. const char* table_name = luaL_checkstring(L,1);
  53. int first_row_removed = (int)luaL_checkinteger(L,2);
  54. int num_rows_removed = (int)luaL_checkinteger(L,3);
  55. obj->NotifyRowRemove(table_name,first_row_removed,num_rows_removed);
  56. return 0;
  57. }
  58. int DataSourceNotifyRowChange(lua_State* L, DataSource* obj)
  59. {
  60. RMLUI_CHECK_OBJ(obj);
  61. const char* table_name = luaL_checkstring(L,1);
  62. if(lua_gettop(L) < 2)
  63. {
  64. obj->NotifyRowChange(table_name);
  65. }
  66. else
  67. {
  68. int first_row_changed = (int)luaL_checkinteger(L,2);
  69. int num_rows_changed = (int)luaL_checkinteger(L,3);
  70. obj->NotifyRowChange(table_name,first_row_changed,num_rows_changed);
  71. }
  72. return 0;
  73. }
  74. int DataSourceSetAttrGetNumRows(lua_State* L)
  75. {
  76. DataSource* obj = LuaType<DataSource>::check(L,1);
  77. RMLUI_CHECK_OBJ(obj);
  78. if(lua_type(L,2) == LUA_TFUNCTION)
  79. {
  80. lua_pushvalue(L,2); //copy of the function, so it is for sure at the top of the stack
  81. obj->getNumRowsRef = luaL_ref(L, LUA_REGISTRYINDEX);
  82. }
  83. else
  84. Log::Message(Log::LT_WARNING, "Lua: Must assign DataSource.GetNumRows as a function, value received was of %s type", lua_typename(L,2));
  85. return 0;
  86. }
  87. int DataSourceSetAttrGetRow(lua_State* L)
  88. {
  89. DataSource* obj = LuaType<DataSource>::check(L,1);
  90. RMLUI_CHECK_OBJ(obj);
  91. if(lua_type(L,2) == LUA_TFUNCTION)
  92. {
  93. lua_pushvalue(L,2); //copy of the functions, so it is for sure at the top of the stack
  94. obj->getRowRef = luaL_ref(L, LUA_REGISTRYINDEX);
  95. }
  96. else
  97. Log::Message(Log::LT_WARNING, "Lua: Must assign DataSource.GetRow as a function, value received was of %s type", lua_typename(L,2));
  98. return 0;
  99. }
  100. RegType<DataSource> DataSourceMethods[] =
  101. {
  102. RMLUI_LUAMETHOD(DataSource,NotifyRowAdd)
  103. RMLUI_LUAMETHOD(DataSource,NotifyRowRemove)
  104. RMLUI_LUAMETHOD(DataSource,NotifyRowChange)
  105. { nullptr, nullptr },
  106. };
  107. luaL_Reg DataSourceGetters[] =
  108. {
  109. { nullptr, nullptr },
  110. };
  111. luaL_Reg DataSourceSetters[] =
  112. {
  113. RMLUI_LUASETTER(DataSource,GetNumRows)
  114. RMLUI_LUASETTER(DataSource,GetRow)
  115. { nullptr, nullptr },
  116. };
  117. template<> void ExtraInit<LuaDataSource>(lua_State* L, int metatable_index)
  118. {
  119. lua_pushcfunction(L,DataSourcenew);
  120. lua_setfield(L,metatable_index-1,"new");
  121. return;
  122. }
  123. RMLUI_LUATYPE_DEFINE(DataSource)
  124. } // namespace Lua
  125. } // namespace Rml