annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/tuple.h @ 69:33d812a61356

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