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 LIBCXX_TEST_SUPPORT_BOOLEAN_TESTABLE_H
10 #define LIBCXX_TEST_SUPPORT_BOOLEAN_TESTABLE_H
11 
12 #include "test_macros.h"
13 
14 #if TEST_STD_VER > 17
15 
16 class BooleanTestable {
17 public:
18   constexpr operator bool() const {
19     return value_;
20   }
21 
22   friend constexpr BooleanTestable operator==(const BooleanTestable& lhs, const BooleanTestable& rhs) {
23     return lhs.value_ == rhs.value_;
24   }
25 
26   friend constexpr BooleanTestable operator!=(const BooleanTestable& lhs, const BooleanTestable& rhs) {
27     return !(lhs == rhs);
28   }
29 
30   constexpr BooleanTestable operator!() {
31     return BooleanTestable{!value_};
32   }
33 
34   // this class should behave like a bool, so the constructor shouldn't be explicit
BooleanTestable(bool value)35   constexpr BooleanTestable(bool value) : value_{value} {}
36   constexpr BooleanTestable(const BooleanTestable&) = delete;
37   constexpr BooleanTestable(BooleanTestable&&) = delete;
38 
39 private:
40   bool value_;
41 };
42 
43 template <class T>
44 class StrictComparable {
45 public:
46   // this shouldn't be explicit to make it easier to initlaize inside arrays (which it almost always is)
StrictComparable(T value)47   constexpr StrictComparable(T value) : value_{value} {}
48 
49   friend constexpr BooleanTestable operator==(const StrictComparable& lhs, const StrictComparable& rhs) {
50     return (lhs.value_ == rhs.value_);
51   }
52 
53   friend constexpr BooleanTestable operator!=(const StrictComparable& lhs, const StrictComparable& rhs) {
54     return !(lhs == rhs);
55   }
56 
57 private:
58   T value_;
59 };
60 
61 #endif // TEST_STD_VER > 17
62 
63 #endif // LIBCXX_TEST_SUPPORT_BOOLEAN_TESTABLE_H
64