1 // Copyright 2025 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // 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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_intrusive_ptr/internal/ref_counted_base.h" 17 #include "pw_intrusive_ptr/intrusive_ptr.h" 18 19 namespace pw { 20 21 // Base class to be used with the IntrusivePtr. Doesn't provide any public 22 // methods. 23 // 24 // Provides an atomic-based reference counting. Atomics are used irrespective of 25 // the settings, which makes it different from the std::shared_ptr (that relies 26 // on the threading support settings to determine if atomics should be used for 27 // the control block or not). 28 // 29 // RefCounted MUST never be used as a pointer type to store derived objects - 30 // it doesn't provide a virtual destructor. 31 template <typename T> 32 class RefCounted : private internal::RefCountedBase { 33 public: 34 // Type alias for the IntrusivePtr of ref-counted type. 35 using Ptr = IntrusivePtr<T>; 36 37 private: 38 template <typename U> 39 friend class IntrusivePtr; 40 }; 41 42 } // namespace pw 43