YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
78.7M
  clone_ptr() noexcept : t_(nullptr) {}
31
2.01M
  clone_ptr(T* t) noexcept : t_(t) {} // NOLINT
32
33
1.47M
  clone_ptr(const clone_ptr& rhs) : t_(rhs.t_ ? rhs.t_->Clone().release() : nullptr) {}
34
35
73.4M
  clone_ptr(clone_ptr&& rhs) noexcept : t_(rhs.release()) {}
36
37
744k
  void operator=(const clone_ptr& rhs) noexcept {
38
744k
    reset();
39
744k
    if (rhs.t_) {
40
173k
      t_ = rhs.t_->Clone().release();
41
173k
    }
42
744k
  }
43
44
1.22M
  void operator=(clone_ptr&& rhs) noexcept {
45
1.22M
    reset();
46
1.22M
    t_ = rhs.release();
47
1.22M
  }
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
1.68M
  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
157M
  ~clone_ptr() {
73
157M
    reset();
74
157M
  }
75
76
162M
  void reset(T* t = nullptr) {
77
162M
    delete t_;
78
162M
    t_ = t;
79
162M
  }
80
81
74.6M
  T* release() {
82
74.6M
    T* result = t_;
83
74.6M
    t_ = nullptr;
84
74.6M
    return result;
85
74.6M
  }
86
87
150M
  T* get() const {
88
150M
    return t_;
89
150M
  }
90
91
3.75M
  T* operator->() const {
92
3.75M
    return get();
93
3.75M
  }
94
95
286k
  T& operator*() const {
96
286k
    return *get();
97
286k
  }
98
99
16.1M
  explicit operator bool() const {
100
16.1M
    return t_ != nullptr;
101
16.1M
  }
102
103
 private:
104
  T* t_;
105
};
106
107
} // namespace yb
108
109
#endif // YB_UTIL_CLONE_PTR_H