as_returns.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_AS_RETURNS_HPP
  19. #define SOL_AS_RETURNS_HPP
  20. #include <sol/traits.hpp>
  21. #include <sol/stack.hpp>
  22. #include <sol/ebco.hpp>
  23. namespace sol {
  24. template <typename T>
  25. struct as_returns_t : private detail::ebco<T> {
  26. private:
  27. using base_t = detail::ebco<T>;
  28. public:
  29. using base_t::base_t;
  30. using base_t::value;
  31. };
  32. template <typename Source>
  33. auto as_returns(Source&& source) {
  34. return as_returns_t<std::decay_t<Source>> { std::forward<Source>(source) };
  35. }
  36. namespace stack {
  37. template <typename T>
  38. struct unqualified_pusher<as_returns_t<T>> {
  39. int push(lua_State* L, const as_returns_t<T>& e) {
  40. auto& src = detail::unwrap(e.value());
  41. int p = 0;
  42. for (const auto& i : src) {
  43. p += stack::push(L, i);
  44. }
  45. return p;
  46. }
  47. };
  48. } // namespace stack
  49. } // namespace sol
  50. #endif // SOL_AS_RETURNS_HPP