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