find_index.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___TUPLE_FIND_INDEX_H
  9. #define _LIBCPP___TUPLE_FIND_INDEX_H
  10. #include <__config>
  11. #include <__type_traits/is_same.h>
  12. #include <cstddef>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. #if _LIBCPP_STD_VER >= 14
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. namespace __find_detail {
  19. static constexpr size_t __not_found = static_cast<size_t>(-1);
  20. static constexpr size_t __ambiguous = __not_found - 1;
  21. inline _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_idx_return(size_t __curr_i, size_t __res, bool __matches) {
  22. return !__matches ? __res : (__res == __not_found ? __curr_i : __ambiguous);
  23. }
  24. template <size_t _Nx>
  25. inline _LIBCPP_HIDE_FROM_ABI constexpr size_t __find_idx(size_t __i, const bool (&__matches)[_Nx]) {
  26. return __i == _Nx
  27. ? __not_found
  28. : __find_detail::__find_idx_return(__i, __find_detail::__find_idx(__i + 1, __matches), __matches[__i]);
  29. }
  30. template <class _T1, class... _Args>
  31. struct __find_exactly_one_checked {
  32. static constexpr bool __matches[sizeof...(_Args)] = {is_same<_T1, _Args>::value...};
  33. static constexpr size_t value = __find_detail::__find_idx(0, __matches);
  34. static_assert(value != __not_found, "type not found in type list");
  35. static_assert(value != __ambiguous, "type occurs more than once in type list");
  36. };
  37. template <class _T1>
  38. struct __find_exactly_one_checked<_T1> {
  39. static_assert(!is_same<_T1, _T1>::value, "type not in empty type list");
  40. };
  41. } // namespace __find_detail
  42. template <typename _T1, typename... _Args>
  43. struct __find_exactly_one_t : public __find_detail::__find_exactly_one_checked<_T1, _Args...> {};
  44. _LIBCPP_END_NAMESPACE_STD
  45. #endif // _LIBCPP_STD_VER >= 14
  46. #endif // _LIBCPP___TUPLE_FIND_INDEX_H