DataFormatter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "DataFormatter.h"
  29. using Rocket::Core::Log;
  30. namespace Rocket {
  31. namespace Controls {
  32. namespace Lua {
  33. //method
  34. int DataFormatternew(lua_State* L)
  35. {
  36. DataFormatter* df;
  37. int ref = LUA_NOREF;
  38. int top = lua_gettop(L);
  39. if(top == 0)
  40. df = new DataFormatter();
  41. else if (top > 0) //at least one element means at least a name
  42. {
  43. if(top > 1) //if a function as well
  44. {
  45. if(lua_type(L,2) != LUA_TFUNCTION)
  46. {
  47. 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)));
  48. }
  49. else //if it is a function
  50. {
  51. LuaDataFormatter::PushDataFormatterFunctionTable(L);
  52. lua_pushvalue(L,2); //re-push the function so it is on the top of the stack
  53. ref = luaL_ref(L,-2);
  54. lua_pop(L,1); //pop the function table
  55. }
  56. }
  57. df = new DataFormatter(luaL_checkstring(L,1));
  58. df->ref_FormatData = ref; //will either be valid or LUA_NOREF
  59. }
  60. LuaType<DataFormatter>::push(L,df,true);
  61. return 1;
  62. }
  63. //setter
  64. int DataFormatterSetAttrFormatData(lua_State* L)
  65. {
  66. DataFormatter* obj = LuaType<DataFormatter>::check(L,1);
  67. LUACHECKOBJ(obj);
  68. int ref = LUA_NOREF;
  69. if(lua_type(L,2) != LUA_TFUNCTION)
  70. {
  71. 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)));
  72. }
  73. else //if it is a function
  74. {
  75. LuaDataFormatter::PushDataFormatterFunctionTable(L);
  76. lua_pushvalue(L,2); //re-push the function so it is on the top of the stack
  77. ref = luaL_ref(L,-2);
  78. lua_pop(L,1); //pop the function table
  79. }
  80. obj->ref_FormatData = ref;
  81. return 0;
  82. }
  83. Rocket::Core::Lua::RegType<DataFormatter> DataFormatterMethods[] =
  84. {
  85. { NULL, NULL },
  86. };
  87. luaL_Reg DataFormatterGetters[] =
  88. {
  89. { NULL, NULL },
  90. };
  91. luaL_Reg DataFormatterSetters[] =
  92. {
  93. LUASETTER(DataFormatter,FormatData)
  94. { NULL, NULL },
  95. };
  96. }
  97. }
  98. }
  99. namespace Rocket {
  100. namespace Core {
  101. namespace Lua {
  102. using Rocket::Controls::Lua::DataFormatter;
  103. template<> void ExtraInit<DataFormatter>(lua_State* L, int metatable_index)
  104. {
  105. lua_pushcfunction(L,Rocket::Controls::Lua::DataFormatternew);
  106. lua_setfield(L,metatable_index-1,"new");
  107. return;
  108. }
  109. LUACONTROLSTYPEDEFINE(DataFormatter,false)
  110. }
  111. }
  112. }