stack_proxy_base.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_STACK_PROXY_BASE_HPP
  19. #define SOL_STACK_PROXY_BASE_HPP
  20. #include <sol/stack.hpp>
  21. #include <sol/proxy_base.hpp>
  22. namespace sol {
  23. struct stack_proxy_base : public proxy_base<stack_proxy_base> {
  24. private:
  25. lua_State* m_L;
  26. int m_index;
  27. public:
  28. stack_proxy_base() : m_L(nullptr), m_index(0) {
  29. }
  30. stack_proxy_base(lua_State* L_, int index_) : m_L(L_), m_index(index_) {
  31. }
  32. template <typename T>
  33. decltype(auto) get() const {
  34. return stack::get<T>(m_L, stack_index());
  35. }
  36. template <typename T>
  37. bool is() const {
  38. return stack::check<T>(m_L, stack_index());
  39. }
  40. template <typename T>
  41. decltype(auto) as() const {
  42. return get<T>();
  43. }
  44. type get_type() const noexcept {
  45. return type_of(lua_state(), stack_index());
  46. }
  47. int push() const {
  48. return push(m_L);
  49. }
  50. int push(lua_State* L_) const {
  51. lua_pushvalue(L_, m_index);
  52. return 1;
  53. }
  54. lua_State* lua_state() const {
  55. return m_L;
  56. }
  57. int stack_index() const {
  58. return m_index;
  59. }
  60. };
  61. namespace stack {
  62. template <>
  63. struct unqualified_getter<stack_proxy_base> {
  64. static stack_proxy_base get(lua_State* L_, int index_ = -1) {
  65. return stack_proxy_base(L_, index_);
  66. }
  67. };
  68. template <>
  69. struct unqualified_pusher<stack_proxy_base> {
  70. static int push(lua_State*, const stack_proxy_base& proxy_reference) {
  71. return proxy_reference.push();
  72. }
  73. };
  74. } // namespace stack
  75. } // namespace sol
  76. #endif // SOL_STACK_PROXY_BASE_HPP