DataFormatter.cpp 3.7 KB

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