swap_test_class.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2007-2008 Joseph Gauterin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Tests class used by the Boost.Swap tests
  7. #ifndef BOOST_UTILITY_SWAP_TEST_CLASS_HPP
  8. #define BOOST_UTILITY_SWAP_TEST_CLASS_HPP
  9. class swap_test_class
  10. {
  11. int m_data;
  12. public:
  13. explicit swap_test_class(int arg = 0)
  14. :
  15. m_data(arg)
  16. {
  17. ++constructCount();
  18. }
  19. ~swap_test_class()
  20. {
  21. ++destructCount();
  22. }
  23. swap_test_class(const swap_test_class& arg)
  24. :
  25. m_data(arg.m_data)
  26. {
  27. ++copyCount();
  28. ++destructCount();
  29. }
  30. swap_test_class& operator=(const swap_test_class& arg)
  31. {
  32. m_data = arg.m_data;
  33. ++copyCount();
  34. return *this;
  35. }
  36. void swap(swap_test_class& other)
  37. {
  38. const int temp = m_data;
  39. m_data = other.m_data;
  40. other.m_data = temp;
  41. ++swapCount();
  42. }
  43. int get_data() const
  44. {
  45. return m_data;
  46. }
  47. void set_data(int arg)
  48. {
  49. m_data = arg;
  50. }
  51. static unsigned int swap_count(){ return swapCount(); }
  52. static unsigned int copy_count(){ return copyCount(); }
  53. static unsigned int construct_count(){ return constructCount(); }
  54. static unsigned int destruct_count(){ return destructCount(); }
  55. static void reset()
  56. {
  57. swapCount() = 0;
  58. copyCount() = 0;
  59. constructCount() = 0;
  60. destructCount() = 0;
  61. }
  62. private:
  63. static unsigned int& swapCount()
  64. {
  65. static unsigned int value = 0;
  66. return value;
  67. }
  68. static unsigned int& copyCount()
  69. {
  70. static unsigned int value = 0;
  71. return value;
  72. }
  73. static unsigned int& constructCount()
  74. {
  75. static unsigned int value = 0;
  76. return value;
  77. }
  78. static unsigned int& destructCount()
  79. {
  80. static unsigned int value = 0;
  81. return value;
  82. }
  83. };
  84. inline bool operator==(const swap_test_class & lhs, const swap_test_class & rhs)
  85. {
  86. return lhs.get_data() == rhs.get_data();
  87. }
  88. inline bool operator!=(const swap_test_class & lhs, const swap_test_class & rhs)
  89. {
  90. return !(lhs == rhs);
  91. }
  92. #endif