ExtensionRegistry.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Protocol Buffers for Objective C
  2. //
  3. // Copyright 2010 Booyah Inc.
  4. // Copyright 2008 Cyrus Najmabadi
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. /**
  18. * A table of known extensions, searchable by name or field number. When
  19. * parsing a protocol message that might have extensions, you must provide
  20. * an {@code ExtensionRegistry} in which you have registered any extensions
  21. * that you want to be able to parse. Otherwise, those extensions will just
  22. * be treated like unknown fields.
  23. *
  24. * <p>For example, if you had the {@code .proto} file:
  25. *
  26. * <pre>
  27. * option java_class = "MyProto";
  28. *
  29. * message Foo {
  30. * extensions 1000 to max;
  31. * }
  32. *
  33. * extend Foo {
  34. * optional int32 bar;
  35. * }
  36. * </pre>
  37. *
  38. * Then you might write code like:
  39. *
  40. * <pre>
  41. * ExtensionRegistry registry = ExtensionRegistry.newInstance();
  42. * registry.add(MyProto.bar);
  43. * MyProto.Foo message = MyProto.Foo.parseFrom(input, registry);
  44. * </pre>
  45. *
  46. * <p>Background:
  47. *
  48. * <p>You might wonder why this is necessary. Two alternatives might come to
  49. * mind. First, you might imagine a system where generated extensions are
  50. * automatically registered when their containing classes are loaded. This
  51. * is a popular technique, but is bad design; among other things, it creates a
  52. * situation where behavior can change depending on what classes happen to be
  53. * loaded. It also introduces a security vulnerability, because an
  54. * unprivileged class could cause its code to be called unexpectedly from a
  55. * privileged class by registering itself as an extension of the right type.
  56. *
  57. * <p>Another option you might consider is lazy parsing: do not parse an
  58. * extension until it is first requested, at which point the caller must
  59. * provide a type to use. This introduces a different set of problems. First,
  60. * it would require a mutex lock any time an extension was accessed, which
  61. * would be slow. Second, corrupt data would not be detected until first
  62. * access, at which point it would be much harder to deal with it. Third, it
  63. * could violate the expectation that message objects are immutable, since the
  64. * type provided could be any arbitrary message class. An unpriviledged user
  65. * could take advantage of this to inject a mutable object into a message
  66. * belonging to priviledged code and create mischief.
  67. *
  68. * @author Cyrus Najmabadi
  69. */
  70. #import "ExtensionField.h"
  71. @interface PBExtensionRegistry : NSObject {
  72. @protected
  73. NSDictionary* classMap;
  74. }
  75. //空的Registry,默认值
  76. + (PBExtensionRegistry*) emptyRegistry;
  77. //通过fieldNumber获取Field
  78. - (id<PBExtensionField>) getExtension:(Class) clazz fieldNumber:(SInt32) fieldNumber;
  79. /* @protected */
  80. - (instancetype) initWithClassMap:(NSDictionary*) classMap;
  81. //字符串转为类
  82. - (id) keyForClass:(Class) clazz;
  83. @end