ObjFW
OFDictionary.h
1 /*
2  * Copyright (c) 2008-2021 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 "OFSerialization.h"
29 #import "OFJSONRepresentation.h"
30 #import "OFMessagePackRepresentation.h"
31 
32 OF_ASSUME_NONNULL_BEGIN
33 
34 @class OFArray OF_GENERIC(ObjectType);
35 
36 #ifdef OF_HAVE_BLOCKS
37 typedef void (^of_dictionary_enumeration_block_t)(id key, id object,
38  bool *stop);
39 typedef bool (^of_dictionary_filter_block_t)(id key, id object);
40 typedef id _Nonnull (^of_dictionary_map_block_t)(id key, id object);
41 #endif
42 
56 @interface OFDictionary OF_GENERIC(KeyType, ObjectType): OFObject <OFCopying,
59 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
60 # define KeyType id
61 # define ObjectType id
62 #endif
66 @property (readonly, nonatomic) OFArray OF_GENERIC(KeyType) *allKeys;
67 
71 @property (readonly, nonatomic) OFArray OF_GENERIC(ObjectType) *allObjects;
72 
76 @property (readonly, nonatomic) OFString *stringByURLEncoding;
77 
83 + (instancetype)dictionary;
84 
91 + (instancetype)dictionaryWithDictionary:
92  (OFDictionary OF_GENERIC(KeyType, ObjectType) *)dictionary;
93 
101 + (instancetype)dictionaryWithObject: (ObjectType)object forKey: (KeyType)key;
102 
110 + (instancetype)
111  dictionaryWithObjects: (OFArray OF_GENERIC(ObjectType) *)objects
112  forKeys: (OFArray OF_GENERIC(KeyType) *)keys;
113 
122 + (instancetype)
123  dictionaryWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
124  forKeys: (KeyType const _Nonnull *_Nonnull)keys
125  count: (size_t)count;
126 
133 + (instancetype)dictionaryWithKeysAndObjects: (KeyType)firstKey, ...
134  OF_SENTINEL;
135 
143 - (instancetype)initWithDictionary:
144  (OFDictionary OF_GENERIC(KeyType, ObjectType) *)dictionary;
145 
154 - (instancetype)initWithObject: (ObjectType)object forKey: (KeyType)key;
155 
164 - (instancetype)initWithObjects: (OFArray OF_GENERIC(ObjectType) *)objects
165  forKeys: (OFArray OF_GENERIC(KeyType) *)keys;
166 
176 - (instancetype)initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
177  forKeys: (KeyType const _Nonnull *_Nonnull)keys
178  count: (size_t)count;
179 
187 - (instancetype)initWithKeysAndObjects: (KeyType)firstKey, ... OF_SENTINEL;
188 
197 - (instancetype)initWithKey: (KeyType)firstKey arguments: (va_list)arguments;
198 
209 - (nullable ObjectType)objectForKey: (KeyType)key;
210 - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key;
211 
223 - (nullable id)valueForKey: (OFString *)key;
224 
234 - (void)setValue: (nullable id)value forKey: (OFString *)key;
235 
243 - (bool)containsObject: (ObjectType)object;
244 
253 - (bool)containsObjectIdenticalTo: (ObjectType)object;
254 
260 - (OFEnumerator OF_GENERIC(KeyType) *)keyEnumerator;
261 
267 - (OFEnumerator OF_GENERIC(ObjectType) *)objectEnumerator;
268 
269 #ifdef OF_HAVE_BLOCKS
275 - (void)enumerateKeysAndObjectsUsingBlock:
276  (of_dictionary_enumeration_block_t)block;
277 
285 - (OFDictionary OF_GENERIC(KeyType, id) *)mappedDictionaryUsingBlock:
286  (of_dictionary_map_block_t)block;
287 
296 - (OFDictionary OF_GENERIC(KeyType, ObjectType) *)filteredDictionaryUsingBlock:
297  (of_dictionary_filter_block_t)block;
298 #endif
299 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
300 # undef KeyType
301 # undef ObjectType
302 #endif
303 @end
304 
305 OF_ASSUME_NONNULL_END
306 
307 #import "OFMutableDictionary.h"
308 
309 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
310 /* Required for dictionary literals to work */
311 @compatibility_alias NSDictionary OFDictionary;
312 #endif
struct objc_object * id
A pointer to any object.
Definition: ObjFWRT.h:90
An abstract class for storing objects in an array.
Definition: OFArray.h:92
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:58
OFArray * allObjects
An array of all objects.
Definition: OFDictionary.h:71
OFArray * allKeys
An array of all keys.
Definition: OFDictionary.h:66
instancetype dictionary()
Creates a new OFDictionary.
Definition: OFDictionary.m:218
OFEnumerator * keyEnumerator()
Returns an OFEnumerator to enumerate through the dictionary's keys.
Definition: OFDictionary.m:499
OFString * stringByURLEncoding
A URL-encoded string with the contents of the dictionary.
Definition: OFDictionary.h:76
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through the dictionary's objects.
Definition: OFDictionary.m:504
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:101
The root class for all other classes inside ObjFW.
Definition: OFObject.h:520
A class for handling strings.
Definition: OFString.h:132
A protocol with methods common for all collections.
Definition: OFCollection.h:25
A protocol for the creation of copies.
Definition: OFObject.h:1187
A protocol implemented by classes that support encoding to a JSON representation.
Definition: OFJSONRepresentation.h:39
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:29
A protocol for the creation of mutable copies.
Definition: OFObject.h:1208
A protocol for serializing objects.
Definition: OFSerialization.h:29