xref: /minix3/external/bsd/libc++/dist/libcxx/test/support/Counter.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #ifndef COUNTER_H
11*0a6a1f1dSLionel Sambuc #define COUNTER_H
12*0a6a1f1dSLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <functional> // for std::hash
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc struct Counter_base { static int gConstructed; };
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc template <typename T>
18*0a6a1f1dSLionel Sambuc class Counter : public Counter_base
19*0a6a1f1dSLionel Sambuc {
20*0a6a1f1dSLionel Sambuc public:
Counter()21*0a6a1f1dSLionel Sambuc     Counter() : data_()                             { ++gConstructed; }
Counter(const T & data)22*0a6a1f1dSLionel Sambuc     Counter(const T &data) : data_(data)            { ++gConstructed; }
Counter(const Counter & rhs)23*0a6a1f1dSLionel Sambuc     Counter(const Counter& rhs) : data_(rhs.data_)  { ++gConstructed; }
24*0a6a1f1dSLionel Sambuc     Counter& operator=(const Counter& rhs)          { ++gConstructed; data_ = rhs.data_; return *this; }
25*0a6a1f1dSLionel Sambuc #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Counter(Counter && rhs)26*0a6a1f1dSLionel Sambuc     Counter(Counter&& rhs) : data_(std::move(rhs.data_))  { ++gConstructed; }
27*0a6a1f1dSLionel Sambuc     Counter& operator=(Counter&& rhs) { ++gConstructed; data_ = std::move(rhs.data_); return *this; }
28*0a6a1f1dSLionel Sambuc #endif
~Counter()29*0a6a1f1dSLionel Sambuc     ~Counter() { --gConstructed; }
30*0a6a1f1dSLionel Sambuc 
get()31*0a6a1f1dSLionel Sambuc     const T& get() const {return data_;}
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc     bool operator==(const Counter& x) const {return data_ == x.data_;}
34*0a6a1f1dSLionel Sambuc     bool operator< (const Counter& x) const {return data_ <  x.data_;}
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc private:
37*0a6a1f1dSLionel Sambuc     T data_;
38*0a6a1f1dSLionel Sambuc };
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc int Counter_base::gConstructed = 0;
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc namespace std {
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc template <class T>
45*0a6a1f1dSLionel Sambuc struct hash<Counter<T> >
46*0a6a1f1dSLionel Sambuc     : public std::unary_function<Counter<T>, std::size_t>
47*0a6a1f1dSLionel Sambuc {
48*0a6a1f1dSLionel Sambuc     std::size_t operator()(const Counter<T>& x) const {return std::hash<T>(x.get());}
49*0a6a1f1dSLionel Sambuc };
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc #endif
53