Skip to content

Commit 292a3bd

Browse files
cocoaui: weakify/strongify macros
1 parent c6946ab commit 292a3bd

File tree

11 files changed

+117
-68
lines changed

11 files changed

+117
-68
lines changed

osx/deadbeef.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@
888888
2D30D20C25E2A8930023A299 /* WidgetTopLevelView.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D30D20A25E2A8930023A299 /* WidgetTopLevelView.h */; };
889889
2D30D20D25E2A8930023A299 /* WidgetTopLevelView.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D30D20A25E2A8930023A299 /* WidgetTopLevelView.h */; };
890890
2D30D20E25E2A8930023A299 /* WidgetTopLevelView.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D30D20B25E2A8930023A299 /* WidgetTopLevelView.m */; };
891+
2D3104502D7B51C600B2E846 /* Weakify.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D31044F2D7B51C300B2E846 /* Weakify.h */; };
891892
2D3138251D08AB3400CBD871 /* artwork.dylib in Copy Plugins */ = {isa = PBXBuildFile; fileRef = 2D621F831CD92C9000EB6D22 /* artwork.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
892893
2D35A24D2B74D95B006D9356 /* filereader.c in Sources */ = {isa = PBXBuildFile; fileRef = 2D35A24B2B74D60F006D9356 /* filereader.c */; };
893894
2D3992E125C2038700CFA936 /* WeakRefWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D3992DF25C2038700CFA936 /* WeakRefWrapper.h */; };
@@ -5597,6 +5598,7 @@
55975598
2D30D0B325E2A5DD0023A299 /* DesignModeState.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DesignModeState.m; sourceTree = "<group>"; };
55985599
2D30D20A25E2A8930023A299 /* WidgetTopLevelView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WidgetTopLevelView.h; sourceTree = "<group>"; };
55995600
2D30D20B25E2A8930023A299 /* WidgetTopLevelView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WidgetTopLevelView.m; sourceTree = "<group>"; };
5601+
2D31044F2D7B51C300B2E846 /* Weakify.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Weakify.h; sourceTree = "<group>"; };
56005602
2D35A24A2B74D60F006D9356 /* filereader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = filereader.h; sourceTree = "<group>"; };
56015603
2D35A24B2B74D60F006D9356 /* filereader.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = filereader.c; sourceTree = "<group>"; };
56025604
2D3992DF25C2038700CFA936 /* WeakRefWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WeakRefWrapper.h; sourceTree = "<group>"; };
@@ -9807,6 +9809,7 @@
98079809
2DB52F7B24CF7CF20046D516 /* TrackContextMenu.m */,
98089810
2DAA402B269AF864006D2754 /* TrackPositionFormatter.h */,
98099811
2DAA402C269AF864006D2754 /* TrackPositionFormatter.m */,
9812+
2D31044F2D7B51C300B2E846 /* Weakify.h */,
98109813
2D3992DF25C2038700CFA936 /* WeakRefWrapper.h */,
98119814
2D3992E025C2038700CFA936 /* WeakRefWrapper.m */,
98129815
);
@@ -14907,6 +14910,7 @@
1490714910
2DB951B626B07E7B00602876 /* decodedblock.h in Headers */,
1490814911
2DDD796325E15ED200FA6FE5 /* PlaylistWidget.h in Headers */,
1490914912
2DC65827274AB03800583E14 /* LyricsWidget.h in Headers */,
14913+
2D3104502D7B51C600B2E846 /* Weakify.h in Headers */,
1491014914
2D0A6B1B237718DA00252E6D /* playmodes.h in Headers */,
1491114915
2D46221D226DBA57003997E9 /* FlippedClipView.h in Headers */,
1491214916
2DD3774627404401007AD315 /* AppearancePreferencesViewController.h in Headers */,

plugins/cocoaui/DdbTabStrip/DdbTabStrip.m

+6-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "PlaylistContextMenu.h"
2929
#import "RenamePlaylistViewController.h"
3030
#import "TrackPropertiesWindowController.h"
31+
#import "Weakify.h"
3132

3233
extern DB_functions_t *deadbeef;
3334

@@ -862,17 +863,17 @@ - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
862863

863864
NSPoint coord = sender.draggingLocation;
864865
coord = [self convertPoint:coord fromView:nil];
865-
__weak DdbTabStrip *weakSelf = self;
866+
weakify(self);
866867
self.pickDragTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:NO block:^(NSTimer * _Nonnull timer) {
867-
DdbTabStrip *tabStrip = weakSelf;
868-
if (tabStrip == nil) {
868+
strongify(self);
869+
if (self == nil) {
869870
return;
870871
}
871-
int tabUnderCursor = [tabStrip tabUnderCursor: coord.x];
872+
int tabUnderCursor = [self tabUnderCursor: coord.x];
872873
if (tabUnderCursor != -1) {
873874
deadbeef->plt_set_curr_idx (tabUnderCursor);
874875
}
875-
tabStrip.pickDragTimer = nil;
876+
self.pickDragTimer = nil;
876877
}];
877878

