You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tracked.h 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_CONTAINER_INTERNAL_TRACKED_H_
  15. #define ABSL_CONTAINER_INTERNAL_TRACKED_H_
  16. #include <stddef.h>
  17. #include <memory>
  18. #include <utility>
  19. #include "absl/base/config.h"
  20. namespace absl
  21. {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace container_internal
  24. {
  25. // A class that tracks its copies and moves so that it can be queried in tests.
  26. template<class T>
  27. class Tracked
  28. {
  29. public:
  30. Tracked()
  31. {
  32. }
  33. // NOLINTNEXTLINE(runtime/explicit)
  34. Tracked(const T& val) :
  35. val_(val)
  36. {
  37. }
  38. Tracked(const Tracked& that) :
  39. val_(that.val_),
  40. num_moves_(that.num_moves_),
  41. num_copies_(that.num_copies_)
  42. {
  43. ++(*num_copies_);
  44. }
  45. Tracked(Tracked&& that) :
  46. val_(std::move(that.val_)),
  47. num_moves_(std::move(that.num_moves_)),
  48. num_copies_(std::move(that.num_copies_))
  49. {
  50. ++(*num_moves_);
  51. }
  52. Tracked& operator=(const Tracked& that)
  53. {
  54. val_ = that.val_;
  55. num_moves_ = that.num_moves_;
  56. num_copies_ = that.num_copies_;
  57. ++(*num_copies_);
  58. }
  59. Tracked& operator=(Tracked&& that)
  60. {
  61. val_ = std::move(that.val_);
  62. num_moves_ = std::move(that.num_moves_);
  63. num_copies_ = std::move(that.num_copies_);
  64. ++(*num_moves_);
  65. }
  66. const T& val() const
  67. {
  68. return val_;
  69. }
  70. friend bool operator==(const Tracked& a, const Tracked& b)
  71. {
  72. return a.val_ == b.val_;
  73. }
  74. friend bool operator!=(const Tracked& a, const Tracked& b)
  75. {
  76. return !(a == b);
  77. }
  78. size_t num_copies()
  79. {
  80. return *num_copies_;
  81. }
  82. size_t num_moves()
  83. {
  84. return *num_moves_;
  85. }
  86. private:
  87. T val_;
  88. std::shared_ptr<size_t> num_moves_ = std::make_shared<size_t>(0);
  89. std::shared_ptr<size_t> num_copies_ = std::make_shared<size_t>(0);
  90. };
  91. } // namespace container_internal
  92. ABSL_NAMESPACE_END
  93. } // namespace absl
  94. #endif // ABSL_CONTAINER_INTERNAL_TRACKED_H_