Overview
Comment: | Add OFNotificationCenter |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | notifications |
Files: | files | file ages | folders |
SHA3-256: |
611cde23adf16c90e47f2dd5cc9373c5 |
User & Date: | js on 2021-10-31 17:41:57 |
Other Links: | branch diff | manifest | tags |
Context
2021-10-31
| ||
18:02 | OFNotificationCenter: Move posting out of the lock check-in: 93851ccd0f user: js tags: notifications | |
17:41 | Add OFNotificationCenter check-in: 611cde23ad user: js tags: notifications | |
15:37 | Add OFNotification check-in: c9f9469826 user: js tags: notifications | |
Changes
Modified src/Makefile from [39c6ddf577] to [0d79d5eef0].
︙ | ︙ | |||
50 51 52 53 54 55 56 57 58 59 60 61 62 63 | OFMutableSet.m \ OFMutableString.m \ OFMutableTarArchiveEntry.m \ OFMutableTriple.m \ OFMutableURL.m \ OFMutableZIPArchiveEntry.m \ OFNotification.m \ OFNull.m \ OFNumber.m \ OFObject.m \ OFObject+KeyValueCoding.m \ OFObject+Serialization.m \ OFOnce.m \ OFOptionsParser.m \ | > | 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | OFMutableSet.m \ OFMutableString.m \ OFMutableTarArchiveEntry.m \ OFMutableTriple.m \ OFMutableURL.m \ OFMutableZIPArchiveEntry.m \ OFNotification.m \ OFNotificationCenter.m \ OFNull.m \ OFNumber.m \ OFObject.m \ OFObject+KeyValueCoding.m \ OFObject+Serialization.m \ OFOnce.m \ OFOptionsParser.m \ |
︙ | ︙ |
Modified src/OFNotification.h from [f8e842743c] to [ac572ecafc].
︙ | ︙ | |||
26 27 28 29 30 31 32 | * @brief A name for a notification. */ typedef OFConstantString *OFNotificationName; /** * @class OFNotification OFNotification.h ObjFW/OFNotification.h * | | | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | * @brief A name for a notification. */ typedef OFConstantString *OFNotificationName; /** * @class OFNotification OFNotification.h ObjFW/OFNotification.h * * @brief A class to represent a notification for or from * @ref OFNotificationCenter. */ OF_SUBCLASSING_RESTRICTED @interface OFNotification: OFObject <OFCopying> { OFNotificationName _name; id _Nullable _object; |
︙ | ︙ |
Added src/OFNotificationCenter.h version [7a53480f1b].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | /* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #import "OFNotification.h" OF_ASSUME_NONNULL_BEGIN @class OFMutableDictionary OF_GENERIC(KeyType, ObjectType); #ifdef OF_HAVE_THREADS @class OFMutex; #endif /** * @class OFNotificationCenter OFNotificationCenter.h \ * ObjFW/OFNotificationCenter.h * * @brief A class to send and register for notifications. */ @interface OFNotificationCenter: OFObject { #ifdef OF_HAVE_THREADS OFMutex *_mutex; #endif OFMutableDictionary *_notifications; OF_RESERVE_IVARS(OFNotificationCenter, 4) } #ifdef OF_HAVE_CLASS_PROPERTIES @property (class, readonly, nonatomic) OFNotificationCenter *defaultCenter; #endif /** * @brief Returns the default notification center. */ + (OFNotificationCenter *)defaultCenter; /** * @brief Adds an observer for the specified notification and object. * * @param observer The object that should receive notifications * @param selector The selector to call on the observer on notifications. The * method must take exactly one object of type @ref * OFNotification. * @param name The name of the notification to observe * @param object The object that should be sending the notification, or `nil` * if the object should be ignored to determine what * notifications to deliver */ - (void)addObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (nullable id)object; /** * @brief Removes an observer. All parameters must match those used with * @ref addObserver:selector:name:object:. * * @param observer The observer that was specified when adding the observer * @param selector The selector that was specified when adding the observer * @param name The name that was specified when adding the observer * @param object The object that was specified when adding the observer */ - (void)removeObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (nullable id)object; /** * @brief Posts the specified notification. * * @param notification The notification to post */ - (void)postNotification: (OFNotification *)notification; @end OF_ASSUME_NONNULL_END |
Added src/OFNotificationCenter.m version [c832dbfaa5].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFNotificationCenter.h" #import "OFDictionary.h" #ifdef OF_HAVE_THREADS # import "OFMutex.h" #endif #import "OFSet.h" #import "OFString.h" @interface OFDefaultNotificationCenter: OFNotificationCenter @end @interface OFNotificationRegistration: OFObject { @public id _observer; SEL _selector; unsigned long _selectorHash; id _object; } - (instancetype)initWithObserver: (id)observer selector: (SEL)selector object: (id)object; @end static OFNotificationCenter *defaultCenter; @implementation OFNotificationRegistration - (instancetype)initWithObserver: (id)observer selector: (SEL)selector object: (id)object { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); _observer = [observer retain]; _selector = selector; _object = [object retain]; _selectorHash = [[OFString stringWithUTF8String: sel_getName(_selector)] hash]; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_observer release]; [_object release]; [super dealloc]; } - (bool)isEqual: (OFNotificationRegistration *)registration { if (![registration isKindOfClass: [OFNotificationRegistration class]]) return false; if (![registration->_observer isEqual: _observer]) return false; if (!sel_isEqual(registration->_selector, _selector)) return false; if (registration->_object != _object && ![registration->_object isEqual: _object]) return false; return true; } - (unsigned long)hash { unsigned long hash; OFHashInit(&hash); OFHashAddHash(&hash, [_observer hash]); OFHashAddHash(&hash, _selectorHash); OFHashAddHash(&hash, [_object hash]); OFHashFinalize(&hash); return hash; } @end @implementation OFNotificationCenter + (void)initialize { if (self != [OFNotificationCenter class]) return; defaultCenter = [[OFDefaultNotificationCenter alloc] init]; } + (OFNotificationCenter *)defaultCenter { return defaultCenter; } - (instancetype)init { self = [super init]; @try { #ifdef OF_HAVE_THREADS _mutex = [[OFMutex alloc] init]; #endif _notifications = [[OFMutableDictionary alloc] init]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { #ifdef OF_HAVE_THREADS [_mutex release]; #endif [_notifications release]; [super dealloc]; } - (void)addObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (id)object { void *pool = objc_autoreleasePoolPush(); OFNotificationRegistration *registration = [[[OFNotificationRegistration alloc] initWithObserver: observer selector: selector object: object] autorelease]; #ifdef OF_HAVE_THREADS [_mutex lock]; @try { OFMutableSet *notificationsForName = [_notifications objectForKey: name]; if (notificationsForName == nil) { notificationsForName = [OFMutableSet set]; [_notifications setObject: notificationsForName forKey: name]; } [notificationsForName addObject: registration]; #endif #ifdef OF_HAVE_THREADS } @finally { [_mutex unlock]; } #endif objc_autoreleasePoolPop(pool); } - (void)removeObserver: (id)observer selector: (SEL)selector name: (OFNotificationName)name object: (id)object { void *pool = objc_autoreleasePoolPush(); OFNotificationRegistration *registration = [[[OFNotificationRegistration alloc] initWithObserver: observer selector: selector object: object] autorelease]; #ifdef OF_HAVE_THREADS [_mutex lock]; @try { [[_notifications objectForKey: name] removeObject: registration]; #endif #ifdef OF_HAVE_THREADS } @finally { [_mutex unlock]; } #endif objc_autoreleasePoolPop(pool); } - (void)postNotification: (OFNotification *)notification { #ifdef OF_HAVE_THREADS [_mutex lock]; @try { for (OFNotificationRegistration *registration in [_notifications objectForKey: notification.name]) { void (*callback)(id, SEL, OFNotification *); if (registration->_object != nil && registration->_object != notification.object) continue; callback = (void (*)(id, SEL, OFNotification *)) [registration->_observer methodForSelector: registration->_selector]; callback(registration->_observer, registration->_selector, notification); } #endif #ifdef OF_HAVE_THREADS } @finally { [_mutex unlock]; } #endif } @end @implementation OFDefaultNotificationCenter - (instancetype)autorelease { return self; } - (instancetype)retain { return self; } - (void)release { } - (unsigned int)retainCount { return OFMaxRetainCount; } @end |
Modified src/ObjFW.h from [6f7a0b780f] to [a6b2ac582f].
︙ | ︙ | |||
47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #import "OFDate.h" #import "OFUUID.h" #import "OFURL.h" #import "OFURLHandler.h" #import "OFColor.h" #import "OFNotification.h" #import "OFStream.h" #import "OFStdIOStream.h" #import "OFInflateStream.h" #import "OFInflate64Stream.h" #import "OFGZIPStream.h" #import "OFLHAArchive.h" | > | 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #import "OFDate.h" #import "OFUUID.h" #import "OFURL.h" #import "OFURLHandler.h" #import "OFColor.h" #import "OFNotification.h" #import "OFNotificationCenter.h" #import "OFStream.h" #import "OFStdIOStream.h" #import "OFInflateStream.h" #import "OFInflate64Stream.h" #import "OFGZIPStream.h" #import "OFLHAArchive.h" |
︙ | ︙ |
Modified tests/Makefile from [e55f50a669] to [bb692e21b0].
︙ | ︙ | |||
20 21 22 23 24 25 26 27 28 29 30 31 32 33 | OFDateTests.m \ OFDictionaryTests.m \ OFInvocationTests.m \ OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMethodSignatureTests.m \ OFNumberTests.m \ OFObjectTests.m \ OFPBKDF2Tests.m \ OFPropertyListTests.m \ OFScryptTests.m \ OFSetTests.m \ OFStreamTests.m \ | > | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | OFDateTests.m \ OFDictionaryTests.m \ OFInvocationTests.m \ OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMethodSignatureTests.m \ OFNotificationCenterTests.m \ OFNumberTests.m \ OFObjectTests.m \ OFPBKDF2Tests.m \ OFPropertyListTests.m \ OFScryptTests.m \ OFSetTests.m \ OFStreamTests.m \ |
︙ | ︙ |
Added tests/OFNotificationCenterTests.m version [1ce580811f].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "TestsAppDelegate.h" static OFString *const module = @"OFNotificationCenter"; static const OFNotificationName notificationName = @"OFNotificationCenterTestName"; static const OFNotificationName otherNotificationName = @"OFNotificationCenterTestOtherName"; @interface OFNotificationCenterTest: OFObject { @public id _expectedObject; int _received; } - (void)handleNotification: (OFNotification *)notification; @end @implementation OFNotificationCenterTest - (void)handleNotification: (OFNotification *)notification { OFEnsure([notification.name isEqual: notificationName]); OFEnsure(_expectedObject == nil || notification.object == _expectedObject); _received++; } @end @implementation TestsAppDelegate (OFNotificationCenterTests) - (void)notificationCenterTests { void *pool = objc_autoreleasePoolPush(); OFNotificationCenter *center = [OFNotificationCenter defaultCenter]; OFNotificationCenterTest *test1, *test2, *test3, *test4; OFNotification *notification; test1 = [[[OFNotificationCenterTest alloc] init] autorelease]; test1->_expectedObject = self; test2 = [[[OFNotificationCenterTest alloc] init] autorelease]; test3 = [[[OFNotificationCenterTest alloc] init] autorelease]; test3->_expectedObject = self; test4 = [[[OFNotificationCenterTest alloc] init] autorelease]; /* First one intentionally added twice to test deduplication. */ TEST(@"-[addObserver:selector:name:object:]", R([center addObserver: test1 selector: @selector(handleNotification:) name: notificationName object: self]) && R([center addObserver: test1 selector: @selector(handleNotification:) name: notificationName object: self]) && R([center addObserver: test2 selector: @selector(handleNotification:) name: notificationName object: nil]) && R([center addObserver: test3 selector: @selector(handleNotification:) name: otherNotificationName object: self]) && R([center addObserver: test4 selector: @selector(handleNotification:) name: otherNotificationName object: nil])) notification = [OFNotification notificationWithName: notificationName object: nil]; TEST(@"-[postNotification:] #1", R([center postNotification: notification]) && test1->_received == 0 && test2->_received == 1 && test3->_received == 0 && test4->_received == 0) notification = [OFNotification notificationWithName: notificationName object: self]; TEST(@"-[postNotification:] #2", R([center postNotification: notification]) && test1->_received == 1 && test2->_received == 2 && test3->_received == 0 && test4->_received == 0) notification = [OFNotification notificationWithName: notificationName object: @"foo"]; TEST(@"-[postNotification:] #3", R([center postNotification: notification]) && test1->_received == 1 && test2->_received == 3 && test3->_received == 0 && test4->_received == 0) TEST(@"-[removeObserver:selector:name:object:]", R([center removeObserver: test1 selector: @selector(handleNotification:) name: notificationName object: self]) && R([center removeObserver: test2 selector: @selector(handleNotification:) name: notificationName object: nil]) && R([center removeObserver: test3 selector: @selector(handleNotification:) name: otherNotificationName object: self]) && R([center removeObserver: test4 selector: @selector(handleNotification:) name: otherNotificationName object: nil])) notification = [OFNotification notificationWithName: notificationName object: self]; TEST(@"-[postNotification:] with no observers", R([center postNotification: notification]) && test1->_received == 1 && test2->_received == 3 && test3->_received == 0 && test4->_received == 0) objc_autoreleasePoolPop(pool); } @end |
Modified tests/TestsAppDelegate.h from [2604a9aa38] to [42baf73d8f].
︙ | ︙ | |||
134 135 136 137 138 139 140 141 142 143 144 145 146 147 | @interface TestsAppDelegate (OFMD5HashTests) - (void)MD5HashTests; @end @interface TestsAppDelegate (OFMethodSignatureTests) - (void)methodSignatureTests; @end @interface TestsAppDelegate (OFNumberTests) - (void)numberTests; @end @interface TestsAppDelegate (OFObjectTests) - (void)objectTests; | > > > > | 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | @interface TestsAppDelegate (OFMD5HashTests) - (void)MD5HashTests; @end @interface TestsAppDelegate (OFMethodSignatureTests) - (void)methodSignatureTests; @end @interface TestsAppDelegate (OFNotificationCenterTests) - (void)notificationCenterTests; @end @interface TestsAppDelegate (OFNumberTests) - (void)numberTests; @end @interface TestsAppDelegate (OFObjectTests) - (void)objectTests; |
︙ | ︙ |
Modified tests/TestsAppDelegate.m from [bd24271038] to [863ce9faf4].
︙ | ︙ | |||
327 328 329 330 331 332 333 334 335 336 337 338 339 340 | [self dictionaryTests]; [self listTests]; [self setTests]; [self dateTests]; [self valueTests]; [self numberTests]; [self streamTests]; #ifdef OF_HAVE_FILES [self MD5HashTests]; [self RIPEMD160HashTests]; [self SHA1HashTests]; [self SHA224HashTests]; [self SHA256HashTests]; [self SHA384HashTests]; | > | 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | [self dictionaryTests]; [self listTests]; [self setTests]; [self dateTests]; [self valueTests]; [self numberTests]; [self streamTests]; [self notificationCenterTests]; #ifdef OF_HAVE_FILES [self MD5HashTests]; [self RIPEMD160HashTests]; [self SHA1HashTests]; [self SHA224HashTests]; [self SHA256HashTests]; [self SHA384HashTests]; |
︙ | ︙ |