878879
return NSDragOperationNone;

plugins/cocoaui/KeyboardShortcuts/KeyboardShortcutTextField.m

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#import "KeyboardShortcutTextField.h"
2525
#import "KeyboardShortcutConverter.h"
26+
#import "Weakify.h"
2627

2728
@interface KeyboardShortcutTextField()
2829
@property (nonatomic) id eventMonitor;
@@ -82,17 +83,17 @@ - (BOOL)becomeFirstResponder {
8283
}
8384

8485
NSEventMask eventMask = (NSEventMaskKeyDown | NSEventMaskFlagsChanged);
85-
__weak KeyboardShortcutTextField *weakSelf = self;
86+
weakify(self);
8687
self.eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
87-
KeyboardShortcutTextField *strongSelf = weakSelf;
88+
strongify(self);
8889
if (event.type != NSEventTypeKeyDown) {
8990
return event;
9091
}
91-
if (strongSelf == nil) {
92+
if (self == nil) {
9293
return event;
9394
}
9495

95-
return [strongSelf handleKeyDownEvent:event];
96+
return [self handleKeyDownEvent:event];
9697
}];
9798

9899
return YES;

plugins/cocoaui/MainWindowController.m

+8-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#import "ScriptableSelectViewController.h"
3737
#import "SidebarSplitViewController.h"
3838
#import "TrackPositionFormatter.h"
39+
#import "Weakify.h"
40+
3941
#include <sys/time.h>
4042

4143
extern DB_functions_t *deadbeef;
@@ -175,18 +177,18 @@ - (void)ensureRefresh {
175177
return;
176178
}
177179

178-
__weak MainWindowController *weakSelf = self;
180+
weakify(self);
179181
_updateTimer = [NSTimer timerWithTimeInterval:1.0f/10.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
180-
MainWindowController *strongSelf = weakSelf;
181-
if (strongSelf == nil) {
182+
strongify(self);
183+
if (self == nil) {
182184
return;
183185
}
184186

185-
[strongSelf frameUpdate];
187+
[self frameUpdate];
186188

187189
if (deadbeef->get_output()->state() != DDB_PLAYBACK_STATE_PLAYING) {
188-
[strongSelf->_updateTimer invalidate];
189-
strongSelf->_updateTimer = nil;
190+
[self->_updateTimer invalidate];
191+
self->_updateTimer = nil;
190192
}
191193
}];
192194

plugins/cocoaui/MediaLibrary/MediaLibraryOutlineViewController.m

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "TrackContextMenu.h"
2020
#import "TrackPropertiesWindowController.h"
2121
#import "UndoIntegration.h"
22+
#import "Weakify.h"
2223

2324
extern DB_functions_t *deadbeef;
2425

