xref: /openbsd-src/gnu/llvm/libcxx/include/__algorithm/comp_ref_type.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
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 <__config>
13 #include <__debug>
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
__debug_less__debug_less27     __debug_less(_Compare& __c) : __comp_(__c) {}
28 
29     template <class _Tp, class _Up>
30     _LIBCPP_CONSTEXPR_SINCE_CXX14
operator__debug_less31     bool operator()(const _Tp& __x,  const _Up& __y)
32     {
33         bool __r = __comp_(__x, __y);
34         if (__r)
35             __do_compare_assert(0, __y, __x);
36         return __r;
37     }
38 
39     template <class _Tp, class _Up>
40     _LIBCPP_CONSTEXPR_SINCE_CXX14
operator__debug_less41     bool operator()(_Tp& __x,  _Up& __y)
42     {
43         bool __r = __comp_(__x, __y);
44         if (__r)
45             __do_compare_assert(0, __y, __x);
46         return __r;
47     }
48 
49     template <class _LHS, class _RHS>
50     _LIBCPP_CONSTEXPR_SINCE_CXX14
51     inline _LIBCPP_INLINE_VISIBILITY
52     decltype((void)std::declval<_Compare&>()(
53         std::declval<_LHS &>(), std::declval<_RHS &>()))
__do_compare_assert__debug_less54     __do_compare_assert(int, _LHS & __l, _RHS & __r) {
55         _LIBCPP_DEBUG_ASSERT(!__comp_(__l, __r),
56             "Comparator does not induce a strict weak ordering");
57         (void)__l;
58         (void)__r;
59     }
60 
61     template <class _LHS, class _RHS>
62     _LIBCPP_CONSTEXPR_SINCE_CXX14
63     inline _LIBCPP_INLINE_VISIBILITY
__do_compare_assert__debug_less64     void __do_compare_assert(long, _LHS &, _RHS &) {}
65 };
66 
67 // Pass the comparator by lvalue reference. Or in debug mode, using a
68 // debugging wrapper that stores a reference.
69 #ifdef _LIBCPP_ENABLE_DEBUG_MODE
70 template <class _Comp>
71 using __comp_ref_type = __debug_less<_Comp>;
72 #else
73 template <class _Comp>
74 using __comp_ref_type = _Comp&;
75 #endif
76 
77 _LIBCPP_END_NAMESPACE_STD
78 
79 #endif // _LIBCPP___ALGORITHM_COMP_REF_TYPE_H
80