/**************************************************************************/ /* tuple.h */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #ifndef TUPLE_H #define TUPLE_H // Simple recursive Tuple type that has no runtime overhead. // // The compile-time recursion works as follows: // Assume the following: Tuple my_tuple(42, 3.14f); // This expands to a class hierarchy that inherits from the previous step. // So in this case this leads to: // - struct Tuple : Tuple <--- This contains the int value. // - struct Tuple <--- This contains the float value. // where each of the classes has a single field of the type for that step in the // recursion. So: float value; int value; etc. // // This works by splitting up the parameter pack for each step in the recursion minus the first. // so the the first step creates the "T value" from the first template parameter. // any further template arguments end up in "Rest", which we then use to instantiate a new // tuple, but now minus the first argument. To write this all out: // // Tuple // step 1: Tuple T = int, Rest = float. Results in a Tuple : Tuple // step 2: Tuple T = float, no Rest. Results in a Tuple // // tuple_get works through a similar recursion, using the inheritance chain to walk to the right node. // In order to tuple_get<1>(my_tuple), from the example tuple above: // // 1. We want tuple_get<1> to return the float, which is one level "up" from Tuple : Tuple, // (the real type of the Tuple "root"). // 2. Since index 1 > 0, it casts the tuple to its parent type (Tuple). This works because // we cast to Tuple which in this case is just float. // 3. Now we're looking for index 0 in Tuple, which directly returns its value field. Note // how get<0> is a template specialization. // // At compile time, this gets fully resolved. The compiler sees get<1>(my_tuple) and: // 1. Creates TupleGet<1, Tuple>::tuple_get which contains the cast to Tuple. // 2. Creates TupleGet<0, Tuple>::tuple_get which directly returns the value. // 3. The compiler will then simply optimize all of this nonsense away and return the float directly. #include "core/typedefs.h" template struct Tuple; template <> struct Tuple<> {}; template struct Tuple : Tuple { T value; Tuple() = default; template _FORCE_INLINE_ Tuple(F &&f, R &&...rest) : Tuple(std::forward(rest)...), value(std::forward(f)) {} }; template struct TupleGet; template struct TupleGet<0, Tuple> { _FORCE_INLINE_ static First &tuple_get(Tuple &t) { return t.value; } }; // Rationale for using auto here is that the alternative is writing a // helper struct to create an otherwise useless type. we would have to write // a second recursive template chain like: TupleGetType>::type // just to recover the type in the most baroque way possible. template struct TupleGet> { _FORCE_INLINE_ static auto &tuple_get(Tuple &t) { return TupleGet>::tuple_get(static_cast &>(t)); } }; template _FORCE_INLINE_ auto &tuple_get(Tuple &t) { return TupleGet>::tuple_get(t); } template _FORCE_INLINE_ const auto &tuple_get(const Tuple &t) { return TupleGet>::tuple_get(t); } #endif // TUPLE_H