Home | History | Annotate | Download | only in support
      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 
     15 struct Counter_base { static int gConstructed; };
     16 
     17 template <typename T>
     18 class Counter : public Counter_base
     19 {
     20 public:
     21     Counter() : data_()                             { ++gConstructed; }
     22     Counter(const T &data) : data_(data)            { ++gConstructed; }
     23     Counter(const Counter& rhs) : data_(rhs.data_)  { ++gConstructed; }
     24     Counter& operator=(const Counter& rhs)          { ++gConstructed; data_ = rhs.data_; return *this; }
     25 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     26     Counter(Counter&& rhs) : data_(std::move(rhs.data_))  { ++gConstructed; }
     27     Counter& operator=(Counter&& rhs) { ++gConstructed; data_ = std::move(rhs.data_); return *this; }
     28 #endif
     29     ~Counter() { --gConstructed; }
     30 
     31     const T& get() const {return data_;}
     32 
     33     bool operator==(const Counter& x) const {return data_ == x.data_;}
     34     bool operator< (const Counter& x) const {return data_ <  x.data_;}
     35 
     36 private:
     37     T data_;
     38 };
     39 
     40 int Counter_base::gConstructed = 0;
     41 
     42 namespace std {
     43 
     44 template <class T>
     45 struct hash<Counter<T> >
     46     : public std::unary_function<Counter<T>, std::size_t>
     47 {
     48     std::size_t operator()(const Counter<T>& x) const {return std::hash<T>(x.get());}
     49 };
     50 }
     51 
     52 #endif
     53