ObjFW
OFDictionary.h
1 /*
2  * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This file is part of ObjFW. It may be distributed under the terms of the
7  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
8  * the packaging of this file.
9  *
10  * Alternatively, it may be distributed under the terms of the GNU General
11  * Public License, either version 2 or 3, which can be found in the file
12  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
13  * file.
14  */
15 
16 #ifndef __STDC_LIMIT_MACROS
17 # define __STDC_LIMIT_MACROS
18 #endif
19 #ifndef __STDC_CONSTANT_MACROS
20 # define __STDC_CONSTANT_MACROS
21 #endif
22 
23 #include <stdarg.h>
24 
25 #import "OFObject.h"
26 #import "OFCollection.h"
27 #import "OFEnumerator.h"
28 #import "OFJSONRepresentation.h"
29 #import "OFMessagePackRepresentation.h"
30 
31 OF_ASSUME_NONNULL_BEGIN
32 
33 @class OFArray OF_GENERIC(ObjectType);
34 
35 #ifdef OF_HAVE_BLOCKS
36 
44 typedef void (^OFDictionaryEnumerationBlock)(id key, id object, bool *stop);
45 
53 typedef bool (^OFDictionaryFilterBlock)(id key, id object);
54 
62 typedef id _Nonnull (^OFDictionaryMapBlock)(id key, id object);
63 #endif
64 
78 @interface OFDictionary OF_GENERIC(KeyType, ObjectType): OFObject <OFCopying,
81 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
82 # define KeyType id
83 # define ObjectType id
84 #endif
85 
88 @property (readonly, nonatomic) OFArray OF_GENERIC(KeyType) *allKeys;
89 
93 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *allObjects;
94 
100 + (instancetype)dictionary;
101 
108 + (instancetype)dictionaryWithDictionary:
109  (OFDictionary OF_GENERIC(KeyType, ObjectType) *)dictionary;
110 
118 + (instancetype)dictionaryWithObject: (ObjectType)object forKey: (KeyType)key;
119 
127 + (instancetype)dictionaryWithObjects: (OFArray OF_GENERIC(ObjectType) *)objects
128  forKeys: (OFArray OF_GENERIC(KeyType) *)keys;
129 
138 + (instancetype)
139  dictionaryWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
140  forKeys: (KeyType const _Nonnull *_Nonnull)keys
141  count: (size_t)count;
142 
149 + (instancetype)dictionaryWithKeysAndObjects: (KeyType)firstKey, ...
150  OF_SENTINEL;
151 
157 - (instancetype)init OF_DESIGNATED_INITIALIZER;
158 
166 - (instancetype)initWithDictionary:
167  (OFDictionary OF_GENERIC(KeyType, ObjectType) *)dictionary;
168 
177 - (instancetype)initWithObject: (ObjectType)object forKey: (KeyType)key;
178 
187 - (instancetype)initWithObjects: (OFArray OF_GENERIC(ObjectType) *)objects
188  forKeys: (OFArray OF_GENERIC(KeyType) *)keys;
189 
199 - (instancetype)initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
200  forKeys: (KeyType const _Nonnull *_Nonnull)keys
201  count: (size_t)count OF_DESIGNATED_INITIALIZER;
202 
210 - (instancetype)initWithKeysAndObjects: (KeyType)firstKey, ... OF_SENTINEL;
211 
220 - (instancetype)initWithKey: (KeyType)firstKey arguments: (va_list)arguments;
221 
232 - (nullable ObjectType)objectForKey: (KeyType)key;
233 - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key;
234 
246 - (nullable id)valueForKey: (OFString *)key;
247 
257 - (void)setValue: (nullable id)value forKey: (OFString *)key;
258 
266 - (bool)containsObject: (ObjectType)object;
267 
276 - (bool)containsObjectIdenticalTo: (ObjectType)object;
277 
283 - (OFEnumerator OF_GENERIC(KeyType) *)keyEnumerator;
284 
290 - (OFEnumerator OF_GENERIC(ObjectType) *)objectEnumerator;
291 
292 #ifdef OF_HAVE_BLOCKS
293 
298 - (void)enumerateKeysAndObjectsUsingBlock: (OFDictionaryEnumerationBlock)block;
299 
307 - (OFDictionary OF_GENERIC(KeyType, id) *)
308  mappedDictionaryUsingBlock: (OFDictionaryMapBlock)block;
309 
318 - (OFDictionary OF_GENERIC(KeyType, ObjectType) *)
319  filteredDictionaryUsingBlock: (OFDictionaryFilterBlock)block;
320 #endif
321 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
322 # undef KeyType
323 # undef ObjectType
324 #endif
325 @end
326 
327 OF_ASSUME_NONNULL_END
328 
329 #import "OFMutableDictionary.h"
330 
331 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
332 /* Required for dictionary literals to work */
333 @compatibility_alias NSDictionary OFDictionary;
334 #endif
An abstract class for storing objects in an array.
Definition: OFArray.h:103
OFArray * allObjects
An array of all objects.
Definition: OFDictionary.h:93
instancetype dictionary()
Creates a new OFDictionary.
Definition: OFDictionary.m:136
The root class for all other classes inside ObjFW.
Definition: OFObject.h:686
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:44
A class for handling strings.
Definition: OFString.h:134
OFEnumerator * keyEnumerator()
Returns an OFEnumerator to enumerate through the dictionary&#39;s keys.
Definition: OFDictionary.m:509
instancetype init()
Initializes an already allocated OFDictionary to be empty.
Definition: OFDictionary.m:181
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:78
A protocol with methods common for all collections.
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:29
OFArray * allKeys
An array of all keys.
Definition: OFDictionary.h:88
A protocol for the creation of mutable copies.
Definition: OFObject.h:1367
A protocol for the creation of copies.
Definition: OFObject.h:1346
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:99
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through the dictionary&#39;s objects.
Definition: OFDictionary.m:514