Counter.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===----------------------------------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef COUNTER_H
  9. #define COUNTER_H
  10. #include <functional> // for std::hash
  11. #include "test_macros.h"
  12. struct Counter_base { static int gConstructed; };
  13. template <typename T>
  14. class Counter : public Counter_base
  15. {
  16. public:
  17. Counter() : data_() { ++gConstructed; }
  18. Counter(const T &data) : data_(data) { ++gConstructed; }
  19. Counter(const Counter& rhs) : data_(rhs.data_) { ++gConstructed; }
  20. Counter& operator=(const Counter& rhs) { data_ = rhs.data_; return *this; }
  21. #if TEST_STD_VER >= 11
  22. Counter(Counter&& rhs) : data_(std::move(rhs.data_)) { ++gConstructed; }
  23. Counter& operator=(Counter&& rhs) { ++gConstructed; data_ = std::move(rhs.data_); return *this; }
  24. #endif
  25. ~Counter() { --gConstructed; }
  26. const T& get() const {return data_;}
  27. bool operator==(const Counter& x) const {return data_ == x.data_;}
  28. bool operator< (const Counter& x) const {return data_ < x.data_;}
  29. private:
  30. T data_;
  31. };
  32. int Counter_base::gConstructed = 0;
  33. namespace std {
  34. template <class T>
  35. struct hash<Counter<T> >
  36. {
  37. typedef Counter<T> argument_type;
  38. typedef std::size_t result_type;
  39. std::size_t operator()(const Counter<T>& x) const {return std::hash<T>()(x.get());}
  40. };
  41. }
  42. #endif