annotate win32-mingw/include/kj/tuple.h @ 63:0f2d93caa50c

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