ObjFW
OFDictionary.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3  * Jonathan Schleifer <js@webkeks.org>
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 OF_NONNULL (^of_dictionary_map_block_t)(id key, id object);
42 #endif
43 
54 #ifdef OF_HAVE_GENERICS
55 @interface OFDictionary <KeyType, ObjectType>:
56 #else
57 # ifndef DOXYGEN
58 # define KeyType id
59 # define ObjectType id
60 # endif
61 @interface OFDictionary:
62 #endif
70 + (instancetype)dictionary;
71 
78 + (instancetype)dictionaryWithDictionary:
79  (OFDictionary OF_GENERIC(KeyType, ObjectType)*)dictionary;
80 
88 + (instancetype)dictionaryWithObject: (ObjectType)object
89  forKey: (KeyType)key;
90 
98 + (instancetype)
99  dictionaryWithObjects: (OFArray OF_GENERIC(ObjectType)*)objects
100  forKeys: (OFArray OF_GENERIC(KeyType)*)keys;
101 
110 + (instancetype)
111  dictionaryWithObjects: (ObjectType const OF_NONNULL *OF_NONNULL)objects
112  forKeys: (KeyType const OF_NONNULL *OF_NONNULL)keys
113  count: (size_t)count;
114 
121 + (instancetype)dictionaryWithKeysAndObjects: (KeyType)firstKey, ...
122  OF_SENTINEL;
123 
131 - initWithDictionary: (OFDictionary OF_GENERIC(KeyType, ObjectType)*)dictionary;
132 
141 - initWithObject: (ObjectType)object
142  forKey: (KeyType)key;
143 
152 - initWithObjects: (OFArray OF_GENERIC(ObjectType)*)objects
153  forKeys: (OFArray OF_GENERIC(KeyType)*)keys;
154 
164 - initWithObjects: (ObjectType const OF_NONNULL *OF_NONNULL)objects
165  forKeys: (KeyType const OF_NONNULL *OF_NONNULL)keys
166  count: (size_t)count;
167 
175 - initWithKeysAndObjects: (KeyType)firstKey, ... OF_SENTINEL;
176 
185 - initWithKey: (KeyType)firstKey
186  arguments: (va_list)arguments;
187 
197 - (nullable ObjectType)objectForKey: (KeyType)key;
198 - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key;
199 
207 - (bool)containsObject: (nullable ObjectType)object;
208 
217 - (bool)containsObjectIdenticalTo: (nullable ObjectType)object;
218 
224 - (OFArray OF_GENERIC(KeyType)*)allKeys;
225 
231 - (OFArray OF_GENERIC(ObjectType)*)allObjects;
232 
238 - (OFEnumerator OF_GENERIC(KeyType)*)keyEnumerator;
239 
245 - (OFEnumerator OF_GENERIC(ObjectType)*)objectEnumerator;
246 
247 #ifdef OF_HAVE_BLOCKS
248 
253 - (void)enumerateKeysAndObjectsUsingBlock:
254  (of_dictionary_enumeration_block_t)block;
255 
263 - (OFDictionary OF_GENERIC(KeyType, id)*)mappedDictionaryUsingBlock:
264  (of_dictionary_map_block_t)block;
265 
274 - (OFDictionary OF_GENERIC(KeyType, ObjectType)*)filteredDictionaryUsingBlock:
275  (of_dictionary_filter_block_t)block;
276 #endif
277 @end
278 #if !defined(OF_HAVE_GENERICS) && !defined(DOXYGEN)
279 # undef KeyType
280 # undef ObjectType
281 #endif
282 
283 OF_ASSUME_NONNULL_END
284 
285 #import "OFMutableDictionary.h"
286 
287 #ifndef NSINTEGER_DEFINED
288 /* Required for dictionary literals to work */
289 @compatibility_alias NSDictionary OFDictionary;
290 #endif
A protocol for serializing objects.
Definition: OFSerialization.h:30
An abstract class for storing objects in an array.
Definition: OFArray.h:95
instancetype dictionary()
Creates a new OFDictionary.
Definition: OFDictionary.m:144
The root class for all other classes inside ObjFW.
Definition: OFObject.h:364
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:38
size_t count()
Returns the number of objects in the collection.
OFEnumerator OF_GENERIC(KeyType keyEnumerator()
Returns an OFEnumerator to enumerate through the dictionary's keys.
Definition: OFDictionary.m:415
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:61
A protocol with methods common for all collections.
Definition: OFCollection.h:26
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:30
A protocol for the creation of mutable copies.
Definition: OFObject.h:917
A protocol for the creation of copies.
Definition: OFObject.h:896
OFArray OF_GENERIC(ObjectType allObjects()
Returns an array of all objects.
Definition: OFDictionary.m:398
OFEnumerator OF_GENERIC(ObjectType objectEnumerator()
Returns an OFEnumerator to enumerate through the dictionary's objects.
Definition: OFDictionary.m:420
OFArray OF_GENERIC(KeyType allKeys()
Returns an array of all keys.
Definition: OFDictionary.m:381