annotate osx/include/kj/tuple.h @ 68:85d5306e114e

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