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