blob: 63ed60801555906e69dcb940b329a17b054e9751 [file] [log] [blame]
Marshall Clowd5f461c2015-01-28 19:54:25 +00001//===----------------------------------------------------------------------===//
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 Fiselier9adebed2017-04-19 01:02:49 +000015#include "test_macros.h"
16
Marshall Clowd5f461c2015-01-28 19:54:25 +000017struct Counter_base { static int gConstructed; };
Eric Fiselierd04c6852016-06-01 21:35:39 +000018
Marshall Clowd5f461c2015-01-28 19:54:25 +000019template <typename T>
20class Counter : public Counter_base
21{
22public:
23 Counter() : data_() { ++gConstructed; }
24 Counter(const T &data) : data_(data) { ++gConstructed; }
25 Counter(const Counter& rhs) : data_(rhs.data_) { ++gConstructed; }
Erik Pilkingtonb0386a52018-08-01 01:33:38 +000026 Counter& operator=(const Counter& rhs) { data_ = rhs.data_; return *this; }
Eric Fiselier9adebed2017-04-19 01:02:49 +000027#if TEST_STD_VER >= 11
Marshall Clowd5f461c2015-01-28 19:54:25 +000028 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 Fiselierd04c6852016-06-01 21:35:39 +000032
Marshall Clowd5f461c2015-01-28 19:54:25 +000033 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
38private:
39 T data_;
40};
41
42int Counter_base::gConstructed = 0;
43
44namespace std {
45
46template <class T>
47struct hash<Counter<T> >
Marshall Clowd5f461c2015-01-28 19:54:25 +000048{
Stephan T. Lavavej3ed719b2018-04-12 23:56:10 +000049 typedef Counter<T> argument_type;
50 typedef std::size_t result_type;
51
Erik Pilkingtonb0386a52018-08-01 01:33:38 +000052 std::size_t operator()(const Counter<T>& x) const {return std::hash<T>()(x.get());}
Marshall Clowd5f461c2015-01-28 19:54:25 +000053};
54}
55
56#endif