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 <type_traits>
14 
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 #pragma GCC system_header
17 #endif
18 
19 _LIBCPP_PUSH_MACROS
20 #include <__undef_macros>
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #ifdef _LIBCPP_DEBUG
25 
26 template <class _Compare>
27 struct __debug_less
28 {
29     _Compare &__comp_;
30     _LIBCPP_CONSTEXPR_AFTER_CXX17
__debug_less__debug_less31     __debug_less(_Compare& __c) : __comp_(__c) {}
32 
33     template <class _Tp, class _Up>
34     _LIBCPP_CONSTEXPR_AFTER_CXX17
operator__debug_less35     bool operator()(const _Tp& __x,  const _Up& __y)
36     {
37         bool __r = __comp_(__x, __y);
38         if (__r)
39             __do_compare_assert(0, __y, __x);
40         return __r;
41     }
42 
43     template <class _Tp, class _Up>
44     _LIBCPP_CONSTEXPR_AFTER_CXX17
operator__debug_less45     bool operator()(_Tp& __x,  _Up& __y)
46     {
47         bool __r = __comp_(__x, __y);
48         if (__r)
49             __do_compare_assert(0, __y, __x);
50         return __r;
51     }
52 
53     template <class _LHS, class _RHS>
54     _LIBCPP_CONSTEXPR_AFTER_CXX17
55     inline _LIBCPP_INLINE_VISIBILITY
56     decltype((void)declval<_Compare&>()(
57         declval<_LHS &>(), declval<_RHS &>()))
__do_compare_assert__debug_less58     __do_compare_assert(int, _LHS & __l, _RHS & __r) {
59         _LIBCPP_ASSERT(!__comp_(__l, __r),
60             "Comparator does not induce a strict weak ordering");
61     }
62 
63     template <class _LHS, class _RHS>
64     _LIBCPP_CONSTEXPR_AFTER_CXX17
65     inline _LIBCPP_INLINE_VISIBILITY
__do_compare_assert__debug_less66     void __do_compare_assert(long, _LHS &, _RHS &) {}
67 };
68 
69 #endif // _LIBCPP_DEBUG
70 
71 template <class _Comp>
72 struct __comp_ref_type {
73   // Pass the comparator by lvalue reference. Or in debug mode, using a
74   // debugging wrapper that stores a reference.
75 #ifndef _LIBCPP_DEBUG
76   typedef typename add_lvalue_reference<_Comp>::type type;
77 #else
78   typedef __debug_less<_Comp> type;
79 #endif
80 };
81 
82 
83 _LIBCPP_END_NAMESPACE_STD
84 
85 _LIBCPP_POP_MACROS
86 
87 #endif // _LIBCPP___ALGORITHM_COMP_REF_TYPE_H
88