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