Marshall Clow | d5f461c | 2015-01-28 19:54:25 +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 | |
| 10 | #ifndef COUNTER_H |
| 11 | #define COUNTER_H |
| 12 | |
| 13 | #include <functional> // for std::hash |
| 14 | |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 15 | #include "test_macros.h" |
| 16 | |
Marshall Clow | d5f461c | 2015-01-28 19:54:25 +0000 | [diff] [blame] | 17 | struct Counter_base { static int gConstructed; }; |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 18 | |
Marshall Clow | d5f461c | 2015-01-28 19:54:25 +0000 | [diff] [blame] | 19 | template <typename T> |
| 20 | class Counter : public Counter_base |
| 21 | { |
| 22 | public: |
| 23 | Counter() : data_() { ++gConstructed; } |
| 24 | Counter(const T &data) : data_(data) { ++gConstructed; } |
| 25 | Counter(const Counter& rhs) : data_(rhs.data_) { ++gConstructed; } |
| 26 | Counter& operator=(const Counter& rhs) { ++gConstructed; data_ = rhs.data_; return *this; } |
Eric Fiselier | 9adebed | 2017-04-19 01:02:49 +0000 | [diff] [blame] | 27 | #if TEST_STD_VER >= 11 |
Marshall Clow | d5f461c | 2015-01-28 19:54:25 +0000 | [diff] [blame] | 28 | Counter(Counter&& rhs) : data_(std::move(rhs.data_)) { ++gConstructed; } |
| 29 | Counter& operator=(Counter&& rhs) { ++gConstructed; data_ = std::move(rhs.data_); return *this; } |
| 30 | #endif |
| 31 | ~Counter() { --gConstructed; } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 32 | |
Marshall Clow | d5f461c | 2015-01-28 19:54:25 +0000 | [diff] [blame] | 33 | const T& get() const {return data_;} |
| 34 | |
| 35 | bool operator==(const Counter& x) const {return data_ == x.data_;} |
| 36 | bool operator< (const Counter& x) const {return data_ < x.data_;} |
| 37 | |
| 38 | private: |
| 39 | T data_; |
| 40 | }; |
| 41 | |
| 42 | int Counter_base::gConstructed = 0; |
| 43 | |
| 44 | namespace std { |
| 45 | |
| 46 | template <class T> |
| 47 | struct hash<Counter<T> > |
| 48 | : public std::unary_function<Counter<T>, std::size_t> |
| 49 | { |
| 50 | std::size_t operator()(const Counter<T>& x) const {return std::hash<T>(x.get());} |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | #endif |