annotate win64-msvc/include/kj/tuple.h @ 84:08ae793730bd

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