xref: /llvm-project/libcxx/test/support/MoveOnly.h (revision 37ef15143a5d77a0fba0ece4c26a72cfb9e050a0)
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     constexpr bool operator==(const MoveOnly& x) const {return data_ == x.data_;}
35     constexpr bool operator< (const MoveOnly& x) const {return data_ <  x.data_;}
36     TEST_CONSTEXPR_CXX14 MoveOnly operator+(const MoveOnly& x) const
37         { return MoveOnly{data_ + x.data_}; }
38     TEST_CONSTEXPR_CXX14 MoveOnly operator*(const MoveOnly& x) const
39         { return MoveOnly{data_ * x.data_}; }
40 };
41 
42 namespace std {
43 
44 template <>
45 struct hash<MoveOnly>
46 {
47     typedef MoveOnly argument_type;
48     typedef size_t result_type;
49     constexpr std::size_t operator()(const MoveOnly& x) const {return x.get();}
50 };
51 
52 }
53 
54 #endif  // TEST_STD_VER >= 11
55 
56 #endif  // MOVEONLY_H
57