You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
5.0 KiB

6 years ago
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBExtensionRegistry.h"
  31. #import "GPBBootstrap.h"
  32. #import "GPBDescriptor.h"
  33. @implementation GPBExtensionRegistry {
  34. NSMutableDictionary *mutableClassMap_;
  35. }
  36. - (instancetype)init {
  37. if ((self = [super init])) {
  38. mutableClassMap_ = [[NSMutableDictionary alloc] init];
  39. }
  40. return self;
  41. }
  42. - (void)dealloc {
  43. [mutableClassMap_ release];
  44. [super dealloc];
  45. }
  46. // Direct access is use for speed, to avoid even internally declaring things
  47. // read/write, etc. The warning is enabled in the project to ensure code calling
  48. // protos can turn on -Wdirect-ivar-access without issues.
  49. #pragma clang diagnostic push
  50. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  51. - (instancetype)copyWithZone:(NSZone *)zone {
  52. GPBExtensionRegistry *result = [[[self class] allocWithZone:zone] init];
  53. [result addExtensions:self];
  54. return result;
  55. }
  56. - (void)addExtension:(GPBExtensionDescriptor *)extension {
  57. if (extension == nil) {
  58. return;
  59. }
  60. Class containingMessageClass = extension.containingMessageClass;
  61. CFMutableDictionaryRef extensionMap = (CFMutableDictionaryRef)
  62. [mutableClassMap_ objectForKey:containingMessageClass];
  63. if (extensionMap == nil) {
  64. // Use a custom dictionary here because the keys are numbers and conversion
  65. // back and forth from NSNumber isn't worth the cost.
  66. extensionMap = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL,
  67. &kCFTypeDictionaryValueCallBacks);
  68. [mutableClassMap_ setObject:(id)extensionMap
  69. forKey:(id<NSCopying>)containingMessageClass];
  70. CFRelease(extensionMap);
  71. }
  72. ssize_t key = extension.fieldNumber;
  73. CFDictionarySetValue(extensionMap, (const void *)key, extension);
  74. }
  75. - (GPBExtensionDescriptor *)extensionForDescriptor:(GPBDescriptor *)descriptor
  76. fieldNumber:(NSInteger)fieldNumber {
  77. Class messageClass = descriptor.messageClass;
  78. CFMutableDictionaryRef extensionMap = (CFMutableDictionaryRef)
  79. [mutableClassMap_ objectForKey:messageClass];
  80. ssize_t key = fieldNumber;
  81. GPBExtensionDescriptor *result =
  82. (extensionMap
  83. ? CFDictionaryGetValue(extensionMap, (const void *)key)
  84. : nil);
  85. return result;
  86. }
  87. static void CopyKeyValue(const void *key, const void *value, void *context) {
  88. CFMutableDictionaryRef extensionMap = (CFMutableDictionaryRef)context;
  89. CFDictionarySetValue(extensionMap, key, value);
  90. }
  91. - (void)addExtensions:(GPBExtensionRegistry *)registry {
  92. if (registry == nil) {
  93. // In the case where there are no extensions just ignore.
  94. return;
  95. }
  96. NSMutableDictionary *otherClassMap = registry->mutableClassMap_;
  97. [otherClassMap enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL * stop) {
  98. #pragma unused(stop)
  99. Class containingMessageClass = key;
  100. CFMutableDictionaryRef otherExtensionMap = (CFMutableDictionaryRef)value;
  101. CFMutableDictionaryRef extensionMap = (CFMutableDictionaryRef)
  102. [mutableClassMap_ objectForKey:containingMessageClass];
  103. if (extensionMap == nil) {
  104. extensionMap = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, otherExtensionMap);
  105. [mutableClassMap_ setObject:(id)extensionMap
  106. forKey:(id<NSCopying>)containingMessageClass];
  107. CFRelease(extensionMap);
  108. } else {
  109. CFDictionaryApplyFunction(otherExtensionMap, CopyKeyValue, extensionMap);
  110. }
  111. }];
  112. }
  113. #pragma clang diagnostic pop
  114. @end