variadic_results.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_VARIADIC_RESULTS_HPP
  19. #define SOL_VARIADIC_RESULTS_HPP
  20. #include <sol/stack.hpp>
  21. #include <sol/object.hpp>
  22. #include <sol/as_returns.hpp>
  23. #include <sol/function_result.hpp>
  24. #include <sol/protected_function_result.hpp>
  25. #include <vector>
  26. namespace sol {
  27. template <typename Al = typename std::allocator<object>>
  28. struct basic_variadic_results : public std::vector<object, Al> {
  29. private:
  30. using base_t = std::vector<object, Al>;
  31. public:
  32. basic_variadic_results() : base_t() {
  33. }
  34. basic_variadic_results(unsafe_function_result fr) : base_t() {
  35. this->reserve(fr.return_count());
  36. this->insert(this->cend(), fr.begin(), fr.end());
  37. }
  38. basic_variadic_results(protected_function_result fr) : base_t() {
  39. this->reserve(fr.return_count());
  40. this->insert(this->cend(), fr.begin(), fr.end());
  41. }
  42. template <typename Arg0, typename... Args,
  43. meta::disable_any<std::is_same<meta::unqualified_t<Arg0>, basic_variadic_results>, std::is_same<meta::unqualified_t<Arg0>, function_result>,
  44. std::is_same<meta::unqualified_t<Arg0>, protected_function_result>> = meta::enabler>
  45. basic_variadic_results(Arg0&& arg0, Args&&... args) : base_t(std::forward<Arg0>(arg0), std::forward<Args>(args)...) {
  46. }
  47. basic_variadic_results(const basic_variadic_results&) = default;
  48. basic_variadic_results(basic_variadic_results&&) = default;
  49. };
  50. struct variadic_results : public basic_variadic_results<> {
  51. private:
  52. using base_t = basic_variadic_results<>;
  53. public:
  54. using base_t::base_t;
  55. };
  56. template <typename Al>
  57. struct is_container<basic_variadic_results<Al>> : std::false_type { };
  58. template <>
  59. struct is_container<variadic_results> : std::false_type { };
  60. namespace stack {
  61. template <typename Al>
  62. struct unqualified_pusher<basic_variadic_results<Al>> {
  63. int push(lua_State* L, const basic_variadic_results<Al>& e) {
  64. int p = 0;
  65. for (const auto& i : e) {
  66. p += stack::push(L, i);
  67. }
  68. return p;
  69. }
  70. };
  71. template <>
  72. struct unqualified_pusher<variadic_results> {
  73. int push(lua_State* L, const variadic_results& r) {
  74. using base_t = basic_variadic_results<>;
  75. return stack::push(L, static_cast<const base_t&>(r));
  76. }
  77. };
  78. } // namespace stack
  79. } // namespace sol
  80. #endif // SOL_VARIADIC_RESULTS_HPP