xref: /llvm-project/libcxx/test/support/MoveOnly.h (revision 16bf43398a62604e6a4146deeb1c43dfa1e78e04)
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 #if TEST_STD_VER >= 11
15 
16 #include <cstddef>
17 #include <functional>
18 
19 class MoveOnly
20 {
21     MoveOnly(const MoveOnly&);
22     MoveOnly& operator=(const MoveOnly&);
23 
24     int data_;
25 public:
26     constexpr MoveOnly(int data = 1) : data_(data) {}
27     TEST_CONSTEXPR_CXX14 MoveOnly(MoveOnly&& x)
28         : data_(x.data_) {x.data_ = 0;}
29     TEST_CONSTEXPR_CXX14 MoveOnly& operator=(MoveOnly&& x)
30         {data_ = x.data_; x.data_ = 0; return *this;}
31 
32     constexpr int get() const {return data_;}
33 
34     friend constexpr bool operator==(const MoveOnly& x, const MoveOnly& y)
35         { return x.data_ == y.data_; }
36     friend constexpr bool operator!=(const MoveOnly& x, const MoveOnly& y)
37         { return x.data_ != y.data_; }
38     friend constexpr bool operator< (const MoveOnly& x, const MoveOnly& y)
39         { return x.data_ <  y.data_; }
40     friend constexpr bool operator<=(const MoveOnly& x, const MoveOnly& y)
41         { return x.data_ <= y.data_; }
42     friend constexpr bool operator> (const MoveOnly& x, const MoveOnly& y)
43         { return x.data_ >  y.data_; }
44     friend constexpr bool operator>=(const MoveOnly& x, const MoveOnly& y)
45         { return x.data_ >= y.data_; }
46 
47     TEST_CONSTEXPR_CXX14 MoveOnly operator+(const MoveOnly& x) const
48         { return MoveOnly{data_ + x.data_}; }
49     TEST_CONSTEXPR_CXX14 MoveOnly operator*(const MoveOnly& x) const
50         { return MoveOnly{data_ * x.data_}; }
51 
52     template<class T, class U>
53     friend void operator,(T t, U u) = delete;
54 };
55 
56 namespace std {
57 
58 template <>
59 struct hash<MoveOnly>
60 {
61     typedef MoveOnly argument_type;
62     typedef size_t result_type;
63     constexpr size_t operator()(const MoveOnly& x) const {return x.get();}
64 };
65 
66 }
67 
68 #endif // TEST_STD_VER >= 11
69 
70 #endif // MOVEONLY_H
71