YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/clone_ptr.h
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_CLONE_PTR_H
15
#define YB_UTIL_CLONE_PTR_H
16
17
#include <memory>
18
19
namespace yb {
20
21
// A utility class that owns a cloneable pointer.
22
//
23
// This class uniquely owns a single object, so this object is destroyed with the clone_ptr
24
// instance.  During copying of a clone_ptr it clones the owned object, which should provide a Clone
25
// function.  This can be used to implement value types that are accessible via an abstract
26
// interface.
27
template <class T>
28
class clone_ptr {
29
 public:
30
201M
  clone_ptr() noexcept : t_(nullptr) {}
31
20.6M
  clone_ptr(T* t) noexcept : t_(t) {} // NOLINT
32
33
1.70M
  clone_ptr(const clone_ptr& rhs) : t_(rhs.t_ ? rhs.t_->Clone().release() : nullptr) {}
34
35
177M
  clone_ptr(clone_ptr&& rhs) noexcept : t_(rhs.release()) {}
36
37
1.29M
  void operator=(const clone_ptr& rhs) noexcept {
38
1.29M
    reset();
39
1.29M
    if (rhs.t_) {
40
656k
      t_ = rhs.t_->Clone().release();
41
656k
    }
42
1.29M
  }
43
44
3.82M
  void operator=(clone_ptr&& rhs) noexcept {
45
3.82M
    reset();
46
3.82M
    t_ = rhs.release();
47
3.82M
  }
48
49
  template <class U>
50
  clone_ptr(const clone_ptr<U>& rhs) : t_(rhs.t_ ? rhs.t_->Clone().release() : nullptr) {}
51
52
  template <class U>
53
  clone_ptr(clone_ptr<U>&& rhs) noexcept : t_(rhs.release()) {}
54
55
  template <class U>
56
8.41M
  clone_ptr(std::unique_ptr<U>&& rhs) noexcept : t_(rhs.release()) { } // NOLINT
57
58
  template <class U>
59
  void operator=(const clone_ptr<U>& rhs) {
60
    reset();
61
    if (rhs.t_) {
62
      t_ = rhs.t_->Clone();
63
    }
64
  }
65
66
  template <class U>
67
  void operator=(clone_ptr<U>&& rhs) noexcept {
68
    reset();
69
    t_ = rhs.release();
70
  }
71
72
410M
  ~clone_ptr() {
73
410M
    reset();
74
410M
  }
75
76
419M
  void reset(T* t = nullptr) {
77
419M
    delete t_;
78
419M
    t_ = t;
79
419M
  }
80
81
181M
  T* release() {
82
181M
    T* result = t_;
83
181M
    t_ = nullptr;
84
181M
    return result;
85
181M
  }
86
87
360M
  T* get() const {
88
360M
    return t_;
89
360M
  }
90
91
12.9M
  T* operator->() const {
92
12.9M
    return get();
93
12.9M
  }
94
95
371k
  T& operator*() const {
96
371k
    return *get();
97
371k
  }
98
99
86.5M
  explicit operator bool() const {
100
86.5M
    return t_ != nullptr;
101
86.5M
  }
102
103
 private:
104
  T* t_;
105
};
106
107
} // namespace yb
108
109
#endif // YB_UTIL_CLONE_PTR_H