| 1 |
efrain |
1 |
// Copyright 2017 Google
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
#import "GoogleUtilities/Network/Public/GoogleUtilities/GULMutableDictionary.h"
|
|
|
16 |
|
|
|
17 |
@implementation GULMutableDictionary {
|
|
|
18 |
/// The mutable dictionary.
|
|
|
19 |
NSMutableDictionary *_objects;
|
|
|
20 |
|
|
|
21 |
/// Serial synchronization queue. All reads should use dispatch_sync, while writes use
|
|
|
22 |
/// dispatch_async.
|
|
|
23 |
dispatch_queue_t _queue;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
- (instancetype)init {
|
|
|
27 |
self = [super init];
|
|
|
28 |
|
|
|
29 |
if (self) {
|
|
|
30 |
_objects = [[NSMutableDictionary alloc] init];
|
|
|
31 |
_queue = dispatch_queue_create("GULMutableDictionary", DISPATCH_QUEUE_SERIAL);
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
return self;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
- (NSString *)description {
|
|
|
38 |
__block NSString *description;
|
|
|
39 |
dispatch_sync(_queue, ^{
|
|
|
40 |
description = self->_objects.description;
|
|
|
41 |
});
|
|
|
42 |
return description;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
- (id)objectForKey:(id)key {
|
|
|
46 |
__block id object;
|
|
|
47 |
dispatch_sync(_queue, ^{
|
|
|
48 |
object = [self->_objects objectForKey:key];
|
|
|
49 |
});
|
|
|
50 |
return object;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
- (void)setObject:(id)object forKey:(id<NSCopying>)key {
|
|
|
54 |
dispatch_async(_queue, ^{
|
|
|
55 |
[self->_objects setObject:object forKey:key];
|
|
|
56 |
});
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
- (void)removeObjectForKey:(id)key {
|
|
|
60 |
dispatch_async(_queue, ^{
|
|
|
61 |
[self->_objects removeObjectForKey:key];
|
|
|
62 |
});
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
- (void)removeAllObjects {
|
|
|
66 |
dispatch_async(_queue, ^{
|
|
|
67 |
[self->_objects removeAllObjects];
|
|
|
68 |
});
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
- (NSUInteger)count {
|
|
|
72 |
__block NSUInteger count;
|
|
|
73 |
dispatch_sync(_queue, ^{
|
|
|
74 |
count = self->_objects.count;
|
|
|
75 |
});
|
|
|
76 |
return count;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
- (id)objectForKeyedSubscript:(id<NSCopying>)key {
|
|
|
80 |
__block id object;
|
|
|
81 |
dispatch_sync(_queue, ^{
|
|
|
82 |
object = self->_objects[key];
|
|
|
83 |
});
|
|
|
84 |
return object;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
|
|
|
88 |
dispatch_async(_queue, ^{
|
|
|
89 |
self->_objects[key] = obj;
|
|
|
90 |
});
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
- (NSDictionary *)dictionary {
|
|
|
94 |
__block NSDictionary *dictionary;
|
|
|
95 |
dispatch_sync(_queue, ^{
|
|
|
96 |
dictionary = [self->_objects copy];
|
|
|
97 |
});
|
|
|
98 |
return dictionary;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
@end
|