libs-base/Source/NSKVOInternal.h
Hugo Melder 6681a3da47
Initial port of WinObjC's KVO implementation to GNUstep (#420)
* GSAtomic: Add prefix to macro definitions

* NSKVOSupport: Import

* NSKVOSupport: Add test cases

* NSKVOSwizzling: Ugly C Rewrite

* NSKeyValueObserving: Use old implementation as fallback

* NSKeyValueObserving: Rename TypeEncodingCases header

* NSKVOSupport: Fix new objects not being added to NSKeyValueChangeNew set on set mutation

* NSKeyValueMutableSet: Fix will and didChange notifications for set operations

* NSKeyValueMutableSet: Document Accessor Search Patterns

* NSKVOSupport: Add toMany test

* NSKeyValueCoding: Change notifications when changing value via setValue:forKey:

* NSKVOSupport: Add more tests

* NSKVOSupport: Do not wrap block in try/finally to avoid crash in windows

* NSKVOSwizzling: use _alloca on Windows

* NSKVOSupport: Do not autorelease newWithObservee:

* NSKVOSupport: Do not leak Observee and TestFacade objects

* Improve runtime detection in makefile

* Add file extension of source file in GNUMakefile

* NSKVOSupport: Remove @status comments

* NSKVOSupport: Implement private notify method

* NSUserDefaults: KVO Support and fix macOS incompatibilities

* NSKeyValueObserving: Set old to null if nil

* NSKeyValueObserving: Remove cached new value

* NSMethodSignature: Add signature cache

* NSKVOSupport: Remove ObjC2 features and mark tests failing on GCC as hopeful

* Call class method instead of private _keyPathsForValuesAffectingValueForKey

* Move _keyPathsForValuesAffectingValueForKey body into class method and statically construct empty NSSet

* NSUserDefaults: Change notification should contain old value from other domains aswell

* NSUserDefaults: Fetch new value from all domains

* NSKVOInternal: Fixup filename in header

* NSUserDefaults: Go through search list instead of only one domain in KVO change

* Making indentation a bit less worse

* Add NSUserDefaults KVO tests

* NSKVOSupport: NSUserDefaults test small fixes

* Add autoreleasepool

* NSUserDefaults: Only emit change notifications if value changed

* Avoid compiler warnings and tidy some of the whitespace/formatting

---------

Co-authored-by: Frederik Seiffert <frederik@algoriddim.com>
Co-authored-by: rfm <richardfrithmacdonald@gmail.com>
Co-authored-by: rfm <rfm@gnu.org>
2024-11-10 17:05:23 +01:00

129 lines
5 KiB
Objective-C

/**
NSKVOInternal.h
Copyright (C) 2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: June 2024
Based on WinObjC KVO tests by Microsoft Corporation.
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
/**
Copyright (c) Microsoft. All rights reserved.
This code is licensed under the MIT License (MIT).
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* This Key Value Observing Implementation is tied to libobjc2 */
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSKeyValueObserving.h>
#import <Foundation/NSException.h>
#if defined(__OBJC2__)
#import "GSPThread.h"
#define NS_COLLECTION_THROW_ILLEGAL_KVO(keyPath) \
do \
{ \
[NSException \
raise: NSInvalidArgumentException \
format: @"-[%s %s] is not supported. Key path: %@", \
object_getClassName(self), sel_getName(_cmd), keyPath]; \
} while (false)
@class _NSKVOKeypathObserver;
@interface _NSKVOKeyObserver : NSObject
- (instancetype)initWithObject:(id)object
keypathObserver:(_NSKVOKeypathObserver *)keypathObserver
key:(NSString *)key
restOfKeypath:(NSString *)restOfKeypath
affectedObservers:(NSArray *)affectedObservers;
@property (nonatomic, retain) _NSKVOKeypathObserver *keypathObserver;
@property (nonatomic, retain) _NSKVOKeyObserver *restOfKeypathObserver;
@property (nonatomic, retain) NSArray *dependentObservers;
@property (nonatomic, assign) id object;
@property (nonatomic, copy) NSString *key;
@property (nonatomic, copy) NSString *restOfKeypath;
@property (nonatomic, retain) NSArray *affectedObservers;
@property (nonatomic, assign) BOOL root;
@property (nonatomic, readonly) BOOL isRemoved;
@end
@interface _NSKVOKeypathObserver : NSObject
- (instancetype)initWithObject:(id)object
observer:(id)observer
keyPath:(NSString *)keypath
options:(NSKeyValueObservingOptions)options
context:(void *)context;
@property (nonatomic, assign) id object;
@property (nonatomic, assign) id observer;
@property (nonatomic, copy) NSString *keypath;
@property (nonatomic, assign) NSKeyValueObservingOptions options;
@property (nonatomic, assign) void *context;
@property (atomic, retain) NSMutableDictionary *pendingChange;
@end
@interface _NSKVOObservationInfo : NSObject
{
NSMutableDictionary<NSString *, NSMutableArray<_NSKVOKeyObserver *> *>
*_keyObserverMap;
NSInteger _dependencyDepth;
NSMutableSet<NSString *> *_existingDependentKeys;
gs_mutex_t _lock;
}
- (instancetype)init;
- (NSArray *)observersForKey:(NSString *)key;
- (void)addObserver:(_NSKVOKeyObserver *)observer;
@end
// From NSKVOSwizzling
void
_NSKVOEnsureKeyWillNotify(id object, NSString *key);
#endif
/* Implementation in NSKVOSupport.m for ObjC2 and NSKeyValueObserving
* respectively
*/
@interface
NSObject (NSKeyValueObservingPrivate)
- (void)_notifyObserversOfChangeForKey:(NSString *)key
oldValue:(id)oldValue
newValue:(id)newValue;
@end