annotate win32-mingw/include/kj/tuple.h @ 163:5cc1366da2e9

Apply patch from Tim Bunnell on PortAudio mailing list (2016-12-28, Mac 10.11 deprecation warning)
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 30 Oct 2019 11:28:45 +0000
parents 279b18cc7785
children
rev   line source
cannam@149 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@149 2 // Licensed under the MIT License:
cannam@149 3 //
cannam@149 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@149 5 // of this software and associated documentation files (the "Software"), to deal
cannam@149 6 // in the Software without restriction, including without limitation the rights
cannam@149 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@149 8 // copies of the Software, and to permit persons to whom the Software is
cannam@149 9 // furnished to do so, subject to the following conditions:
cannam@149 10 //
cannam@149 11 // The above copyright notice and this permission notice shall be included in
cannam@149 12 // all copies or substantial portions of the Software.
cannam@149 13 //
cannam@149 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@149 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@149 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@149 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@149 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@149 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@149 20 // THE SOFTWARE.
cannam@149 21
cannam@149 22 // This file defines a notion of tuples that is simpler that `std::tuple`. It works as follows:
cannam@149 23 // - `kj::Tuple<A, B, C> is the type of a tuple of an A, a B, and a C.
cannam@149 24 // - `kj::tuple(a, b, c)` returns a tuple containing a, b, and c. If any of these are themselves
cannam@149 25 // tuples, they are flattened, so `tuple(a, tuple(b, c), d)` is equivalent to `tuple(a, b, c, d)`.
cannam@149 26 // - `kj::get<n>(myTuple)` returns the element of `myTuple` at index n.
cannam@149 27 // - `kj::apply(func, ...)` calls func on the following arguments after first expanding any tuples
cannam@149 28 // in the argument list. So `kj::apply(foo, a, tuple(b, c), d)` would call `foo(a, b, c, d)`.
cannam@149 29 //
cannam@149 30 // Note that:
cannam@149 31 // - The type `Tuple<T>` is a synonym for T. This is why `get` and `apply` are not members of the
cannam@149 32 // type.
cannam@149 33 // - It is illegal for an element of `Tuple` to itself be a tuple, as tuples are meant to be
cannam@149 34 // flattened.
cannam@149 35 // - It is illegal for an element of `Tuple` to be a reference, due to problems this would cause
cannam@149 36 // with type inference and `tuple()`.
cannam@149 37
cannam@149 38 #ifndef KJ_TUPLE_H_
cannam@149 39 #define KJ_TUPLE_H_
cannam@149 40
cannam@149 41 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@149 42 #pragma GCC system_header
cannam@149 43 #endif
cannam@149 44
cannam@149 45 #include "common.h"
cannam@149 46
cannam@149 47 namespace kj {
cannam@149 48 namespace _ { // private
cannam@149 49
cannam@149 50 template <size_t index, typename... T>
cannam@149 51 struct TypeByIndex_;
cannam@149 52 template <typename First, typename... Rest>
cannam@149 53 struct TypeByIndex_<0, First, Rest...> {
cannam@149 54 typedef First Type;
cannam@149 55 };
cannam@149 56 template <size_t index, typename First, typename... Rest>
cannam@149 57 struct TypeByIndex_<index, First, Rest...>
cannam@149 58 : public TypeByIndex_<index - 1, Rest...> {};
cannam@149 59 template <size_t index>
cannam@149 60 struct TypeByIndex_<index> {
cannam@149 61 static_assert(index != index, "Index out-of-range.");
cannam@149 62 };
cannam@149 63 template <size_t index, typename... T>
cannam@149 64 using TypeByIndex = typename TypeByIndex_<index, T...>::Type;
cannam@149 65 // Chose a particular type out of a list of types, by index.
cannam@149 66
cannam@149 67 template <size_t... s>
cannam@149 68 struct Indexes {};
cannam@149 69 // Dummy helper type that just encapsulates a sequential list of indexes, so that we can match
cannam@149 70 // templates against them and unpack them with '...'.
cannam@149 71
cannam@149 72 template <size_t end, size_t... prefix>
cannam@149 73 struct MakeIndexes_: public MakeIndexes_<end - 1, end - 1, prefix...> {};
cannam@149 74 template <size_t... prefix>
cannam@149 75 struct MakeIndexes_<0, prefix...> {
cannam@149 76 typedef Indexes<prefix...> Type;
cannam@149 77 };
cannam@149 78 template <size_t end>
cannam@149 79 using MakeIndexes = typename MakeIndexes_<end>::Type;
cannam@149 80 // Equivalent to Indexes<0, 1, 2, ..., end>.
cannam@149 81
cannam@149 82 template <typename... T>
cannam@149 83 class Tuple;
cannam@149 84 template <size_t index, typename... U>
cannam@149 85 inline TypeByIndex<index, U...>& getImpl(Tuple<U...>& tuple);
cannam@149 86 template <size_t index, typename... U>
cannam@149 87 inline TypeByIndex<index, U...>&& getImpl(Tuple<U...>&& tuple);
cannam@149 88 template <size_t index, typename... U>
cannam@149 89 inline const TypeByIndex<index, U...>& getImpl(const Tuple<U...>& tuple);
cannam@149 90
cannam@149 91 template <uint index, typename T>
cannam@149 92 struct TupleElement {
cannam@149 93 // Encapsulates one element of a tuple. The actual tuple implementation multiply-inherits
cannam@149 94 // from a TupleElement for each element, which is more efficient than a recursive definition.
cannam@149 95
cannam@149 96 T value;
cannam@149 97 TupleElement() = default;
cannam@149 98 constexpr inline TupleElement(const T& value): value(value) {}
cannam@149 99 constexpr inline TupleElement(T&& value): value(kj::mv(value)) {}
cannam@149 100 };
cannam@149 101
cannam@149 102 template <uint index, typename T>
cannam@149 103 struct TupleElement<index, T&> {
cannam@149 104 // If tuples contained references, one of the following would have to be true:
cannam@149 105 // - `auto x = tuple(y, z)` would cause x to be a tuple of references to y and z, which is
cannam@149 106 // probably not what you expected.
cannam@149 107 // - `Tuple<Foo&, Bar&> x = tuple(a, b)` would not work, because `tuple()` returned
cannam@149 108 // Tuple<Foo, Bar>.
cannam@149 109 static_assert(sizeof(T*) == 0, "Sorry, tuples cannot contain references.");
cannam@149 110 };
cannam@149 111
cannam@149 112 template <uint index, typename... T>
cannam@149 113 struct TupleElement<index, Tuple<T...>> {
cannam@149 114 static_assert(sizeof(Tuple<T...>*) == 0,
cannam@149 115 "Tuples cannot contain other tuples -- they should be flattened.");
cannam@149 116 };
cannam@149 117
cannam@149 118 template <typename Indexes, typename... Types>
cannam@149 119 struct TupleImpl;
cannam@149 120
cannam@149 121 template <size_t... indexes, typename... Types>
cannam@149 122 struct TupleImpl<Indexes<indexes...>, Types...>
cannam@149 123 : public TupleElement<indexes, Types>... {
cannam@149 124 // Implementation of Tuple. The only reason we need this rather than rolling this into class
cannam@149 125 // Tuple (below) is so that we can get "indexes" as an unpackable list.
cannam@149 126
cannam@149 127 static_assert(sizeof...(indexes) == sizeof...(Types), "Incorrect use of TupleImpl.");
cannam@149 128
cannam@149 129 template <typename... Params>
cannam@149 130 inline TupleImpl(Params&&... params)
cannam@149 131 : TupleElement<indexes, Types>(kj::fwd<Params>(params))... {
cannam@149 132 // Work around Clang 3.2 bug 16303 where this is not detected. (Unfortunately, Clang sometimes
cannam@149 133 // segfaults instead.)
cannam@149 134 static_assert(sizeof...(params) == sizeof...(indexes),
cannam@149 135 "Wrong number of parameters to Tuple constructor.");
cannam@149 136 }
cannam@149 137
cannam@149 138 template <typename... U>
cannam@149 139 constexpr inline TupleImpl(Tuple<U...>&& other)
cannam@149 140 : TupleElement<indexes, Types>(kj::mv(getImpl<indexes>(other)))... {}
cannam@149 141 template <typename... U>
cannam@149 142 constexpr inline TupleImpl(Tuple<U...>& other)
cannam@149 143 : TupleElement<indexes, Types>(getImpl<indexes>(other))... {}
cannam@149 144 template <typename... U>
cannam@149 145 constexpr inline TupleImpl(const Tuple<U...>& other)
cannam@149 146 : TupleElement<indexes, Types>(getImpl<indexes>(other))... {}
cannam@149 147 };
cannam@149 148
cannam@149 149 struct MakeTupleFunc;
cannam@149 150
cannam@149 151 template <typename... T>
cannam@149 152 class Tuple {
cannam@149 153 // The actual Tuple class (used for tuples of size other than 1).
cannam@149 154
cannam@149 155 public:
cannam@149 156 template <typename... U>
cannam@149 157 constexpr inline Tuple(Tuple<U...>&& other): impl(kj::mv(other)) {}
cannam@149 158 template <typename... U>
cannam@149 159 constexpr inline Tuple(Tuple<U...>& other): impl(other) {}
cannam@149 160 template <typename... U>
cannam@149 161 constexpr inline Tuple(const Tuple<U...>& other): impl(other) {}
cannam@149 162
cannam@149 163 private:
cannam@149 164 template <typename... Params>
cannam@149 165 constexpr Tuple(Params&&... params): impl(kj::fwd<Params>(params)...) {}
cannam@149 166
cannam@149 167 TupleImpl<MakeIndexes<sizeof...(T)>, T...> impl;
cannam@149 168
cannam@149 169 template <size_t index, typename... U>
cannam@149 170 friend inline TypeByIndex<index, U...>& getImpl(Tuple<U...>& tuple);
cannam@149 171 template <size_t index, typename... U>
cannam@149 172 friend inline TypeByIndex<index, U...>&& getImpl(Tuple<U...>&& tuple);
cannam@149 173 template <size_t index, typename... U>
cannam@149 174 friend inline const TypeByIndex<index, U...>& getImpl(const Tuple<U...>& tuple);
cannam@149 175 friend struct MakeTupleFunc;
cannam@149 176 };
cannam@149 177
cannam@149 178 template <>
cannam@149 179 class Tuple<> {
cannam@149 180 // Simplified zero-member version of Tuple. In particular this is important to make sure that
cannam@149 181 // Tuple<>() is constexpr.
cannam@149 182 };
cannam@149 183
cannam@149 184 template <typename T>
cannam@149 185 class Tuple<T>;
cannam@149 186 // Single-element tuple should never be used. The public API should ensure this.
cannam@149 187
cannam@149 188 template <size_t index, typename... T>
cannam@149 189 inline TypeByIndex<index, T...>& getImpl(Tuple<T...>& tuple) {
cannam@149 190 // Get member of a Tuple by index, e.g. `get<2>(myTuple)`.
cannam@149 191 static_assert(index < sizeof...(T), "Tuple element index out-of-bounds.");
cannam@149 192 return implicitCast<TupleElement<index, TypeByIndex<index, T...>>&>(tuple.impl).value;
cannam@149 193 }
cannam@149 194 template <size_t index, typename... T>
cannam@149 195 inline TypeByIndex<index, T...>&& getImpl(Tuple<T...>&& tuple) {
cannam@149 196 // Get member of a Tuple by index, e.g. `get<2>(myTuple)`.
cannam@149 197 static_assert(index < sizeof...(T), "Tuple element index out-of-bounds.");
cannam@149 198 return kj::mv(implicitCast<TupleElement<index, TypeByIndex<index, T...>>&>(tuple.impl).value);
cannam@149 199 }
cannam@149 200 template <size_t index, typename... T>
cannam@149 201 inline const TypeByIndex<index, T...>& getImpl(const Tuple<T...>& tuple) {
cannam@149 202 // Get member of a Tuple by index, e.g. `get<2>(myTuple)`.
cannam@149 203 static_assert(index < sizeof...(T), "Tuple element index out-of-bounds.");
cannam@149 204 return implicitCast<const TupleElement<index, TypeByIndex<index, T...>>&>(tuple.impl).value;
cannam@149 205 }
cannam@149 206 template <size_t index, typename T>
cannam@149 207 inline T&& getImpl(T&& value) {
cannam@149 208 // Get member of a Tuple by index, e.g. `getImpl<2>(myTuple)`.
cannam@149 209
cannam@149 210 // Non-tuples are equivalent to one-element tuples.
cannam@149 211 static_assert(index == 0, "Tuple element index out-of-bounds.");
cannam@149 212 return kj::fwd<T>(value);
cannam@149 213 }
cannam@149 214
cannam@149 215
cannam@149 216 template <typename Func, typename SoFar, typename... T>
cannam@149 217 struct ExpandAndApplyResult_;
cannam@149 218 // Template which computes the return type of applying Func to T... after flattening tuples.
cannam@149 219 // SoFar starts as Tuple<> and accumulates the flattened parameter types -- so after this template
cannam@149 220 // is recursively expanded, T... is empty and SoFar is a Tuple containing all the parameters.
cannam@149 221
cannam@149 222 template <typename Func, typename First, typename... Rest, typename... T>
cannam@149 223 struct ExpandAndApplyResult_<Func, Tuple<T...>, First, Rest...>
cannam@149 224 : public ExpandAndApplyResult_<Func, Tuple<T..., First>, Rest...> {};
cannam@149 225 template <typename Func, typename... FirstTypes, typename... Rest, typename... T>
cannam@149 226 struct ExpandAndApplyResult_<Func, Tuple<T...>, Tuple<FirstTypes...>, Rest...>
cannam@149 227 : public ExpandAndApplyResult_<Func, Tuple<T...>, FirstTypes&&..., Rest...> {};
cannam@149 228 template <typename Func, typename... FirstTypes, typename... Rest, typename... T>
cannam@149 229 struct ExpandAndApplyResult_<Func, Tuple<T...>, Tuple<FirstTypes...>&, Rest...>
cannam@149 230 : public ExpandAndApplyResult_<Func, Tuple<T...>, FirstTypes&..., Rest...> {};
cannam@149 231 template <typename Func, typename... FirstTypes, typename... Rest, typename... T>
cannam@149 232 struct ExpandAndApplyResult_<Func, Tuple<T...>, const Tuple<FirstTypes...>&, Rest...>
cannam@149 233 : public ExpandAndApplyResult_<Func, Tuple<T...>, const FirstTypes&..., Rest...> {};
cannam@149 234 template <typename Func, typename... T>
cannam@149 235 struct ExpandAndApplyResult_<Func, Tuple<T...>> {
cannam@149 236 typedef decltype(instance<Func>()(instance<T&&>()...)) Type;
cannam@149 237 };
cannam@149 238 template <typename Func, typename... T>
cannam@149 239 using ExpandAndApplyResult = typename ExpandAndApplyResult_<Func, Tuple<>, T...>::Type;
cannam@149 240 // Computes the expected return type of `expandAndApply()`.
cannam@149 241
cannam@149 242 template <typename Func>
cannam@149 243 inline auto expandAndApply(Func&& func) -> ExpandAndApplyResult<Func> {
cannam@149 244 return func();
cannam@149 245 }
cannam@149 246
cannam@149 247 template <typename Func, typename First, typename... Rest>
cannam@149 248 struct ExpandAndApplyFunc {
cannam@149 249 Func&& func;
cannam@149 250 First&& first;
cannam@149 251 ExpandAndApplyFunc(Func&& func, First&& first)
cannam@149 252 : func(kj::fwd<Func>(func)), first(kj::fwd<First>(first)) {}
cannam@149 253 template <typename... T>
cannam@149 254 auto operator()(T&&... params)
cannam@149 255 -> decltype(this->func(kj::fwd<First>(first), kj::fwd<T>(params)...)) {
cannam@149 256 return this->func(kj::fwd<First>(first), kj::fwd<T>(params)...);
cannam@149 257 }
cannam@149 258 };
cannam@149 259
cannam@149 260 template <typename Func, typename First, typename... Rest>
cannam@149 261 inline auto expandAndApply(Func&& func, First&& first, Rest&&... rest)
cannam@149 262 -> ExpandAndApplyResult<Func, First, Rest...> {
cannam@149 263
cannam@149 264 return expandAndApply(
cannam@149 265 ExpandAndApplyFunc<Func, First, Rest...>(kj::fwd<Func>(func), kj::fwd<First>(first)),
cannam@149 266 kj::fwd<Rest>(rest)...);
cannam@149 267 }
cannam@149 268
cannam@149 269 template <typename Func, typename... FirstTypes, typename... Rest>
cannam@149 270 inline auto expandAndApply(Func&& func, Tuple<FirstTypes...>&& first, Rest&&... rest)
cannam@149 271 -> ExpandAndApplyResult<Func, FirstTypes&&..., Rest...> {
cannam@149 272 return expandAndApplyWithIndexes(MakeIndexes<sizeof...(FirstTypes)>(),
cannam@149 273 kj::fwd<Func>(func), kj::mv(first), kj::fwd<Rest>(rest)...);
cannam@149 274 }
cannam@149 275
cannam@149 276 template <typename Func, typename... FirstTypes, typename... Rest>
cannam@149 277 inline auto expandAndApply(Func&& func, Tuple<FirstTypes...>& first, Rest&&... rest)
cannam@149 278 -> ExpandAndApplyResult<Func, FirstTypes..., Rest...> {
cannam@149 279 return expandAndApplyWithIndexes(MakeIndexes<sizeof...(FirstTypes)>(),
cannam@149 280 kj::fwd<Func>(func), first, kj::fwd<Rest>(rest)...);
cannam@149 281 }
cannam@149 282
cannam@149 283 template <typename Func, typename... FirstTypes, typename... Rest>
cannam@149 284 inline auto expandAndApply(Func&& func, const Tuple<FirstTypes...>& first, Rest&&... rest)
cannam@149 285 -> ExpandAndApplyResult<Func, FirstTypes..., Rest...> {
cannam@149 286 return expandAndApplyWithIndexes(MakeIndexes<sizeof...(FirstTypes)>(),
cannam@149 287 kj::fwd<Func>(func), first, kj::fwd<Rest>(rest)...);
cannam@149 288 }
cannam@149 289
cannam@149 290 template <typename Func, typename... FirstTypes, typename... Rest, size_t... indexes>
cannam@149 291 inline auto expandAndApplyWithIndexes(
cannam@149 292 Indexes<indexes...>, Func&& func, Tuple<FirstTypes...>&& first, Rest&&... rest)
cannam@149 293 -> ExpandAndApplyResult<Func, FirstTypes&&..., Rest...> {
cannam@149 294 return expandAndApply(kj::fwd<Func>(func), kj::mv(getImpl<indexes>(first))...,
cannam@149 295 kj::fwd<Rest>(rest)...);
cannam@149 296 }
cannam@149 297
cannam@149 298 template <typename Func, typename... FirstTypes, typename... Rest, size_t... indexes>
cannam@149 299 inline auto expandAndApplyWithIndexes(
cannam@149 300 Indexes<indexes...>, Func&& func, const Tuple<FirstTypes...>& first, Rest&&... rest)
cannam@149 301 -> ExpandAndApplyResult<Func, FirstTypes..., Rest...> {
cannam@149 302 return expandAndApply(kj::fwd<Func>(func), getImpl<indexes>(first)...,
cannam@149 303 kj::fwd<Rest>(rest)...);
cannam@149 304 }
cannam@149 305
cannam@149 306 struct MakeTupleFunc {
cannam@149 307 template <typename... Params>
cannam@149 308 Tuple<Decay<Params>...> operator()(Params&&... params) {
cannam@149 309 return Tuple<Decay<Params>...>(kj::fwd<Params>(params)...);
cannam@149 310 }
cannam@149 311 template <typename Param>
cannam@149 312 Decay<Param> operator()(Param&& param) {
cannam@149 313 return kj::fwd<Param>(param);
cannam@149 314 }
cannam@149 315 };
cannam@149 316
cannam@149 317 } // namespace _ (private)
cannam@149 318
cannam@149 319 template <typename... T> struct Tuple_ { typedef _::Tuple<T...> Type; };
cannam@149 320 template <typename T> struct Tuple_<T> { typedef T Type; };
cannam@149 321
cannam@149 322 template <typename... T> using Tuple = typename Tuple_<T...>::Type;
cannam@149 323 // Tuple type. `Tuple<T>` (i.e. a single-element tuple) is a synonym for `T`. Tuples of size
cannam@149 324 // other than 1 expand to an internal type. Either way, you can construct a Tuple using
cannam@149 325 // `kj::tuple(...)`, get an element by index `i` using `kj::get<i>(myTuple)`, and expand the tuple
cannam@149 326 // as arguments to a function using `kj::apply(func, myTuple)`.
cannam@149 327 //
cannam@149 328 // Tuples are always flat -- that is, no element of a Tuple is ever itself a Tuple. If you
cannam@149 329 // construct a tuple from other tuples, the elements are flattened and concatenated.
cannam@149 330
cannam@149 331 template <typename... Params>
cannam@149 332 inline auto tuple(Params&&... params)
cannam@149 333 -> decltype(_::expandAndApply(_::MakeTupleFunc(), kj::fwd<Params>(params)...)) {
cannam@149 334 // Construct a new tuple from the given values. Any tuples in the argument list will be
cannam@149 335 // flattened into the result.
cannam@149 336 return _::expandAndApply(_::MakeTupleFunc(), kj::fwd<Params>(params)...);
cannam@149 337 }
cannam@149 338
cannam@149 339 template <size_t index, typename Tuple>
cannam@149 340 inline auto get(Tuple&& tuple) -> decltype(_::getImpl<index>(kj::fwd<Tuple>(tuple))) {
cannam@149 341 // Unpack and return the tuple element at the given index. The index is specified as a template
cannam@149 342 // parameter, e.g. `kj::get<3>(myTuple)`.
cannam@149 343 return _::getImpl<index>(kj::fwd<Tuple>(tuple));
cannam@149 344 }
cannam@149 345
cannam@149 346 template <typename Func, typename... Params>
cannam@149 347 inline auto apply(Func&& func, Params&&... params)
cannam@149 348 -> decltype(_::expandAndApply(kj::fwd<Func>(func), kj::fwd<Params>(params)...)) {
cannam@149 349 // Apply a function to some arguments, expanding tuples into separate arguments.
cannam@149 350 return _::expandAndApply(kj::fwd<Func>(func), kj::fwd<Params>(params)...);
cannam@149 351 }
cannam@149 352
cannam@149 353 template <typename T> struct TupleSize_ { static constexpr size_t size = 1; };
cannam@149 354 template <typename... T> struct TupleSize_<_::Tuple<T...>> {
cannam@149 355 static constexpr size_t size = sizeof...(T);
cannam@149 356 };
cannam@149 357
cannam@149 358 template <typename T>
cannam@149 359 constexpr size_t tupleSize() { return TupleSize_<T>::size; }
cannam@149 360 // Returns size of the tuple T.
cannam@149 361
cannam@149 362 } // namespace kj
cannam@149 363
cannam@149 364 #endif // KJ_TUPLE_H_