policies.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_FILTERS_HPP
  19. #define SOL_FILTERS_HPP
  20. #include <sol/traits.hpp>
  21. #include <array>
  22. namespace sol {
  23. namespace detail {
  24. struct policy_base_tag { };
  25. } // namespace detail
  26. template <int Target, int... In>
  27. struct static_stack_dependencies : detail::policy_base_tag { };
  28. typedef static_stack_dependencies<-1, 1> self_dependency;
  29. template <int... In>
  30. struct returns_self_with : detail::policy_base_tag { };
  31. typedef returns_self_with<> returns_self;
  32. struct stack_dependencies : detail::policy_base_tag {
  33. int target;
  34. std::array<int, 64> stack_indices;
  35. std::size_t len;
  36. template <typename... Args>
  37. stack_dependencies(int stack_target, Args&&... args) : target(stack_target), stack_indices(), len(sizeof...(Args)) {
  38. std::size_t i = 0;
  39. (void)detail::swallow { int(), (stack_indices[i++] = static_cast<int>(std::forward<Args>(args)), int())... };
  40. }
  41. int& operator[](std::size_t i) {
  42. return stack_indices[i];
  43. }
  44. const int& operator[](std::size_t i) const {
  45. return stack_indices[i];
  46. }
  47. std::size_t size() const {
  48. return len;
  49. }
  50. };
  51. template <typename F, typename... Policies>
  52. struct policy_wrapper {
  53. typedef std::index_sequence_for<Policies...> indices;
  54. F value;
  55. std::tuple<Policies...> policies;
  56. template <typename Fx, typename... Args, meta::enable<meta::neg<std::is_same<meta::unqualified_t<Fx>, policy_wrapper>>> = meta::enabler>
  57. policy_wrapper(Fx&& fx, Args&&... args) : value(std::forward<Fx>(fx)), policies(std::forward<Args>(args)...) {
  58. }
  59. policy_wrapper(const policy_wrapper&) = default;
  60. policy_wrapper& operator=(const policy_wrapper&) = default;
  61. policy_wrapper(policy_wrapper&&) = default;
  62. policy_wrapper& operator=(policy_wrapper&&) = default;
  63. };
  64. template <typename F, typename... Args>
  65. auto policies(F&& f, Args&&... args) {
  66. return policy_wrapper<std::decay_t<F>, std::decay_t<Args>...>(std::forward<F>(f), std::forward<Args>(args)...);
  67. }
  68. namespace detail {
  69. template <typename T>
  70. using is_policy = meta::is_specialization_of<T, policy_wrapper>;
  71. template <typename T>
  72. inline constexpr bool is_policy_v = is_policy<T>::value;
  73. } // namespace detail
  74. } // namespace sol
  75. #endif // SOL_FILTERS_HPP