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