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