Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:31:40

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #ifndef ABSL_CONTAINER_INTERNAL_TRACKED_H_
0016 #define ABSL_CONTAINER_INTERNAL_TRACKED_H_
0017 
0018 #include <stddef.h>
0019 
0020 #include <memory>
0021 #include <utility>
0022 
0023 #include "absl/base/config.h"
0024 
0025 namespace absl {
0026 ABSL_NAMESPACE_BEGIN
0027 namespace container_internal {
0028 
0029 // A class that tracks its copies and moves so that it can be queried in tests.
0030 template <class T>
0031 class Tracked {
0032  public:
0033   Tracked() {}
0034   // NOLINTNEXTLINE(runtime/explicit)
0035   Tracked(const T& val) : val_(val) {}
0036   Tracked(const Tracked& that)
0037       : val_(that.val_),
0038         num_moves_(that.num_moves_),
0039         num_copies_(that.num_copies_) {
0040     ++(*num_copies_);
0041   }
0042   Tracked(Tracked&& that)
0043       : val_(std::move(that.val_)),
0044         num_moves_(std::move(that.num_moves_)),
0045         num_copies_(std::move(that.num_copies_)) {
0046     ++(*num_moves_);
0047   }
0048   Tracked& operator=(const Tracked& that) {
0049     val_ = that.val_;
0050     num_moves_ = that.num_moves_;
0051     num_copies_ = that.num_copies_;
0052     ++(*num_copies_);
0053   }
0054   Tracked& operator=(Tracked&& that) {
0055     val_ = std::move(that.val_);
0056     num_moves_ = std::move(that.num_moves_);
0057     num_copies_ = std::move(that.num_copies_);
0058     ++(*num_moves_);
0059   }
0060 
0061   const T& val() const { return val_; }
0062 
0063   friend bool operator==(const Tracked& a, const Tracked& b) {
0064     return a.val_ == b.val_;
0065   }
0066   friend bool operator!=(const Tracked& a, const Tracked& b) {
0067     return !(a == b);
0068   }
0069 
0070   size_t num_copies() { return *num_copies_; }
0071   size_t num_moves() { return *num_moves_; }
0072 
0073  private:
0074   T val_;
0075   std::shared_ptr<size_t> num_moves_ = std::make_shared<size_t>(0);
0076   std::shared_ptr<size_t> num_copies_ = std::make_shared<size_t>(0);
0077 };
0078 
0079 }  // namespace container_internal
0080 ABSL_NAMESPACE_END
0081 }  // namespace absl
0082 
0083 #endif  // ABSL_CONTAINER_INTERNAL_TRACKED_H_