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 MOVEONLY_H 10 #define MOVEONLY_H 11 12 #include "test_macros.h" 13 14 #include <cstddef> 15 #include <functional> 16 17 class MoveOnly 18 { 19 int data_; 20 public: data_(data)21 TEST_CONSTEXPR MoveOnly(int data = 1) : data_(data) {} 22 23 MoveOnly(const MoveOnly&) = delete; 24 MoveOnly& operator=(const MoveOnly&) = delete; 25 MoveOnly(MoveOnly && x)26 TEST_CONSTEXPR_CXX14 MoveOnly(MoveOnly&& x) TEST_NOEXCEPT 27 : data_(x.data_) {x.data_ = 0;} 28 TEST_CONSTEXPR_CXX14 MoveOnly& operator=(MoveOnly&& x) 29 {data_ = x.data_; x.data_ = 0; return *this;} 30 get()31 TEST_CONSTEXPR int get() const {return data_;} 32 33 friend TEST_CONSTEXPR bool operator==(const MoveOnly& x, const MoveOnly& y) 34 { return x.data_ == y.data_; } 35 friend TEST_CONSTEXPR bool operator!=(const MoveOnly& x, const MoveOnly& y) 36 { return x.data_ != y.data_; } 37 friend TEST_CONSTEXPR bool operator< (const MoveOnly& x, const MoveOnly& y) 38 { return x.data_ < y.data_; } 39 friend TEST_CONSTEXPR bool operator<=(const MoveOnly& x, const MoveOnly& y) 40 { return x.data_ <= y.data_; } 41 friend TEST_CONSTEXPR bool operator> (const MoveOnly& x, const MoveOnly& y) 42 { return x.data_ > y.data_; } 43 friend TEST_CONSTEXPR bool operator>=(const MoveOnly& x, const MoveOnly& y) 44 { return x.data_ >= y.data_; } 45 46 #if TEST_STD_VER > 17 47 friend constexpr auto operator<=>(const MoveOnly&, const MoveOnly&) = default; 48 #endif // TEST_STD_VER > 17 49 50 TEST_CONSTEXPR_CXX14 MoveOnly operator+(const MoveOnly& x) const 51 { return MoveOnly(data_ + x.data_); } 52 TEST_CONSTEXPR_CXX14 MoveOnly operator*(const MoveOnly& x) const 53 { return MoveOnly(data_ * x.data_); } 54 55 template<class T> 56 friend void operator,(MoveOnly const&, T) = delete; 57 58 template<class T> 59 friend void operator,(T, MoveOnly const&) = delete; 60 }; 61 62 template <> 63 struct std::hash<MoveOnly> 64 { 65 typedef MoveOnly argument_type; 66 typedef std::size_t result_type; 67 TEST_CONSTEXPR std::size_t operator()(const MoveOnly& x) const {return static_cast<size_t>(x.get());} 68 }; 69 70 class TrivialMoveOnly { 71 int data_; 72 73 public: 74 TEST_CONSTEXPR TrivialMoveOnly(int data = 1) : data_(data) {} 75 76 TrivialMoveOnly(const TrivialMoveOnly&) = delete; 77 TrivialMoveOnly& operator=(const TrivialMoveOnly&) = delete; 78 79 TrivialMoveOnly(TrivialMoveOnly&&) = default; 80 TrivialMoveOnly& operator=(TrivialMoveOnly&&) = default; 81 82 TEST_CONSTEXPR int get() const { return data_; } 83 84 friend TEST_CONSTEXPR bool operator==(const TrivialMoveOnly& x, const TrivialMoveOnly& y) { 85 return x.data_ == y.data_; 86 } 87 friend TEST_CONSTEXPR bool operator!=(const TrivialMoveOnly& x, const TrivialMoveOnly& y) { 88 return x.data_ != y.data_; 89 } 90 friend TEST_CONSTEXPR bool operator<(const TrivialMoveOnly& x, const TrivialMoveOnly& y) { 91 return x.data_ < y.data_; 92 } 93 friend TEST_CONSTEXPR bool operator<=(const TrivialMoveOnly& x, const TrivialMoveOnly& y) { 94 return x.data_ <= y.data_; 95 } 96 friend TEST_CONSTEXPR bool operator>(const TrivialMoveOnly& x, const TrivialMoveOnly& y) { 97 return x.data_ > y.data_; 98 } 99 friend TEST_CONSTEXPR bool operator>=(const TrivialMoveOnly& x, const TrivialMoveOnly& y) { 100 return x.data_ >= y.data_; 101 } 102 103 #if TEST_STD_VER > 17 104 friend constexpr auto operator<=>(const TrivialMoveOnly&, const TrivialMoveOnly&) = default; 105 #endif // TEST_STD_VER > 17 106 107 TEST_CONSTEXPR_CXX14 TrivialMoveOnly operator+(const TrivialMoveOnly& x) const { 108 return TrivialMoveOnly(data_ + x.data_); 109 } 110 TEST_CONSTEXPR_CXX14 TrivialMoveOnly operator*(const TrivialMoveOnly& x) const { 111 return TrivialMoveOnly(data_ * x.data_); 112 } 113 114 template<class T> 115 friend void operator,(TrivialMoveOnly const&, T) = delete; 116 117 template<class T> 118 friend void operator,(T, TrivialMoveOnly const&) = delete; 119 }; 120 121 template <> 122 struct std::hash<TrivialMoveOnly> { 123 typedef TrivialMoveOnly argument_type; 124 typedef std::size_t result_type; 125 TEST_CONSTEXPR std::size_t operator()(const TrivialMoveOnly& x) const { return static_cast<size_t>(x.get()); } 126 }; 127 128 #endif // MOVEONLY_H 129