added iOS source code
[wl-app.git] / iOS / Pods / GoogleUtilities / GoogleUtilities / Network / GULMutableDictionary.m
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 "Private/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[key];
49   });
50   return object;
51 }
52
53 - (void)setObject:(id)object forKey:(id<NSCopying>)key {
54   dispatch_async(_queue, ^{
55     self->_objects[key] = object;
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   // The method this calls is already synchronized.
81   return [self objectForKey:key];
82 }
83
84 - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
85   // The method this calls is already synchronized.
86   [self setObject:obj forKey:key];
87 }
88
89 - (NSDictionary *)dictionary {
90   __block NSDictionary *dictionary;
91   dispatch_sync(_queue, ^{
92     dictionary = [self->_objects copy];
93   });
94   return dictionary;
95 }
96
97 @end