Chris@64: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: // This file defines a notion of tuples that is simpler that `std::tuple`. It works as follows: Chris@64: // - `kj::Tuple is the type of a tuple of an A, a B, and a C. Chris@64: // - `kj::tuple(a, b, c)` returns a tuple containing a, b, and c. If any of these are themselves Chris@64: // tuples, they are flattened, so `tuple(a, tuple(b, c), d)` is equivalent to `tuple(a, b, c, d)`. Chris@64: // - `kj::get(myTuple)` returns the element of `myTuple` at index n. Chris@64: // - `kj::apply(func, ...)` calls func on the following arguments after first expanding any tuples Chris@64: // in the argument list. So `kj::apply(foo, a, tuple(b, c), d)` would call `foo(a, b, c, d)`. Chris@64: // Chris@64: // Note that: Chris@64: // - The type `Tuple` is a synonym for T. This is why `get` and `apply` are not members of the Chris@64: // type. Chris@64: // - It is illegal for an element of `Tuple` to itself be a tuple, as tuples are meant to be Chris@64: // flattened. Chris@64: // - It is illegal for an element of `Tuple` to be a reference, due to problems this would cause Chris@64: // with type inference and `tuple()`. Chris@64: Chris@64: #ifndef KJ_TUPLE_H_ Chris@64: #define KJ_TUPLE_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: #include "common.h" Chris@64: Chris@64: namespace kj { Chris@64: namespace _ { // private Chris@64: Chris@64: template Chris@64: struct TypeByIndex_; Chris@64: template Chris@64: struct TypeByIndex_<0, First, Rest...> { Chris@64: typedef First Type; Chris@64: }; Chris@64: template Chris@64: struct TypeByIndex_ Chris@64: : public TypeByIndex_ {}; Chris@64: template Chris@64: struct TypeByIndex_ { Chris@64: static_assert(index != index, "Index out-of-range."); Chris@64: }; Chris@64: template Chris@64: using TypeByIndex = typename TypeByIndex_::Type; Chris@64: // Chose a particular type out of a list of types, by index. Chris@64: Chris@64: template Chris@64: struct Indexes {}; Chris@64: // Dummy helper type that just encapsulates a sequential list of indexes, so that we can match Chris@64: // templates against them and unpack them with '...'. Chris@64: Chris@64: template Chris@64: struct MakeIndexes_: public MakeIndexes_ {}; Chris@64: template Chris@64: struct MakeIndexes_<0, prefix...> { Chris@64: typedef Indexes Type; Chris@64: }; Chris@64: template Chris@64: using MakeIndexes = typename MakeIndexes_::Type; Chris@64: // Equivalent to Indexes<0, 1, 2, ..., end>. Chris@64: Chris@64: template Chris@64: class Tuple; Chris@64: template Chris@64: inline TypeByIndex& getImpl(Tuple& tuple); Chris@64: template Chris@64: inline TypeByIndex&& getImpl(Tuple&& tuple); Chris@64: template Chris@64: inline const TypeByIndex& getImpl(const Tuple& tuple); Chris@64: Chris@64: template Chris@64: struct TupleElement { Chris@64: // Encapsulates one element of a tuple. The actual tuple implementation multiply-inherits Chris@64: // from a TupleElement for each element, which is more efficient than a recursive definition. Chris@64: Chris@64: T value; Chris@64: TupleElement() = default; Chris@64: constexpr inline TupleElement(const T& value): value(value) {} Chris@64: constexpr inline TupleElement(T&& value): value(kj::mv(value)) {} Chris@64: }; Chris@64: Chris@64: template Chris@64: struct TupleElement { Chris@64: // If tuples contained references, one of the following would have to be true: Chris@64: // - `auto x = tuple(y, z)` would cause x to be a tuple of references to y and z, which is Chris@64: // probably not what you expected. Chris@64: // - `Tuple x = tuple(a, b)` would not work, because `tuple()` returned Chris@64: // Tuple. Chris@64: static_assert(sizeof(T*) == 0, "Sorry, tuples cannot contain references."); Chris@64: }; Chris@64: Chris@64: template Chris@64: struct TupleElement> { Chris@64: static_assert(sizeof(Tuple*) == 0, Chris@64: "Tuples cannot contain other tuples -- they should be flattened."); Chris@64: }; Chris@64: Chris@64: template Chris@64: struct TupleImpl; Chris@64: Chris@64: template Chris@64: struct TupleImpl, Types...> Chris@64: : public TupleElement... { Chris@64: // Implementation of Tuple. The only reason we need this rather than rolling this into class Chris@64: // Tuple (below) is so that we can get "indexes" as an unpackable list. Chris@64: Chris@64: static_assert(sizeof...(indexes) == sizeof...(Types), "Incorrect use of TupleImpl."); Chris@64: Chris@64: template Chris@64: inline TupleImpl(Params&&... params) Chris@64: : TupleElement(kj::fwd(params))... { Chris@64: // Work around Clang 3.2 bug 16303 where this is not detected. (Unfortunately, Clang sometimes Chris@64: // segfaults instead.) Chris@64: static_assert(sizeof...(params) == sizeof...(indexes), Chris@64: "Wrong number of parameters to Tuple constructor."); Chris@64: } Chris@64: Chris@64: template Chris@64: constexpr inline TupleImpl(Tuple&& other) Chris@64: : TupleElement(kj::mv(getImpl(other)))... {} Chris@64: template Chris@64: constexpr inline TupleImpl(Tuple& other) Chris@64: : TupleElement(getImpl(other))... {} Chris@64: template Chris@64: constexpr inline TupleImpl(const Tuple& other) Chris@64: : TupleElement(getImpl(other))... {} Chris@64: }; Chris@64: Chris@64: struct MakeTupleFunc; Chris@64: Chris@64: template Chris@64: class Tuple { Chris@64: // The actual Tuple class (used for tuples of size other than 1). Chris@64: Chris@64: public: Chris@64: template Chris@64: constexpr inline Tuple(Tuple&& other): impl(kj::mv(other)) {} Chris@64: template Chris@64: constexpr inline Tuple(Tuple& other): impl(other) {} Chris@64: template Chris@64: constexpr inline Tuple(const Tuple& other): impl(other) {} Chris@64: Chris@64: private: Chris@64: template Chris@64: constexpr Tuple(Params&&... params): impl(kj::fwd(params)...) {} Chris@64: Chris@64: TupleImpl, T...> impl; Chris@64: Chris@64: template Chris@64: friend inline TypeByIndex& getImpl(Tuple& tuple); Chris@64: template Chris@64: friend inline TypeByIndex&& getImpl(Tuple&& tuple); Chris@64: template Chris@64: friend inline const TypeByIndex& getImpl(const Tuple& tuple); Chris@64: friend struct MakeTupleFunc; Chris@64: }; Chris@64: Chris@64: template <> Chris@64: class Tuple<> { Chris@64: // Simplified zero-member version of Tuple. In particular this is important to make sure that Chris@64: // Tuple<>() is constexpr. Chris@64: }; Chris@64: Chris@64: template Chris@64: class Tuple; Chris@64: // Single-element tuple should never be used. The public API should ensure this. Chris@64: Chris@64: template Chris@64: inline TypeByIndex& getImpl(Tuple& tuple) { Chris@64: // Get member of a Tuple by index, e.g. `get<2>(myTuple)`. Chris@64: static_assert(index < sizeof...(T), "Tuple element index out-of-bounds."); Chris@64: return implicitCast>&>(tuple.impl).value; Chris@64: } Chris@64: template Chris@64: inline TypeByIndex&& getImpl(Tuple&& tuple) { Chris@64: // Get member of a Tuple by index, e.g. `get<2>(myTuple)`. Chris@64: static_assert(index < sizeof...(T), "Tuple element index out-of-bounds."); Chris@64: return kj::mv(implicitCast>&>(tuple.impl).value); Chris@64: } Chris@64: template Chris@64: inline const TypeByIndex& getImpl(const Tuple& tuple) { Chris@64: // Get member of a Tuple by index, e.g. `get<2>(myTuple)`. Chris@64: static_assert(index < sizeof...(T), "Tuple element index out-of-bounds."); Chris@64: return implicitCast>&>(tuple.impl).value; Chris@64: } Chris@64: template Chris@64: inline T&& getImpl(T&& value) { Chris@64: // Get member of a Tuple by index, e.g. `getImpl<2>(myTuple)`. Chris@64: Chris@64: // Non-tuples are equivalent to one-element tuples. Chris@64: static_assert(index == 0, "Tuple element index out-of-bounds."); Chris@64: return kj::fwd(value); Chris@64: } Chris@64: Chris@64: Chris@64: template Chris@64: struct ExpandAndApplyResult_; Chris@64: // Template which computes the return type of applying Func to T... after flattening tuples. Chris@64: // SoFar starts as Tuple<> and accumulates the flattened parameter types -- so after this template Chris@64: // is recursively expanded, T... is empty and SoFar is a Tuple containing all the parameters. Chris@64: Chris@64: template Chris@64: struct ExpandAndApplyResult_, First, Rest...> Chris@64: : public ExpandAndApplyResult_, Rest...> {}; Chris@64: template Chris@64: struct ExpandAndApplyResult_, Tuple, Rest...> Chris@64: : public ExpandAndApplyResult_, FirstTypes&&..., Rest...> {}; Chris@64: template Chris@64: struct ExpandAndApplyResult_, Tuple&, Rest...> Chris@64: : public ExpandAndApplyResult_, FirstTypes&..., Rest...> {}; Chris@64: template Chris@64: struct ExpandAndApplyResult_, const Tuple&, Rest...> Chris@64: : public ExpandAndApplyResult_, const FirstTypes&..., Rest...> {}; Chris@64: template Chris@64: struct ExpandAndApplyResult_> { Chris@64: typedef decltype(instance()(instance()...)) Type; Chris@64: }; Chris@64: template Chris@64: using ExpandAndApplyResult = typename ExpandAndApplyResult_, T...>::Type; Chris@64: // Computes the expected return type of `expandAndApply()`. Chris@64: Chris@64: template Chris@64: inline auto expandAndApply(Func&& func) -> ExpandAndApplyResult { Chris@64: return func(); Chris@64: } Chris@64: Chris@64: template Chris@64: struct ExpandAndApplyFunc { Chris@64: Func&& func; Chris@64: First&& first; Chris@64: ExpandAndApplyFunc(Func&& func, First&& first) Chris@64: : func(kj::fwd(func)), first(kj::fwd(first)) {} Chris@64: template Chris@64: auto operator()(T&&... params) Chris@64: -> decltype(this->func(kj::fwd(first), kj::fwd(params)...)) { Chris@64: return this->func(kj::fwd(first), kj::fwd(params)...); Chris@64: } Chris@64: }; Chris@64: Chris@64: template Chris@64: inline auto expandAndApply(Func&& func, First&& first, Rest&&... rest) Chris@64: -> ExpandAndApplyResult { Chris@64: Chris@64: return expandAndApply( Chris@64: ExpandAndApplyFunc(kj::fwd(func), kj::fwd(first)), Chris@64: kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto expandAndApply(Func&& func, Tuple&& first, Rest&&... rest) Chris@64: -> ExpandAndApplyResult { Chris@64: return expandAndApplyWithIndexes(MakeIndexes(), Chris@64: kj::fwd(func), kj::mv(first), kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto expandAndApply(Func&& func, Tuple& first, Rest&&... rest) Chris@64: -> ExpandAndApplyResult { Chris@64: return expandAndApplyWithIndexes(MakeIndexes(), Chris@64: kj::fwd(func), first, kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto expandAndApply(Func&& func, const Tuple& first, Rest&&... rest) Chris@64: -> ExpandAndApplyResult { Chris@64: return expandAndApplyWithIndexes(MakeIndexes(), Chris@64: kj::fwd(func), first, kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto expandAndApplyWithIndexes( Chris@64: Indexes, Func&& func, Tuple&& first, Rest&&... rest) Chris@64: -> ExpandAndApplyResult { Chris@64: return expandAndApply(kj::fwd(func), kj::mv(getImpl(first))..., Chris@64: kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto expandAndApplyWithIndexes( Chris@64: Indexes, Func&& func, const Tuple& first, Rest&&... rest) Chris@64: -> ExpandAndApplyResult { Chris@64: return expandAndApply(kj::fwd(func), getImpl(first)..., Chris@64: kj::fwd(rest)...); Chris@64: } Chris@64: Chris@64: struct MakeTupleFunc { Chris@64: template Chris@64: Tuple...> operator()(Params&&... params) { Chris@64: return Tuple...>(kj::fwd(params)...); Chris@64: } Chris@64: template Chris@64: Decay operator()(Param&& param) { Chris@64: return kj::fwd(param); Chris@64: } Chris@64: }; Chris@64: Chris@64: } // namespace _ (private) Chris@64: Chris@64: template struct Tuple_ { typedef _::Tuple Type; }; Chris@64: template struct Tuple_ { typedef T Type; }; Chris@64: Chris@64: template using Tuple = typename Tuple_::Type; Chris@64: // Tuple type. `Tuple` (i.e. a single-element tuple) is a synonym for `T`. Tuples of size Chris@64: // other than 1 expand to an internal type. Either way, you can construct a Tuple using Chris@64: // `kj::tuple(...)`, get an element by index `i` using `kj::get(myTuple)`, and expand the tuple Chris@64: // as arguments to a function using `kj::apply(func, myTuple)`. Chris@64: // Chris@64: // Tuples are always flat -- that is, no element of a Tuple is ever itself a Tuple. If you Chris@64: // construct a tuple from other tuples, the elements are flattened and concatenated. Chris@64: Chris@64: template Chris@64: inline auto tuple(Params&&... params) Chris@64: -> decltype(_::expandAndApply(_::MakeTupleFunc(), kj::fwd(params)...)) { Chris@64: // Construct a new tuple from the given values. Any tuples in the argument list will be Chris@64: // flattened into the result. Chris@64: return _::expandAndApply(_::MakeTupleFunc(), kj::fwd(params)...); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto get(Tuple&& tuple) -> decltype(_::getImpl(kj::fwd(tuple))) { Chris@64: // Unpack and return the tuple element at the given index. The index is specified as a template Chris@64: // parameter, e.g. `kj::get<3>(myTuple)`. Chris@64: return _::getImpl(kj::fwd(tuple)); Chris@64: } Chris@64: Chris@64: template Chris@64: inline auto apply(Func&& func, Params&&... params) Chris@64: -> decltype(_::expandAndApply(kj::fwd(func), kj::fwd(params)...)) { Chris@64: // Apply a function to some arguments, expanding tuples into separate arguments. Chris@64: return _::expandAndApply(kj::fwd(func), kj::fwd(params)...); Chris@64: } Chris@64: Chris@64: template struct TupleSize_ { static constexpr size_t size = 1; }; Chris@64: template struct TupleSize_<_::Tuple> { Chris@64: static constexpr size_t size = sizeof...(T); Chris@64: }; Chris@64: Chris@64: template Chris@64: constexpr size_t tupleSize() { return TupleSize_::size; } Chris@64: // Returns size of the tuple T. Chris@64: Chris@64: } // namespace kj Chris@64: Chris@64: #endif // KJ_TUPLE_H_