tuple.h 5.7 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. #ifndef TUPLE_H
  31. #define TUPLE_H
  32. // Simple recursive Tuple type that has no runtime overhead.
  33. //
  34. // The compile-time recursion works as follows:
  35. // Assume the following: Tuple<int, float> my_tuple(42, 3.14f);
  36. // This expands to a class hierarchy that inherits from the previous step.
  37. // So in this case this leads to:
  38. // - struct Tuple<int> : Tuple<float> <--- This contains the int value.
  39. // - struct Tuple<float> <--- This contains the float value.
  40. // where each of the classes has a single field of the type for that step in the
  41. // recursion. So: float value; int value; etc.
  42. //
  43. // This works by splitting up the parameter pack for each step in the recursion minus the first.
  44. // so the the first step creates the "T value" from the first template parameter.
  45. // any further template arguments end up in "Rest", which we then use to instantiate a new
  46. // tuple, but now minus the first argument. To write this all out:
  47. //
  48. // Tuple<int, float>
  49. // step 1: Tuple T = int, Rest = float. Results in a Tuple<int> : Tuple<float>
  50. // step 2: Tuple T = float, no Rest. Results in a Tuple<float>
  51. //
  52. // tuple_get<I> works through a similar recursion, using the inheritance chain to walk to the right node.
  53. // In order to tuple_get<1>(my_tuple), from the example tuple above:
  54. //
  55. // 1. We want tuple_get<1> to return the float, which is one level "up" from Tuple<int> : Tuple<float>,
  56. // (the real type of the Tuple "root").
  57. // 2. Since index 1 > 0, it casts the tuple to its parent type (Tuple<float>). This works because
  58. // we cast to Tuple<Rest...> which in this case is just float.
  59. // 3. Now we're looking for index 0 in Tuple<float>, which directly returns its value field. Note
  60. // how get<0> is a template specialization.
  61. //
  62. // At compile time, this gets fully resolved. The compiler sees get<1>(my_tuple) and:
  63. // 1. Creates TupleGet<1, Tuple<int, float>>::tuple_get which contains the cast to Tuple<float>.
  64. // 2. Creates TupleGet<0, Tuple<float>>::tuple_get which directly returns the value.
  65. // 3. The compiler will then simply optimize all of this nonsense away and return the float directly.
  66. #include "core/typedefs.h"
  67. template <typename... Types>
  68. struct Tuple;
  69. template <>
  70. struct Tuple<> {};
  71. template <typename T, typename... Rest>
  72. struct Tuple<T, Rest...> : Tuple<Rest...> {
  73. T value;
  74. Tuple() = default;
  75. template <typename F, typename... R>
  76. _FORCE_INLINE_ Tuple(F &&f, R &&...rest) :
  77. Tuple<Rest...>(std::forward<R>(rest)...),
  78. value(std::forward<F>(f)) {}
  79. };
  80. template <size_t I, typename Tuple>
  81. struct TupleGet;
  82. template <typename First, typename... Rest>
  83. struct TupleGet<0, Tuple<First, Rest...>> {
  84. _FORCE_INLINE_ static First &tuple_get(Tuple<First, Rest...> &t) {
  85. return t.value;
  86. }
  87. };
  88. // Rationale for using auto here is that the alternative is writing a
  89. // helper struct to create an otherwise useless type. we would have to write
  90. // a second recursive template chain like: TupleGetType<I, Tuple<First, Rest...>>::type
  91. // just to recover the type in the most baroque way possible.
  92. template <size_t I, typename First, typename... Rest>
  93. struct TupleGet<I, Tuple<First, Rest...>> {
  94. _FORCE_INLINE_ static auto &tuple_get(Tuple<First, Rest...> &t) {
  95. return TupleGet<I - 1, Tuple<Rest...>>::tuple_get(static_cast<Tuple<Rest...> &>(t));
  96. }
  97. };
  98. template <size_t I, typename... Types>
  99. _FORCE_INLINE_ auto &tuple_get(Tuple<Types...> &t) {
  100. return TupleGet<I, Tuple<Types...>>::tuple_get(t);
  101. }
  102. template <size_t I, typename... Types>
  103. _FORCE_INLINE_ const auto &tuple_get(const Tuple<Types...> &t) {
  104. return TupleGet<I, Tuple<Types...>>::tuple_get(t);
  105. }
  106. #endif // TUPLE_H