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