stack_probe.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_PROBE_HPP
  19. #define SOL_STACK_PROBE_HPP
  20. #include <sol/stack_core.hpp>
  21. #include <sol/stack_field.hpp>
  22. #include <sol/stack_check.hpp>
  23. namespace sol { namespace stack {
  24. template <typename T, typename P, bool b, bool raw, typename>
  25. struct probe_field_getter {
  26. template <typename Key>
  27. probe get(lua_State* L, Key&& key, int tableindex = -2) {
  28. if constexpr (!b) {
  29. if (!maybe_indexable(L, tableindex)) {
  30. return probe(false, 0);
  31. }
  32. }
  33. get_field<b, raw>(L, std::forward<Key>(key), tableindex);
  34. return probe(check<P>(L), 1);
  35. }
  36. };
  37. template <typename A, typename B, typename P, bool b, bool raw, typename C>
  38. struct probe_field_getter<std::pair<A, B>, P, b, raw, C> {
  39. template <typename Keys>
  40. probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
  41. if (!b && !maybe_indexable(L, tableindex)) {
  42. return probe(false, 0);
  43. }
  44. get_field<b, raw>(L, std::get<0>(keys), tableindex);
  45. if (!maybe_indexable(L)) {
  46. return probe(false, 1);
  47. }
  48. get_field<false, raw>(L, std::get<1>(keys), tableindex);
  49. return probe(check<P>(L), 2);
  50. }
  51. };
  52. template <typename... Args, typename P, bool b, bool raw, typename C>
  53. struct probe_field_getter<std::tuple<Args...>, P, b, raw, C> {
  54. template <std::size_t I, typename Keys>
  55. probe apply(std::index_sequence<I>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
  56. get_field<(I < 1) && b, raw>(L, std::get<I>(keys), tableindex);
  57. return probe(check<P>(L), sofar);
  58. }
  59. template <std::size_t I, std::size_t I1, std::size_t... In, typename Keys>
  60. probe apply(std::index_sequence<I, I1, In...>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
  61. get_field < I<1 && b, raw>(L, std::get<I>(keys), tableindex);
  62. if (!maybe_indexable(L)) {
  63. return probe(false, sofar);
  64. }
  65. return apply(std::index_sequence<I1, In...>(), sofar + 1, L, std::forward<Keys>(keys), -1);
  66. }
  67. template <typename Keys>
  68. probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
  69. if constexpr (!b) {
  70. if (!maybe_indexable(L, tableindex)) {
  71. return probe(false, 0);
  72. }
  73. return apply(std::index_sequence_for<Args...>(), 1, L, std::forward<Keys>(keys), tableindex);
  74. }
  75. else {
  76. return apply(std::index_sequence_for<Args...>(), 1, L, std::forward<Keys>(keys), tableindex);
  77. }
  78. }
  79. };
  80. }} // namespace sol::stack
  81. #endif // SOL_STACK_PROBE_HPP