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: 21 TEST_CONSTEXPR MoveOnly(int data = 1) : data_(data) {} 22 23 MoveOnly(const MoveOnly&) = delete; 24 MoveOnly& operator=(const MoveOnly&) = delete; 25 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 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, class U> 56 friend void operator,(T t, U u) = delete; 57 }; 58 59 60 template <> 61 struct std::hash<MoveOnly> 62 { 63 typedef MoveOnly argument_type; 64 typedef size_t result_type; 65 TEST_CONSTEXPR size_t operator()(const MoveOnly& x) const {return static_cast<size_t>(x.get());} 66 }; 67 68 #endif // MOVEONLY_H 69