这个类提供了最基本的引用计数管理,界面库中,经常都需要消息发送,而带来的后果就是不知道消息中包含的对象是否还存在,如果不能很好管理的话就容易出现访问销毁了的对象这样的情况,所以,juce的界面无素也基于引用计数是个不错的选择
#ifndef JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED #define JUCE_REFERENCECOUNTEDOBJECT_H_INCLUDED //============================================================================== /** A base class which provides methods for reference-counting. To add reference-counting to a class, derive it from this class, and use the ReferenceCountedObjectPtr class to point to it. e.g. @code class MyClass : public ReferenceCountedObject { void foo(); // This is a neat way of declaring a typedef for a pointer class, // rather than typing out the full templated name each time.. typedef ReferenceCountedObjectPtr<MyClass> Ptr; }; MyClass::Ptr p = new MyClass(); MyClass::Ptr p2 = p; p = nullptr; p2->foo(); @endcode Once a new ReferenceCountedObject has been assigned to a pointer, be careful not to delete the object manually. This class uses an Atomic<int> value to hold the reference count, so that it the pointers can be passed between threads safely. For a faster but non-thread-safe version, use SingleThreadedReferenceCountedObject instead. @see ReferenceCountedObjectPtr, ReferenceCountedArray, SingleThreadedReferenceCountedObject */ class JUCE_API ReferenceCountedObject { public: //============================================================================== /** Increments the object‘s reference count. This is done automatically by the smart pointer, but is public just in case it‘s needed for nefarious purposes. */ void incReferenceCount() noexcept { ++refCount; } /** Decreases the object‘s reference count. If the count gets to zero, the object will be deleted. */ void decReferenceCount() noexcept { jassert (getReferenceCount() > 0); if (--refCount == 0) delete this; } /** Decreases the object‘s reference count. If the count gets to zero, the object will not be deleted, but this method will return true, allowing the caller to take care of deletion. */ bool decReferenceCountWithoutDeleting() noexcept { jassert (getReferenceCount() > 0); return --refCount == 0; } /** Returns the object‘s current reference count. */ int getReferenceCount() const noexcept { return refCount.get(); } protected: //============================================================================== /** Creates the reference-counted object (with an initial ref count of zero). */ ReferenceCountedObject() {} /** Destructor. */ virtual ~ReferenceCountedObject() { // it‘s dangerous to delete an object that‘s still referenced by something else! jassert (getReferenceCount() == 0); } /** Resets the reference count to zero without deleting the object. You should probably never need to use this! */ void resetReferenceCount() noexcept { refCount = 0; } private: //============================================================================== Atomic <int> refCount; friend struct ContainerDeletePolicy<ReferenceCountedObject>; JUCE_DECLARE_NON_COPYABLE (ReferenceCountedObject) };
时间: 2024-10-13 10:52:09