Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -51,10 +51,11 @@ OFMutableString.m \ OFMutableTarArchiveEntry.m \ OFMutableTriple.m \ OFMutableURL.m \ OFMutableZIPArchiveEntry.m \ + OFNotification.m \ OFNull.m \ OFNumber.m \ OFObject.m \ OFObject+KeyValueCoding.m \ OFObject+Serialization.m \ ADDED src/OFNotification.h Index: src/OFNotification.h ================================================================== --- src/OFNotification.h +++ src/OFNotification.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2008-2021 Jonathan Schleifer + * + * 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" + +OF_ASSUME_NONNULL_BEGIN + +/** @file */ + +@class OFConstantString; +@class OFDictionary OF_GENERIC(KeyType, ObjectType); + +/** + * @brief A name for a notification. + */ +typedef OFConstantString *OFNotificationName; + +/** + * @class OFNotification OFNotification.h ObjFW/OFNotification.h + * + * @brief An class to represent a notification for or from + * @ref OFNotificationCenter. + */ +OF_SUBCLASSING_RESTRICTED +@interface OFNotification: OFObject +{ + OFNotificationName _name; + id _Nullable _object; + OFDictionary *_Nullable _userInfo; +} + +/** + * @brief The name of the notification. + */ +@property (readonly, nonatomic) OFNotificationName name; + +/** + * @brief The object of the notification. This is commonly the sender of the + * notification. + */ +@property OF_NULLABLE_PROPERTY (readonly, nonatomic) id object; + +/** + * @brief Additional information about the notification. + */ +@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFDictionary *userInfo; + +/** + * @brief Creates a new notification with the specified name and object. + * + * @param name The name for the notification + * @param object The object for the notification. This is commonly the sender + * of the notification. + * @return A new, autoreleased OFNotification + */ ++ (instancetype)notificationWithName: (OFNotificationName)name + object: (nullable id)object; + +/** + * @brief Creates a new notification with the specified name, object and + * additional information. + * + * @param name The name for the notification + * @param object The object for the notification. This is commonly the sender + * of the notification. + * @param userInfo Additional information for the notification + * @return A new, autoreleased OFNotification + */ ++ (instancetype)notificationWithName: (OFNotificationName)name + object: (nullable id)object + userInfo: (nullable OFDictionary *)userInfo; + +/** + * @brief Initializes an already allocated notification with the specified + * name and object. + * + * @param name The name for the notification + * @param object The object for the notification. This is commonly the sender + * of the notification. + * @return An initialized OFNotification + */ +- (instancetype)initWithName: (OFNotificationName)name + object: (nullable id)object; + +/** + * @brief Initializes an already allocated notification with the specified + * name, object and additional information. + * + * @param name The name for the notification + * @param object The object for the notification. This is commonly the sender + * of the notification. + * @param userInfo Additional information for the notification + * @return An initialized OFNotification + */ +- (instancetype)initWithName: (OFNotificationName)name + object: (nullable id)object + userInfo: (nullable OFDictionary *)userInfo + OF_DESIGNATED_INITIALIZER; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFNotification.m Index: src/OFNotification.m ================================================================== --- src/OFNotification.m +++ src/OFNotification.m @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2008-2021 Jonathan Schleifer + * + * 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 "OFNotification.h" +#import "OFDictionary.h" +#import "OFString.h" + +@implementation OFNotification +@synthesize name = _name, object = _object, userInfo = _userInfo; + ++ (instancetype)notificationWithName: (OFNotificationName)name + object: (id)object +{ + return [[[self alloc] initWithName: name object: object] autorelease]; +} + ++ (instancetype)notificationWithName: (OFNotificationName)name + object: (id)object + userInfo: (OFDictionary *)userInfo +{ + return [[[self alloc] initWithName: name + object: object + userInfo: userInfo] autorelease]; +} + +- (instancetype)initWithName: (OFNotificationName)name object: (id)object +{ + return [self initWithName: name object: object userInfo: nil]; +} + +- (instancetype)initWithName: (OFNotificationName)name + object: (id)object + userInfo: (OFDictionary *)userInfo +{ + self = [super init]; + + @try { + _name = [name copy]; + _object = [object retain]; + _userInfo = [userInfo copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_name release]; + [_object release]; + [_userInfo release]; + + [super dealloc]; +} + +- (id)copy +{ + return [self retain]; +} +@end Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -47,10 +47,12 @@ #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"