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_AFTER_CXX11
27     __debug_less(_Compare& __c) : __comp_(__c) {}
28 
29     template <class _Tp, class _Up>
30     _LIBCPP_CONSTEXPR_AFTER_CXX11
31     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_AFTER_CXX11
41     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_AFTER_CXX11
51     inline _LIBCPP_INLINE_VISIBILITY
52     decltype((void)declval<_Compare&>()(
53         declval<_LHS &>(), declval<_RHS &>()))
54     __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_AFTER_CXX11
63     inline _LIBCPP_INLINE_VISIBILITY
64     void __do_compare_assert(long, _LHS &, _RHS &) {}
65 };
66 
67 template <class _Comp>
68 struct __comp_ref_type {
69   // Pass the comparator by lvalue reference. Or in debug mode, using a
70   // debugging wrapper that stores a reference.
71 #ifdef _LIBCPP_ENABLE_DEBUG_MODE
72   typedef __debug_less<_Comp> type;
73 #else
74   typedef _Comp& type;
75 #endif
76 };
77 
78 _LIBCPP_END_NAMESPACE_STD
79 
80 #endif // _LIBCPP___ALGORITHM_COMP_REF_TYPE_H
81