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 9 #ifndef _LIBCPP___ALGORITHM_COMP_REF_TYPE_H 10 #define _LIBCPP___ALGORITHM_COMP_REF_TYPE_H 11 12 #include <__assert> 13 #include <__config> 14 #include <__utility/declval.h> 15 16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 17 # pragma GCC system_header 18 #endif 19 20 _LIBCPP_BEGIN_NAMESPACE_STD 21 22 template <class _Compare> 23 struct __debug_less 24 { 25 _Compare &__comp_; 26 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI __debug_less(_Compare& __c) : __comp_(__c) {} 27 28 template <class _Tp, class _Up> 29 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI 30 bool operator()(const _Tp& __x, const _Up& __y) 31 { 32 bool __r = __comp_(__x, __y); 33 if (__r) 34 __do_compare_assert(0, __y, __x); 35 return __r; 36 } 37 38 template <class _Tp, class _Up> 39 _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI 40 bool operator()(_Tp& __x, _Up& __y) 41 { 42 bool __r = __comp_(__x, __y); 43 if (__r) 44 __do_compare_assert(0, __y, __x); 45 return __r; 46 } 47 48 template <class _LHS, class _RHS> 49 _LIBCPP_CONSTEXPR_SINCE_CXX14 50 inline _LIBCPP_INLINE_VISIBILITY 51 decltype((void)std::declval<_Compare&>()( 52 std::declval<_LHS &>(), std::declval<_RHS &>())) 53 __do_compare_assert(int, _LHS & __l, _RHS & __r) { 54 _LIBCPP_ASSERT_UNCATEGORIZED(!__comp_(__l, __r), 55 "Comparator does not induce a strict weak ordering"); 56 (void)__l; 57 (void)__r; 58 } 59 60 template <class _LHS, class _RHS> 61 _LIBCPP_CONSTEXPR_SINCE_CXX14 62 inline _LIBCPP_INLINE_VISIBILITY 63 void __do_compare_assert(long, _LHS &, _RHS &) {} 64 }; 65 66 // Pass the comparator by lvalue reference. Or in debug mode, using a 67 // debugging wrapper that stores a reference. 68 #if _LIBCPP_ENABLE_DEBUG_MODE 69 template <class _Comp> 70 using __comp_ref_type = __debug_less<_Comp>; 71 #else 72 template <class _Comp> 73 using __comp_ref_type = _Comp&; 74 #endif 75 76 _LIBCPP_END_NAMESPACE_STD 77 78 #endif // _LIBCPP___ALGORITHM_COMP_REF_TYPE_H 79