tuple.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**************************************************************************/
  2. /* tuple.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. // Simple recursive Tuple type that has no runtime overhead.
  32. //
  33. // The compile-time recursion works as follows:
  34. // Assume the following: Tuple<int, float> my_tuple(42, 3.14f);
  35. // This expands to a class hierarchy that inherits from the previous step.
  36. // So in this case this leads to:
  37. // - struct Tuple<int> : Tuple<float> <--- This contains the int value.
  38. // - struct Tuple<float> <--- This contains the float value.
  39. // where each of the classes has a single field of the type for that step in the
  40. // recursion. So: float value; int value; etc.
  41. //
  42. // This works by splitting up the parameter pack for each step in the recursion minus the first.
  43. // so the first step creates the "T value" from the first template parameter.
  44. // any further template arguments end up in "Rest", which we then use to instantiate a new
  45. // tuple, but now minus the first argument. To write this all out:
  46. //
  47. // Tuple<int, float>
  48. // step 1: Tuple T = int, Rest = float. Results in a Tuple<int> : Tuple<float>
  49. // step 2: Tuple T = float, no Rest. Results in a Tuple<float>
  50. //
  51. // tuple_get<I> works through a similar recursion, using the inheritance chain to walk to the right node.
  52. // In order to tuple_get<1>(my_tuple), from the example tuple above:
  53. //
  54. // 1. We want tuple_get<1> to return the float, which is one level "up" from Tuple<int> : Tuple<float>,
  55. // (the real type of the Tuple "root").
  56. // 2. Since index 1 > 0, it casts the tuple to its parent type (Tuple<float>). This works because
  57. // we cast to Tuple<Rest...> which in this case is just float.
  58. // 3. Now we're looking for index 0 in Tuple<float>, which directly returns its value field. Note
  59. // how get<0> is a template specialization.
  60. //
  61. // At compile time, this gets fully resolved. The compiler sees get<1>(my_tuple) and:
  62. // 1. Creates TupleGet<1, Tuple<int, float>>::tuple_get which contains the cast to Tuple<float>.
  63. // 2. Creates TupleGet<0, Tuple<float>>::tuple_get which directly returns the value.
  64. // 3. The compiler will then simply optimize all of this nonsense away and return the float directly.
  65. #include "core/typedefs.h"
  66. template <typename... Types>
  67. struct Tuple;
  68. template <>
  69. struct Tuple<> {};
  70. template <typename T, typename... Rest>
  71. struct Tuple<T, Rest...> : Tuple<Rest...> {
  72. T value;
  73. Tuple() = default;
  74. template <typename F, typename... R>
  75. _FORCE_INLINE_ Tuple(F &&f, R &&...rest) :
  76. Tuple<Rest...>(std::forward<R>(rest)...),
  77. value(std::forward<F>(f)) {}
  78. };
  79. // Tuple is zero-constructible if and only if all constrained types are zero-constructible.
  80. template <typename... Types>
  81. struct is_zero_constructible<Tuple<Types...>> : std::conjunction<is_zero_constructible<Types>...> {};
  82. template <size_t I, typename Tuple>
  83. struct TupleGet;
  84. template <typename First, typename... Rest>
  85. struct TupleGet<0, Tuple<First, Rest...>> {
  86. _FORCE_INLINE_ static First &tuple_get(Tuple<First, Rest...> &t) {
  87. return t.value;
  88. }
  89. };
  90. // Rationale for using auto here is that the alternative is writing a
  91. // helper struct to create an otherwise useless type. we would have to write
  92. // a second recursive template chain like: TupleGetType<I, Tuple<First, Rest...>>::type
  93. // just to recover the type in the most baroque way possible.
  94. template <size_t I, typename First, typename... Rest>
  95. struct TupleGet<I, Tuple<First, Rest...>> {
  96. _FORCE_INLINE_ static auto &tuple_get(Tuple<First, Rest...> &t) {
  97. return TupleGet<I - 1, Tuple<Rest...>>::tuple_get(static_cast<Tuple<Rest...> &>(t));
  98. }
  99. };
  100. template <size_t I, typename... Types>
  101. _FORCE_INLINE_ auto &tuple_get(Tuple<Types...> &t) {
  102. return TupleGet<I, Tuple<Types...>>::tuple_get(t);
  103. }
  104. template <size_t I, typename... Types>
  105. _FORCE_INLINE_ const auto &tuple_get(const Tuple<Types...> &t) {
  106. return TupleGet<I, Tuple<Types...>>::tuple_get(t);
  107. }