make_reference.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_MAKE_REFERENCE_HPP
  19. #define SOL_MAKE_REFERENCE_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/stack.hpp>
  22. namespace sol {
  23. template <typename R = reference, bool should_pop = !is_stack_based_v<R>, typename T>
  24. R make_reference(lua_State* L, T&& value) {
  25. int backpedal = stack::push(L, std::forward<T>(value));
  26. R r = stack::get<R>(L, -backpedal);
  27. if (should_pop) {
  28. lua_pop(L, backpedal);
  29. }
  30. return r;
  31. }
  32. template <typename T, typename R = reference, bool should_pop = !is_stack_based_v<R>, typename... Args>
  33. R make_reference(lua_State* L, Args&&... args) {
  34. int backpedal = stack::push<T>(L, std::forward<Args>(args)...);
  35. R r = stack::get<R>(L, -backpedal);
  36. if (should_pop) {
  37. lua_pop(L, backpedal);
  38. }
  39. return r;
  40. }
  41. template <typename R = reference, bool should_pop = !is_stack_based_v<R>, typename T>
  42. R make_reference_userdata(lua_State* L, T&& value) {
  43. int backpedal = stack::push_userdata(L, std::forward<T>(value));
  44. R r = stack::get<R>(L, -backpedal);
  45. if (should_pop) {
  46. lua_pop(L, backpedal);
  47. }
  48. return r;
  49. }
  50. template <typename T, typename R = reference, bool should_pop = !is_stack_based_v<R>, typename... Args>
  51. R make_reference_userdata(lua_State* L, Args&&... args) {
  52. int backpedal = stack::push_userdata<T>(L, std::forward<Args>(args)...);
  53. R r = stack::get<R>(L, -backpedal);
  54. if (should_pop) {
  55. lua_pop(L, backpedal);
  56. }
  57. return r;
  58. }
  59. } // namespace sol
  60. #endif // SOL_MAKE_REFERENCE_HPP