1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* A utility for expanding a tuple into a variadic argument list.
8 * Based on std::index_sequence. */
15 * You have a variadic function Foo:
17 * template <typename... Args> void Foo(Args...);
19 * And a variadic function Bar, which contains a tuple:
21 * template <typename... Args>
27 * And inside Bar, you want to call Foo with the elements of the tuple as
32 * Foo(Get<0>(t), Get<1>(t), ..., Get<N>(t))
34 * but you can't literally write that, because N is different for different
35 * instantiations of Bar.
39 * Write a helper function which takes the tuple, and an index sequence
40 * containing indices corresponding to the tuple indices.
42 * template <typename... Args, size_t... Indices>
43 * void Helper(const Tuple<Args...>& t, IndexSequence<Indices...>)
45 * Foo(Get<Indices>(t)...);
48 * Assuming 'Indices...' are 0, 1, ..., N - 1, where N is the size of the
49 * tuple, pack expansion will expand the pack 'Get<Indices>(t)...' to
50 * 'Get<0>(t), Get<1>(t), ..., Get<N>(t)'.
52 * Finally, call the helper, creating the index sequence to pass in like so:
54 * template <typename... Args>
58 * Helper(t, typename IndexSequenceFor<Args...>::Type());
62 #ifndef mozilla_IndexSequence_h
63 #define mozilla_IndexSequence_h
65 #include "mozilla/Attributes.h"
72 * Represents a compile-time sequence of integer indices.
74 template <size_t... Indices
>
75 struct IndexSequence
{
76 static constexpr size_t Size() { return sizeof...(Indices
); }
81 // Helpers used by MakeIndexSequence.
83 template <size_t... Indices
>
85 typedef IndexTuple
<Indices
..., sizeof...(Indices
)> Next
;
88 // Builds IndexTuple<0, 1, ..., N - 1>.
90 struct BuildIndexTuple
{
91 typedef typename BuildIndexTuple
<N
- 1>::Type::Next Type
;
95 struct BuildIndexTuple
<0> {
96 typedef IndexTuple
<> Type
;
99 template <size_t N
, typename IndexTuple
>
100 struct MakeIndexSequenceImpl
;
102 template <size_t N
, size_t... Indices
>
103 struct MakeIndexSequenceImpl
<N
, IndexTuple
<Indices
...>> {
104 typedef IndexSequence
<Indices
...> Type
;
107 } // namespace detail
110 * A utility for building an IndexSequence of consecutive indices.
111 * MakeIndexSequence<N>::Type evaluates to IndexSequence<0, 1, .., N - 1>.
112 * Note: unlike std::make_index_sequence, this is not an alias template
113 * to work around bugs in MSVC 2013.
116 struct MakeIndexSequence
{
117 typedef typename
detail::MakeIndexSequenceImpl
<
118 N
, typename
detail::BuildIndexTuple
<N
>::Type
>::Type Type
;
122 * A utility for building an IndexSequence of consecutive indices
123 * corresponding to a variadic argument list.
124 * IndexSequenceFor<Types...> evaluates to IndexSequence<0, 1, ..., N - 1>
125 * where N is the number of types in Types.
126 * Note: unlike std::index_sequence_for, this is not an alias template
127 * to work around bugs in MSVC 2013.
129 template <typename
... Types
>
130 struct IndexSequenceFor
{
131 typedef typename MakeIndexSequence
<sizeof...(Types
)>::Type Type
;
134 } // namespace mozilla
136 #endif /* mozilla_IndexSequence_h */