diff --git a/modules/juce_core/native/juce_CFHelpers_mac.h b/modules/juce_core/native/juce_CFHelpers_mac.h index f134fa45fc..2af51295a0 100644 --- a/modules/juce_core/native/juce_CFHelpers_mac.h +++ b/modules/juce_core/native/juce_CFHelpers_mac.h @@ -38,10 +38,9 @@ namespace juce { -template struct CFObjectDeleter { - void operator() (CFType object) const noexcept + void operator() (CFTypeRef object) const noexcept { if (object != nullptr) CFRelease (object); @@ -49,7 +48,17 @@ struct CFObjectDeleter }; template -using CFUniquePtr = std::unique_ptr, CFObjectDeleter>; +struct CFRefRemover +{ + static_assert (std::is_pointer_v, "CFType must be a Ref type"); + using Type = std::remove_pointer_t; +}; + +template +using CFRemoveRef = typename CFRefRemover::Type; + +template +using CFUniquePtr = std::unique_ptr, CFObjectDeleter>; template struct CFObjectHolder diff --git a/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.h b/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.h index 3c0d2d4a09..abb29dd6bb 100644 --- a/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.h +++ b/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.h @@ -37,43 +37,14 @@ namespace juce namespace detail { - struct ColorSpaceDelete - { - void operator() (CGColorSpaceRef ptr) const noexcept { CGColorSpaceRelease (ptr); } - }; - - struct ContextDelete - { - void operator() (CGContextRef ptr) const noexcept { CGContextRelease (ptr); } - }; - - struct DataProviderDelete - { - void operator() (CGDataProviderRef ptr) const noexcept { CGDataProviderRelease (ptr); } - }; - - struct ImageDelete - { - void operator() (CGImageRef ptr) const noexcept { CGImageRelease (ptr); } - }; - - struct GradientDelete - { - void operator() (CGGradientRef ptr) const noexcept { CGGradientRelease (ptr); } - }; - - struct ColorDelete - { - void operator() (CGColorRef ptr) const noexcept { CGColorRelease (ptr); } - }; - - //============================================================================== - using ColorSpacePtr = std::unique_ptr; - using ContextPtr = std::unique_ptr; - using DataProviderPtr = std::unique_ptr; - using ImagePtr = std::unique_ptr; - using GradientPtr = std::unique_ptr; - using ColorPtr = std::unique_ptr; + using ColorSpacePtr = CFUniquePtr; + using ContextPtr = CFUniquePtr; + using DataProviderPtr = CFUniquePtr; + using ImagePtr = CFUniquePtr; + using GradientPtr = CFUniquePtr; + using ColorPtr = CFUniquePtr; + using PathPtr = CFUniquePtr; + using MutablePathPtr = CFUniquePtr; } //============================================================================== diff --git a/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.mm b/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.mm index f31634d35e..27e24f14c0 100644 --- a/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.mm +++ b/modules/juce_graphics/native/juce_CoreGraphicsContext_mac.mm @@ -637,10 +637,9 @@ void CoreGraphicsContext::drawRoundedRectangle (const Rectangle& r, float CGContextSetLineCap (context.get(), kCGLineCapButt); CGContextSetLineJoin (context.get(), kCGLineJoinMiter); - CGPathRef path = CGPathCreateWithRoundedRect (convertToCGRectFlipped (r), cornerSize, cornerSize, nullptr); - CGContextAddPath (context.get(), path); + detail::PathPtr path { CGPathCreateWithRoundedRect (convertToCGRectFlipped (r), cornerSize, cornerSize, nullptr) }; + CGContextAddPath (context.get(), path.get()); drawCurrentPath (kCGPathStroke); - CGPathRelease (path); } else { @@ -652,28 +651,26 @@ void CoreGraphicsContext::drawRoundedRectangle (const Rectangle& r, float const auto insideRect = r.reduced (lineThickness); const auto insideCornerSize = cornerSize - lineThickness; - CGMutablePathRef path = CGPathCreateMutable(); - CGPathAddRoundedRect (path, nullptr, convertToCGRectFlipped (outsideRect), + detail::MutablePathPtr path { CGPathCreateMutable() }; + CGPathAddRoundedRect (path.get(), nullptr, convertToCGRectFlipped (outsideRect), jmin (outsideCornerSize, outsideRect.getWidth() / 2.0f), jmin (outsideCornerSize, outsideRect.getHeight() / 2.0f)); - CGPathAddRoundedRect (path, nullptr, convertToCGRectFlipped (insideRect), + CGPathAddRoundedRect (path.get(), nullptr, convertToCGRectFlipped (insideRect), jmin (insideCornerSize, insideRect.getWidth() / 2.0f), jmin (insideCornerSize, insideRect.getHeight() / 2.0f)); - CGContextAddPath (context.get(), path); + CGContextAddPath (context.get(), path.get()); drawCurrentPath (kCGPathEOFill); - CGPathRelease (path); } } void CoreGraphicsContext::fillRoundedRectangle (const Rectangle& r, float cornerSize) { CGContextBeginPath (context.get()); - CGPathRef path = CGPathCreateWithRoundedRect (convertToCGRectFlipped (r), cornerSize, cornerSize, nullptr); - CGContextAddPath (context.get(), path); + detail::PathPtr path { CGPathCreateWithRoundedRect (convertToCGRectFlipped (r), cornerSize, cornerSize, nullptr) }; + CGContextAddPath (context.get(), path.get()); drawCurrentPath (kCGPathFill); - CGPathRelease (path); } void CoreGraphicsContext::drawLineWithThickness (const Line& line, float lineThickness)