GPBMessage_PackagePrivate.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // This header is private to the ProtobolBuffers library and must NOT be
  31. // included by any sources outside this library. The contents of this file are
  32. // subject to change at any time without notice.
  33. #import "GPBMessage.h"
  34. #import <libkern/OSAtomic.h>
  35. #import "GPBBootstrap.h"
  36. typedef struct GPBMessage_Storage {
  37. uint32_t _has_storage_[0];
  38. } GPBMessage_Storage;
  39. typedef struct GPBMessage_Storage *GPBMessage_StoragePtr;
  40. @interface GPBMessage () {
  41. @package
  42. // NOTE: Because of the +allocWithZone code using NSAllocateObject(),
  43. // this structure should ideally always be kept pointer aligned where the
  44. // real storage starts is also pointer aligned. The compiler/runtime already
  45. // do this, but it may not be documented.
  46. // A pointer to the actual fields of the subclasses. The actual structure
  47. // pointed to by this pointer will depend on the subclass.
  48. // All of the actual structures will start the same as
  49. // GPBMessage_Storage with _has_storage__ as the first field.
  50. // Kept public because static functions need to access it.
  51. GPBMessage_StoragePtr messageStorage_;
  52. // A lock to provide mutual exclusion from internal data that can be modified
  53. // by *read* operations such as getters (autocreation of message fields and
  54. // message extensions, not setting of values). Used to guarantee thread safety
  55. // for concurrent reads on the message.
  56. // NOTE: OSSpinLock may seem like a good fit here but Apple engineers have
  57. // pointed out that they are vulnerable to live locking on iOS in cases of
  58. // priority inversion:
  59. // http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
  60. // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
  61. // Use of readOnlySemaphore_ must be prefaced by a call to
  62. // GPBPrepareReadOnlySemaphore to ensure it has been created. This allows
  63. // readOnlySemaphore_ to be only created when actually needed.
  64. dispatch_once_t readOnlySemaphoreCreationOnce_;
  65. dispatch_semaphore_t readOnlySemaphore_;
  66. }
  67. // Gets an extension value without autocreating the result if not found. (i.e.
  68. // returns nil if the extension is not set)
  69. - (id)getExistingExtension:(GPBExtensionDescriptor *)extension;
  70. // Parses a message of this type from the input and merges it with this
  71. // message.
  72. //
  73. // Warning: This does not verify that all required fields are present in
  74. // the input message.
  75. // Note: The caller should call
  76. // -[CodedInputStream checkLastTagWas:] after calling this to
  77. // verify that the last tag seen was the appropriate end-group tag,
  78. // or zero for EOF.
  79. // NOTE: This will throw if there is an error while parsing.
  80. - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
  81. extensionRegistry:(GPBExtensionRegistry *)extensionRegistry;
  82. // Parses the next delimited message of this type from the input and merges it
  83. // with this message.
  84. - (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
  85. extensionRegistry:
  86. (GPBExtensionRegistry *)extensionRegistry;
  87. - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data;
  88. @end
  89. CF_EXTERN_C_BEGIN
  90. // Call this before using the readOnlySemaphore_. This ensures it is created only once.
  91. NS_INLINE void GPBPrepareReadOnlySemaphore(GPBMessage *self) {
  92. #pragma clang diagnostic push
  93. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  94. dispatch_once(&self->readOnlySemaphoreCreationOnce_, ^{
  95. self->readOnlySemaphore_ = dispatch_semaphore_create(1);
  96. });
  97. #pragma clang diagnostic pop
  98. }
  99. // Returns a new instance that was automatically created by |autocreator| for
  100. // its field |field|.
  101. GPBMessage *GPBCreateMessageWithAutocreator(Class msgClass,
  102. GPBMessage *autocreator,
  103. GPBFieldDescriptor *field)
  104. __attribute__((ns_returns_retained));
  105. // Returns whether |message| autocreated this message. This is NO if the message
  106. // was not autocreated by |message| or if it has been mutated since
  107. // autocreation.
  108. BOOL GPBWasMessageAutocreatedBy(GPBMessage *message, GPBMessage *parent);
  109. // Call this when you mutate a message. It will cause the message to become
  110. // visible to its autocreator.
  111. void GPBBecomeVisibleToAutocreator(GPBMessage *self);
  112. // Call this when an array/dictionary is mutated so the parent message that
  113. // autocreated it can react.
  114. void GPBAutocreatedArrayModified(GPBMessage *self, id array);
  115. void GPBAutocreatedDictionaryModified(GPBMessage *self, id dictionary);
  116. // Clear the autocreator, if any. Asserts if the autocreator still has an
  117. // autocreated reference to this message.
  118. void GPBClearMessageAutocreator(GPBMessage *self);
  119. CF_EXTERN_C_END