annotate win64-msvc/include/kj/tuple.h @ 62:0994c39f1e94

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