mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
MouseCursor: Simplify implementation using shared_ptr instead of manual refcounting
This commit is contained in:
parent
17b8708852
commit
5c50ee4f94
2 changed files with 36 additions and 93 deletions
|
|
@ -45,96 +45,65 @@ class MouseCursor::SharedCursorHandle
|
|||
{
|
||||
public:
|
||||
explicit SharedCursorHandle (const MouseCursor::StandardCursorType type)
|
||||
: handle (createStandardMouseCursor (type)),
|
||||
standardType (type),
|
||||
isStandard (true)
|
||||
: handle (createStandardMouseCursor (type), Deleter { true }),
|
||||
standardType (type)
|
||||
{
|
||||
}
|
||||
|
||||
SharedCursorHandle (const Image& image, Point<int> hotSpot, float scaleFactor)
|
||||
: info (new CustomMouseCursorInfo (image, hotSpot, scaleFactor)),
|
||||
handle (info->create()),
|
||||
standardType (MouseCursor::NormalCursor),
|
||||
isStandard (false)
|
||||
: info (std::make_unique<CustomMouseCursorInfo> (image, hotSpot, scaleFactor)),
|
||||
handle (info->create(), Deleter { false }),
|
||||
standardType (MouseCursor::NormalCursor)
|
||||
{
|
||||
// your hotspot needs to be within the bounds of the image!
|
||||
jassert (image.getBounds().contains (hotSpot));
|
||||
}
|
||||
|
||||
~SharedCursorHandle()
|
||||
static std::shared_ptr<SharedCursorHandle> createStandard (const MouseCursor::StandardCursorType type)
|
||||
{
|
||||
deleteMouseCursor (handle, isStandard);
|
||||
}
|
||||
if (! isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes))
|
||||
return nullptr;
|
||||
|
||||
static SharedCursorHandle* createStandard (const MouseCursor::StandardCursorType type)
|
||||
{
|
||||
jassert (isPositiveAndBelow (type, MouseCursor::NumStandardCursorTypes));
|
||||
static SpinLock mutex;
|
||||
static std::array<std::weak_ptr<SharedCursorHandle>, MouseCursor::NumStandardCursorTypes> cursors;
|
||||
|
||||
const SpinLock::ScopedLockType sl (lock);
|
||||
auto& c = getSharedCursor (type);
|
||||
const SpinLock::ScopedLockType sl (mutex);
|
||||
|
||||
if (c == nullptr)
|
||||
c = new SharedCursorHandle (type);
|
||||
else
|
||||
c->retain();
|
||||
auto& weak = cursors[type];
|
||||
|
||||
return c;
|
||||
if (auto strong = weak.lock())
|
||||
return strong;
|
||||
|
||||
auto strong = std::make_shared<SharedCursorHandle> (type);
|
||||
weak = strong;
|
||||
return strong;
|
||||
}
|
||||
|
||||
bool isStandardType (MouseCursor::StandardCursorType type) const noexcept
|
||||
{
|
||||
return type == standardType && isStandard;
|
||||
return type == standardType && handle.get_deleter().isStandard;
|
||||
}
|
||||
|
||||
SharedCursorHandle* retain() noexcept
|
||||
{
|
||||
++refCount;
|
||||
return this;
|
||||
}
|
||||
|
||||
void release()
|
||||
{
|
||||
if (--refCount == 0)
|
||||
{
|
||||
if (isStandard)
|
||||
{
|
||||
const SpinLock::ScopedLockType sl (lock);
|
||||
getSharedCursor (standardType) = nullptr;
|
||||
}
|
||||
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
void* getHandle() const noexcept { return handle; }
|
||||
void setHandle (void* newHandle) { handle = newHandle; }
|
||||
|
||||
void* getHandle() const noexcept { return handle.get(); }
|
||||
MouseCursor::StandardCursorType getType() const noexcept { return standardType; }
|
||||
CustomMouseCursorInfo* getCustomInfo() const noexcept { return info.get(); }
|
||||
|
||||
private:
|
||||
std::unique_ptr<CustomMouseCursorInfo> info;
|
||||
void* handle;
|
||||
Atomic<int> refCount { 1 };
|
||||
const MouseCursor::StandardCursorType standardType;
|
||||
const bool isStandard;
|
||||
static SpinLock lock;
|
||||
|
||||
static SharedCursorHandle*& getSharedCursor (const MouseCursor::StandardCursorType type)
|
||||
struct Deleter
|
||||
{
|
||||
static SharedCursorHandle* cursors[MouseCursor::NumStandardCursorTypes] = {};
|
||||
return cursors[type];
|
||||
}
|
||||
void operator() (void* ptr) const noexcept { deleteMouseCursor (ptr, isStandard); }
|
||||
const bool isStandard;
|
||||
};
|
||||
|
||||
std::unique_ptr<CustomMouseCursorInfo> info;
|
||||
std::unique_ptr<void, Deleter> handle;
|
||||
const MouseCursor::StandardCursorType standardType;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedCursorHandle)
|
||||
};
|
||||
|
||||
SpinLock MouseCursor::SharedCursorHandle::lock;
|
||||
|
||||
//==============================================================================
|
||||
MouseCursor::MouseCursor() noexcept
|
||||
{
|
||||
}
|
||||
MouseCursor::MouseCursor() noexcept = default;
|
||||
|
||||
MouseCursor::MouseCursor (const StandardCursorType type)
|
||||
: cursorHandle (type != MouseCursor::NormalCursor ? SharedCursorHandle::createStandard (type) : nullptr)
|
||||
|
|
@ -147,44 +116,19 @@ MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY)
|
|||
}
|
||||
|
||||
MouseCursor::MouseCursor (const Image& image, int hotSpotX, int hotSpotY, float scaleFactor)
|
||||
: cursorHandle (new SharedCursorHandle (image, { hotSpotX, hotSpotY }, scaleFactor))
|
||||
: cursorHandle (std::make_shared<SharedCursorHandle> (image, Point<int> { hotSpotX, hotSpotY }, scaleFactor))
|
||||
{
|
||||
}
|
||||
|
||||
MouseCursor::MouseCursor (const MouseCursor& other)
|
||||
: cursorHandle (other.cursorHandle == nullptr ? nullptr : other.cursorHandle->retain())
|
||||
{
|
||||
}
|
||||
MouseCursor::MouseCursor (const MouseCursor&) = default;
|
||||
|
||||
MouseCursor::~MouseCursor()
|
||||
{
|
||||
if (cursorHandle != nullptr)
|
||||
cursorHandle->release();
|
||||
}
|
||||
MouseCursor::~MouseCursor() = default;
|
||||
|
||||
MouseCursor& MouseCursor::operator= (const MouseCursor& other)
|
||||
{
|
||||
if (other.cursorHandle != nullptr)
|
||||
other.cursorHandle->retain();
|
||||
MouseCursor& MouseCursor::operator= (const MouseCursor&) = default;
|
||||
|
||||
if (cursorHandle != nullptr)
|
||||
cursorHandle->release();
|
||||
MouseCursor::MouseCursor (MouseCursor&&) noexcept = default;
|
||||
|
||||
cursorHandle = other.cursorHandle;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MouseCursor::MouseCursor (MouseCursor&& other) noexcept
|
||||
: cursorHandle (other.cursorHandle)
|
||||
{
|
||||
other.cursorHandle = nullptr;
|
||||
}
|
||||
|
||||
MouseCursor& MouseCursor::operator= (MouseCursor&& other) noexcept
|
||||
{
|
||||
std::swap (cursorHandle, other.cursorHandle);
|
||||
return *this;
|
||||
}
|
||||
MouseCursor& MouseCursor::operator= (MouseCursor&&) noexcept = default;
|
||||
|
||||
bool MouseCursor::operator== (const MouseCursor& other) const noexcept
|
||||
{
|
||||
|
|
|
|||
|
|
@ -166,8 +166,7 @@ public:
|
|||
private:
|
||||
//==============================================================================
|
||||
class SharedCursorHandle;
|
||||
friend class SharedCursorHandle;
|
||||
SharedCursorHandle* cursorHandle = nullptr;
|
||||
std::shared_ptr<SharedCursorHandle> cursorHandle;
|
||||
|
||||
friend class MouseInputSourceInternal;
|
||||
void showInWindow (ComponentPeer*) const;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue