IndexSequence.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* A utility for expanding a tuple into a variadic argument list.
  6. * Based on std::index_sequence. */
  7. /**
  8. * Example usage:
  9. *
  10. * Problem:
  11. *
  12. * You have a variadic function Foo:
  13. *
  14. * template <typename... Args> void Foo(Args...);
  15. *
  16. * And a variadic function Bar, which contains a tuple:
  17. *
  18. * template <typename... Args>
  19. * void Bar() {
  20. * // ...
  21. * Tuple<Args...> t;
  22. * }
  23. *
  24. * And inside Bar, you want to call Foo with the elements of the tuple as
  25. * arguments to Foo.
  26. *
  27. * You want to write:
  28. *
  29. * Foo(Get<0>(t), Get<1>(t), ..., Get<N>(t))
  30. *
  31. * but you can't literally write that, because N is different for different
  32. * instantiations of Bar.
  33. *
  34. * Solution:
  35. *
  36. * Write a helper function which takes the tuple, and an index sequence
  37. * containing indices corresponding to the tuple indices.
  38. *
  39. * template <typename... Args, size_t... Indices>
  40. * void Helper(const Tuple<Args...>& t, IndexSequence<Indices>)
  41. * {
  42. * Foo(Get<Indices>(t)...);
  43. * }
  44. *
  45. * Assuming 'Indices...' are 0, 1, ..., N - 1, where N is the size of the
  46. * tuple, pack expansion will expand the pack 'Get<Indices>(t)...' to
  47. * 'Get<0>(t), Get<1>(t), ..., Get<N>(t)'.
  48. *
  49. * Finally, call the helper, creating the index sequence to pass in like so:
  50. *
  51. * template <typename... Args>
  52. * void Bar() {
  53. * // ...
  54. * Tuple<Args...> t;
  55. * Helper(t, typename IndexSequenceFor<Args...>::Type());
  56. * }
  57. */
  58. #ifndef mozilla_IndexSequence_h
  59. #define mozilla_IndexSequence_h
  60. #include "mozilla/Attributes.h"
  61. #include <stddef.h>
  62. namespace mozilla {
  63. /**
  64. * Represents a compile-time sequence of integer indices.
  65. */
  66. template<size_t... Indices>
  67. struct IndexSequence
  68. {
  69. static constexpr size_t Size() { return sizeof...(Indices); }
  70. };
  71. namespace detail {
  72. // Helpers used by MakeIndexSequence.
  73. template<size_t... Indices>
  74. struct IndexTuple
  75. {
  76. typedef IndexTuple<Indices..., sizeof...(Indices)> Next;
  77. };
  78. // Builds IndexTuple<0, 1, ..., N - 1>.
  79. template<size_t N>
  80. struct BuildIndexTuple
  81. {
  82. typedef typename BuildIndexTuple<N - 1>::Type::Next Type;
  83. };
  84. template<>
  85. struct BuildIndexTuple<0>
  86. {
  87. typedef IndexTuple<> Type;
  88. };
  89. template<size_t N, typename IndexTuple>
  90. struct MakeIndexSequenceImpl;
  91. template<size_t N, size_t... Indices>
  92. struct MakeIndexSequenceImpl<N, IndexTuple<Indices...>>
  93. {
  94. typedef IndexSequence<Indices...> Type;
  95. };
  96. } // namespace detail
  97. /**
  98. * A utility for building an IndexSequence of consecutive indices.
  99. * MakeIndexSequence<N>::Type evaluates to IndexSequence<0, 1, .., N - 1>.
  100. * Note: unlike std::make_index_sequence, this is not an alias template
  101. * to work around bugs in MSVC 2013.
  102. */
  103. template<size_t N>
  104. struct MakeIndexSequence
  105. {
  106. typedef typename detail::MakeIndexSequenceImpl<N,
  107. typename detail::BuildIndexTuple<N>::Type>::Type Type;
  108. };
  109. /**
  110. * A utility for building an IndexSequence of consecutive indices
  111. * corresponding to a variadic argument list.
  112. * IndexSequenceFor<Types...> evaluates to IndexSequence<0, 1, ..., N - 1>
  113. * where N is the number of types in Types.
  114. * Note: unlike std::index_sequence_for, this is not an alias template
  115. * to work around bugs in MSVC 2013.
  116. */
  117. template<typename... Types>
  118. struct IndexSequenceFor
  119. {
  120. typedef typename MakeIndexSequence<sizeof...(Types)>::Type Type;
  121. };
  122. } // namespace mozilla
  123. #endif /* mozilla_IndexSequence_h */