tracked.h
Go to the documentation of this file.
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 
15 #ifndef ABSL_CONTAINER_INTERNAL_TRACKED_H_
16 #define ABSL_CONTAINER_INTERNAL_TRACKED_H_
17 
18 #include <stddef.h>
19 #include <memory>
20 #include <utility>
21 
22 namespace absl {
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  public:
29  Tracked() {}
30  // NOLINTNEXTLINE(runtime/explicit)
31  Tracked(const T& val) : val_(val) {}
32  Tracked(const Tracked& that)
33  : val_(that.val_),
34  num_moves_(that.num_moves_),
35  num_copies_(that.num_copies_) {
36  ++(*num_copies_);
37  }
38  Tracked(Tracked&& that)
39  : val_(std::move(that.val_)),
40  num_moves_(std::move(that.num_moves_)),
42  ++(*num_moves_);
43  }
44  Tracked& operator=(const Tracked& that) {
45  val_ = that.val_;
46  num_moves_ = that.num_moves_;
47  num_copies_ = that.num_copies_;
48  ++(*num_copies_);
49  }
51  val_ = std::move(that.val_);
52  num_moves_ = std::move(that.num_moves_);
53  num_copies_ = std::move(that.num_copies_);
54  ++(*num_moves_);
55  }
56 
57  const T& val() const { return val_; }
58 
59  friend bool operator==(const Tracked& a, const Tracked& b) {
60  return a.val_ == b.val_;
61  }
62  friend bool operator!=(const Tracked& a, const Tracked& b) {
63  return !(a == b);
64  }
65 
66  size_t num_copies() { return *num_copies_; }
67  size_t num_moves() { return *num_moves_; }
68 
69  private:
70  T val_;
71  std::shared_ptr<size_t> num_moves_ = std::make_shared<size_t>(0);
72  std::shared_ptr<size_t> num_copies_ = std::make_shared<size_t>(0);
73 };
74 
75 } // namespace container_internal
76 } // namespace absl
77 
78 #endif // ABSL_CONTAINER_INTERNAL_TRACKED_H_
friend bool operator!=(const Tracked &a, const Tracked &b)
Definition: tracked.h:62
Definition: algorithm.h:29
std::shared_ptr< size_t > num_copies_
Definition: tracked.h:72
Tracked & operator=(const Tracked &that)
Definition: tracked.h:44
friend bool operator==(const Tracked &a, const Tracked &b)
Definition: tracked.h:59
Tracked(const Tracked &that)
Definition: tracked.h:32
std::shared_ptr< size_t > num_moves_
Definition: tracked.h:71
uint64_t b
Definition: layout_test.cc:50
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
const T & val() const
Definition: tracked.h:57
Tracked & operator=(Tracked &&that)
Definition: tracked.h:50


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:37