No Description

GULMutableDictionary.m 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #import "Private/GULMutableDictionary.h"
  15. @implementation GULMutableDictionary {
  16. /// The mutable dictionary.
  17. NSMutableDictionary *_objects;
  18. /// Serial synchronization queue. All reads should use dispatch_sync, while writes use
  19. /// dispatch_async.
  20. dispatch_queue_t _queue;
  21. }
  22. - (instancetype)init {
  23. self = [super init];
  24. if (self) {
  25. _objects = [[NSMutableDictionary alloc] init];
  26. _queue = dispatch_queue_create("GULMutableDictionary", DISPATCH_QUEUE_SERIAL);
  27. }
  28. return self;
  29. }
  30. - (NSString *)description {
  31. __block NSString *description;
  32. dispatch_sync(_queue, ^{
  33. description = self->_objects.description;
  34. });
  35. return description;
  36. }
  37. - (id)objectForKey:(id)key {
  38. __block id object;
  39. dispatch_sync(_queue, ^{
  40. object = [self->_objects objectForKey:key];
  41. });
  42. return object;
  43. }
  44. - (void)setObject:(id)object forKey:(id<NSCopying>)key {
  45. dispatch_async(_queue, ^{
  46. [self->_objects setObject:object forKey:key];
  47. });
  48. }
  49. - (void)removeObjectForKey:(id)key {
  50. dispatch_async(_queue, ^{
  51. [self->_objects removeObjectForKey:key];
  52. });
  53. }
  54. - (void)removeAllObjects {
  55. dispatch_async(_queue, ^{
  56. [self->_objects removeAllObjects];
  57. });
  58. }
  59. - (NSUInteger)count {
  60. __block NSUInteger count;
  61. dispatch_sync(_queue, ^{
  62. count = self->_objects.count;
  63. });
  64. return count;
  65. }
  66. - (id)objectForKeyedSubscript:(id<NSCopying>)key {
  67. __block id object;
  68. dispatch_sync(_queue, ^{
  69. object = self->_objects[key];
  70. });
  71. return object;
  72. }
  73. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  74. dispatch_async(_queue, ^{
  75. self->_objects[key] = obj;
  76. });
  77. }
  78. - (NSDictionary *)dictionary {
  79. __block NSDictionary *dictionary;
  80. dispatch_sync(_queue, ^{
  81. dictionary = [self->_objects copy];
  82. });
  83. return dictionary;
  84. }
  85. @end