LuaEnvironment.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "lua.hpp"
  25. #include "Config.h"
  26. #include "Types.h"
  27. #include "Macros.h"
  28. namespace crown
  29. {
  30. enum LuaArgumentType
  31. {
  32. ARGUMENT_FLOAT
  33. };
  34. struct LuaResource;
  35. /// LuaEnvironment is a wrapper of a subset of Lua functions and
  36. /// provides utilities for extending Lua
  37. class LuaEnvironment
  38. {
  39. public:
  40. LuaEnvironment(lua_State* L);
  41. /// Loads and execute the given @a res_name Lua resource.
  42. void load_and_execute(const char* res_name);
  43. /// Loads and executes the given @a s lua string.
  44. void execute_string(const char* s);
  45. /// Loads the function with the given @a name and @a func into the table @a module.
  46. void load_module_function(const char* module, const char* name, const lua_CFunction func);
  47. void load_module_function(const char* module, const char* name, const char* value);
  48. void load_module_constructor(const char* module, const lua_CFunction func);
  49. /// Loads the enum with the given @a name and @a value into the table @a module.
  50. void load_module_enum(const char* module, const char* name, uint32_t value);
  51. /// Calls the global function @a func with @a argc argument number.
  52. /// Each argument is a pair (type, value).
  53. /// Example call:
  54. /// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
  55. /// Returns true if success, false otherwise
  56. void call_global(const char* func, uint8_t argc, ...);
  57. private:
  58. // Disable copying
  59. LuaEnvironment(const LuaEnvironment&);
  60. LuaEnvironment& operator=(const LuaEnvironment&);
  61. private:
  62. lua_State* m_L;
  63. };
  64. } // namespace crown