Marshall Clow | 354d39c | 2014-01-16 16:58:45 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 10 | #ifndef MOVEONLY_H |
| 11 | #define MOVEONLY_H |
| 12 | |
Eric Fiselier | f2f2a63 | 2016-06-14 21:31:42 +0000 | [diff] [blame] | 13 | #include "test_macros.h" |
| 14 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 15 | #if TEST_STD_VER >= 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | |
| 17 | #include <cstddef> |
| 18 | #include <functional> |
| 19 | |
| 20 | class MoveOnly |
| 21 | { |
| 22 | MoveOnly(const MoveOnly&); |
| 23 | MoveOnly& operator=(const MoveOnly&); |
| 24 | |
| 25 | int data_; |
| 26 | public: |
David Blaikie | c6dbc22 | 2014-08-09 22:42:19 +0000 | [diff] [blame] | 27 | MoveOnly(int data = 1) : data_(data) {} |
| 28 | MoveOnly(MoveOnly&& x) |
| 29 | : data_(x.data_) {x.data_ = 0;} |
| 30 | MoveOnly& operator=(MoveOnly&& x) |
| 31 | {data_ = x.data_; x.data_ = 0; return *this;} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | |
| 33 | int get() const {return data_;} |
| 34 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 35 | bool operator==(const MoveOnly& x) const {return data_ == x.data_;} |
| 36 | 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] | 37 | MoveOnly operator+(const MoveOnly& x) const { return MoveOnly{data_ + x.data_}; } |
| 38 | MoveOnly operator*(const MoveOnly& x) const { return MoveOnly{data_ * x.data_}; } |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | namespace std { |
| 42 | |
| 43 | template <> |
| 44 | struct hash<MoveOnly> |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | { |
Eric Fiselier | ae2c8de | 2017-01-18 01:48:54 +0000 | [diff] [blame] | 46 | typedef MoveOnly argument_type; |
| 47 | typedef size_t result_type; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | std::size_t operator()(const MoveOnly& x) const {return x.get();} |
| 49 | }; |
| 50 | |
| 51 | } |
| 52 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 53 | #endif // TEST_STD_VER >= 11 |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 55 | #endif // MOVEONLY_H |