tie.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_TIE_HPP
  19. #define SOL_TIE_HPP
  20. #include <sol/traits.hpp>
  21. namespace sol {
  22. namespace detail {
  23. template <typename T>
  24. struct is_speshul : std::false_type { };
  25. } // namespace detail
  26. template <typename T>
  27. struct tie_size : std::tuple_size<T> { };
  28. template <typename T>
  29. struct is_tieable : std::integral_constant<bool, (::sol::tie_size<T>::value > 0)> { };
  30. template <typename... Tn>
  31. struct tie_t : public std::tuple<std::add_lvalue_reference_t<Tn>...> {
  32. private:
  33. typedef std::tuple<std::add_lvalue_reference_t<Tn>...> base_t;
  34. template <typename T>
  35. void set(std::false_type, T&& target) {
  36. std::get<0>(*this) = std::forward<T>(target);
  37. }
  38. template <typename T>
  39. void set(std::true_type, T&& target) {
  40. typedef tie_size<meta::unqualified_t<T>> value_size;
  41. typedef tie_size<std::tuple<Tn...>> tie_size;
  42. typedef meta::conditional_t<(value_size::value < tie_size::value), value_size, tie_size> indices_size;
  43. typedef std::make_index_sequence<indices_size::value> indices;
  44. set_extra(detail::is_speshul<meta::unqualified_t<T>>(), indices(), std::forward<T>(target));
  45. }
  46. template <std::size_t... I, typename T>
  47. void set_extra(std::true_type, std::index_sequence<I...>, T&& target) {
  48. using std::get;
  49. (void)detail::swallow { 0, (get<I>(static_cast<base_t&>(*this)) = get<I>(types<Tn...>(), target), 0)..., 0 };
  50. }
  51. template <std::size_t... I, typename T>
  52. void set_extra(std::false_type, std::index_sequence<I...>, T&& target) {
  53. using std::get;
  54. (void)detail::swallow { 0, (get<I>(static_cast<base_t&>(*this)) = get<I>(target), 0)..., 0 };
  55. }
  56. public:
  57. using base_t::base_t;
  58. template <typename T>
  59. tie_t& operator=(T&& value) {
  60. typedef is_tieable<meta::unqualified_t<T>> tieable;
  61. set(tieable(), std::forward<T>(value));
  62. return *this;
  63. }
  64. };
  65. template <typename... Tn>
  66. struct tie_size<tie_t<Tn...>> : std::tuple_size<std::tuple<Tn...>> { };
  67. namespace adl_barrier_detail {
  68. template <typename... Tn>
  69. inline tie_t<std::remove_reference_t<Tn>...> tie(Tn&&... argn) {
  70. return tie_t<std::remove_reference_t<Tn>...>(std::forward<Tn>(argn)...);
  71. }
  72. } // namespace adl_barrier_detail
  73. using namespace adl_barrier_detail;
  74. } // namespace sol
  75. #endif // SOL_TIE_HPP