proxy_base.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_PROXY_BASE_HPP
  19. #define SOL_PROXY_BASE_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/tuple.hpp>
  22. #include <sol/stack.hpp>
  23. namespace sol {
  24. struct proxy_base_tag { };
  25. namespace detail {
  26. template <typename T>
  27. using proxy_key_t = meta::conditional_t<meta::is_specialization_of_v<meta::unqualified_t<T>, std::tuple>, T,
  28. std::tuple<meta::conditional_t<std::is_array_v<meta::unqualified_t<T>>, std::remove_reference_t<T>&, meta::unqualified_t<T>>>>;
  29. }
  30. template <typename Super>
  31. struct proxy_base : public proxy_base_tag {
  32. lua_State* lua_state() const {
  33. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  34. return super.lua_state();
  35. }
  36. operator std::string() const {
  37. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  38. return super.template get<std::string>();
  39. }
  40. template <typename T, meta::enable<meta::neg<meta::is_string_constructible<T>>, is_proxy_primitive<meta::unqualified_t<T>>> = meta::enabler>
  41. operator T() const {
  42. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  43. return super.template get<T>();
  44. }
  45. template <typename T,
  46. meta::enable<meta::neg<meta::is_string_constructible<T>>, meta::neg<is_proxy_primitive<meta::unqualified_t<T>>>> = meta::enabler>
  47. operator T&() const {
  48. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  49. return super.template get<T&>();
  50. }
  51. };
  52. } // namespace sol
  53. #endif // SOL_PROXY_BASE_HPP