cpp_future.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.11.3
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-FileCopyrightText: 2018 The Abseil Authors
  8. // SPDX-License-Identifier: MIT
  9. #pragma once
  10. #include <array> // array
  11. #include <cstddef> // size_t
  12. #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
  13. #include <utility> // index_sequence, make_index_sequence, index_sequence_for
  14. #include <nlohmann/detail/macro_scope.hpp>
  15. NLOHMANN_JSON_NAMESPACE_BEGIN
  16. namespace detail
  17. {
  18. template<typename T>
  19. using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  20. #ifdef JSON_HAS_CPP_14
  21. // the following utilities are natively available in C++14
  22. using std::enable_if_t;
  23. using std::index_sequence;
  24. using std::make_index_sequence;
  25. using std::index_sequence_for;
  26. #else
  27. // alias templates to reduce boilerplate
  28. template<bool B, typename T = void>
  29. using enable_if_t = typename std::enable_if<B, T>::type;
  30. // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
  31. // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
  32. //// START OF CODE FROM GOOGLE ABSEIL
  33. // integer_sequence
  34. //
  35. // Class template representing a compile-time integer sequence. An instantiation
  36. // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
  37. // type through its template arguments (which is a common need when
  38. // working with C++11 variadic templates). `absl::integer_sequence` is designed
  39. // to be a drop-in replacement for C++14's `std::integer_sequence`.
  40. //
  41. // Example:
  42. //
  43. // template< class T, T... Ints >
  44. // void user_function(integer_sequence<T, Ints...>);
  45. //
  46. // int main()
  47. // {
  48. // // user_function's `T` will be deduced to `int` and `Ints...`
  49. // // will be deduced to `0, 1, 2, 3, 4`.
  50. // user_function(make_integer_sequence<int, 5>());
  51. // }
  52. template <typename T, T... Ints>
  53. struct integer_sequence
  54. {
  55. using value_type = T;
  56. static constexpr std::size_t size() noexcept
  57. {
  58. return sizeof...(Ints);
  59. }
  60. };
  61. // index_sequence
  62. //
  63. // A helper template for an `integer_sequence` of `size_t`,
  64. // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
  65. // `std::index_sequence`.
  66. template <size_t... Ints>
  67. using index_sequence = integer_sequence<size_t, Ints...>;
  68. namespace utility_internal
  69. {
  70. template <typename Seq, size_t SeqSize, size_t Rem>
  71. struct Extend;
  72. // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
  73. template <typename T, T... Ints, size_t SeqSize>
  74. struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
  75. {
  76. using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
  77. };
  78. template <typename T, T... Ints, size_t SeqSize>
  79. struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
  80. {
  81. using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
  82. };
  83. // Recursion helper for 'make_integer_sequence<T, N>'.
  84. // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
  85. template <typename T, size_t N>
  86. struct Gen
  87. {
  88. using type =
  89. typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
  90. };
  91. template <typename T>
  92. struct Gen<T, 0>
  93. {
  94. using type = integer_sequence<T>;
  95. };
  96. } // namespace utility_internal
  97. // Compile-time sequences of integers
  98. // make_integer_sequence
  99. //
  100. // This template alias is equivalent to
  101. // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
  102. // replacement for C++14's `std::make_integer_sequence`.
  103. template <typename T, T N>
  104. using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
  105. // make_index_sequence
  106. //
  107. // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
  108. // and is designed to be a drop-in replacement for C++14's
  109. // `std::make_index_sequence`.
  110. template <size_t N>
  111. using make_index_sequence = make_integer_sequence<size_t, N>;
  112. // index_sequence_for
  113. //
  114. // Converts a typename pack into an index sequence of the same length, and
  115. // is designed to be a drop-in replacement for C++14's
  116. // `std::index_sequence_for()`
  117. template <typename... Ts>
  118. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  119. //// END OF CODE FROM GOOGLE ABSEIL
  120. #endif
  121. // dispatch utility (taken from ranges-v3)
  122. template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
  123. template<> struct priority_tag<0> {};
  124. // taken from ranges-v3
  125. template<typename T>
  126. struct static_const
  127. {
  128. static JSON_INLINE_VARIABLE constexpr T value{};
  129. };
  130. #ifndef JSON_HAS_CPP_17
  131. template<typename T>
  132. constexpr T static_const<T>::value;
  133. #endif
  134. template<typename T, typename... Args>
  135. inline constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
  136. {
  137. return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
  138. }
  139. } // namespace detail
  140. NLOHMANN_JSON_NAMESPACE_END