blob: 8dbdaeb3600147dbfa43589a726fdee2405fedab [file] [log] [blame]
Marshall Clowd5f461c2015-01-28 19:54:25 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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 Clowd5f461c2015-01-28 19:54:25 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef COUNTER_H
10#define COUNTER_H
11
12#include <functional> // for std::hash
13
Eric Fiselier9adebed2017-04-19 01:02:49 +000014#include "test_macros.h"
15
Marshall Clowd5f461c2015-01-28 19:54:25 +000016struct Counter_base { static int gConstructed; };
Eric Fiselierd04c6852016-06-01 21:35:39 +000017
Marshall Clowd5f461c2015-01-28 19:54:25 +000018template <typename T>
19class Counter : public Counter_base
20{
21public:
22 Counter() : data_() { ++gConstructed; }
23 Counter(const T &data) : data_(data) { ++gConstructed; }
24 Counter(const Counter& rhs) : data_(rhs.data_) { ++gConstructed; }
Erik Pilkingtonb0386a52018-08-01 01:33:38 +000025 Counter& operator=(const Counter& rhs) { data_ = rhs.data_; return *this; }
Eric Fiselier9adebed2017-04-19 01:02:49 +000026#if TEST_STD_VER >= 11
Marshall Clowd5f461c2015-01-28 19:54:25 +000027 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 Fiselierd04c6852016-06-01 21:35:39 +000031
Marshall Clowd5f461c2015-01-28 19:54:25 +000032 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
37private:
38 T data_;
39};
40
41int Counter_base::gConstructed = 0;
42
43namespace std {
44
45template <class T>
46struct hash<Counter<T> >
Marshall Clowd5f461c2015-01-28 19:54:25 +000047{
Stephan T. Lavavej3ed719b2018-04-12 23:56:10 +000048 typedef Counter<T> argument_type;
49 typedef std::size_t result_type;
50
Erik Pilkingtonb0386a52018-08-01 01:33:38 +000051 std::size_t operator()(const Counter<T>& x) const {return std::hash<T>()(x.get());}
Marshall Clowd5f461c2015-01-28 19:54:25 +000052};
53}
54
55#endif