counter.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef BOOST_CONTRACT_TEST_DETAIL_COUNTER_HPP_
  2. #define BOOST_CONTRACT_TEST_DETAIL_COUNTER_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. namespace boost { namespace contract { namespace test { namespace detail {
  8. // Helper to count copies and evaluations of type (e.g., for old values).
  9. template<class Tag, typename T>
  10. struct counter {
  11. T value;
  12. counter() : value() { ++ctors_; }
  13. static unsigned ctors() { return ctors_; }
  14. ~counter() { ++dtors_; }
  15. static unsigned dtors() { return dtors_; }
  16. /* implicit */ counter(counter const& other) : value(other.value) {
  17. ++ctor_copies_;
  18. ++ctors_;
  19. }
  20. counter& operator=(counter const& other) {
  21. value = other.value;
  22. ++op_copies_;
  23. return *this;
  24. }
  25. static unsigned copies() { return ctor_copies_ + op_copies_; }
  26. static counter const& eval(counter const& me) { ++me.evals_; return me; }
  27. static unsigned evals() { return evals_; }
  28. private:
  29. static unsigned ctors_; // Total constructions (including copies).
  30. static unsigned dtors_;
  31. static unsigned ctor_copies_;
  32. static unsigned op_copies_;
  33. static unsigned evals_;
  34. };
  35. template<class Tag, typename T> unsigned counter<Tag, T>::ctors_ = 0;
  36. template<class Tag, typename T> unsigned counter<Tag, T>::dtors_ = 0;
  37. template<class Tag, typename T> unsigned counter<Tag, T>::ctor_copies_ = 0;
  38. template<class Tag, typename T> unsigned counter<Tag, T>::op_copies_ = 0;
  39. template<class Tag, typename T> unsigned counter<Tag, T>::evals_ = 0;
  40. } } } } // namespace
  41. #endif // #include guard