Debian Codegraph @

core/templates/tuple.h

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