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