Marshall Clow | 354d39c | 2014-01-16 16:58:45 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | 354d39c | 2014-01-16 16:58:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 9 | #ifndef MOVEONLY_H |
| 10 | #define MOVEONLY_H |
| 11 | |
Eric Fiselier | f2f2a63 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 12 | #include "test_macros.h" |
| 13 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 14 | #if TEST_STD_VER >= 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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: |
David Blaikie | c6dbc22 | 2014-08-09 22:42:19 +0000 | [diff] [blame] | 26 | MoveOnly(int data = 1) : data_(data) {} |
| 27 | MoveOnly(MoveOnly&& x) |
| 28 | : data_(x.data_) {x.data_ = 0;} |
| 29 | MoveOnly& operator=(MoveOnly&& x) |
| 30 | {data_ = x.data_; x.data_ = 0; return *this;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | |
| 32 | int get() const {return data_;} |
| 33 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 34 | bool operator==(const MoveOnly& x) const {return data_ == x.data_;} |
| 35 | bool operator< (const MoveOnly& x) const {return data_ < x.data_;} |
Billy Robert O'Neal III | 3770e40 | 2018-01-05 01:32:00 +0000 | [diff] [blame] | 36 | MoveOnly operator+(const MoveOnly& x) const { return MoveOnly{data_ + x.data_}; } |
| 37 | MoveOnly operator*(const MoveOnly& x) const { return MoveOnly{data_ * x.data_}; } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | namespace std { |
| 41 | |
| 42 | template <> |
| 43 | struct hash<MoveOnly> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | { |
Eric Fiselier | ae2c8de | 2017-01-18 01:48:54 +0000 | [diff] [blame] | 45 | typedef MoveOnly argument_type; |
| 46 | typedef size_t result_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | std::size_t operator()(const MoveOnly& x) const {return x.get();} |
| 48 | }; |
| 49 | |
| 50 | } |
| 51 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 52 | #endif // TEST_STD_VER >= 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 54 | #endif // MOVEONLY_H |