ObjFW
OFDictionary.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #ifndef __STDC_LIMIT_MACROS
18 # define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 # define __STDC_CONSTANT_MACROS
22 #endif
23 
24 #include <stdarg.h>
25 
26 #import "OFObject.h"
27 #import "OFCollection.h"
28 #import "OFEnumerator.h"
29 #import "OFSerialization.h"
30 #import "OFJSONRepresentation.h"
31 #import "OFMessagePackRepresentation.h"
32 
33 OF_ASSUME_NONNULL_BEGIN
34 
35 @class OFArray OF_GENERIC(ObjectType);
36 
37 #ifdef OF_HAVE_BLOCKS
38 typedef void (^of_dictionary_enumeration_block_t)(id key, id object,
39  bool *stop);
40 typedef bool (^of_dictionary_filter_block_t)(id key, id object);
41 typedef id _Nonnull (^of_dictionary_map_block_t)(id key, id object);
42 #endif
43 
54 @interface OFDictionary OF_GENERIC(KeyType, ObjectType): OFObject <OFCopying,
57 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
58 # define KeyType id
59 # define ObjectType id
60 #endif
61 
66 + (instancetype)dictionary;
67 
74 + (instancetype)dictionaryWithDictionary:
75  (OFDictionary OF_GENERIC(KeyType, ObjectType) *)dictionary;
76 
84 + (instancetype)dictionaryWithObject: (ObjectType)object
85  forKey: (KeyType)key;
86 
94 + (instancetype)
95  dictionaryWithObjects: (OFArray OF_GENERIC(ObjectType) *)objects
96  forKeys: (OFArray OF_GENERIC(KeyType) *)keys;
97 
106 + (instancetype)
107  dictionaryWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
108  forKeys: (KeyType const _Nonnull *_Nonnull)keys
109  count: (size_t)count;
110 
117 + (instancetype)dictionaryWithKeysAndObjects: (KeyType)firstKey, ...
118  OF_SENTINEL;
119 
127 - initWithDictionary:
128  (OFDictionary OF_GENERIC(KeyType, ObjectType) *)dictionary;
129 
138 - initWithObject: (ObjectType)object
139  forKey: (KeyType)key;
140 
149 - initWithObjects: (OFArray OF_GENERIC(ObjectType) *)objects
150  forKeys: (OFArray OF_GENERIC(KeyType) *)keys;
151 
161 - initWithObjects: (ObjectType const _Nonnull *_Nonnull)objects
162  forKeys: (KeyType const _Nonnull *_Nonnull)keys
163  count: (size_t)count;
164 
172 - initWithKeysAndObjects: (KeyType)firstKey, ... OF_SENTINEL;
173 
182 - initWithKey: (KeyType)firstKey
183  arguments: (va_list)arguments;
184 
195 - (nullable ObjectType)objectForKey: (KeyType)key;
196 - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key;
197 
210 - (nullable id)valueForKey: (OFString *)key;
211 
224 - (void)setValue: (id)value
225  forKey: (OFString *)key;
226 
234 - (bool)containsObject: (ObjectType)object;
235 
244 - (bool)containsObjectIdenticalTo: (ObjectType)object;
245 
251 - (OFArray OF_GENERIC(KeyType) *)allKeys;
252 
258 - (OFArray OF_GENERIC(ObjectType) *)allObjects;
259 
266 
272 - (OFEnumerator OF_GENERIC(KeyType) *)keyEnumerator;
273 
279 - (OFEnumerator OF_GENERIC(ObjectType) *)objectEnumerator;
280 
281 #ifdef OF_HAVE_BLOCKS
282 
287 - (void)enumerateKeysAndObjectsUsingBlock:
288  (of_dictionary_enumeration_block_t)block;
289 
297 - (OFDictionary OF_GENERIC(KeyType, id) *)mappedDictionaryUsingBlock:
298  (of_dictionary_map_block_t)block;
299 
308 - (OFDictionary OF_GENERIC(KeyType, ObjectType) *)filteredDictionaryUsingBlock:
309  (of_dictionary_filter_block_t)block;
310 #endif
311 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
312 # undef KeyType
313 # undef ObjectType
314 #endif
315 @end
316 
317 OF_ASSUME_NONNULL_END
318 
319 #import "OFMutableDictionary.h"
320 
321 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
322 /* Required for dictionary literals to work */
323 @compatibility_alias NSDictionary OFDictionary;
324 #endif
OFString * stringByURLEncoding()
Creates a string by URL-encoding the contents of the dictionary.
Definition: OFDictionary.m:573
instancetype dictionary()
Creates a new OFDictionary.
Definition: OFDictionary.m:142
A protocol for the creation of mutable copies.
Definition: OFObject.h:935
OFArray * allObjects()
Returns an array of all objects.
Definition: OFDictionary.m:432
A class for handling strings.
Definition: OFString.h:114
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:38
A class which provides methods to enumerate through collections.
Definition: OFEnumerator.h:44
A protocol with methods common for all collections.
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:30
A protocol for the creation of copies.
Definition: OFObject.h:914
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:54
OFEnumerator * objectEnumerator()
Returns an OFEnumerator to enumerate through the dictionary&#39;s objects.
Definition: OFDictionary.m:454
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
OFEnumerator * keyEnumerator()
Returns an OFEnumerator to enumerate through the dictionary&#39;s keys.
Definition: OFDictionary.m:449
An abstract class for storing objects in an array.
Definition: OFArray.h:89
A protocol for serializing objects.
Definition: OFSerialization.h:30
OFArray * allKeys()
Returns an array of all keys.
Definition: OFDictionary.m:420