IndexSequence.h 3.6 KB

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