object_base.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_OBJECT_BASE_HPP
  19. #define SOL_OBJECT_BASE_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/stack.hpp>
  22. namespace sol {
  23. template <typename ref_t>
  24. class basic_object_base : public ref_t {
  25. private:
  26. using base_t = ref_t;
  27. template <typename T>
  28. decltype(auto) as_stack(std::true_type) const {
  29. return stack::get<T>(base_t::lua_state(), base_t::stack_index());
  30. }
  31. template <typename T>
  32. decltype(auto) as_stack(std::false_type) const {
  33. base_t::push();
  34. return stack::pop<T>(base_t::lua_state());
  35. }
  36. template <typename T>
  37. bool is_stack(std::true_type) const {
  38. return stack::check<T>(base_t::lua_state(), base_t::stack_index(), &no_panic);
  39. }
  40. template <typename T>
  41. bool is_stack(std::false_type) const {
  42. int r = base_t::registry_index();
  43. if (r == LUA_REFNIL)
  44. return meta::any_same<meta::unqualified_t<T>, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false;
  45. if (r == LUA_NOREF)
  46. return false;
  47. auto pp = stack::push_pop(*this);
  48. return stack::check<T>(base_t::lua_state(), -1, &no_panic);
  49. }
  50. public:
  51. basic_object_base() noexcept = default;
  52. basic_object_base(const basic_object_base&) = default;
  53. basic_object_base(basic_object_base&&) = default;
  54. basic_object_base& operator=(const basic_object_base&) = default;
  55. basic_object_base& operator=(basic_object_base&&) = default;
  56. template <typename T, typename... Args, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_object_base>>> = meta::enabler>
  57. basic_object_base(T&& arg, Args&&... args) : base_t(std::forward<T>(arg), std::forward<Args>(args)...) {
  58. }
  59. template <typename T>
  60. decltype(auto) as() const {
  61. return as_stack<T>(is_stack_based<base_t>());
  62. }
  63. template <typename T>
  64. bool is() const {
  65. return is_stack<T>(is_stack_based<base_t>());
  66. }
  67. };
  68. } // namespace sol
  69. #endif // SOL_OBJECT_BASE_HPP