annotate win64-msvc/include/kj/tuple.h @ 149:279b18cc7785

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