Debian Codegraph @

tests/tracked.h

raw
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 PHMAP_PRIV_TRACKED_H_
16 #define PHMAP_PRIV_TRACKED_H_
17
18 #include <stddef.h>
19 #include <memory>
20 #include <utility>
21
22 namespace phmap {
23 namespace priv {
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_)),
41 num_copies_(std::move(that.num_copies_)) {
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 }
50 Tracked& operator=(Tracked&& that) {
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 priv
76 } // namespace phmap
77
78 #endif // PHMAP_PRIV_TRACKED_H_