Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2025 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include "ReferenceCountedHandle.h"
21 : #include <lib/support/CodeUtils.h>
22 :
23 : namespace chip {
24 :
25 : /** ReferenceCountedPtr acts like a shared_ptr to an object derived from ReferenceCounted. */
26 : template <typename T>
27 : class ReferenceCountedPtr
28 : {
29 : public:
30 136584 : ReferenceCountedPtr() : mRefCounted(nullptr) {}
31 874 : ReferenceCountedPtr(T * releasable) : mRefCounted(releasable ? releasable->Ref() : nullptr) {}
32 179 : ReferenceCountedPtr(const ReferenceCountedPtr & src) : ReferenceCountedPtr(src.mRefCounted) {}
33 : ReferenceCountedPtr(const ReferenceCountedHandle<T> & src) : ReferenceCountedPtr(&src.Get()) {}
34 73 : ReferenceCountedPtr(ReferenceCountedPtr && src) noexcept : mRefCounted(src.mRefCounted) { src.mRefCounted = nullptr; }
35 :
36 321 : ReferenceCountedPtr & operator=(const ReferenceCountedPtr & src)
37 : {
38 321 : Set(src.mRefCounted);
39 321 : return *this;
40 : }
41 229 : ReferenceCountedPtr & operator=(ReferenceCountedPtr && src) noexcept
42 : {
43 229 : if (this != &src)
44 : {
45 229 : Release();
46 229 : mRefCounted = src.mRefCounted;
47 229 : src.mRefCounted = nullptr;
48 : }
49 229 : return *this;
50 : }
51 :
52 137531 : ~ReferenceCountedPtr() { Release(); }
53 :
54 966 : inline T * operator->() const { return mRefCounted; }
55 128 : inline T & operator*() const { return *mRefCounted; }
56 :
57 143 : inline operator bool() { return mRefCounted != nullptr; }
58 1086 : inline bool IsNull() const { return mRefCounted == nullptr; }
59 :
60 61 : inline bool operator==(const ReferenceCountedPtr & other) const { return this->mRefCounted == other.mRefCounted; }
61 2 : inline bool operator!=(const ReferenceCountedPtr & other) const { return this->mRefCounted != other.mRefCounted; }
62 : inline bool operator==(const ReferenceCountedHandle<T> & other) const { return this->mRefCounted == &other.Get(); }
63 : inline bool operator!=(const ReferenceCountedHandle<T> & other) const { return this->mRefCounted != &other.Get(); }
64 29 : inline bool operator==(const T & other) const { return this->mRefCounted == &other; }
65 : inline bool operator!=(const T & other) const { return this->mRefCounted != &other; }
66 :
67 138025 : void Release()
68 : {
69 138025 : VerifyOrReturn(mRefCounted != nullptr);
70 631 : mRefCounted->Unref();
71 631 : mRefCounted = nullptr;
72 : }
73 :
74 321 : void Set(T * releasable)
75 : {
76 321 : if (mRefCounted != releasable)
77 : {
78 82 : Release();
79 82 : mRefCounted = releasable ? releasable->Ref() : nullptr;
80 : }
81 321 : }
82 :
83 : protected:
84 : T * mRefCounted = nullptr;
85 : };
86 :
87 : } // namespace chip
|