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) 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 TEST_CONSTEXPR_CXX14 MoveOnly operator+(const MoveOnly& x) const 47 { return MoveOnly(data_ + x.data_); } 48 TEST_CONSTEXPR_CXX14 MoveOnly operator*(const MoveOnly& x) const 49 { return MoveOnly(data_ * x.data_); } 50 51 template<class T, class U> 52 friend void operator,(T t, U u) = delete; 53 }; 54 55 56 template <> 57 struct std::hash<MoveOnly> 58 { 59 typedef MoveOnly argument_type; 60 typedef size_t result_type; 61 TEST_CONSTEXPR size_t operator()(const MoveOnly& x) const {return x.get();} 62 }; 63 64 #endif // MOVEONLY_H 65