@@ -221,10 +222,10 @@ - (void)initializeTreeView {
221222

222223
// Restore selected/expanded state
223224
// Defer one frame, since the row indexes are unavailable immediately.
224-
__weak MediaLibraryOutlineViewController *weakSelf = self;
225+
weakify(self);
225226
dispatch_async(dispatch_get_main_queue(), ^{
226-
MediaLibraryOutlineViewController *strongSelf = weakSelf;
227-
if (strongSelf == nil || strongSelf.medialibPlugin == NULL) {
227+
strongify(self);
228+
if (self == nil || self.medialibPlugin == NULL) {
228229
return;
229230
}
230231
[self applyStoredState];

plugins/cocoaui/NowPlayable/NowPlayable.m

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "NowPlayable.h"
1010
#import <MediaPlayer/MediaPlayer.h>
1111
#include <deadbeef/deadbeef.h>
12+
#import "Weakify.h"
1213

1314
extern DB_functions_t *deadbeef;
1415

@@ -58,11 +59,11 @@ - (instancetype)init
5859
[commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changePlaybackPositionCommand:)];
5960
commandCenter.changePlaybackPositionCommand.enabled = YES;
6061

61-
__weak NowPlayable *weakSelf = self;
62+
weakify(self);
6263

6364
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) {
64-
NowPlayable *nowPlayable = weakSelf;
65-
if (!nowPlayable) {
65+
strongify(self);
66+
if (!self) {
6667
return;
6768
}
6869

@@ -93,9 +94,9 @@ - (instancetype)init
9394
.it = it
9495
};
9596

96-
deadbeef->tf_eval (&ctx, nowPlayable.artist_tf, text, sizeof (text));
97+
deadbeef->tf_eval (&ctx, self.artist_tf, text, sizeof (text));
9798
info[MPMediaItemPropertyArtist] = @(text);
98-
deadbeef->tf_eval (&ctx, nowPlayable.album_tf, text, sizeof (text));
99+
deadbeef->tf_eval (&ctx, self.album_tf, text, sizeof (text));
99100
info[MPMediaItemPropertyAlbumTitle] = @(text);
100101

101102
deadbeef->pl_item_unref (it);

plugins/cocoaui/Playlist/PlaylistViewController.m

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "TrackPropertiesManager.h"
3434
#import "tftintutil.h"
3535
#import "DdbPlayItemPasteboardSerializer.h"
36+
#import "Weakify.h"
3637

3738
#include <deadbeef/deadbeef.h>
3839
#include "utf8.h"
@@ -909,17 +910,17 @@ - (void)drawCell:(NSUInteger)idx forRow:(DdbListviewRow_t)row forColumn:(DdbList
909910

910911
if (self.view.window.isVisible
911912
&& deadbeef->get_output()->state() == DDB_PLAYBACK_STATE_PLAYING) {
912-
__weak PlaylistViewController *weakSelf = self;
913+
weakify(self);
913914
self.playPosUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:ctx.update/1000.0 repeats:NO block:^(NSTimer * _Nonnull timer) {
914-
PlaylistViewController *strongSelf = weakSelf;
915-
if (strongSelf == nil) {
915+
strongify(self);
916+
if (self == nil) {
916917
return;
917918
}
918919
ddb_playlist_t *curr = deadbeef->plt_get_curr ();
919-
DB_playItem_t *trk = deadbeef->pl_get_for_idx_and_iter (ctx.idx, [strongSelf playlistIter]);
920+
DB_playItem_t *trk = deadbeef->pl_get_for_idx_and_iter (ctx.idx, [self playlistIter]);
920921

921-
if (ctx.plt == curr && trk == strongSelf.playPosUpdateTrack) {
922-
PlaylistView *lv = (PlaylistView *)strongSelf.view;
922+
if (ctx.plt == curr && trk == self.playPosUpdateTrack) {
923+
PlaylistView *lv = (PlaylistView *)self.view;
923924
[lv.contentView drawRow:(int)idx];
924925
}
925926
if (trk) {

plugins/cocoaui/ReplayGainScannerController.m

+27-25
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323

2424
#import "ReplayGainScannerController.h"
25+
#import "Weakify.h"
26+
2527
#include "rg_scanner.h"
2628
#include <sys/time.h>
2729

@@ -163,16 +165,16 @@ - (void)runScanner:(int)mode forTracks:(DB_playItem_t **)tracks count:(int)count
163165
[self progress:0];
164166

165167
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
166-
__weak ReplayGainScannerController *weakSelf = self;
168+
weakify(self);
167169
dispatch_async(aQueue, ^{
168-
ReplayGainScannerController *strongSelf = weakSelf;
170+
strongify(self);
169171
if (self == nil) {
170172
return;
171173
}
172174
_rg->scan (&self->_rg_settings);
173175
deadbeef->background_job_decrement ();
174176
dispatch_async(dispatch_get_main_queue(), ^{
175-
if (strongSelf.abort_flag) {
177+
if (self.abort_flag) {
176178
[self dismissController:self];
177179
return;
178180
}
@@ -195,31 +197,31 @@ - (void)removeRgTagsFromTracks:(DB_playItem_t **)tracks count:(int)count {
195197
_updateTagsProgressWindow.isVisible = YES;
196198
_abortTagWriting = NO;
197199

198-
__weak ReplayGainScannerController *weakSelf = self;
200+
weakify(self);
199201
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
200202
dispatch_async(aQueue, ^{
201-
ReplayGainScannerController *strongSelf = weakSelf;
202-
if (strongSelf == nil) {
203+
strongify(self);
204+
if (self == nil) {
203205
return;
204206
}
205-
for (int i = 0; i < strongSelf->_rg_settings.num_tracks; i++) {
206-
_rg->remove (strongSelf->_rg_settings.tracks[i]);
207-
if (strongSelf.abortTagWriting) {
207+
for (int i = 0; i < self->_rg_settings.num_tracks; i++) {
208+
_rg->remove (self->_rg_settings.tracks[i]);
209+
if (self.abortTagWriting) {
208210
break;
209211
}
210212
dispatch_async(dispatch_get_main_queue(), ^{
211213
// progress
212214
deadbeef->pl_lock ();
213-
NSString *path = @(deadbeef->pl_find_meta_raw (strongSelf->_rg_settings.tracks[i], ":URI"));
215+
NSString *path = @(deadbeef->pl_find_meta_raw (self->_rg_settings.tracks[i], ":URI"));
214216
deadbeef->pl_unlock ();
215-
strongSelf.updateTagsProgressText.stringValue = path;
216-
strongSelf.updateTagsProgressIndicator.doubleValue = (double)i/strongSelf->_rg_settings.num_tracks*100;
217+
self.updateTagsProgressText.stringValue = path;
218+
self.updateTagsProgressIndicator.doubleValue = (double)i/self->_rg_settings.num_tracks*100;
217219
});
218220
}
219221
deadbeef->pl_save_all ();
220222
deadbeef->background_job_decrement ();
221223
dispatch_async(dispatch_get_main_queue(), ^{
222-
[strongSelf.updateTagsProgressWindow close];
224+
[self.updateTagsProgressWindow close];
223225
[self dismissController:self];
224226
});
225227
});
@@ -299,38 +301,38 @@ - (IBAction)updateFileTagsAction:(id)sender {
299301
_abortTagWriting = NO;
300302
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
301303

302-
__weak ReplayGainScannerController *weakSelf = self;
304+
weakify(self);
303305

304306
dispatch_async(aQueue, ^{
305-
ReplayGainScannerController *strongSelf = weakSelf;
306-
if (strongSelf == nil) {
307+
strongify(self);
308+
if (self == nil) {
307309
return;
308310
}
309-
for (int i = 0; i < strongSelf->_rg_settings.num_tracks; i++) {
310-
if (strongSelf.abortTagWriting) {
311+
for (int i = 0; i < self->_rg_settings.num_tracks; i++) {
312+
if (self.abortTagWriting) {
311313
break;
312314
}
313315

314-
if (strongSelf->_rg_settings.results[i].scan_result == DDB_RG_SCAN_RESULT_SUCCESS) {
316+
if (self->_rg_settings.results[i].scan_result == DDB_RG_SCAN_RESULT_SUCCESS) {
315317
deadbeef->pl_lock ();
316-
NSString *path = @(deadbeef->pl_find_meta_raw (strongSelf->_rg_settings.tracks[i], ":URI"));
318+
NSString *path = @(deadbeef->pl_find_meta_raw (self->_rg_settings.tracks[i], ":URI"));
317319
deadbeef->pl_unlock ();
318320
dispatch_async(dispatch_get_main_queue(), ^{
319-
strongSelf.updateTagsProgressText.stringValue = path;
320-
strongSelf.updateTagsProgressIndicator.doubleValue = (double)i/strongSelf->_rg_settings.num_tracks*100;
321+
self.updateTagsProgressText.stringValue = path;
322+
self.updateTagsProgressIndicator.doubleValue = (double)i/self->_rg_settings.num_tracks*100;
321323
});
322324

323325
uint32_t flags = (1<<DDB_REPLAYGAIN_TRACKGAIN)|(1<<DDB_REPLAYGAIN_TRACKPEAK);
324-
if (strongSelf->_rg_settings.mode != DDB_RG_SCAN_MODE_TRACK) {
326+
if (self->_rg_settings.mode != DDB_RG_SCAN_MODE_TRACK) {
325327
flags |= (1<<DDB_REPLAYGAIN_ALBUMGAIN)|(1<<DDB_REPLAYGAIN_ALBUMPEAK);
326328
}
327-
_rg->apply (strongSelf->_rg_settings.tracks[i], flags, strongSelf->_rg_settings.results[i].track_gain, strongSelf->_rg_settings.results[i].track_peak, strongSelf->_rg_settings.results[i].album_gain, strongSelf->_rg_settings.results[i].album_peak);
329+
_rg->apply (self->_rg_settings.tracks[i], flags, self->_rg_settings.results[i].track_gain, self->_rg_settings.results[i].track_peak, self->_rg_settings.results[i].album_gain, self->_rg_settings.results[i].album_peak);
328330
}
329331
}
330332
deadbeef->pl_save_all ();
331333

332334
dispatch_async(dispatch_get_main_queue(), ^{
333-
[self.window endSheet:strongSelf.updateTagsProgressWindow returnCode:NSModalResponseOK];
335+
[self.window endSheet:self.updateTagsProgressWindow returnCode:NSModalResponseOK];
334336
[self dismissController:self];
335337
});
336338
});

plugins/cocoaui/Utility/Weakify.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
DeaDBeeF -- the music player
3+
Copyright (C) 2009-2025 Oleksiy Yakovenko and other contributors
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
18+
2. Altered source versions must be plainly marked as such, and must not be
19+
misrepresented as being the original software.
20+
21+
3. This notice may not be removed or altered from any source distribution.
22+
*/
23+
24+
#define weakify(obj) \
25+
_Pragma("clang diagnostic push") \
26+
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
27+
__weak __typeof__(obj) weak##obj = obj; \
28+
_Pragma("clang diagnostic pop")
29+
30+
#define strongify(obj) \
31+
_Pragma("clang diagnostic push") \
32+
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
33+
__strong __typeof__(obj) obj = weak##obj; \
34+
_Pragma("clang diagnostic pop")

0 commit comments

Comments
 (0)