Utilities.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef ROCKETCORELUAUTILITIES_H
  28. #define ROCKETCORELUAUTILITIES_H
  29. /*
  30. This file is for free-floating functions that are used across more than one file.
  31. */
  32. #include <Rocket/Core/Lua/Header.h>
  33. #include <Rocket/Core/Lua/lua.hpp>
  34. #include <Rocket/Core/Lua/LuaType.h>
  35. #include <Rocket/Core/Variant.h>
  36. namespace Rocket {
  37. namespace Core {
  38. namespace Lua {
  39. /** casts the variant to its specific type before pushing it to the stack
  40. @relates Rocket::Core::Lua::LuaType */
  41. void ROCKETLUA_API PushVariant(lua_State* L, Variant* var);
  42. /** If there are errors on the top of the stack, this will print those out to the log.
  43. @param L A Lua state, and if not passed in, will use the Interpreter's state
  44. @param place A string that will be printed to the log right before the error message, seperated by a space. Set
  45. this when you would get no information about where the error happens.
  46. @relates Rocket::Core::Lua::Interpreter */
  47. void ROCKETLUA_API Report(lua_State* L = NULL, const Rocket::Core::String& place = "");
  48. //Helper function, so that the types don't have to define individual functions themselves
  49. // to fill the Elements.As table
  50. template<typename ToType>
  51. int CastFromElementTo(lua_State* L)
  52. {
  53. Element* ele = LuaType<Element>::check(L,1);
  54. LUACHECKOBJ(ele);
  55. LuaType<ToType>::push(L,(ToType*)ele,false);
  56. return 1;
  57. }
  58. //Adds to the Element.As table the name of the type, and the function to use to cast
  59. template<typename T>
  60. void AddTypeToElementAsTable(lua_State* L)
  61. {
  62. int top = lua_gettop(L);
  63. lua_getglobal(L,"Element");
  64. lua_getfield(L,-1,"As");
  65. if(!lua_isnoneornil(L,-1))
  66. {
  67. lua_pushcfunction(L,CastFromElementTo<T>);
  68. lua_setfield(L,-2,GetTClassName<T>());
  69. }
  70. lua_settop(L,top); //pop "As" and "Element"
  71. }
  72. }
  73. }
  74. }
  75. #endif