Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 | |
| 31 | #import "GPBDictionary_PackagePrivate.h" |
| 32 | |
| 33 | #import "GPBCodedInputStream_PackagePrivate.h" |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 34 | #import "GPBCodedOutputStream_PackagePrivate.h" |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 35 | #import "GPBDescriptor_PackagePrivate.h" |
| 36 | #import "GPBMessage_PackagePrivate.h" |
| 37 | #import "GPBUtilities_PackagePrivate.h" |
| 38 | |
| 39 | // ------------------------------ NOTE ------------------------------ |
| 40 | // At the moment, this is all using NSNumbers in NSDictionaries under |
| 41 | // the hood, but it is all hidden so we can come back and optimize |
| 42 | // with direct CFDictionary usage later. The reason that wasn't |
| 43 | // done yet is needing to support 32bit iOS builds. Otherwise |
| 44 | // it would be pretty simple to store all this data in CFDictionaries |
| 45 | // directly. |
| 46 | // ------------------------------------------------------------------ |
| 47 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 48 | // Direct access is use for speed, to avoid even internally declaring things |
| 49 | // read/write, etc. The warning is enabled in the project to ensure code calling |
| 50 | // protos can turn on -Wdirect-ivar-access without issues. |
| 51 | #pragma clang diagnostic push |
| 52 | #pragma clang diagnostic ignored "-Wdirect-ivar-access" |
| 53 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 54 | // Used to include code only visible to specific versions of the static |
| 55 | // analyzer. Useful for wrapping code that only exists to silence the analyzer. |
| 56 | // Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION, |
| 57 | // END_APPLE_BUILD_VERSION using: |
| 58 | // xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__ |
| 59 | // Example usage: |
| 60 | // #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif |
| 61 | #define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \ |
| 62 | (defined(__clang_analyzer__) && \ |
| 63 | (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \ |
| 64 | __apple_build_version__ <= END_APPLE_BUILD_VERSION)) |
| 65 | |
| 66 | enum { |
| 67 | kMapKeyFieldNumber = 1, |
| 68 | kMapValueFieldNumber = 2, |
| 69 | }; |
| 70 | |
| 71 | static BOOL DictDefault_IsValidValue(int32_t value) { |
| 72 | // Anything but the bad value marker is allowed. |
| 73 | return (value != kGPBUnrecognizedEnumeratorValue); |
| 74 | } |
| 75 | |
| 76 | //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2) |
| 77 | //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) { |
| 78 | //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) { |
| 79 | //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value); |
| 80 | //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) { |
| 81 | //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value); |
| 82 | //% } else { |
| 83 | //% NSCAssert(NO, @"Unexpected type %d", dataType); |
| 84 | //% return 0; |
| 85 | //% } |
| 86 | //%} |
| 87 | //% |
| 88 | //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) { |
| 89 | //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) { |
| 90 | //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value]; |
| 91 | //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) { |
| 92 | //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value]; |
| 93 | //% } else { |
| 94 | //% NSCAssert(NO, @"Unexpected type %d", dataType); |
| 95 | //% } |
| 96 | //%} |
| 97 | //% |
| 98 | //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3) |
| 99 | //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) { |
| 100 | //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) { |
| 101 | //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value); |
| 102 | //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) { |
| 103 | //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value); |
| 104 | //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) { |
| 105 | //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value); |
| 106 | //% } else { |
| 107 | //% NSCAssert(NO, @"Unexpected type %d", dataType); |
| 108 | //% return 0; |
| 109 | //% } |
| 110 | //%} |
| 111 | //% |
| 112 | //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) { |
| 113 | //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) { |
| 114 | //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value]; |
| 115 | //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) { |
| 116 | //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value]; |
| 117 | //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) { |
| 118 | //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value]; |
| 119 | //% } else { |
| 120 | //% NSCAssert(NO, @"Unexpected type %d", dataType); |
| 121 | //% } |
| 122 | //%} |
| 123 | //% |
| 124 | //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP) |
| 125 | //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) { |
| 126 | //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType); |
| 127 | //% #pragma unused(dataType) // For when asserts are off in release. |
| 128 | //% return GPBCompute##VALUE_NAME##Size(fieldNum, value); |
| 129 | //%} |
| 130 | //% |
| 131 | //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) { |
| 132 | //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType); |
| 133 | //% #pragma unused(dataType) // For when asserts are off in release. |
| 134 | //% [stream write##VALUE_NAME##:fieldNum value:value]; |
| 135 | //%} |
| 136 | //% |
| 137 | //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS() |
| 138 | //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32) |
| 139 | //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32) |
| 140 | //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64) |
| 141 | //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64) |
| 142 | //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, ) |
| 143 | //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, ) |
| 144 | //%SIMPLE_SERIALIZE_SUPPORT(Float, float, ) |
| 145 | //%SIMPLE_SERIALIZE_SUPPORT(Double, double, ) |
| 146 | //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *) |
| 147 | //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes) |
| 148 | //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS() |
| 149 | // This block of code is generated, do not edit it directly. |
| 150 | |
| 151 | static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 152 | if (dataType == GPBDataTypeInt32) { |
| 153 | return GPBComputeInt32Size(fieldNum, value); |
| 154 | } else if (dataType == GPBDataTypeSInt32) { |
| 155 | return GPBComputeSInt32Size(fieldNum, value); |
| 156 | } else if (dataType == GPBDataTypeSFixed32) { |
| 157 | return GPBComputeSFixed32Size(fieldNum, value); |
| 158 | } else { |
| 159 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 160 | return 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 165 | if (dataType == GPBDataTypeInt32) { |
| 166 | [stream writeInt32:fieldNum value:value]; |
| 167 | } else if (dataType == GPBDataTypeSInt32) { |
| 168 | [stream writeSInt32:fieldNum value:value]; |
| 169 | } else if (dataType == GPBDataTypeSFixed32) { |
| 170 | [stream writeSFixed32:fieldNum value:value]; |
| 171 | } else { |
| 172 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 177 | if (dataType == GPBDataTypeUInt32) { |
| 178 | return GPBComputeUInt32Size(fieldNum, value); |
| 179 | } else if (dataType == GPBDataTypeFixed32) { |
| 180 | return GPBComputeFixed32Size(fieldNum, value); |
| 181 | } else { |
| 182 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 183 | return 0; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 188 | if (dataType == GPBDataTypeUInt32) { |
| 189 | [stream writeUInt32:fieldNum value:value]; |
| 190 | } else if (dataType == GPBDataTypeFixed32) { |
| 191 | [stream writeFixed32:fieldNum value:value]; |
| 192 | } else { |
| 193 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 198 | if (dataType == GPBDataTypeInt64) { |
| 199 | return GPBComputeInt64Size(fieldNum, value); |
| 200 | } else if (dataType == GPBDataTypeSInt64) { |
| 201 | return GPBComputeSInt64Size(fieldNum, value); |
| 202 | } else if (dataType == GPBDataTypeSFixed64) { |
| 203 | return GPBComputeSFixed64Size(fieldNum, value); |
| 204 | } else { |
| 205 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 211 | if (dataType == GPBDataTypeInt64) { |
| 212 | [stream writeInt64:fieldNum value:value]; |
| 213 | } else if (dataType == GPBDataTypeSInt64) { |
| 214 | [stream writeSInt64:fieldNum value:value]; |
| 215 | } else if (dataType == GPBDataTypeSFixed64) { |
| 216 | [stream writeSFixed64:fieldNum value:value]; |
| 217 | } else { |
| 218 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 223 | if (dataType == GPBDataTypeUInt64) { |
| 224 | return GPBComputeUInt64Size(fieldNum, value); |
| 225 | } else if (dataType == GPBDataTypeFixed64) { |
| 226 | return GPBComputeFixed64Size(fieldNum, value); |
| 227 | } else { |
| 228 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 229 | return 0; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 234 | if (dataType == GPBDataTypeUInt64) { |
| 235 | [stream writeUInt64:fieldNum value:value]; |
| 236 | } else if (dataType == GPBDataTypeFixed64) { |
| 237 | [stream writeFixed64:fieldNum value:value]; |
| 238 | } else { |
| 239 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) { |
| 244 | NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType); |
| 245 | #pragma unused(dataType) // For when asserts are off in release. |
| 246 | return GPBComputeBoolSize(fieldNum, value); |
| 247 | } |
| 248 | |
| 249 | static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) { |
| 250 | NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType); |
| 251 | #pragma unused(dataType) // For when asserts are off in release. |
| 252 | [stream writeBool:fieldNum value:value]; |
| 253 | } |
| 254 | |
| 255 | static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 256 | NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType); |
| 257 | #pragma unused(dataType) // For when asserts are off in release. |
| 258 | return GPBComputeEnumSize(fieldNum, value); |
| 259 | } |
| 260 | |
| 261 | static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) { |
| 262 | NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType); |
| 263 | #pragma unused(dataType) // For when asserts are off in release. |
| 264 | [stream writeEnum:fieldNum value:value]; |
| 265 | } |
| 266 | |
| 267 | static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) { |
| 268 | NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType); |
| 269 | #pragma unused(dataType) // For when asserts are off in release. |
| 270 | return GPBComputeFloatSize(fieldNum, value); |
| 271 | } |
| 272 | |
| 273 | static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) { |
| 274 | NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType); |
| 275 | #pragma unused(dataType) // For when asserts are off in release. |
| 276 | [stream writeFloat:fieldNum value:value]; |
| 277 | } |
| 278 | |
| 279 | static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) { |
| 280 | NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType); |
| 281 | #pragma unused(dataType) // For when asserts are off in release. |
| 282 | return GPBComputeDoubleSize(fieldNum, value); |
| 283 | } |
| 284 | |
| 285 | static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) { |
| 286 | NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType); |
| 287 | #pragma unused(dataType) // For when asserts are off in release. |
| 288 | [stream writeDouble:fieldNum value:value]; |
| 289 | } |
| 290 | |
| 291 | static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) { |
| 292 | NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType); |
| 293 | #pragma unused(dataType) // For when asserts are off in release. |
| 294 | return GPBComputeStringSize(fieldNum, value); |
| 295 | } |
| 296 | |
| 297 | static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) { |
| 298 | NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType); |
| 299 | #pragma unused(dataType) // For when asserts are off in release. |
| 300 | [stream writeString:fieldNum value:value]; |
| 301 | } |
| 302 | |
| 303 | static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) { |
| 304 | if (dataType == GPBDataTypeMessage) { |
| 305 | return GPBComputeMessageSize(fieldNum, value); |
| 306 | } else if (dataType == GPBDataTypeString) { |
| 307 | return GPBComputeStringSize(fieldNum, value); |
| 308 | } else if (dataType == GPBDataTypeBytes) { |
| 309 | return GPBComputeBytesSize(fieldNum, value); |
| 310 | } else { |
| 311 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 312 | return 0; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) { |
| 317 | if (dataType == GPBDataTypeMessage) { |
| 318 | [stream writeMessage:fieldNum value:value]; |
| 319 | } else if (dataType == GPBDataTypeString) { |
| 320 | [stream writeString:fieldNum value:value]; |
| 321 | } else if (dataType == GPBDataTypeBytes) { |
| 322 | [stream writeBytes:fieldNum value:value]; |
| 323 | } else { |
| 324 | NSCAssert(NO, @"Unexpected type %d", dataType); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS() |
| 329 | |
| 330 | size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) { |
| 331 | GPBDataType mapValueType = GPBGetFieldDataType(field); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 332 | size_t result = 0; |
| 333 | NSString *key; |
| 334 | NSEnumerator *keys = [dict keyEnumerator]; |
| 335 | while ((key = [keys nextObject])) { |
| 336 | id obj = dict[key]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 337 | size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key); |
| 338 | msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType); |
| 339 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 340 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 341 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 342 | result += tagSize * dict.count; |
| 343 | return result; |
| 344 | } |
| 345 | |
| 346 | void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream, |
| 347 | NSDictionary *dict, |
| 348 | GPBFieldDescriptor *field) { |
| 349 | NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type"); |
| 350 | GPBDataType mapValueType = GPBGetFieldDataType(field); |
| 351 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 352 | NSString *key; |
| 353 | NSEnumerator *keys = [dict keyEnumerator]; |
| 354 | while ((key = [keys nextObject])) { |
| 355 | id obj = dict[key]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 356 | // Write the tag. |
| 357 | [outputStream writeInt32NoTag:tag]; |
| 358 | // Write the size of the message. |
| 359 | size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key); |
| 360 | msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType); |
| 361 | |
| 362 | // Write the size and fields. |
| 363 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 364 | [outputStream writeString:kMapKeyFieldNumber value:key]; |
| 365 | WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 366 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) { |
| 370 | NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type"); |
| 371 | NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type"); |
| 372 | #pragma unused(field) // For when asserts are off in release. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 373 | GPBMessage *msg; |
| 374 | NSEnumerator *objects = [dict objectEnumerator]; |
| 375 | while ((msg = [objects nextObject])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 376 | if (!msg.initialized) { |
| 377 | return NO; |
| 378 | } |
| 379 | } |
| 380 | return YES; |
| 381 | } |
| 382 | |
| 383 | // Note: if the type is an object, it the retain pass back to the caller. |
| 384 | static void ReadValue(GPBCodedInputStream *stream, |
| 385 | GPBGenericValue *valueToFill, |
| 386 | GPBDataType type, |
| 387 | GPBExtensionRegistry *registry, |
| 388 | GPBFieldDescriptor *field) { |
| 389 | switch (type) { |
| 390 | case GPBDataTypeBool: |
| 391 | valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_); |
| 392 | break; |
| 393 | case GPBDataTypeFixed32: |
| 394 | valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_); |
| 395 | break; |
| 396 | case GPBDataTypeSFixed32: |
| 397 | valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_); |
| 398 | break; |
| 399 | case GPBDataTypeFloat: |
| 400 | valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_); |
| 401 | break; |
| 402 | case GPBDataTypeFixed64: |
| 403 | valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_); |
| 404 | break; |
| 405 | case GPBDataTypeSFixed64: |
| 406 | valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_); |
| 407 | break; |
| 408 | case GPBDataTypeDouble: |
| 409 | valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_); |
| 410 | break; |
| 411 | case GPBDataTypeInt32: |
| 412 | valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_); |
| 413 | break; |
| 414 | case GPBDataTypeInt64: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 415 | valueToFill->valueInt64 = GPBCodedInputStreamReadInt64(&stream->state_); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 416 | break; |
| 417 | case GPBDataTypeSInt32: |
| 418 | valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_); |
| 419 | break; |
| 420 | case GPBDataTypeSInt64: |
| 421 | valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_); |
| 422 | break; |
| 423 | case GPBDataTypeUInt32: |
| 424 | valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_); |
| 425 | break; |
| 426 | case GPBDataTypeUInt64: |
| 427 | valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_); |
| 428 | break; |
| 429 | case GPBDataTypeBytes: |
| 430 | [valueToFill->valueData release]; |
| 431 | valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_); |
| 432 | break; |
| 433 | case GPBDataTypeString: |
| 434 | [valueToFill->valueString release]; |
| 435 | valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_); |
| 436 | break; |
| 437 | case GPBDataTypeMessage: { |
| 438 | GPBMessage *message = [[field.msgClass alloc] init]; |
| 439 | [stream readMessage:message extensionRegistry:registry]; |
| 440 | [valueToFill->valueMessage release]; |
| 441 | valueToFill->valueMessage = message; |
| 442 | break; |
| 443 | } |
| 444 | case GPBDataTypeGroup: |
| 445 | NSCAssert(NO, @"Can't happen"); |
| 446 | break; |
| 447 | case GPBDataTypeEnum: |
| 448 | valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_); |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void GPBDictionaryReadEntry(id mapDictionary, |
| 454 | GPBCodedInputStream *stream, |
| 455 | GPBExtensionRegistry *registry, |
| 456 | GPBFieldDescriptor *field, |
| 457 | GPBMessage *parentMessage) { |
| 458 | GPBDataType keyDataType = field.mapKeyDataType; |
| 459 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 460 | |
| 461 | GPBGenericValue key; |
| 462 | GPBGenericValue value; |
| 463 | // Zero them (but pick up any enum default for proto2). |
| 464 | key.valueString = value.valueString = nil; |
| 465 | if (valueDataType == GPBDataTypeEnum) { |
| 466 | value = field.defaultValue; |
| 467 | } |
| 468 | |
| 469 | GPBCodedInputStreamState *state = &stream->state_; |
| 470 | uint32_t keyTag = |
| 471 | GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO)); |
| 472 | uint32_t valueTag = |
| 473 | GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO)); |
| 474 | |
| 475 | BOOL hitError = NO; |
| 476 | while (YES) { |
| 477 | uint32_t tag = GPBCodedInputStreamReadTag(state); |
| 478 | if (tag == keyTag) { |
| 479 | ReadValue(stream, &key, keyDataType, registry, field); |
| 480 | } else if (tag == valueTag) { |
| 481 | ReadValue(stream, &value, valueDataType, registry, field); |
| 482 | } else if (tag == 0) { |
| 483 | // zero signals EOF / limit reached |
| 484 | break; |
| 485 | } else { // Unknown |
| 486 | if (![stream skipField:tag]){ |
| 487 | hitError = YES; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (!hitError) { |
| 494 | // Handle the special defaults and/or missing key/value. |
| 495 | if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) { |
| 496 | key.valueString = [@"" retain]; |
| 497 | } |
| 498 | if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 499 | #pragma clang diagnostic push |
| 500 | #pragma clang diagnostic ignored "-Wswitch-enum" |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 501 | switch (valueDataType) { |
| 502 | case GPBDataTypeString: |
| 503 | value.valueString = [@"" retain]; |
| 504 | break; |
| 505 | case GPBDataTypeBytes: |
| 506 | value.valueData = [GPBEmptyNSData() retain]; |
| 507 | break; |
| 508 | #if defined(__clang_analyzer__) |
| 509 | case GPBDataTypeGroup: |
| 510 | // Maps can't really have Groups as the value type, but this case is needed |
| 511 | // so the analyzer won't report the posibility of send nil in for the value |
| 512 | // in the NSMutableDictionary case below. |
| 513 | #endif |
| 514 | case GPBDataTypeMessage: { |
| 515 | value.valueMessage = [[field.msgClass alloc] init]; |
| 516 | break; |
| 517 | } |
| 518 | default: |
| 519 | // Nothing |
| 520 | break; |
| 521 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 522 | #pragma clang diagnostic pop |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) { |
| 526 | #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181) |
| 527 | // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can |
| 528 | // be raised as needed for new Xcodes. |
| 529 | // |
| 530 | // This is only needed on a "shallow" analyze; on a "deep" analyze, the |
| 531 | // existing code path gets this correct. In shallow, the analyzer decides |
| 532 | // GPBDataTypeIsObject(valueDataType) is both false and true on a single |
| 533 | // path through this function, allowing nil to be used for the |
| 534 | // setObject:forKey:. |
| 535 | if (value.valueString == nil) { |
| 536 | value.valueString = [@"" retain]; |
| 537 | } |
| 538 | #endif |
| 539 | // mapDictionary is an NSMutableDictionary |
| 540 | [(NSMutableDictionary *)mapDictionary setObject:value.valueString |
| 541 | forKey:key.valueString]; |
| 542 | } else { |
| 543 | if (valueDataType == GPBDataTypeEnum) { |
| 544 | if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) || |
| 545 | [field isValidEnumValue:value.valueEnum]) { |
| 546 | [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key]; |
| 547 | } else { |
| 548 | NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum |
| 549 | forKey:&key |
| 550 | keyDataType:keyDataType]; |
| 551 | [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data]; |
| 552 | } |
| 553 | } else { |
| 554 | [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key]; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | if (GPBDataTypeIsObject(keyDataType)) { |
| 560 | [key.valueString release]; |
| 561 | } |
| 562 | if (GPBDataTypeIsObject(valueDataType)) { |
| 563 | [value.valueString release]; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // |
| 568 | // Macros for the common basic cases. |
| 569 | // |
| 570 | |
| 571 | //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE) |
| 572 | //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD) |
| 573 | //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id) |
| 574 | |
| 575 | //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER) |
| 576 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER) |
| 577 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER) |
| 578 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER) |
| 579 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER) |
| 580 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER) |
| 581 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER) |
| 582 | //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER) |
| 583 | //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER) |
| 584 | |
| 585 | //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 586 | //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 587 | |
| 588 | //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 589 | //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 590 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 591 | //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 592 | //%#pragma mark - KEY_NAME -> VALUE_NAME |
| 593 | //% |
| 594 | //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary { |
| 595 | //% @package |
| 596 | //% NSMutableDictionary *_dictionary; |
| 597 | //%} |
| 598 | //% |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 599 | //%- (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 600 | //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 601 | //%} |
| 602 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 603 | //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 604 | //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys |
| 605 | //% ##VNAME$S## count:(NSUInteger)count { |
| 606 | //% self = [super init]; |
| 607 | //% if (self) { |
| 608 | //% _dictionary = [[NSMutableDictionary alloc] init]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 609 | //% if (count && VNAME_VAR##s && keys) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 610 | //% for (NSUInteger i = 0; i < count; ++i) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 611 | //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR##s[i]) forKey:WRAPPED##KHELPER(keys[i])]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 612 | //% } |
| 613 | //% } |
| 614 | //% } |
| 615 | //% return self; |
| 616 | //%} |
| 617 | //% |
| 618 | //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 619 | //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 620 | //% if (self) { |
| 621 | //% if (dictionary) { |
| 622 | //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 623 | //% } |
| 624 | //% } |
| 625 | //% return self; |
| 626 | //%} |
| 627 | //% |
| 628 | //%- (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 629 | //% #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 630 | //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 631 | //%} |
| 632 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 633 | //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 634 | //% |
| 635 | //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER) |
| 636 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 637 | //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 638 | //% |
| 639 | //%@end |
| 640 | //% |
| 641 | |
| 642 | //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER) |
| 643 | //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD) |
| 644 | //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER) |
| 645 | //%#pragma mark - KEY_NAME -> VALUE_NAME |
| 646 | //% |
| 647 | //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary { |
| 648 | //% @package |
| 649 | //% NSMutableDictionary *_dictionary; |
| 650 | //% GPBEnumValidationFunc _validationFunc; |
| 651 | //%} |
| 652 | //% |
| 653 | //%@synthesize validationFunc = _validationFunc; |
| 654 | //% |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 655 | //%- (instancetype)init { |
| 656 | //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 657 | //%} |
| 658 | //% |
| 659 | //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 660 | //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 661 | //%} |
| 662 | //% |
| 663 | //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 664 | //% rawValues:(const VALUE_TYPE [])rawValues |
| 665 | //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys |
| 666 | //% count:(NSUInteger)count { |
| 667 | //% self = [super init]; |
| 668 | //% if (self) { |
| 669 | //% _dictionary = [[NSMutableDictionary alloc] init]; |
| 670 | //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 671 | //% if (count && rawValues && keys) { |
| 672 | //% for (NSUInteger i = 0; i < count; ++i) { |
| 673 | //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])]; |
| 674 | //% } |
| 675 | //% } |
| 676 | //% } |
| 677 | //% return self; |
| 678 | //%} |
| 679 | //% |
| 680 | //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary { |
| 681 | //% self = [self initWithValidationFunction:dictionary.validationFunc |
| 682 | //% rawValues:NULL |
| 683 | //% forKeys:NULL |
| 684 | //% count:0]; |
| 685 | //% if (self) { |
| 686 | //% if (dictionary) { |
| 687 | //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 688 | //% } |
| 689 | //% } |
| 690 | //% return self; |
| 691 | //%} |
| 692 | //% |
| 693 | //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 694 | //% capacity:(NSUInteger)numItems { |
| 695 | //% #pragma unused(numItems) |
| 696 | //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 697 | //%} |
| 698 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 699 | //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 700 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 701 | //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 702 | //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)]; |
| 703 | //% if (wrapped && value) { |
| 704 | //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped); |
| 705 | //% if (!_validationFunc(result)) { |
| 706 | //% result = kGPBUnrecognizedEnumeratorValue; |
| 707 | //% } |
| 708 | //% *value = result; |
| 709 | //% } |
| 710 | //% return (wrapped != NULL); |
| 711 | //%} |
| 712 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 713 | //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 714 | //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)]; |
| 715 | //% if (wrapped && rawValue) { |
| 716 | //% *rawValue = UNWRAP##VALUE_NAME(wrapped); |
| 717 | //% } |
| 718 | //% return (wrapped != NULL); |
| 719 | //%} |
| 720 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 721 | //%- (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 722 | //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block { |
| 723 | //% GPBEnumValidationFunc func = _validationFunc; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 724 | //% BOOL stop = NO; |
| 725 | //% NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 726 | //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey; |
| 727 | //% while ((aKey = [keys nextObject])) { |
| 728 | //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 729 | //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue); |
| 730 | //% if (!func(unwrapped)) { |
| 731 | //% unwrapped = kGPBUnrecognizedEnumeratorValue; |
| 732 | //% } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 733 | //% block(UNWRAP##KEY_NAME(aKey), unwrapped, &stop); |
| 734 | //% if (stop) { |
| 735 | //% break; |
| 736 | //% } |
| 737 | //% } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 738 | //%} |
| 739 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 740 | //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 741 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 742 | //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 743 | //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) { |
| 744 | //% [NSException raise:NSInvalidArgumentException |
| 745 | //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)", |
| 746 | //% value]; |
| 747 | //% } |
| 748 | //% |
| 749 | //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)]; |
| 750 | //% if (_autocreator) { |
| 751 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 752 | //% } |
| 753 | //%} |
| 754 | //% |
| 755 | //%@end |
| 756 | //% |
| 757 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 758 | //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 759 | //%- (void)dealloc { |
| 760 | //% NSAssert(!_autocreator, |
| 761 | //% @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 762 | //% [self class], _autocreator); |
| 763 | //% [_dictionary release]; |
| 764 | //% [super dealloc]; |
| 765 | //%} |
| 766 | //% |
| 767 | //%- (instancetype)copyWithZone:(NSZone *)zone { |
| 768 | //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 769 | //%} |
| 770 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 771 | //%- (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 772 | //% if (self == other) { |
| 773 | //% return YES; |
| 774 | //% } |
| 775 | //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) { |
| 776 | //% return NO; |
| 777 | //% } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 778 | //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other; |
| 779 | //% return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 780 | //%} |
| 781 | //% |
| 782 | //%- (NSUInteger)hash { |
| 783 | //% return _dictionary.count; |
| 784 | //%} |
| 785 | //% |
| 786 | //%- (NSString *)description { |
| 787 | //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 788 | //%} |
| 789 | //% |
| 790 | //%- (NSUInteger)count { |
| 791 | //% return _dictionary.count; |
| 792 | //%} |
| 793 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 794 | //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock: |
| 795 | //% (void (^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block { |
| 796 | //% BOOL stop = NO; |
| 797 | //% NSDictionary *internal = _dictionary; |
| 798 | //% NSEnumerator *keys = [internal keyEnumerator]; |
| 799 | //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey; |
| 800 | //% while ((aKey = [keys nextObject])) { |
| 801 | //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey]; |
| 802 | //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), &stop); |
| 803 | //% if (stop) { |
| 804 | //% break; |
| 805 | //% } |
| 806 | //% } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 807 | //%} |
| 808 | //% |
| 809 | //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 810 | //% NSDictionary *internal = _dictionary; |
| 811 | //% NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 812 | //% if (count == 0) { |
| 813 | //% return 0; |
| 814 | //% } |
| 815 | //% |
| 816 | //% GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 817 | //% GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 818 | //% size_t result = 0; |
| 819 | //% NSEnumerator *keys = [internal keyEnumerator]; |
| 820 | //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey; |
| 821 | //% while ((aKey = [keys nextObject])) { |
| 822 | //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 823 | //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 824 | //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 825 | //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 826 | //% } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 827 | //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 828 | //% result += tagSize * count; |
| 829 | //% return result; |
| 830 | //%} |
| 831 | //% |
| 832 | //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 833 | //% asField:(GPBFieldDescriptor *)field { |
| 834 | //% GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 835 | //% GPBDataType keyDataType = field.mapKeyDataType; |
| 836 | //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 837 | //% NSDictionary *internal = _dictionary; |
| 838 | //% NSEnumerator *keys = [internal keyEnumerator]; |
| 839 | //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey; |
| 840 | //% while ((aKey = [keys nextObject])) { |
| 841 | //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 842 | //% [outputStream writeInt32NoTag:tag]; |
| 843 | //% // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 844 | //% KEY_TYPE KisP##unwrappedKey = UNWRAP##KEY_NAME(aKey); |
| 845 | //% VALUE_TYPE unwrappedValue = UNWRAP##VALUE_NAME(a##VNAME_VAR$u); |
| 846 | //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 847 | //% msgSize += ComputeDict##VALUE_NAME##FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 848 | //% [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 849 | //% // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 850 | //% WriteDict##KEY_NAME##Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 851 | //% WriteDict##VALUE_NAME##Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 852 | //% } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 853 | //%} |
| 854 | //% |
| 855 | //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value |
| 856 | //% forGPBGenericValueKey:(GPBGenericValue *)key { |
| 857 | //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)]; |
| 858 | //%} |
| 859 | //% |
| 860 | //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 861 | //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 862 | //% #pragma unused(stop) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 863 | //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 864 | //% }]; |
| 865 | //%} |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 866 | //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME) |
| 867 | //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME) |
| 868 | //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 869 | //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary { |
| 870 | //% if (otherDictionary) { |
| 871 | //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 872 | //% if (_autocreator) { |
| 873 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 874 | //% } |
| 875 | //% } |
| 876 | //%} |
| 877 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 878 | //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key { |
| 879 | //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 880 | //% if (_autocreator) { |
| 881 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 882 | //% } |
| 883 | //%} |
| 884 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 885 | //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 886 | //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)]; |
| 887 | //%} |
| 888 | //% |
| 889 | //%- (void)removeAll { |
| 890 | //% [_dictionary removeAllObjects]; |
| 891 | //%} |
| 892 | |
| 893 | // |
| 894 | // Custom Generation for Bool keys |
| 895 | // |
| 896 | |
| 897 | //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 898 | //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 899 | //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 900 | //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 901 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 902 | //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 903 | //%#pragma mark - Bool -> VALUE_NAME |
| 904 | //% |
| 905 | //%@implementation GPBBool##VALUE_NAME##Dictionary { |
| 906 | //% @package |
| 907 | //% VALUE_TYPE _values[2]; |
| 908 | //%BOOL_DICT_HAS_STORAGE_##HELPER()} |
| 909 | //% |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 910 | //%- (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 911 | //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 912 | //%} |
| 913 | //% |
| 914 | //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE) |
| 915 | //% |
| 916 | //%- (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 917 | //% #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 918 | //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 919 | //%} |
| 920 | //% |
| 921 | //%BOOL_DICT_DEALLOC##HELPER() |
| 922 | //% |
| 923 | //%- (instancetype)copyWithZone:(NSZone *)zone { |
| 924 | //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 925 | //%} |
| 926 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 927 | //%- (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 928 | //% if (self == other) { |
| 929 | //% return YES; |
| 930 | //% } |
| 931 | //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) { |
| 932 | //% return NO; |
| 933 | //% } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 934 | //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other; |
| 935 | //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) || |
| 936 | //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 937 | //% return NO; |
| 938 | //% } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 939 | //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) || |
| 940 | //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 941 | //% return NO; |
| 942 | //% } |
| 943 | //% return YES; |
| 944 | //%} |
| 945 | //% |
| 946 | //%- (NSUInteger)hash { |
| 947 | //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0); |
| 948 | //%} |
| 949 | //% |
| 950 | //%- (NSString *)description { |
| 951 | //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 952 | //% if (BOOL_DICT_W_HAS##HELPER(0, )) { |
| 953 | //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]]; |
| 954 | //% } |
| 955 | //% if (BOOL_DICT_W_HAS##HELPER(1, )) { |
| 956 | //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]]; |
| 957 | //% } |
| 958 | //% [result appendString:@" }"]; |
| 959 | //% return result; |
| 960 | //%} |
| 961 | //% |
| 962 | //%- (NSUInteger)count { |
| 963 | //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0); |
| 964 | //%} |
| 965 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 966 | //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 967 | //% |
| 968 | //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP) |
| 969 | //% |
| 970 | //%- (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 971 | //% if (BOOL_DICT_HAS##HELPER(0, )) { |
| 972 | //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0])); |
| 973 | //% } |
| 974 | //% if (BOOL_DICT_W_HAS##HELPER(1, )) { |
| 975 | //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1])); |
| 976 | //% } |
| 977 | //%} |
| 978 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 979 | //%- (void)enumerateKeysAnd##VNAME##sUsingBlock: |
| 980 | //% (void (^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 981 | //% BOOL stop = NO; |
| 982 | //% if (BOOL_DICT_HAS##HELPER(0, )) { |
| 983 | //% block(NO, _values[0], &stop); |
| 984 | //% } |
| 985 | //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) { |
| 986 | //% block(YES, _values[1], &stop); |
| 987 | //% } |
| 988 | //%} |
| 989 | //% |
| 990 | //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 991 | //% GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 992 | //% NSUInteger count = 0; |
| 993 | //% size_t result = 0; |
| 994 | //% for (int i = 0; i < 2; ++i) { |
| 995 | //% if (BOOL_DICT_HAS##HELPER(i, )) { |
| 996 | //% ++count; |
| 997 | //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 998 | //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 999 | //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 1000 | //% } |
| 1001 | //% } |
| 1002 | //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 1003 | //% result += tagSize * count; |
| 1004 | //% return result; |
| 1005 | //%} |
| 1006 | //% |
| 1007 | //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 1008 | //% asField:(GPBFieldDescriptor *)field { |
| 1009 | //% GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1010 | //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 1011 | //% for (int i = 0; i < 2; ++i) { |
| 1012 | //% if (BOOL_DICT_HAS##HELPER(i, )) { |
| 1013 | //% // Write the tag. |
| 1014 | //% [outputStream writeInt32NoTag:tag]; |
| 1015 | //% // Write the size of the message. |
| 1016 | //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 1017 | //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 1018 | //% [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 1019 | //% // Write the fields. |
| 1020 | //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 1021 | //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 1022 | //% } |
| 1023 | //% } |
| 1024 | //%} |
| 1025 | //% |
| 1026 | //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE) |
| 1027 | //% |
| 1028 | //%@end |
| 1029 | //% |
| 1030 | |
| 1031 | |
| 1032 | // |
| 1033 | // Helpers for PODs |
| 1034 | // |
| 1035 | |
| 1036 | //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1037 | //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1038 | //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)]; |
| 1039 | //% if (wrapped && value) { |
| 1040 | //% *value = UNWRAP##VALUE_NAME(wrapped); |
| 1041 | //% } |
| 1042 | //% return (wrapped != NULL); |
| 1043 | //%} |
| 1044 | //%PDDM-DEFINE WRAPPEDPOD(VALUE) |
| 1045 | //%@(VALUE) |
| 1046 | //%PDDM-DEFINE UNWRAPUInt32(VALUE) |
| 1047 | //%[VALUE unsignedIntValue] |
| 1048 | //%PDDM-DEFINE UNWRAPInt32(VALUE) |
| 1049 | //%[VALUE intValue] |
| 1050 | //%PDDM-DEFINE UNWRAPUInt64(VALUE) |
| 1051 | //%[VALUE unsignedLongLongValue] |
| 1052 | //%PDDM-DEFINE UNWRAPInt64(VALUE) |
| 1053 | //%[VALUE longLongValue] |
| 1054 | //%PDDM-DEFINE UNWRAPBool(VALUE) |
| 1055 | //%[VALUE boolValue] |
| 1056 | //%PDDM-DEFINE UNWRAPFloat(VALUE) |
| 1057 | //%[VALUE floatValue] |
| 1058 | //%PDDM-DEFINE UNWRAPDouble(VALUE) |
| 1059 | //%[VALUE doubleValue] |
| 1060 | //%PDDM-DEFINE UNWRAPEnum(VALUE) |
| 1061 | //%[VALUE intValue] |
| 1062 | //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE) |
| 1063 | //%[NSString stringWithFormat:@"%u", VALUE] |
| 1064 | //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE) |
| 1065 | //%[NSString stringWithFormat:@"%d", VALUE] |
| 1066 | //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE) |
| 1067 | //%[NSString stringWithFormat:@"%llu", VALUE] |
| 1068 | //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE) |
| 1069 | //%[NSString stringWithFormat:@"%lld", VALUE] |
| 1070 | //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE) |
| 1071 | //%(VALUE ? @"true" : @"false") |
| 1072 | //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE) |
| 1073 | //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE] |
| 1074 | //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE) |
| 1075 | //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE] |
| 1076 | //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE) |
| 1077 | //%@(VALUE) |
| 1078 | //%PDDM-DEFINE ENUM_TYPEPOD(TYPE) |
| 1079 | //%NSNumber * |
| 1080 | //%PDDM-DEFINE NEQ_POD(VAL1, VAL2) |
| 1081 | //%VAL1 != VAL2 |
| 1082 | //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME) |
| 1083 | // Empty |
| 1084 | //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME) |
| 1085 | // Empty |
| 1086 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME) |
| 1087 | //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME) |
| 1088 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME) |
| 1089 | // Empty |
| 1090 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME) |
| 1091 | // Empty |
| 1092 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME) |
| 1093 | // Empty |
| 1094 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME) |
| 1095 | // Empty |
| 1096 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME) |
| 1097 | // Empty |
| 1098 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME) |
| 1099 | // Empty |
| 1100 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME) |
| 1101 | // Empty |
| 1102 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME) |
| 1103 | //%- (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 1104 | //% forKey:(GPBGenericValue *)key |
| 1105 | //% keyDataType:(GPBDataType)keyDataType { |
| 1106 | //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType); |
| 1107 | //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 1108 | //% NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 1109 | //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 1110 | //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType); |
| 1111 | //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 1112 | //% [outputStream release]; |
| 1113 | //% return data; |
| 1114 | //%} |
| 1115 | //% |
| 1116 | //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME) |
| 1117 | //%value##VALUE_NAME |
| 1118 | //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT) |
| 1119 | // Empty |
| 1120 | //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT) |
| 1121 | // Empty |
| 1122 | |
| 1123 | //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD() |
| 1124 | //% BOOL _valueSet[2]; |
| 1125 | //% |
| 1126 | //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1127 | //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values |
| 1128 | //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys |
| 1129 | //% ##VALUE_NAME$S## count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1130 | //% self = [super init]; |
| 1131 | //% if (self) { |
| 1132 | //% for (NSUInteger i = 0; i < count; ++i) { |
| 1133 | //% int idx = keys[i] ? 1 : 0; |
| 1134 | //% _values[idx] = values[i]; |
| 1135 | //% _valueSet[idx] = YES; |
| 1136 | //% } |
| 1137 | //% } |
| 1138 | //% return self; |
| 1139 | //%} |
| 1140 | //% |
| 1141 | //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1142 | //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1143 | //% if (self) { |
| 1144 | //% if (dictionary) { |
| 1145 | //% for (int i = 0; i < 2; ++i) { |
| 1146 | //% if (dictionary->_valueSet[i]) { |
| 1147 | //% _values[i] = dictionary->_values[i]; |
| 1148 | //% _valueSet[i] = YES; |
| 1149 | //% } |
| 1150 | //% } |
| 1151 | //% } |
| 1152 | //% } |
| 1153 | //% return self; |
| 1154 | //%} |
| 1155 | //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD() |
| 1156 | //%#if !defined(NS_BLOCK_ASSERTIONS) |
| 1157 | //%- (void)dealloc { |
| 1158 | //% NSAssert(!_autocreator, |
| 1159 | //% @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 1160 | //% [self class], _autocreator); |
| 1161 | //% [super dealloc]; |
| 1162 | //%} |
| 1163 | //%#endif // !defined(NS_BLOCK_ASSERTIONS) |
| 1164 | //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF) |
| 1165 | //%BOOL_DICT_HASPOD(IDX, REF) |
| 1166 | //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF) |
| 1167 | //%REF##_valueSet[IDX] |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1168 | //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE) |
| 1169 | //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1170 | //% int idx = (key ? 1 : 0); |
| 1171 | //% if (_valueSet[idx]) { |
| 1172 | //% if (value) { |
| 1173 | //% *value = _values[idx]; |
| 1174 | //% } |
| 1175 | //% return YES; |
| 1176 | //% } |
| 1177 | //% return NO; |
| 1178 | //%} |
| 1179 | //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP) |
| 1180 | //%- (void)setGPBGenericValue:(GPBGenericValue *)value |
| 1181 | //% forGPBGenericValueKey:(GPBGenericValue *)key { |
| 1182 | //% int idx = (key->valueBool ? 1 : 0); |
| 1183 | //% _values[idx] = value->value##VALUE_NAME; |
| 1184 | //% _valueSet[idx] = YES; |
| 1185 | //%} |
| 1186 | //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE) |
| 1187 | //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary { |
| 1188 | //% if (otherDictionary) { |
| 1189 | //% for (int i = 0; i < 2; ++i) { |
| 1190 | //% if (otherDictionary->_valueSet[i]) { |
| 1191 | //% _valueSet[i] = YES; |
| 1192 | //% _values[i] = otherDictionary->_values[i]; |
| 1193 | //% } |
| 1194 | //% } |
| 1195 | //% if (_autocreator) { |
| 1196 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1197 | //% } |
| 1198 | //% } |
| 1199 | //%} |
| 1200 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1201 | //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1202 | //% int idx = (key ? 1 : 0); |
| 1203 | //% _values[idx] = value; |
| 1204 | //% _valueSet[idx] = YES; |
| 1205 | //% if (_autocreator) { |
| 1206 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1207 | //% } |
| 1208 | //%} |
| 1209 | //% |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1210 | //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1211 | //% _valueSet[aKey ? 1 : 0] = NO; |
| 1212 | //%} |
| 1213 | //% |
| 1214 | //%- (void)removeAll { |
| 1215 | //% _valueSet[0] = NO; |
| 1216 | //% _valueSet[1] = NO; |
| 1217 | //%} |
| 1218 | //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME) |
| 1219 | //%STR_FORMAT_##VALUE_NAME() |
| 1220 | //%PDDM-DEFINE STR_FORMAT_UInt32() |
| 1221 | //%%u |
| 1222 | //%PDDM-DEFINE STR_FORMAT_Int32() |
| 1223 | //%%d |
| 1224 | //%PDDM-DEFINE STR_FORMAT_UInt64() |
| 1225 | //%%llu |
| 1226 | //%PDDM-DEFINE STR_FORMAT_Int64() |
| 1227 | //%%lld |
| 1228 | //%PDDM-DEFINE STR_FORMAT_Bool() |
| 1229 | //%%d |
| 1230 | //%PDDM-DEFINE STR_FORMAT_Float() |
| 1231 | //%%f |
| 1232 | //%PDDM-DEFINE STR_FORMAT_Double() |
| 1233 | //%%lf |
| 1234 | |
| 1235 | // |
| 1236 | // Helpers for Objects |
| 1237 | // |
| 1238 | |
| 1239 | //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER) |
| 1240 | //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key { |
| 1241 | //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)]; |
| 1242 | //% return result; |
| 1243 | //%} |
| 1244 | //%PDDM-DEFINE WRAPPEDOBJECT(VALUE) |
| 1245 | //%VALUE |
| 1246 | //%PDDM-DEFINE UNWRAPString(VALUE) |
| 1247 | //%VALUE |
| 1248 | //%PDDM-DEFINE UNWRAPObject(VALUE) |
| 1249 | //%VALUE |
| 1250 | //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE) |
| 1251 | //%VALUE |
| 1252 | //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE) |
| 1253 | //%VALUE |
| 1254 | //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE) |
| 1255 | //%ENUM_TYPEOBJECT_##TYPE() |
| 1256 | //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString() |
| 1257 | //%NSString * |
| 1258 | //%PDDM-DEFINE ENUM_TYPEOBJECT_id() |
| 1259 | //%id ## |
| 1260 | //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2) |
| 1261 | //%![VAL1 isEqual:VAL2] |
| 1262 | //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME) |
| 1263 | //%- (BOOL)isInitialized { |
| 1264 | //% for (GPBMessage *msg in [_dictionary objectEnumerator]) { |
| 1265 | //% if (!msg.initialized) { |
| 1266 | //% return NO; |
| 1267 | //% } |
| 1268 | //% } |
| 1269 | //% return YES; |
| 1270 | //%} |
| 1271 | //% |
| 1272 | //%- (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 1273 | //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict = |
| 1274 | //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1275 | //% NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 1276 | //% id aKey; |
| 1277 | //% NSMutableDictionary *internalDict = newDict->_dictionary; |
| 1278 | //% while ((aKey = [keys nextObject])) { |
| 1279 | //% GPBMessage *msg = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1280 | //% GPBMessage *copiedMsg = [msg copyWithZone:zone]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1281 | //% [internalDict setObject:copiedMsg forKey:aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1282 | //% [copiedMsg release]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1283 | //% } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1284 | //% return newDict; |
| 1285 | //%} |
| 1286 | //% |
| 1287 | //% |
| 1288 | //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME) |
| 1289 | //%- (BOOL)isInitialized { |
| 1290 | //% if (_values[0] && ![_values[0] isInitialized]) { |
| 1291 | //% return NO; |
| 1292 | //% } |
| 1293 | //% if (_values[1] && ![_values[1] isInitialized]) { |
| 1294 | //% return NO; |
| 1295 | //% } |
| 1296 | //% return YES; |
| 1297 | //%} |
| 1298 | //% |
| 1299 | //%- (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 1300 | //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict = |
| 1301 | //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init]; |
| 1302 | //% for (int i = 0; i < 2; ++i) { |
| 1303 | //% if (_values[i] != nil) { |
| 1304 | //% newDict->_values[i] = [_values[i] copyWithZone:zone]; |
| 1305 | //% } |
| 1306 | //% } |
| 1307 | //% return newDict; |
| 1308 | //%} |
| 1309 | //% |
| 1310 | //% |
| 1311 | //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME) |
| 1312 | // Empty |
| 1313 | //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME) |
| 1314 | //%valueString |
| 1315 | //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT) |
| 1316 | //%##EXTRA_INDENT$S## if (!##VALUE_NAME) { |
| 1317 | //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException |
| 1318 | //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"]; |
| 1319 | //%##EXTRA_INDENT$S## } |
| 1320 | //% |
| 1321 | //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT) |
| 1322 | //%##EXTRA_INDENT$S## if (!##KEY_NAME) { |
| 1323 | //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException |
| 1324 | //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"]; |
| 1325 | //%##EXTRA_INDENT$S## } |
| 1326 | //% |
| 1327 | |
| 1328 | //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT() |
| 1329 | // Empty |
| 1330 | //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE) |
| 1331 | //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects |
| 1332 | //% forKeys:(const BOOL [])keys |
| 1333 | //% count:(NSUInteger)count { |
| 1334 | //% self = [super init]; |
| 1335 | //% if (self) { |
| 1336 | //% for (NSUInteger i = 0; i < count; ++i) { |
| 1337 | //% if (!objects[i]) { |
| 1338 | //% [NSException raise:NSInvalidArgumentException |
| 1339 | //% format:@"Attempting to add nil object to a Dictionary"]; |
| 1340 | //% } |
| 1341 | //% int idx = keys[i] ? 1 : 0; |
| 1342 | //% [_values[idx] release]; |
| 1343 | //% _values[idx] = (VALUE_TYPE)[objects[i] retain]; |
| 1344 | //% } |
| 1345 | //% } |
| 1346 | //% return self; |
| 1347 | //%} |
| 1348 | //% |
| 1349 | //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary { |
| 1350 | //% self = [self initWithObjects:NULL forKeys:NULL count:0]; |
| 1351 | //% if (self) { |
| 1352 | //% if (dictionary) { |
| 1353 | //% _values[0] = [dictionary->_values[0] retain]; |
| 1354 | //% _values[1] = [dictionary->_values[1] retain]; |
| 1355 | //% } |
| 1356 | //% } |
| 1357 | //% return self; |
| 1358 | //%} |
| 1359 | //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT() |
| 1360 | //%- (void)dealloc { |
| 1361 | //% NSAssert(!_autocreator, |
| 1362 | //% @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 1363 | //% [self class], _autocreator); |
| 1364 | //% [_values[0] release]; |
| 1365 | //% [_values[1] release]; |
| 1366 | //% [super dealloc]; |
| 1367 | //%} |
| 1368 | //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF) |
| 1369 | //%(BOOL_DICT_HASOBJECT(IDX, REF)) |
| 1370 | //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF) |
| 1371 | //%REF##_values[IDX] != nil |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1372 | //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1373 | //%- (VALUE_TYPE)objectForKey:(BOOL)key { |
| 1374 | //% return _values[key ? 1 : 0]; |
| 1375 | //%} |
| 1376 | //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP) |
| 1377 | //%- (void)setGPBGenericValue:(GPBGenericValue *)value |
| 1378 | //% forGPBGenericValueKey:(GPBGenericValue *)key { |
| 1379 | //% int idx = (key->valueBool ? 1 : 0); |
| 1380 | //% [_values[idx] release]; |
| 1381 | //% _values[idx] = [value->valueString retain]; |
| 1382 | //%} |
| 1383 | |
| 1384 | //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE) |
| 1385 | //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary { |
| 1386 | //% if (otherDictionary) { |
| 1387 | //% for (int i = 0; i < 2; ++i) { |
| 1388 | //% if (otherDictionary->_values[i] != nil) { |
| 1389 | //% [_values[i] release]; |
| 1390 | //% _values[i] = [otherDictionary->_values[i] retain]; |
| 1391 | //% } |
| 1392 | //% } |
| 1393 | //% if (_autocreator) { |
| 1394 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1395 | //% } |
| 1396 | //% } |
| 1397 | //%} |
| 1398 | //% |
| 1399 | //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key { |
| 1400 | //% if (!object) { |
| 1401 | //% [NSException raise:NSInvalidArgumentException |
| 1402 | //% format:@"Attempting to add nil object to a Dictionary"]; |
| 1403 | //% } |
| 1404 | //% int idx = (key ? 1 : 0); |
| 1405 | //% [_values[idx] release]; |
| 1406 | //% _values[idx] = [object retain]; |
| 1407 | //% if (_autocreator) { |
| 1408 | //% GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1409 | //% } |
| 1410 | //%} |
| 1411 | //% |
| 1412 | //%- (void)removeObjectForKey:(BOOL)aKey { |
| 1413 | //% int idx = (aKey ? 1 : 0); |
| 1414 | //% [_values[idx] release]; |
| 1415 | //% _values[idx] = nil; |
| 1416 | //%} |
| 1417 | //% |
| 1418 | //%- (void)removeAll { |
| 1419 | //% for (int i = 0; i < 2; ++i) { |
| 1420 | //% [_values[i] release]; |
| 1421 | //% _values[i] = nil; |
| 1422 | //% } |
| 1423 | //%} |
| 1424 | //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME) |
| 1425 | //%%@ |
| 1426 | |
| 1427 | |
| 1428 | //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t) |
| 1429 | // This block of code is generated, do not edit it directly. |
| 1430 | |
| 1431 | #pragma mark - UInt32 -> UInt32 |
| 1432 | |
| 1433 | @implementation GPBUInt32UInt32Dictionary { |
| 1434 | @package |
| 1435 | NSMutableDictionary *_dictionary; |
| 1436 | } |
| 1437 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1438 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1439 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1442 | - (instancetype)initWithUInt32s:(const uint32_t [])values |
| 1443 | forKeys:(const uint32_t [])keys |
| 1444 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1445 | self = [super init]; |
| 1446 | if (self) { |
| 1447 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 1448 | if (count && values && keys) { |
| 1449 | for (NSUInteger i = 0; i < count; ++i) { |
| 1450 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | return self; |
| 1455 | } |
| 1456 | |
| 1457 | - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1458 | self = [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1459 | if (self) { |
| 1460 | if (dictionary) { |
| 1461 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 1462 | } |
| 1463 | } |
| 1464 | return self; |
| 1465 | } |
| 1466 | |
| 1467 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 1468 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1469 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | - (void)dealloc { |
| 1473 | NSAssert(!_autocreator, |
| 1474 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 1475 | [self class], _autocreator); |
| 1476 | [_dictionary release]; |
| 1477 | [super dealloc]; |
| 1478 | } |
| 1479 | |
| 1480 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 1481 | return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 1482 | } |
| 1483 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1484 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1485 | if (self == other) { |
| 1486 | return YES; |
| 1487 | } |
| 1488 | if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) { |
| 1489 | return NO; |
| 1490 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1491 | GPBUInt32UInt32Dictionary *otherDictionary = other; |
| 1492 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | - (NSUInteger)hash { |
| 1496 | return _dictionary.count; |
| 1497 | } |
| 1498 | |
| 1499 | - (NSString *)description { |
| 1500 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 1501 | } |
| 1502 | |
| 1503 | - (NSUInteger)count { |
| 1504 | return _dictionary.count; |
| 1505 | } |
| 1506 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1507 | - (void)enumerateKeysAndUInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1508 | (void (^)(uint32_t key, uint32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1509 | BOOL stop = NO; |
| 1510 | NSDictionary *internal = _dictionary; |
| 1511 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1512 | NSNumber *aKey; |
| 1513 | while ((aKey = [keys nextObject])) { |
| 1514 | NSNumber *aValue = internal[aKey]; |
| 1515 | block([aKey unsignedIntValue], [aValue unsignedIntValue], &stop); |
| 1516 | if (stop) { |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1523 | NSDictionary *internal = _dictionary; |
| 1524 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1525 | if (count == 0) { |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1530 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1531 | size_t result = 0; |
| 1532 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1533 | NSNumber *aKey; |
| 1534 | while ((aKey = [keys nextObject])) { |
| 1535 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1536 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 1537 | msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType); |
| 1538 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1539 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1540 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 1541 | result += tagSize * count; |
| 1542 | return result; |
| 1543 | } |
| 1544 | |
| 1545 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 1546 | asField:(GPBFieldDescriptor *)field { |
| 1547 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1548 | GPBDataType keyDataType = field.mapKeyDataType; |
| 1549 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1550 | NSDictionary *internal = _dictionary; |
| 1551 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1552 | NSNumber *aKey; |
| 1553 | while ((aKey = [keys nextObject])) { |
| 1554 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1555 | [outputStream writeInt32NoTag:tag]; |
| 1556 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1557 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 1558 | uint32_t unwrappedValue = [aValue unsignedIntValue]; |
| 1559 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 1560 | msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1561 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 1562 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1563 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 1564 | WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 1565 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 1569 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 1570 | [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)]; |
| 1571 | } |
| 1572 | |
| 1573 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1574 | [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1575 | #pragma unused(stop) |
| 1576 | block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]); |
| 1577 | }]; |
| 1578 | } |
| 1579 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1580 | - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1581 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 1582 | if (wrapped && value) { |
| 1583 | *value = [wrapped unsignedIntValue]; |
| 1584 | } |
| 1585 | return (wrapped != NULL); |
| 1586 | } |
| 1587 | |
| 1588 | - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary { |
| 1589 | if (otherDictionary) { |
| 1590 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 1591 | if (_autocreator) { |
| 1592 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1597 | - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1598 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 1599 | if (_autocreator) { |
| 1600 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1601 | } |
| 1602 | } |
| 1603 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1604 | - (void)removeUInt32ForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1605 | [_dictionary removeObjectForKey:@(aKey)]; |
| 1606 | } |
| 1607 | |
| 1608 | - (void)removeAll { |
| 1609 | [_dictionary removeAllObjects]; |
| 1610 | } |
| 1611 | |
| 1612 | @end |
| 1613 | |
| 1614 | #pragma mark - UInt32 -> Int32 |
| 1615 | |
| 1616 | @implementation GPBUInt32Int32Dictionary { |
| 1617 | @package |
| 1618 | NSMutableDictionary *_dictionary; |
| 1619 | } |
| 1620 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1621 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1622 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1623 | } |
| 1624 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1625 | - (instancetype)initWithInt32s:(const int32_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1626 | forKeys:(const uint32_t [])keys |
| 1627 | count:(NSUInteger)count { |
| 1628 | self = [super init]; |
| 1629 | if (self) { |
| 1630 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 1631 | if (count && values && keys) { |
| 1632 | for (NSUInteger i = 0; i < count; ++i) { |
| 1633 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | return self; |
| 1638 | } |
| 1639 | |
| 1640 | - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1641 | self = [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1642 | if (self) { |
| 1643 | if (dictionary) { |
| 1644 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 1645 | } |
| 1646 | } |
| 1647 | return self; |
| 1648 | } |
| 1649 | |
| 1650 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 1651 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1652 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | - (void)dealloc { |
| 1656 | NSAssert(!_autocreator, |
| 1657 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 1658 | [self class], _autocreator); |
| 1659 | [_dictionary release]; |
| 1660 | [super dealloc]; |
| 1661 | } |
| 1662 | |
| 1663 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 1664 | return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 1665 | } |
| 1666 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1667 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1668 | if (self == other) { |
| 1669 | return YES; |
| 1670 | } |
| 1671 | if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) { |
| 1672 | return NO; |
| 1673 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1674 | GPBUInt32Int32Dictionary *otherDictionary = other; |
| 1675 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | - (NSUInteger)hash { |
| 1679 | return _dictionary.count; |
| 1680 | } |
| 1681 | |
| 1682 | - (NSString *)description { |
| 1683 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 1684 | } |
| 1685 | |
| 1686 | - (NSUInteger)count { |
| 1687 | return _dictionary.count; |
| 1688 | } |
| 1689 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1690 | - (void)enumerateKeysAndInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1691 | (void (^)(uint32_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1692 | BOOL stop = NO; |
| 1693 | NSDictionary *internal = _dictionary; |
| 1694 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1695 | NSNumber *aKey; |
| 1696 | while ((aKey = [keys nextObject])) { |
| 1697 | NSNumber *aValue = internal[aKey]; |
| 1698 | block([aKey unsignedIntValue], [aValue intValue], &stop); |
| 1699 | if (stop) { |
| 1700 | break; |
| 1701 | } |
| 1702 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1706 | NSDictionary *internal = _dictionary; |
| 1707 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1708 | if (count == 0) { |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1713 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1714 | size_t result = 0; |
| 1715 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1716 | NSNumber *aKey; |
| 1717 | while ((aKey = [keys nextObject])) { |
| 1718 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1719 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 1720 | msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 1721 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1722 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1723 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 1724 | result += tagSize * count; |
| 1725 | return result; |
| 1726 | } |
| 1727 | |
| 1728 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 1729 | asField:(GPBFieldDescriptor *)field { |
| 1730 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1731 | GPBDataType keyDataType = field.mapKeyDataType; |
| 1732 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1733 | NSDictionary *internal = _dictionary; |
| 1734 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1735 | NSNumber *aKey; |
| 1736 | while ((aKey = [keys nextObject])) { |
| 1737 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1738 | [outputStream writeInt32NoTag:tag]; |
| 1739 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1740 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 1741 | int32_t unwrappedValue = [aValue intValue]; |
| 1742 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 1743 | msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1744 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 1745 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1746 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 1747 | WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 1748 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 1752 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 1753 | [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)]; |
| 1754 | } |
| 1755 | |
| 1756 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1757 | [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1758 | #pragma unused(stop) |
| 1759 | block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]); |
| 1760 | }]; |
| 1761 | } |
| 1762 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1763 | - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1764 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 1765 | if (wrapped && value) { |
| 1766 | *value = [wrapped intValue]; |
| 1767 | } |
| 1768 | return (wrapped != NULL); |
| 1769 | } |
| 1770 | |
| 1771 | - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary { |
| 1772 | if (otherDictionary) { |
| 1773 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 1774 | if (_autocreator) { |
| 1775 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1780 | - (void)setInt32:(int32_t)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1781 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 1782 | if (_autocreator) { |
| 1783 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1784 | } |
| 1785 | } |
| 1786 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1787 | - (void)removeInt32ForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1788 | [_dictionary removeObjectForKey:@(aKey)]; |
| 1789 | } |
| 1790 | |
| 1791 | - (void)removeAll { |
| 1792 | [_dictionary removeAllObjects]; |
| 1793 | } |
| 1794 | |
| 1795 | @end |
| 1796 | |
| 1797 | #pragma mark - UInt32 -> UInt64 |
| 1798 | |
| 1799 | @implementation GPBUInt32UInt64Dictionary { |
| 1800 | @package |
| 1801 | NSMutableDictionary *_dictionary; |
| 1802 | } |
| 1803 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1804 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1805 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1806 | } |
| 1807 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1808 | - (instancetype)initWithUInt64s:(const uint64_t [])values |
| 1809 | forKeys:(const uint32_t [])keys |
| 1810 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1811 | self = [super init]; |
| 1812 | if (self) { |
| 1813 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 1814 | if (count && values && keys) { |
| 1815 | for (NSUInteger i = 0; i < count; ++i) { |
| 1816 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | return self; |
| 1821 | } |
| 1822 | |
| 1823 | - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1824 | self = [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1825 | if (self) { |
| 1826 | if (dictionary) { |
| 1827 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 1828 | } |
| 1829 | } |
| 1830 | return self; |
| 1831 | } |
| 1832 | |
| 1833 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 1834 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1835 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | - (void)dealloc { |
| 1839 | NSAssert(!_autocreator, |
| 1840 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 1841 | [self class], _autocreator); |
| 1842 | [_dictionary release]; |
| 1843 | [super dealloc]; |
| 1844 | } |
| 1845 | |
| 1846 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 1847 | return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 1848 | } |
| 1849 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1850 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1851 | if (self == other) { |
| 1852 | return YES; |
| 1853 | } |
| 1854 | if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) { |
| 1855 | return NO; |
| 1856 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1857 | GPBUInt32UInt64Dictionary *otherDictionary = other; |
| 1858 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | - (NSUInteger)hash { |
| 1862 | return _dictionary.count; |
| 1863 | } |
| 1864 | |
| 1865 | - (NSString *)description { |
| 1866 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 1867 | } |
| 1868 | |
| 1869 | - (NSUInteger)count { |
| 1870 | return _dictionary.count; |
| 1871 | } |
| 1872 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1873 | - (void)enumerateKeysAndUInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1874 | (void (^)(uint32_t key, uint64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1875 | BOOL stop = NO; |
| 1876 | NSDictionary *internal = _dictionary; |
| 1877 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1878 | NSNumber *aKey; |
| 1879 | while ((aKey = [keys nextObject])) { |
| 1880 | NSNumber *aValue = internal[aKey]; |
| 1881 | block([aKey unsignedIntValue], [aValue unsignedLongLongValue], &stop); |
| 1882 | if (stop) { |
| 1883 | break; |
| 1884 | } |
| 1885 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1889 | NSDictionary *internal = _dictionary; |
| 1890 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1891 | if (count == 0) { |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1896 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1897 | size_t result = 0; |
| 1898 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1899 | NSNumber *aKey; |
| 1900 | while ((aKey = [keys nextObject])) { |
| 1901 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1902 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 1903 | msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType); |
| 1904 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1905 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1906 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 1907 | result += tagSize * count; |
| 1908 | return result; |
| 1909 | } |
| 1910 | |
| 1911 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 1912 | asField:(GPBFieldDescriptor *)field { |
| 1913 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 1914 | GPBDataType keyDataType = field.mapKeyDataType; |
| 1915 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1916 | NSDictionary *internal = _dictionary; |
| 1917 | NSEnumerator *keys = [internal keyEnumerator]; |
| 1918 | NSNumber *aKey; |
| 1919 | while ((aKey = [keys nextObject])) { |
| 1920 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1921 | [outputStream writeInt32NoTag:tag]; |
| 1922 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1923 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 1924 | uint64_t unwrappedValue = [aValue unsignedLongLongValue]; |
| 1925 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 1926 | msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1927 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 1928 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1929 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 1930 | WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 1931 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 1935 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 1936 | [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)]; |
| 1937 | } |
| 1938 | |
| 1939 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1940 | [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1941 | #pragma unused(stop) |
| 1942 | block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]); |
| 1943 | }]; |
| 1944 | } |
| 1945 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1946 | - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1947 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 1948 | if (wrapped && value) { |
| 1949 | *value = [wrapped unsignedLongLongValue]; |
| 1950 | } |
| 1951 | return (wrapped != NULL); |
| 1952 | } |
| 1953 | |
| 1954 | - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary { |
| 1955 | if (otherDictionary) { |
| 1956 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 1957 | if (_autocreator) { |
| 1958 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1963 | - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1964 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 1965 | if (_autocreator) { |
| 1966 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 1967 | } |
| 1968 | } |
| 1969 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1970 | - (void)removeUInt64ForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1971 | [_dictionary removeObjectForKey:@(aKey)]; |
| 1972 | } |
| 1973 | |
| 1974 | - (void)removeAll { |
| 1975 | [_dictionary removeAllObjects]; |
| 1976 | } |
| 1977 | |
| 1978 | @end |
| 1979 | |
| 1980 | #pragma mark - UInt32 -> Int64 |
| 1981 | |
| 1982 | @implementation GPBUInt32Int64Dictionary { |
| 1983 | @package |
| 1984 | NSMutableDictionary *_dictionary; |
| 1985 | } |
| 1986 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1987 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1988 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1989 | } |
| 1990 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1991 | - (instancetype)initWithInt64s:(const int64_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1992 | forKeys:(const uint32_t [])keys |
| 1993 | count:(NSUInteger)count { |
| 1994 | self = [super init]; |
| 1995 | if (self) { |
| 1996 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 1997 | if (count && values && keys) { |
| 1998 | for (NSUInteger i = 0; i < count; ++i) { |
| 1999 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | return self; |
| 2004 | } |
| 2005 | |
| 2006 | - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2007 | self = [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2008 | if (self) { |
| 2009 | if (dictionary) { |
| 2010 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 2011 | } |
| 2012 | } |
| 2013 | return self; |
| 2014 | } |
| 2015 | |
| 2016 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 2017 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2018 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | - (void)dealloc { |
| 2022 | NSAssert(!_autocreator, |
| 2023 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 2024 | [self class], _autocreator); |
| 2025 | [_dictionary release]; |
| 2026 | [super dealloc]; |
| 2027 | } |
| 2028 | |
| 2029 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 2030 | return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 2031 | } |
| 2032 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2033 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2034 | if (self == other) { |
| 2035 | return YES; |
| 2036 | } |
| 2037 | if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) { |
| 2038 | return NO; |
| 2039 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2040 | GPBUInt32Int64Dictionary *otherDictionary = other; |
| 2041 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | - (NSUInteger)hash { |
| 2045 | return _dictionary.count; |
| 2046 | } |
| 2047 | |
| 2048 | - (NSString *)description { |
| 2049 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 2050 | } |
| 2051 | |
| 2052 | - (NSUInteger)count { |
| 2053 | return _dictionary.count; |
| 2054 | } |
| 2055 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2056 | - (void)enumerateKeysAndInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2057 | (void (^)(uint32_t key, int64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2058 | BOOL stop = NO; |
| 2059 | NSDictionary *internal = _dictionary; |
| 2060 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2061 | NSNumber *aKey; |
| 2062 | while ((aKey = [keys nextObject])) { |
| 2063 | NSNumber *aValue = internal[aKey]; |
| 2064 | block([aKey unsignedIntValue], [aValue longLongValue], &stop); |
| 2065 | if (stop) { |
| 2066 | break; |
| 2067 | } |
| 2068 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2072 | NSDictionary *internal = _dictionary; |
| 2073 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2074 | if (count == 0) { |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2079 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2080 | size_t result = 0; |
| 2081 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2082 | NSNumber *aKey; |
| 2083 | while ((aKey = [keys nextObject])) { |
| 2084 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2085 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 2086 | msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType); |
| 2087 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2088 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2089 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 2090 | result += tagSize * count; |
| 2091 | return result; |
| 2092 | } |
| 2093 | |
| 2094 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 2095 | asField:(GPBFieldDescriptor *)field { |
| 2096 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2097 | GPBDataType keyDataType = field.mapKeyDataType; |
| 2098 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2099 | NSDictionary *internal = _dictionary; |
| 2100 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2101 | NSNumber *aKey; |
| 2102 | while ((aKey = [keys nextObject])) { |
| 2103 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2104 | [outputStream writeInt32NoTag:tag]; |
| 2105 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2106 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 2107 | int64_t unwrappedValue = [aValue longLongValue]; |
| 2108 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2109 | msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2110 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 2111 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2112 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2113 | WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 2114 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 2118 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 2119 | [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)]; |
| 2120 | } |
| 2121 | |
| 2122 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2123 | [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2124 | #pragma unused(stop) |
| 2125 | block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]); |
| 2126 | }]; |
| 2127 | } |
| 2128 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2129 | - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2130 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 2131 | if (wrapped && value) { |
| 2132 | *value = [wrapped longLongValue]; |
| 2133 | } |
| 2134 | return (wrapped != NULL); |
| 2135 | } |
| 2136 | |
| 2137 | - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary { |
| 2138 | if (otherDictionary) { |
| 2139 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 2140 | if (_autocreator) { |
| 2141 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2146 | - (void)setInt64:(int64_t)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2147 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 2148 | if (_autocreator) { |
| 2149 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2150 | } |
| 2151 | } |
| 2152 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2153 | - (void)removeInt64ForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2154 | [_dictionary removeObjectForKey:@(aKey)]; |
| 2155 | } |
| 2156 | |
| 2157 | - (void)removeAll { |
| 2158 | [_dictionary removeAllObjects]; |
| 2159 | } |
| 2160 | |
| 2161 | @end |
| 2162 | |
| 2163 | #pragma mark - UInt32 -> Bool |
| 2164 | |
| 2165 | @implementation GPBUInt32BoolDictionary { |
| 2166 | @package |
| 2167 | NSMutableDictionary *_dictionary; |
| 2168 | } |
| 2169 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2170 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2171 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2172 | } |
| 2173 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2174 | - (instancetype)initWithBools:(const BOOL [])values |
| 2175 | forKeys:(const uint32_t [])keys |
| 2176 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2177 | self = [super init]; |
| 2178 | if (self) { |
| 2179 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 2180 | if (count && values && keys) { |
| 2181 | for (NSUInteger i = 0; i < count; ++i) { |
| 2182 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | return self; |
| 2187 | } |
| 2188 | |
| 2189 | - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2190 | self = [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2191 | if (self) { |
| 2192 | if (dictionary) { |
| 2193 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 2194 | } |
| 2195 | } |
| 2196 | return self; |
| 2197 | } |
| 2198 | |
| 2199 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 2200 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2201 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | - (void)dealloc { |
| 2205 | NSAssert(!_autocreator, |
| 2206 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 2207 | [self class], _autocreator); |
| 2208 | [_dictionary release]; |
| 2209 | [super dealloc]; |
| 2210 | } |
| 2211 | |
| 2212 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 2213 | return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self]; |
| 2214 | } |
| 2215 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2216 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2217 | if (self == other) { |
| 2218 | return YES; |
| 2219 | } |
| 2220 | if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) { |
| 2221 | return NO; |
| 2222 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2223 | GPBUInt32BoolDictionary *otherDictionary = other; |
| 2224 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | - (NSUInteger)hash { |
| 2228 | return _dictionary.count; |
| 2229 | } |
| 2230 | |
| 2231 | - (NSString *)description { |
| 2232 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 2233 | } |
| 2234 | |
| 2235 | - (NSUInteger)count { |
| 2236 | return _dictionary.count; |
| 2237 | } |
| 2238 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2239 | - (void)enumerateKeysAndBoolsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2240 | (void (^)(uint32_t key, BOOL value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2241 | BOOL stop = NO; |
| 2242 | NSDictionary *internal = _dictionary; |
| 2243 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2244 | NSNumber *aKey; |
| 2245 | while ((aKey = [keys nextObject])) { |
| 2246 | NSNumber *aValue = internal[aKey]; |
| 2247 | block([aKey unsignedIntValue], [aValue boolValue], &stop); |
| 2248 | if (stop) { |
| 2249 | break; |
| 2250 | } |
| 2251 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2255 | NSDictionary *internal = _dictionary; |
| 2256 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2257 | if (count == 0) { |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
| 2261 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2262 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2263 | size_t result = 0; |
| 2264 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2265 | NSNumber *aKey; |
| 2266 | while ((aKey = [keys nextObject])) { |
| 2267 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2268 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 2269 | msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType); |
| 2270 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2271 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2272 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 2273 | result += tagSize * count; |
| 2274 | return result; |
| 2275 | } |
| 2276 | |
| 2277 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 2278 | asField:(GPBFieldDescriptor *)field { |
| 2279 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2280 | GPBDataType keyDataType = field.mapKeyDataType; |
| 2281 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2282 | NSDictionary *internal = _dictionary; |
| 2283 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2284 | NSNumber *aKey; |
| 2285 | while ((aKey = [keys nextObject])) { |
| 2286 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2287 | [outputStream writeInt32NoTag:tag]; |
| 2288 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2289 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 2290 | BOOL unwrappedValue = [aValue boolValue]; |
| 2291 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2292 | msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2293 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 2294 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2295 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2296 | WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 2297 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 2301 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 2302 | [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)]; |
| 2303 | } |
| 2304 | |
| 2305 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2306 | [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2307 | #pragma unused(stop) |
| 2308 | block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false")); |
| 2309 | }]; |
| 2310 | } |
| 2311 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2312 | - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2313 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 2314 | if (wrapped && value) { |
| 2315 | *value = [wrapped boolValue]; |
| 2316 | } |
| 2317 | return (wrapped != NULL); |
| 2318 | } |
| 2319 | |
| 2320 | - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary { |
| 2321 | if (otherDictionary) { |
| 2322 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 2323 | if (_autocreator) { |
| 2324 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2329 | - (void)setBool:(BOOL)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2330 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 2331 | if (_autocreator) { |
| 2332 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2333 | } |
| 2334 | } |
| 2335 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2336 | - (void)removeBoolForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2337 | [_dictionary removeObjectForKey:@(aKey)]; |
| 2338 | } |
| 2339 | |
| 2340 | - (void)removeAll { |
| 2341 | [_dictionary removeAllObjects]; |
| 2342 | } |
| 2343 | |
| 2344 | @end |
| 2345 | |
| 2346 | #pragma mark - UInt32 -> Float |
| 2347 | |
| 2348 | @implementation GPBUInt32FloatDictionary { |
| 2349 | @package |
| 2350 | NSMutableDictionary *_dictionary; |
| 2351 | } |
| 2352 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2353 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2354 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2355 | } |
| 2356 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2357 | - (instancetype)initWithFloats:(const float [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2358 | forKeys:(const uint32_t [])keys |
| 2359 | count:(NSUInteger)count { |
| 2360 | self = [super init]; |
| 2361 | if (self) { |
| 2362 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 2363 | if (count && values && keys) { |
| 2364 | for (NSUInteger i = 0; i < count; ++i) { |
| 2365 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | return self; |
| 2370 | } |
| 2371 | |
| 2372 | - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2373 | self = [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2374 | if (self) { |
| 2375 | if (dictionary) { |
| 2376 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 2377 | } |
| 2378 | } |
| 2379 | return self; |
| 2380 | } |
| 2381 | |
| 2382 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 2383 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2384 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | - (void)dealloc { |
| 2388 | NSAssert(!_autocreator, |
| 2389 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 2390 | [self class], _autocreator); |
| 2391 | [_dictionary release]; |
| 2392 | [super dealloc]; |
| 2393 | } |
| 2394 | |
| 2395 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 2396 | return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self]; |
| 2397 | } |
| 2398 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2399 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2400 | if (self == other) { |
| 2401 | return YES; |
| 2402 | } |
| 2403 | if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) { |
| 2404 | return NO; |
| 2405 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2406 | GPBUInt32FloatDictionary *otherDictionary = other; |
| 2407 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | - (NSUInteger)hash { |
| 2411 | return _dictionary.count; |
| 2412 | } |
| 2413 | |
| 2414 | - (NSString *)description { |
| 2415 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 2416 | } |
| 2417 | |
| 2418 | - (NSUInteger)count { |
| 2419 | return _dictionary.count; |
| 2420 | } |
| 2421 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2422 | - (void)enumerateKeysAndFloatsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2423 | (void (^)(uint32_t key, float value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2424 | BOOL stop = NO; |
| 2425 | NSDictionary *internal = _dictionary; |
| 2426 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2427 | NSNumber *aKey; |
| 2428 | while ((aKey = [keys nextObject])) { |
| 2429 | NSNumber *aValue = internal[aKey]; |
| 2430 | block([aKey unsignedIntValue], [aValue floatValue], &stop); |
| 2431 | if (stop) { |
| 2432 | break; |
| 2433 | } |
| 2434 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2438 | NSDictionary *internal = _dictionary; |
| 2439 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2440 | if (count == 0) { |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2445 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2446 | size_t result = 0; |
| 2447 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2448 | NSNumber *aKey; |
| 2449 | while ((aKey = [keys nextObject])) { |
| 2450 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2451 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 2452 | msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType); |
| 2453 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2454 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2455 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 2456 | result += tagSize * count; |
| 2457 | return result; |
| 2458 | } |
| 2459 | |
| 2460 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 2461 | asField:(GPBFieldDescriptor *)field { |
| 2462 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2463 | GPBDataType keyDataType = field.mapKeyDataType; |
| 2464 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2465 | NSDictionary *internal = _dictionary; |
| 2466 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2467 | NSNumber *aKey; |
| 2468 | while ((aKey = [keys nextObject])) { |
| 2469 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2470 | [outputStream writeInt32NoTag:tag]; |
| 2471 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2472 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 2473 | float unwrappedValue = [aValue floatValue]; |
| 2474 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2475 | msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2476 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 2477 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2478 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2479 | WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 2480 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 2484 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 2485 | [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)]; |
| 2486 | } |
| 2487 | |
| 2488 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2489 | [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2490 | #pragma unused(stop) |
| 2491 | block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); |
| 2492 | }]; |
| 2493 | } |
| 2494 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2495 | - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2496 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 2497 | if (wrapped && value) { |
| 2498 | *value = [wrapped floatValue]; |
| 2499 | } |
| 2500 | return (wrapped != NULL); |
| 2501 | } |
| 2502 | |
| 2503 | - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary { |
| 2504 | if (otherDictionary) { |
| 2505 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 2506 | if (_autocreator) { |
| 2507 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2512 | - (void)setFloat:(float)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2513 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 2514 | if (_autocreator) { |
| 2515 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2516 | } |
| 2517 | } |
| 2518 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2519 | - (void)removeFloatForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2520 | [_dictionary removeObjectForKey:@(aKey)]; |
| 2521 | } |
| 2522 | |
| 2523 | - (void)removeAll { |
| 2524 | [_dictionary removeAllObjects]; |
| 2525 | } |
| 2526 | |
| 2527 | @end |
| 2528 | |
| 2529 | #pragma mark - UInt32 -> Double |
| 2530 | |
| 2531 | @implementation GPBUInt32DoubleDictionary { |
| 2532 | @package |
| 2533 | NSMutableDictionary *_dictionary; |
| 2534 | } |
| 2535 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2536 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2537 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2538 | } |
| 2539 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2540 | - (instancetype)initWithDoubles:(const double [])values |
| 2541 | forKeys:(const uint32_t [])keys |
| 2542 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2543 | self = [super init]; |
| 2544 | if (self) { |
| 2545 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 2546 | if (count && values && keys) { |
| 2547 | for (NSUInteger i = 0; i < count; ++i) { |
| 2548 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 2549 | } |
| 2550 | } |
| 2551 | } |
| 2552 | return self; |
| 2553 | } |
| 2554 | |
| 2555 | - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2556 | self = [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2557 | if (self) { |
| 2558 | if (dictionary) { |
| 2559 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 2560 | } |
| 2561 | } |
| 2562 | return self; |
| 2563 | } |
| 2564 | |
| 2565 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 2566 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2567 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | - (void)dealloc { |
| 2571 | NSAssert(!_autocreator, |
| 2572 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 2573 | [self class], _autocreator); |
| 2574 | [_dictionary release]; |
| 2575 | [super dealloc]; |
| 2576 | } |
| 2577 | |
| 2578 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 2579 | return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self]; |
| 2580 | } |
| 2581 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2582 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2583 | if (self == other) { |
| 2584 | return YES; |
| 2585 | } |
| 2586 | if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) { |
| 2587 | return NO; |
| 2588 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2589 | GPBUInt32DoubleDictionary *otherDictionary = other; |
| 2590 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | - (NSUInteger)hash { |
| 2594 | return _dictionary.count; |
| 2595 | } |
| 2596 | |
| 2597 | - (NSString *)description { |
| 2598 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 2599 | } |
| 2600 | |
| 2601 | - (NSUInteger)count { |
| 2602 | return _dictionary.count; |
| 2603 | } |
| 2604 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2605 | - (void)enumerateKeysAndDoublesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2606 | (void (^)(uint32_t key, double value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2607 | BOOL stop = NO; |
| 2608 | NSDictionary *internal = _dictionary; |
| 2609 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2610 | NSNumber *aKey; |
| 2611 | while ((aKey = [keys nextObject])) { |
| 2612 | NSNumber *aValue = internal[aKey]; |
| 2613 | block([aKey unsignedIntValue], [aValue doubleValue], &stop); |
| 2614 | if (stop) { |
| 2615 | break; |
| 2616 | } |
| 2617 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2621 | NSDictionary *internal = _dictionary; |
| 2622 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2623 | if (count == 0) { |
| 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2628 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2629 | size_t result = 0; |
| 2630 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2631 | NSNumber *aKey; |
| 2632 | while ((aKey = [keys nextObject])) { |
| 2633 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2634 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 2635 | msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType); |
| 2636 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2637 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2638 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 2639 | result += tagSize * count; |
| 2640 | return result; |
| 2641 | } |
| 2642 | |
| 2643 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 2644 | asField:(GPBFieldDescriptor *)field { |
| 2645 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2646 | GPBDataType keyDataType = field.mapKeyDataType; |
| 2647 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2648 | NSDictionary *internal = _dictionary; |
| 2649 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2650 | NSNumber *aKey; |
| 2651 | while ((aKey = [keys nextObject])) { |
| 2652 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2653 | [outputStream writeInt32NoTag:tag]; |
| 2654 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2655 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 2656 | double unwrappedValue = [aValue doubleValue]; |
| 2657 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2658 | msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2659 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 2660 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2661 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2662 | WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 2663 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 2667 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 2668 | [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)]; |
| 2669 | } |
| 2670 | |
| 2671 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2672 | [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2673 | #pragma unused(stop) |
| 2674 | block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); |
| 2675 | }]; |
| 2676 | } |
| 2677 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2678 | - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2679 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 2680 | if (wrapped && value) { |
| 2681 | *value = [wrapped doubleValue]; |
| 2682 | } |
| 2683 | return (wrapped != NULL); |
| 2684 | } |
| 2685 | |
| 2686 | - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary { |
| 2687 | if (otherDictionary) { |
| 2688 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 2689 | if (_autocreator) { |
| 2690 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2695 | - (void)setDouble:(double)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2696 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 2697 | if (_autocreator) { |
| 2698 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2699 | } |
| 2700 | } |
| 2701 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2702 | - (void)removeDoubleForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2703 | [_dictionary removeObjectForKey:@(aKey)]; |
| 2704 | } |
| 2705 | |
| 2706 | - (void)removeAll { |
| 2707 | [_dictionary removeAllObjects]; |
| 2708 | } |
| 2709 | |
| 2710 | @end |
| 2711 | |
| 2712 | #pragma mark - UInt32 -> Enum |
| 2713 | |
| 2714 | @implementation GPBUInt32EnumDictionary { |
| 2715 | @package |
| 2716 | NSMutableDictionary *_dictionary; |
| 2717 | GPBEnumValidationFunc _validationFunc; |
| 2718 | } |
| 2719 | |
| 2720 | @synthesize validationFunc = _validationFunc; |
| 2721 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2722 | - (instancetype)init { |
| 2723 | return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 2724 | } |
| 2725 | |
| 2726 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 2727 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 2728 | } |
| 2729 | |
| 2730 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 2731 | rawValues:(const int32_t [])rawValues |
| 2732 | forKeys:(const uint32_t [])keys |
| 2733 | count:(NSUInteger)count { |
| 2734 | self = [super init]; |
| 2735 | if (self) { |
| 2736 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 2737 | _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 2738 | if (count && rawValues && keys) { |
| 2739 | for (NSUInteger i = 0; i < count; ++i) { |
| 2740 | [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])]; |
| 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | return self; |
| 2745 | } |
| 2746 | |
| 2747 | - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary { |
| 2748 | self = [self initWithValidationFunction:dictionary.validationFunc |
| 2749 | rawValues:NULL |
| 2750 | forKeys:NULL |
| 2751 | count:0]; |
| 2752 | if (self) { |
| 2753 | if (dictionary) { |
| 2754 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 2755 | } |
| 2756 | } |
| 2757 | return self; |
| 2758 | } |
| 2759 | |
| 2760 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 2761 | capacity:(NSUInteger)numItems { |
| 2762 | #pragma unused(numItems) |
| 2763 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 2764 | } |
| 2765 | |
| 2766 | - (void)dealloc { |
| 2767 | NSAssert(!_autocreator, |
| 2768 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 2769 | [self class], _autocreator); |
| 2770 | [_dictionary release]; |
| 2771 | [super dealloc]; |
| 2772 | } |
| 2773 | |
| 2774 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 2775 | return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self]; |
| 2776 | } |
| 2777 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2778 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2779 | if (self == other) { |
| 2780 | return YES; |
| 2781 | } |
| 2782 | if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) { |
| 2783 | return NO; |
| 2784 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2785 | GPBUInt32EnumDictionary *otherDictionary = other; |
| 2786 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | - (NSUInteger)hash { |
| 2790 | return _dictionary.count; |
| 2791 | } |
| 2792 | |
| 2793 | - (NSString *)description { |
| 2794 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 2795 | } |
| 2796 | |
| 2797 | - (NSUInteger)count { |
| 2798 | return _dictionary.count; |
| 2799 | } |
| 2800 | |
| 2801 | - (void)enumerateKeysAndRawValuesUsingBlock: |
| 2802 | (void (^)(uint32_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2803 | BOOL stop = NO; |
| 2804 | NSDictionary *internal = _dictionary; |
| 2805 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2806 | NSNumber *aKey; |
| 2807 | while ((aKey = [keys nextObject])) { |
| 2808 | NSNumber *aValue = internal[aKey]; |
| 2809 | block([aKey unsignedIntValue], [aValue intValue], &stop); |
| 2810 | if (stop) { |
| 2811 | break; |
| 2812 | } |
| 2813 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2817 | NSDictionary *internal = _dictionary; |
| 2818 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2819 | if (count == 0) { |
| 2820 | return 0; |
| 2821 | } |
| 2822 | |
| 2823 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2824 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2825 | size_t result = 0; |
| 2826 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2827 | NSNumber *aKey; |
| 2828 | while ((aKey = [keys nextObject])) { |
| 2829 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2830 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 2831 | msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 2832 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2833 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2834 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 2835 | result += tagSize * count; |
| 2836 | return result; |
| 2837 | } |
| 2838 | |
| 2839 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 2840 | asField:(GPBFieldDescriptor *)field { |
| 2841 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 2842 | GPBDataType keyDataType = field.mapKeyDataType; |
| 2843 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2844 | NSDictionary *internal = _dictionary; |
| 2845 | NSEnumerator *keys = [internal keyEnumerator]; |
| 2846 | NSNumber *aKey; |
| 2847 | while ((aKey = [keys nextObject])) { |
| 2848 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2849 | [outputStream writeInt32NoTag:tag]; |
| 2850 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2851 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 2852 | int32_t unwrappedValue = [aValue intValue]; |
| 2853 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2854 | msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2855 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 2856 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2857 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 2858 | WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 2859 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | - (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 2863 | forKey:(GPBGenericValue *)key |
| 2864 | keyDataType:(GPBDataType)keyDataType { |
| 2865 | size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType); |
| 2866 | msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 2867 | NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 2868 | GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 2869 | WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType); |
| 2870 | WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 2871 | [outputStream release]; |
| 2872 | return data; |
| 2873 | } |
| 2874 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 2875 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 2876 | [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)]; |
| 2877 | } |
| 2878 | |
| 2879 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 2880 | [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) { |
| 2881 | #pragma unused(stop) |
| 2882 | block([NSString stringWithFormat:@"%u", key], @(value)); |
| 2883 | }]; |
| 2884 | } |
| 2885 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2886 | - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2887 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 2888 | if (wrapped && value) { |
| 2889 | int32_t result = [wrapped intValue]; |
| 2890 | if (!_validationFunc(result)) { |
| 2891 | result = kGPBUnrecognizedEnumeratorValue; |
| 2892 | } |
| 2893 | *value = result; |
| 2894 | } |
| 2895 | return (wrapped != NULL); |
| 2896 | } |
| 2897 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2898 | - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2899 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 2900 | if (wrapped && rawValue) { |
| 2901 | *rawValue = [wrapped intValue]; |
| 2902 | } |
| 2903 | return (wrapped != NULL); |
| 2904 | } |
| 2905 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2906 | - (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2907 | (void (^)(uint32_t key, int32_t value, BOOL *stop))block { |
| 2908 | GPBEnumValidationFunc func = _validationFunc; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2909 | BOOL stop = NO; |
| 2910 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 2911 | NSNumber *aKey; |
| 2912 | while ((aKey = [keys nextObject])) { |
| 2913 | NSNumber *aValue = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2914 | int32_t unwrapped = [aValue intValue]; |
| 2915 | if (!func(unwrapped)) { |
| 2916 | unwrapped = kGPBUnrecognizedEnumeratorValue; |
| 2917 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2918 | block([aKey unsignedIntValue], unwrapped, &stop); |
| 2919 | if (stop) { |
| 2920 | break; |
| 2921 | } |
| 2922 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary { |
| 2926 | if (otherDictionary) { |
| 2927 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 2928 | if (_autocreator) { |
| 2929 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2930 | } |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | - (void)setRawValue:(int32_t)value forKey:(uint32_t)key { |
| 2935 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 2936 | if (_autocreator) { |
| 2937 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2938 | } |
| 2939 | } |
| 2940 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2941 | - (void)removeEnumForKey:(uint32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2942 | [_dictionary removeObjectForKey:@(aKey)]; |
| 2943 | } |
| 2944 | |
| 2945 | - (void)removeAll { |
| 2946 | [_dictionary removeAllObjects]; |
| 2947 | } |
| 2948 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 2949 | - (void)setEnum:(int32_t)value forKey:(uint32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2950 | if (!_validationFunc(value)) { |
| 2951 | [NSException raise:NSInvalidArgumentException |
| 2952 | format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)", |
| 2953 | value]; |
| 2954 | } |
| 2955 | |
| 2956 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 2957 | if (_autocreator) { |
| 2958 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | @end |
| 2963 | |
| 2964 | #pragma mark - UInt32 -> Object |
| 2965 | |
| 2966 | @implementation GPBUInt32ObjectDictionary { |
| 2967 | @package |
| 2968 | NSMutableDictionary *_dictionary; |
| 2969 | } |
| 2970 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 2971 | - (instancetype)init { |
| 2972 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 2973 | } |
| 2974 | |
| 2975 | - (instancetype)initWithObjects:(const id [])objects |
| 2976 | forKeys:(const uint32_t [])keys |
| 2977 | count:(NSUInteger)count { |
| 2978 | self = [super init]; |
| 2979 | if (self) { |
| 2980 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 2981 | if (count && objects && keys) { |
| 2982 | for (NSUInteger i = 0; i < count; ++i) { |
| 2983 | if (!objects[i]) { |
| 2984 | [NSException raise:NSInvalidArgumentException |
| 2985 | format:@"Attempting to add nil object to a Dictionary"]; |
| 2986 | } |
| 2987 | [_dictionary setObject:objects[i] forKey:@(keys[i])]; |
| 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | return self; |
| 2992 | } |
| 2993 | |
| 2994 | - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary { |
| 2995 | self = [self initWithObjects:NULL forKeys:NULL count:0]; |
| 2996 | if (self) { |
| 2997 | if (dictionary) { |
| 2998 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 2999 | } |
| 3000 | } |
| 3001 | return self; |
| 3002 | } |
| 3003 | |
| 3004 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 3005 | #pragma unused(numItems) |
| 3006 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 3007 | } |
| 3008 | |
| 3009 | - (void)dealloc { |
| 3010 | NSAssert(!_autocreator, |
| 3011 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 3012 | [self class], _autocreator); |
| 3013 | [_dictionary release]; |
| 3014 | [super dealloc]; |
| 3015 | } |
| 3016 | |
| 3017 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 3018 | return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self]; |
| 3019 | } |
| 3020 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3021 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3022 | if (self == other) { |
| 3023 | return YES; |
| 3024 | } |
| 3025 | if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) { |
| 3026 | return NO; |
| 3027 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3028 | GPBUInt32ObjectDictionary *otherDictionary = other; |
| 3029 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | - (NSUInteger)hash { |
| 3033 | return _dictionary.count; |
| 3034 | } |
| 3035 | |
| 3036 | - (NSString *)description { |
| 3037 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 3038 | } |
| 3039 | |
| 3040 | - (NSUInteger)count { |
| 3041 | return _dictionary.count; |
| 3042 | } |
| 3043 | |
| 3044 | - (void)enumerateKeysAndObjectsUsingBlock: |
| 3045 | (void (^)(uint32_t key, id object, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3046 | BOOL stop = NO; |
| 3047 | NSDictionary *internal = _dictionary; |
| 3048 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3049 | NSNumber *aKey; |
| 3050 | while ((aKey = [keys nextObject])) { |
| 3051 | id aObject = internal[aKey]; |
| 3052 | block([aKey unsignedIntValue], aObject, &stop); |
| 3053 | if (stop) { |
| 3054 | break; |
| 3055 | } |
| 3056 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | - (BOOL)isInitialized { |
| 3060 | for (GPBMessage *msg in [_dictionary objectEnumerator]) { |
| 3061 | if (!msg.initialized) { |
| 3062 | return NO; |
| 3063 | } |
| 3064 | } |
| 3065 | return YES; |
| 3066 | } |
| 3067 | |
| 3068 | - (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 3069 | GPBUInt32ObjectDictionary *newDict = |
| 3070 | [[GPBUInt32ObjectDictionary alloc] init]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3071 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 3072 | id aKey; |
| 3073 | NSMutableDictionary *internalDict = newDict->_dictionary; |
| 3074 | while ((aKey = [keys nextObject])) { |
| 3075 | GPBMessage *msg = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3076 | GPBMessage *copiedMsg = [msg copyWithZone:zone]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3077 | [internalDict setObject:copiedMsg forKey:aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3078 | [copiedMsg release]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3079 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3080 | return newDict; |
| 3081 | } |
| 3082 | |
| 3083 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3084 | NSDictionary *internal = _dictionary; |
| 3085 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3086 | if (count == 0) { |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
| 3090 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3091 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3092 | size_t result = 0; |
| 3093 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3094 | NSNumber *aKey; |
| 3095 | while ((aKey = [keys nextObject])) { |
| 3096 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3097 | size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType); |
| 3098 | msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType); |
| 3099 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3100 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3101 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 3102 | result += tagSize * count; |
| 3103 | return result; |
| 3104 | } |
| 3105 | |
| 3106 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 3107 | asField:(GPBFieldDescriptor *)field { |
| 3108 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3109 | GPBDataType keyDataType = field.mapKeyDataType; |
| 3110 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3111 | NSDictionary *internal = _dictionary; |
| 3112 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3113 | NSNumber *aKey; |
| 3114 | while ((aKey = [keys nextObject])) { |
| 3115 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3116 | [outputStream writeInt32NoTag:tag]; |
| 3117 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3118 | uint32_t unwrappedKey = [aKey unsignedIntValue]; |
| 3119 | id unwrappedValue = aObject; |
| 3120 | size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3121 | msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3122 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 3123 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3124 | WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3125 | WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 3126 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 3130 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 3131 | [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)]; |
| 3132 | } |
| 3133 | |
| 3134 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 3135 | [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) { |
| 3136 | #pragma unused(stop) |
| 3137 | block([NSString stringWithFormat:@"%u", key], object); |
| 3138 | }]; |
| 3139 | } |
| 3140 | |
| 3141 | - (id)objectForKey:(uint32_t)key { |
| 3142 | id result = [_dictionary objectForKey:@(key)]; |
| 3143 | return result; |
| 3144 | } |
| 3145 | |
| 3146 | - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary { |
| 3147 | if (otherDictionary) { |
| 3148 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 3149 | if (_autocreator) { |
| 3150 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | - (void)setObject:(id)object forKey:(uint32_t)key { |
| 3156 | if (!object) { |
| 3157 | [NSException raise:NSInvalidArgumentException |
| 3158 | format:@"Attempting to add nil object to a Dictionary"]; |
| 3159 | } |
| 3160 | [_dictionary setObject:object forKey:@(key)]; |
| 3161 | if (_autocreator) { |
| 3162 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | - (void)removeObjectForKey:(uint32_t)aKey { |
| 3167 | [_dictionary removeObjectForKey:@(aKey)]; |
| 3168 | } |
| 3169 | |
| 3170 | - (void)removeAll { |
| 3171 | [_dictionary removeAllObjects]; |
| 3172 | } |
| 3173 | |
| 3174 | @end |
| 3175 | |
| 3176 | //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t) |
| 3177 | // This block of code is generated, do not edit it directly. |
| 3178 | |
| 3179 | #pragma mark - Int32 -> UInt32 |
| 3180 | |
| 3181 | @implementation GPBInt32UInt32Dictionary { |
| 3182 | @package |
| 3183 | NSMutableDictionary *_dictionary; |
| 3184 | } |
| 3185 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3186 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3187 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3188 | } |
| 3189 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3190 | - (instancetype)initWithUInt32s:(const uint32_t [])values |
| 3191 | forKeys:(const int32_t [])keys |
| 3192 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3193 | self = [super init]; |
| 3194 | if (self) { |
| 3195 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 3196 | if (count && values && keys) { |
| 3197 | for (NSUInteger i = 0; i < count; ++i) { |
| 3198 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 3199 | } |
| 3200 | } |
| 3201 | } |
| 3202 | return self; |
| 3203 | } |
| 3204 | |
| 3205 | - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3206 | self = [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3207 | if (self) { |
| 3208 | if (dictionary) { |
| 3209 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 3210 | } |
| 3211 | } |
| 3212 | return self; |
| 3213 | } |
| 3214 | |
| 3215 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 3216 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3217 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | - (void)dealloc { |
| 3221 | NSAssert(!_autocreator, |
| 3222 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 3223 | [self class], _autocreator); |
| 3224 | [_dictionary release]; |
| 3225 | [super dealloc]; |
| 3226 | } |
| 3227 | |
| 3228 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 3229 | return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 3230 | } |
| 3231 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3232 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3233 | if (self == other) { |
| 3234 | return YES; |
| 3235 | } |
| 3236 | if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) { |
| 3237 | return NO; |
| 3238 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3239 | GPBInt32UInt32Dictionary *otherDictionary = other; |
| 3240 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | - (NSUInteger)hash { |
| 3244 | return _dictionary.count; |
| 3245 | } |
| 3246 | |
| 3247 | - (NSString *)description { |
| 3248 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 3249 | } |
| 3250 | |
| 3251 | - (NSUInteger)count { |
| 3252 | return _dictionary.count; |
| 3253 | } |
| 3254 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3255 | - (void)enumerateKeysAndUInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3256 | (void (^)(int32_t key, uint32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3257 | BOOL stop = NO; |
| 3258 | NSDictionary *internal = _dictionary; |
| 3259 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3260 | NSNumber *aKey; |
| 3261 | while ((aKey = [keys nextObject])) { |
| 3262 | NSNumber *aValue = internal[aKey]; |
| 3263 | block([aKey intValue], [aValue unsignedIntValue], &stop); |
| 3264 | if (stop) { |
| 3265 | break; |
| 3266 | } |
| 3267 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3271 | NSDictionary *internal = _dictionary; |
| 3272 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3273 | if (count == 0) { |
| 3274 | return 0; |
| 3275 | } |
| 3276 | |
| 3277 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3278 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3279 | size_t result = 0; |
| 3280 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3281 | NSNumber *aKey; |
| 3282 | while ((aKey = [keys nextObject])) { |
| 3283 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3284 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 3285 | msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType); |
| 3286 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3287 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3288 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 3289 | result += tagSize * count; |
| 3290 | return result; |
| 3291 | } |
| 3292 | |
| 3293 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 3294 | asField:(GPBFieldDescriptor *)field { |
| 3295 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3296 | GPBDataType keyDataType = field.mapKeyDataType; |
| 3297 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3298 | NSDictionary *internal = _dictionary; |
| 3299 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3300 | NSNumber *aKey; |
| 3301 | while ((aKey = [keys nextObject])) { |
| 3302 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3303 | [outputStream writeInt32NoTag:tag]; |
| 3304 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3305 | int32_t unwrappedKey = [aKey intValue]; |
| 3306 | uint32_t unwrappedValue = [aValue unsignedIntValue]; |
| 3307 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3308 | msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3309 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 3310 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3311 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3312 | WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 3313 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 3317 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 3318 | [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)]; |
| 3319 | } |
| 3320 | |
| 3321 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3322 | [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3323 | #pragma unused(stop) |
| 3324 | block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]); |
| 3325 | }]; |
| 3326 | } |
| 3327 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3328 | - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3329 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 3330 | if (wrapped && value) { |
| 3331 | *value = [wrapped unsignedIntValue]; |
| 3332 | } |
| 3333 | return (wrapped != NULL); |
| 3334 | } |
| 3335 | |
| 3336 | - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary { |
| 3337 | if (otherDictionary) { |
| 3338 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 3339 | if (_autocreator) { |
| 3340 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3341 | } |
| 3342 | } |
| 3343 | } |
| 3344 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3345 | - (void)setUInt32:(uint32_t)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3346 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 3347 | if (_autocreator) { |
| 3348 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3349 | } |
| 3350 | } |
| 3351 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3352 | - (void)removeUInt32ForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3353 | [_dictionary removeObjectForKey:@(aKey)]; |
| 3354 | } |
| 3355 | |
| 3356 | - (void)removeAll { |
| 3357 | [_dictionary removeAllObjects]; |
| 3358 | } |
| 3359 | |
| 3360 | @end |
| 3361 | |
| 3362 | #pragma mark - Int32 -> Int32 |
| 3363 | |
| 3364 | @implementation GPBInt32Int32Dictionary { |
| 3365 | @package |
| 3366 | NSMutableDictionary *_dictionary; |
| 3367 | } |
| 3368 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3369 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3370 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3371 | } |
| 3372 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3373 | - (instancetype)initWithInt32s:(const int32_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3374 | forKeys:(const int32_t [])keys |
| 3375 | count:(NSUInteger)count { |
| 3376 | self = [super init]; |
| 3377 | if (self) { |
| 3378 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 3379 | if (count && values && keys) { |
| 3380 | for (NSUInteger i = 0; i < count; ++i) { |
| 3381 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 3382 | } |
| 3383 | } |
| 3384 | } |
| 3385 | return self; |
| 3386 | } |
| 3387 | |
| 3388 | - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3389 | self = [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3390 | if (self) { |
| 3391 | if (dictionary) { |
| 3392 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 3393 | } |
| 3394 | } |
| 3395 | return self; |
| 3396 | } |
| 3397 | |
| 3398 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 3399 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3400 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | - (void)dealloc { |
| 3404 | NSAssert(!_autocreator, |
| 3405 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 3406 | [self class], _autocreator); |
| 3407 | [_dictionary release]; |
| 3408 | [super dealloc]; |
| 3409 | } |
| 3410 | |
| 3411 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 3412 | return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 3413 | } |
| 3414 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3415 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3416 | if (self == other) { |
| 3417 | return YES; |
| 3418 | } |
| 3419 | if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) { |
| 3420 | return NO; |
| 3421 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3422 | GPBInt32Int32Dictionary *otherDictionary = other; |
| 3423 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | - (NSUInteger)hash { |
| 3427 | return _dictionary.count; |
| 3428 | } |
| 3429 | |
| 3430 | - (NSString *)description { |
| 3431 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 3432 | } |
| 3433 | |
| 3434 | - (NSUInteger)count { |
| 3435 | return _dictionary.count; |
| 3436 | } |
| 3437 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3438 | - (void)enumerateKeysAndInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3439 | (void (^)(int32_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3440 | BOOL stop = NO; |
| 3441 | NSDictionary *internal = _dictionary; |
| 3442 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3443 | NSNumber *aKey; |
| 3444 | while ((aKey = [keys nextObject])) { |
| 3445 | NSNumber *aValue = internal[aKey]; |
| 3446 | block([aKey intValue], [aValue intValue], &stop); |
| 3447 | if (stop) { |
| 3448 | break; |
| 3449 | } |
| 3450 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3454 | NSDictionary *internal = _dictionary; |
| 3455 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3456 | if (count == 0) { |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3461 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3462 | size_t result = 0; |
| 3463 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3464 | NSNumber *aKey; |
| 3465 | while ((aKey = [keys nextObject])) { |
| 3466 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3467 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 3468 | msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 3469 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3470 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3471 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 3472 | result += tagSize * count; |
| 3473 | return result; |
| 3474 | } |
| 3475 | |
| 3476 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 3477 | asField:(GPBFieldDescriptor *)field { |
| 3478 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3479 | GPBDataType keyDataType = field.mapKeyDataType; |
| 3480 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3481 | NSDictionary *internal = _dictionary; |
| 3482 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3483 | NSNumber *aKey; |
| 3484 | while ((aKey = [keys nextObject])) { |
| 3485 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3486 | [outputStream writeInt32NoTag:tag]; |
| 3487 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3488 | int32_t unwrappedKey = [aKey intValue]; |
| 3489 | int32_t unwrappedValue = [aValue intValue]; |
| 3490 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3491 | msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3492 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 3493 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3494 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3495 | WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 3496 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 3500 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 3501 | [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)]; |
| 3502 | } |
| 3503 | |
| 3504 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3505 | [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3506 | #pragma unused(stop) |
| 3507 | block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]); |
| 3508 | }]; |
| 3509 | } |
| 3510 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3511 | - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3512 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 3513 | if (wrapped && value) { |
| 3514 | *value = [wrapped intValue]; |
| 3515 | } |
| 3516 | return (wrapped != NULL); |
| 3517 | } |
| 3518 | |
| 3519 | - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary { |
| 3520 | if (otherDictionary) { |
| 3521 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 3522 | if (_autocreator) { |
| 3523 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3524 | } |
| 3525 | } |
| 3526 | } |
| 3527 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3528 | - (void)setInt32:(int32_t)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3529 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 3530 | if (_autocreator) { |
| 3531 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3532 | } |
| 3533 | } |
| 3534 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3535 | - (void)removeInt32ForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3536 | [_dictionary removeObjectForKey:@(aKey)]; |
| 3537 | } |
| 3538 | |
| 3539 | - (void)removeAll { |
| 3540 | [_dictionary removeAllObjects]; |
| 3541 | } |
| 3542 | |
| 3543 | @end |
| 3544 | |
| 3545 | #pragma mark - Int32 -> UInt64 |
| 3546 | |
| 3547 | @implementation GPBInt32UInt64Dictionary { |
| 3548 | @package |
| 3549 | NSMutableDictionary *_dictionary; |
| 3550 | } |
| 3551 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3552 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3553 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3554 | } |
| 3555 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3556 | - (instancetype)initWithUInt64s:(const uint64_t [])values |
| 3557 | forKeys:(const int32_t [])keys |
| 3558 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3559 | self = [super init]; |
| 3560 | if (self) { |
| 3561 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 3562 | if (count && values && keys) { |
| 3563 | for (NSUInteger i = 0; i < count; ++i) { |
| 3564 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | return self; |
| 3569 | } |
| 3570 | |
| 3571 | - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3572 | self = [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3573 | if (self) { |
| 3574 | if (dictionary) { |
| 3575 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 3576 | } |
| 3577 | } |
| 3578 | return self; |
| 3579 | } |
| 3580 | |
| 3581 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 3582 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3583 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | - (void)dealloc { |
| 3587 | NSAssert(!_autocreator, |
| 3588 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 3589 | [self class], _autocreator); |
| 3590 | [_dictionary release]; |
| 3591 | [super dealloc]; |
| 3592 | } |
| 3593 | |
| 3594 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 3595 | return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 3596 | } |
| 3597 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3598 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3599 | if (self == other) { |
| 3600 | return YES; |
| 3601 | } |
| 3602 | if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) { |
| 3603 | return NO; |
| 3604 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3605 | GPBInt32UInt64Dictionary *otherDictionary = other; |
| 3606 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | - (NSUInteger)hash { |
| 3610 | return _dictionary.count; |
| 3611 | } |
| 3612 | |
| 3613 | - (NSString *)description { |
| 3614 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 3615 | } |
| 3616 | |
| 3617 | - (NSUInteger)count { |
| 3618 | return _dictionary.count; |
| 3619 | } |
| 3620 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3621 | - (void)enumerateKeysAndUInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3622 | (void (^)(int32_t key, uint64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3623 | BOOL stop = NO; |
| 3624 | NSDictionary *internal = _dictionary; |
| 3625 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3626 | NSNumber *aKey; |
| 3627 | while ((aKey = [keys nextObject])) { |
| 3628 | NSNumber *aValue = internal[aKey]; |
| 3629 | block([aKey intValue], [aValue unsignedLongLongValue], &stop); |
| 3630 | if (stop) { |
| 3631 | break; |
| 3632 | } |
| 3633 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3637 | NSDictionary *internal = _dictionary; |
| 3638 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3639 | if (count == 0) { |
| 3640 | return 0; |
| 3641 | } |
| 3642 | |
| 3643 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3644 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3645 | size_t result = 0; |
| 3646 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3647 | NSNumber *aKey; |
| 3648 | while ((aKey = [keys nextObject])) { |
| 3649 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3650 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 3651 | msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType); |
| 3652 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3653 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3654 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 3655 | result += tagSize * count; |
| 3656 | return result; |
| 3657 | } |
| 3658 | |
| 3659 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 3660 | asField:(GPBFieldDescriptor *)field { |
| 3661 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3662 | GPBDataType keyDataType = field.mapKeyDataType; |
| 3663 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3664 | NSDictionary *internal = _dictionary; |
| 3665 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3666 | NSNumber *aKey; |
| 3667 | while ((aKey = [keys nextObject])) { |
| 3668 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3669 | [outputStream writeInt32NoTag:tag]; |
| 3670 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3671 | int32_t unwrappedKey = [aKey intValue]; |
| 3672 | uint64_t unwrappedValue = [aValue unsignedLongLongValue]; |
| 3673 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3674 | msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3675 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 3676 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3677 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3678 | WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 3679 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 3683 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 3684 | [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)]; |
| 3685 | } |
| 3686 | |
| 3687 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3688 | [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3689 | #pragma unused(stop) |
| 3690 | block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]); |
| 3691 | }]; |
| 3692 | } |
| 3693 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3694 | - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3695 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 3696 | if (wrapped && value) { |
| 3697 | *value = [wrapped unsignedLongLongValue]; |
| 3698 | } |
| 3699 | return (wrapped != NULL); |
| 3700 | } |
| 3701 | |
| 3702 | - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary { |
| 3703 | if (otherDictionary) { |
| 3704 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 3705 | if (_autocreator) { |
| 3706 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3711 | - (void)setUInt64:(uint64_t)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3712 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 3713 | if (_autocreator) { |
| 3714 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3715 | } |
| 3716 | } |
| 3717 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3718 | - (void)removeUInt64ForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3719 | [_dictionary removeObjectForKey:@(aKey)]; |
| 3720 | } |
| 3721 | |
| 3722 | - (void)removeAll { |
| 3723 | [_dictionary removeAllObjects]; |
| 3724 | } |
| 3725 | |
| 3726 | @end |
| 3727 | |
| 3728 | #pragma mark - Int32 -> Int64 |
| 3729 | |
| 3730 | @implementation GPBInt32Int64Dictionary { |
| 3731 | @package |
| 3732 | NSMutableDictionary *_dictionary; |
| 3733 | } |
| 3734 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3735 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3736 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3737 | } |
| 3738 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3739 | - (instancetype)initWithInt64s:(const int64_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3740 | forKeys:(const int32_t [])keys |
| 3741 | count:(NSUInteger)count { |
| 3742 | self = [super init]; |
| 3743 | if (self) { |
| 3744 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 3745 | if (count && values && keys) { |
| 3746 | for (NSUInteger i = 0; i < count; ++i) { |
| 3747 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | return self; |
| 3752 | } |
| 3753 | |
| 3754 | - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3755 | self = [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3756 | if (self) { |
| 3757 | if (dictionary) { |
| 3758 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 3759 | } |
| 3760 | } |
| 3761 | return self; |
| 3762 | } |
| 3763 | |
| 3764 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 3765 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3766 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | - (void)dealloc { |
| 3770 | NSAssert(!_autocreator, |
| 3771 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 3772 | [self class], _autocreator); |
| 3773 | [_dictionary release]; |
| 3774 | [super dealloc]; |
| 3775 | } |
| 3776 | |
| 3777 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 3778 | return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 3779 | } |
| 3780 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3781 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3782 | if (self == other) { |
| 3783 | return YES; |
| 3784 | } |
| 3785 | if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) { |
| 3786 | return NO; |
| 3787 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3788 | GPBInt32Int64Dictionary *otherDictionary = other; |
| 3789 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | - (NSUInteger)hash { |
| 3793 | return _dictionary.count; |
| 3794 | } |
| 3795 | |
| 3796 | - (NSString *)description { |
| 3797 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 3798 | } |
| 3799 | |
| 3800 | - (NSUInteger)count { |
| 3801 | return _dictionary.count; |
| 3802 | } |
| 3803 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3804 | - (void)enumerateKeysAndInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3805 | (void (^)(int32_t key, int64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3806 | BOOL stop = NO; |
| 3807 | NSDictionary *internal = _dictionary; |
| 3808 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3809 | NSNumber *aKey; |
| 3810 | while ((aKey = [keys nextObject])) { |
| 3811 | NSNumber *aValue = internal[aKey]; |
| 3812 | block([aKey intValue], [aValue longLongValue], &stop); |
| 3813 | if (stop) { |
| 3814 | break; |
| 3815 | } |
| 3816 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3820 | NSDictionary *internal = _dictionary; |
| 3821 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3822 | if (count == 0) { |
| 3823 | return 0; |
| 3824 | } |
| 3825 | |
| 3826 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3827 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3828 | size_t result = 0; |
| 3829 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3830 | NSNumber *aKey; |
| 3831 | while ((aKey = [keys nextObject])) { |
| 3832 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3833 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 3834 | msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType); |
| 3835 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3836 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3837 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 3838 | result += tagSize * count; |
| 3839 | return result; |
| 3840 | } |
| 3841 | |
| 3842 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 3843 | asField:(GPBFieldDescriptor *)field { |
| 3844 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 3845 | GPBDataType keyDataType = field.mapKeyDataType; |
| 3846 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3847 | NSDictionary *internal = _dictionary; |
| 3848 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3849 | NSNumber *aKey; |
| 3850 | while ((aKey = [keys nextObject])) { |
| 3851 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3852 | [outputStream writeInt32NoTag:tag]; |
| 3853 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3854 | int32_t unwrappedKey = [aKey intValue]; |
| 3855 | int64_t unwrappedValue = [aValue longLongValue]; |
| 3856 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3857 | msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3858 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 3859 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3860 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 3861 | WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 3862 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 3866 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 3867 | [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)]; |
| 3868 | } |
| 3869 | |
| 3870 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3871 | [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3872 | #pragma unused(stop) |
| 3873 | block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]); |
| 3874 | }]; |
| 3875 | } |
| 3876 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3877 | - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3878 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 3879 | if (wrapped && value) { |
| 3880 | *value = [wrapped longLongValue]; |
| 3881 | } |
| 3882 | return (wrapped != NULL); |
| 3883 | } |
| 3884 | |
| 3885 | - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary { |
| 3886 | if (otherDictionary) { |
| 3887 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 3888 | if (_autocreator) { |
| 3889 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3890 | } |
| 3891 | } |
| 3892 | } |
| 3893 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3894 | - (void)setInt64:(int64_t)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3895 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 3896 | if (_autocreator) { |
| 3897 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 3898 | } |
| 3899 | } |
| 3900 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3901 | - (void)removeInt64ForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3902 | [_dictionary removeObjectForKey:@(aKey)]; |
| 3903 | } |
| 3904 | |
| 3905 | - (void)removeAll { |
| 3906 | [_dictionary removeAllObjects]; |
| 3907 | } |
| 3908 | |
| 3909 | @end |
| 3910 | |
| 3911 | #pragma mark - Int32 -> Bool |
| 3912 | |
| 3913 | @implementation GPBInt32BoolDictionary { |
| 3914 | @package |
| 3915 | NSMutableDictionary *_dictionary; |
| 3916 | } |
| 3917 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3918 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3919 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3920 | } |
| 3921 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3922 | - (instancetype)initWithBools:(const BOOL [])values |
| 3923 | forKeys:(const int32_t [])keys |
| 3924 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3925 | self = [super init]; |
| 3926 | if (self) { |
| 3927 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 3928 | if (count && values && keys) { |
| 3929 | for (NSUInteger i = 0; i < count; ++i) { |
| 3930 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 3931 | } |
| 3932 | } |
| 3933 | } |
| 3934 | return self; |
| 3935 | } |
| 3936 | |
| 3937 | - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3938 | self = [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3939 | if (self) { |
| 3940 | if (dictionary) { |
| 3941 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 3942 | } |
| 3943 | } |
| 3944 | return self; |
| 3945 | } |
| 3946 | |
| 3947 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 3948 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3949 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | - (void)dealloc { |
| 3953 | NSAssert(!_autocreator, |
| 3954 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 3955 | [self class], _autocreator); |
| 3956 | [_dictionary release]; |
| 3957 | [super dealloc]; |
| 3958 | } |
| 3959 | |
| 3960 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 3961 | return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self]; |
| 3962 | } |
| 3963 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3964 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3965 | if (self == other) { |
| 3966 | return YES; |
| 3967 | } |
| 3968 | if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) { |
| 3969 | return NO; |
| 3970 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3971 | GPBInt32BoolDictionary *otherDictionary = other; |
| 3972 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | - (NSUInteger)hash { |
| 3976 | return _dictionary.count; |
| 3977 | } |
| 3978 | |
| 3979 | - (NSString *)description { |
| 3980 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 3981 | } |
| 3982 | |
| 3983 | - (NSUInteger)count { |
| 3984 | return _dictionary.count; |
| 3985 | } |
| 3986 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3987 | - (void)enumerateKeysAndBoolsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3988 | (void (^)(int32_t key, BOOL value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 3989 | BOOL stop = NO; |
| 3990 | NSDictionary *internal = _dictionary; |
| 3991 | NSEnumerator *keys = [internal keyEnumerator]; |
| 3992 | NSNumber *aKey; |
| 3993 | while ((aKey = [keys nextObject])) { |
| 3994 | NSNumber *aValue = internal[aKey]; |
| 3995 | block([aKey intValue], [aValue boolValue], &stop); |
| 3996 | if (stop) { |
| 3997 | break; |
| 3998 | } |
| 3999 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4003 | NSDictionary *internal = _dictionary; |
| 4004 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4005 | if (count == 0) { |
| 4006 | return 0; |
| 4007 | } |
| 4008 | |
| 4009 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4010 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4011 | size_t result = 0; |
| 4012 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4013 | NSNumber *aKey; |
| 4014 | while ((aKey = [keys nextObject])) { |
| 4015 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4016 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 4017 | msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType); |
| 4018 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4019 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4020 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 4021 | result += tagSize * count; |
| 4022 | return result; |
| 4023 | } |
| 4024 | |
| 4025 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 4026 | asField:(GPBFieldDescriptor *)field { |
| 4027 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4028 | GPBDataType keyDataType = field.mapKeyDataType; |
| 4029 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4030 | NSDictionary *internal = _dictionary; |
| 4031 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4032 | NSNumber *aKey; |
| 4033 | while ((aKey = [keys nextObject])) { |
| 4034 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4035 | [outputStream writeInt32NoTag:tag]; |
| 4036 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4037 | int32_t unwrappedKey = [aKey intValue]; |
| 4038 | BOOL unwrappedValue = [aValue boolValue]; |
| 4039 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4040 | msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4041 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 4042 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4043 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4044 | WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 4045 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 4049 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 4050 | [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)]; |
| 4051 | } |
| 4052 | |
| 4053 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4054 | [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4055 | #pragma unused(stop) |
| 4056 | block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false")); |
| 4057 | }]; |
| 4058 | } |
| 4059 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4060 | - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4061 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 4062 | if (wrapped && value) { |
| 4063 | *value = [wrapped boolValue]; |
| 4064 | } |
| 4065 | return (wrapped != NULL); |
| 4066 | } |
| 4067 | |
| 4068 | - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary { |
| 4069 | if (otherDictionary) { |
| 4070 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 4071 | if (_autocreator) { |
| 4072 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4077 | - (void)setBool:(BOOL)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4078 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 4079 | if (_autocreator) { |
| 4080 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4081 | } |
| 4082 | } |
| 4083 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4084 | - (void)removeBoolForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4085 | [_dictionary removeObjectForKey:@(aKey)]; |
| 4086 | } |
| 4087 | |
| 4088 | - (void)removeAll { |
| 4089 | [_dictionary removeAllObjects]; |
| 4090 | } |
| 4091 | |
| 4092 | @end |
| 4093 | |
| 4094 | #pragma mark - Int32 -> Float |
| 4095 | |
| 4096 | @implementation GPBInt32FloatDictionary { |
| 4097 | @package |
| 4098 | NSMutableDictionary *_dictionary; |
| 4099 | } |
| 4100 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4101 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4102 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4103 | } |
| 4104 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4105 | - (instancetype)initWithFloats:(const float [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4106 | forKeys:(const int32_t [])keys |
| 4107 | count:(NSUInteger)count { |
| 4108 | self = [super init]; |
| 4109 | if (self) { |
| 4110 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 4111 | if (count && values && keys) { |
| 4112 | for (NSUInteger i = 0; i < count; ++i) { |
| 4113 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 4114 | } |
| 4115 | } |
| 4116 | } |
| 4117 | return self; |
| 4118 | } |
| 4119 | |
| 4120 | - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4121 | self = [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4122 | if (self) { |
| 4123 | if (dictionary) { |
| 4124 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 4125 | } |
| 4126 | } |
| 4127 | return self; |
| 4128 | } |
| 4129 | |
| 4130 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 4131 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4132 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4133 | } |
| 4134 | |
| 4135 | - (void)dealloc { |
| 4136 | NSAssert(!_autocreator, |
| 4137 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 4138 | [self class], _autocreator); |
| 4139 | [_dictionary release]; |
| 4140 | [super dealloc]; |
| 4141 | } |
| 4142 | |
| 4143 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 4144 | return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self]; |
| 4145 | } |
| 4146 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4147 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4148 | if (self == other) { |
| 4149 | return YES; |
| 4150 | } |
| 4151 | if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) { |
| 4152 | return NO; |
| 4153 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4154 | GPBInt32FloatDictionary *otherDictionary = other; |
| 4155 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | - (NSUInteger)hash { |
| 4159 | return _dictionary.count; |
| 4160 | } |
| 4161 | |
| 4162 | - (NSString *)description { |
| 4163 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 4164 | } |
| 4165 | |
| 4166 | - (NSUInteger)count { |
| 4167 | return _dictionary.count; |
| 4168 | } |
| 4169 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4170 | - (void)enumerateKeysAndFloatsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4171 | (void (^)(int32_t key, float value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4172 | BOOL stop = NO; |
| 4173 | NSDictionary *internal = _dictionary; |
| 4174 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4175 | NSNumber *aKey; |
| 4176 | while ((aKey = [keys nextObject])) { |
| 4177 | NSNumber *aValue = internal[aKey]; |
| 4178 | block([aKey intValue], [aValue floatValue], &stop); |
| 4179 | if (stop) { |
| 4180 | break; |
| 4181 | } |
| 4182 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4186 | NSDictionary *internal = _dictionary; |
| 4187 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4188 | if (count == 0) { |
| 4189 | return 0; |
| 4190 | } |
| 4191 | |
| 4192 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4193 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4194 | size_t result = 0; |
| 4195 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4196 | NSNumber *aKey; |
| 4197 | while ((aKey = [keys nextObject])) { |
| 4198 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4199 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 4200 | msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType); |
| 4201 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4202 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4203 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 4204 | result += tagSize * count; |
| 4205 | return result; |
| 4206 | } |
| 4207 | |
| 4208 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 4209 | asField:(GPBFieldDescriptor *)field { |
| 4210 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4211 | GPBDataType keyDataType = field.mapKeyDataType; |
| 4212 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4213 | NSDictionary *internal = _dictionary; |
| 4214 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4215 | NSNumber *aKey; |
| 4216 | while ((aKey = [keys nextObject])) { |
| 4217 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4218 | [outputStream writeInt32NoTag:tag]; |
| 4219 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4220 | int32_t unwrappedKey = [aKey intValue]; |
| 4221 | float unwrappedValue = [aValue floatValue]; |
| 4222 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4223 | msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4224 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 4225 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4226 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4227 | WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 4228 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 4232 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 4233 | [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)]; |
| 4234 | } |
| 4235 | |
| 4236 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4237 | [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4238 | #pragma unused(stop) |
| 4239 | block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); |
| 4240 | }]; |
| 4241 | } |
| 4242 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4243 | - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4244 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 4245 | if (wrapped && value) { |
| 4246 | *value = [wrapped floatValue]; |
| 4247 | } |
| 4248 | return (wrapped != NULL); |
| 4249 | } |
| 4250 | |
| 4251 | - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary { |
| 4252 | if (otherDictionary) { |
| 4253 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 4254 | if (_autocreator) { |
| 4255 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4256 | } |
| 4257 | } |
| 4258 | } |
| 4259 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4260 | - (void)setFloat:(float)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4261 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 4262 | if (_autocreator) { |
| 4263 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4264 | } |
| 4265 | } |
| 4266 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4267 | - (void)removeFloatForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4268 | [_dictionary removeObjectForKey:@(aKey)]; |
| 4269 | } |
| 4270 | |
| 4271 | - (void)removeAll { |
| 4272 | [_dictionary removeAllObjects]; |
| 4273 | } |
| 4274 | |
| 4275 | @end |
| 4276 | |
| 4277 | #pragma mark - Int32 -> Double |
| 4278 | |
| 4279 | @implementation GPBInt32DoubleDictionary { |
| 4280 | @package |
| 4281 | NSMutableDictionary *_dictionary; |
| 4282 | } |
| 4283 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4284 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4285 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4286 | } |
| 4287 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4288 | - (instancetype)initWithDoubles:(const double [])values |
| 4289 | forKeys:(const int32_t [])keys |
| 4290 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4291 | self = [super init]; |
| 4292 | if (self) { |
| 4293 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 4294 | if (count && values && keys) { |
| 4295 | for (NSUInteger i = 0; i < count; ++i) { |
| 4296 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 4297 | } |
| 4298 | } |
| 4299 | } |
| 4300 | return self; |
| 4301 | } |
| 4302 | |
| 4303 | - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4304 | self = [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4305 | if (self) { |
| 4306 | if (dictionary) { |
| 4307 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 4308 | } |
| 4309 | } |
| 4310 | return self; |
| 4311 | } |
| 4312 | |
| 4313 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 4314 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4315 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | - (void)dealloc { |
| 4319 | NSAssert(!_autocreator, |
| 4320 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 4321 | [self class], _autocreator); |
| 4322 | [_dictionary release]; |
| 4323 | [super dealloc]; |
| 4324 | } |
| 4325 | |
| 4326 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 4327 | return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self]; |
| 4328 | } |
| 4329 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4330 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4331 | if (self == other) { |
| 4332 | return YES; |
| 4333 | } |
| 4334 | if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) { |
| 4335 | return NO; |
| 4336 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4337 | GPBInt32DoubleDictionary *otherDictionary = other; |
| 4338 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | - (NSUInteger)hash { |
| 4342 | return _dictionary.count; |
| 4343 | } |
| 4344 | |
| 4345 | - (NSString *)description { |
| 4346 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 4347 | } |
| 4348 | |
| 4349 | - (NSUInteger)count { |
| 4350 | return _dictionary.count; |
| 4351 | } |
| 4352 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4353 | - (void)enumerateKeysAndDoublesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4354 | (void (^)(int32_t key, double value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4355 | BOOL stop = NO; |
| 4356 | NSDictionary *internal = _dictionary; |
| 4357 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4358 | NSNumber *aKey; |
| 4359 | while ((aKey = [keys nextObject])) { |
| 4360 | NSNumber *aValue = internal[aKey]; |
| 4361 | block([aKey intValue], [aValue doubleValue], &stop); |
| 4362 | if (stop) { |
| 4363 | break; |
| 4364 | } |
| 4365 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4369 | NSDictionary *internal = _dictionary; |
| 4370 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4371 | if (count == 0) { |
| 4372 | return 0; |
| 4373 | } |
| 4374 | |
| 4375 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4376 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4377 | size_t result = 0; |
| 4378 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4379 | NSNumber *aKey; |
| 4380 | while ((aKey = [keys nextObject])) { |
| 4381 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4382 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 4383 | msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType); |
| 4384 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4385 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4386 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 4387 | result += tagSize * count; |
| 4388 | return result; |
| 4389 | } |
| 4390 | |
| 4391 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 4392 | asField:(GPBFieldDescriptor *)field { |
| 4393 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4394 | GPBDataType keyDataType = field.mapKeyDataType; |
| 4395 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4396 | NSDictionary *internal = _dictionary; |
| 4397 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4398 | NSNumber *aKey; |
| 4399 | while ((aKey = [keys nextObject])) { |
| 4400 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4401 | [outputStream writeInt32NoTag:tag]; |
| 4402 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4403 | int32_t unwrappedKey = [aKey intValue]; |
| 4404 | double unwrappedValue = [aValue doubleValue]; |
| 4405 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4406 | msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4407 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 4408 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4409 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4410 | WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 4411 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 4415 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 4416 | [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)]; |
| 4417 | } |
| 4418 | |
| 4419 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4420 | [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4421 | #pragma unused(stop) |
| 4422 | block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); |
| 4423 | }]; |
| 4424 | } |
| 4425 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4426 | - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4427 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 4428 | if (wrapped && value) { |
| 4429 | *value = [wrapped doubleValue]; |
| 4430 | } |
| 4431 | return (wrapped != NULL); |
| 4432 | } |
| 4433 | |
| 4434 | - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary { |
| 4435 | if (otherDictionary) { |
| 4436 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 4437 | if (_autocreator) { |
| 4438 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4439 | } |
| 4440 | } |
| 4441 | } |
| 4442 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4443 | - (void)setDouble:(double)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4444 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 4445 | if (_autocreator) { |
| 4446 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4447 | } |
| 4448 | } |
| 4449 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4450 | - (void)removeDoubleForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4451 | [_dictionary removeObjectForKey:@(aKey)]; |
| 4452 | } |
| 4453 | |
| 4454 | - (void)removeAll { |
| 4455 | [_dictionary removeAllObjects]; |
| 4456 | } |
| 4457 | |
| 4458 | @end |
| 4459 | |
| 4460 | #pragma mark - Int32 -> Enum |
| 4461 | |
| 4462 | @implementation GPBInt32EnumDictionary { |
| 4463 | @package |
| 4464 | NSMutableDictionary *_dictionary; |
| 4465 | GPBEnumValidationFunc _validationFunc; |
| 4466 | } |
| 4467 | |
| 4468 | @synthesize validationFunc = _validationFunc; |
| 4469 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4470 | - (instancetype)init { |
| 4471 | return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 4472 | } |
| 4473 | |
| 4474 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 4475 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 4476 | } |
| 4477 | |
| 4478 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 4479 | rawValues:(const int32_t [])rawValues |
| 4480 | forKeys:(const int32_t [])keys |
| 4481 | count:(NSUInteger)count { |
| 4482 | self = [super init]; |
| 4483 | if (self) { |
| 4484 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 4485 | _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 4486 | if (count && rawValues && keys) { |
| 4487 | for (NSUInteger i = 0; i < count; ++i) { |
| 4488 | [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])]; |
| 4489 | } |
| 4490 | } |
| 4491 | } |
| 4492 | return self; |
| 4493 | } |
| 4494 | |
| 4495 | - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary { |
| 4496 | self = [self initWithValidationFunction:dictionary.validationFunc |
| 4497 | rawValues:NULL |
| 4498 | forKeys:NULL |
| 4499 | count:0]; |
| 4500 | if (self) { |
| 4501 | if (dictionary) { |
| 4502 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 4503 | } |
| 4504 | } |
| 4505 | return self; |
| 4506 | } |
| 4507 | |
| 4508 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 4509 | capacity:(NSUInteger)numItems { |
| 4510 | #pragma unused(numItems) |
| 4511 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 4512 | } |
| 4513 | |
| 4514 | - (void)dealloc { |
| 4515 | NSAssert(!_autocreator, |
| 4516 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 4517 | [self class], _autocreator); |
| 4518 | [_dictionary release]; |
| 4519 | [super dealloc]; |
| 4520 | } |
| 4521 | |
| 4522 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 4523 | return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self]; |
| 4524 | } |
| 4525 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4526 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4527 | if (self == other) { |
| 4528 | return YES; |
| 4529 | } |
| 4530 | if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) { |
| 4531 | return NO; |
| 4532 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4533 | GPBInt32EnumDictionary *otherDictionary = other; |
| 4534 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | - (NSUInteger)hash { |
| 4538 | return _dictionary.count; |
| 4539 | } |
| 4540 | |
| 4541 | - (NSString *)description { |
| 4542 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 4543 | } |
| 4544 | |
| 4545 | - (NSUInteger)count { |
| 4546 | return _dictionary.count; |
| 4547 | } |
| 4548 | |
| 4549 | - (void)enumerateKeysAndRawValuesUsingBlock: |
| 4550 | (void (^)(int32_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4551 | BOOL stop = NO; |
| 4552 | NSDictionary *internal = _dictionary; |
| 4553 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4554 | NSNumber *aKey; |
| 4555 | while ((aKey = [keys nextObject])) { |
| 4556 | NSNumber *aValue = internal[aKey]; |
| 4557 | block([aKey intValue], [aValue intValue], &stop); |
| 4558 | if (stop) { |
| 4559 | break; |
| 4560 | } |
| 4561 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4565 | NSDictionary *internal = _dictionary; |
| 4566 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4567 | if (count == 0) { |
| 4568 | return 0; |
| 4569 | } |
| 4570 | |
| 4571 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4572 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4573 | size_t result = 0; |
| 4574 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4575 | NSNumber *aKey; |
| 4576 | while ((aKey = [keys nextObject])) { |
| 4577 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4578 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 4579 | msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 4580 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4581 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4582 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 4583 | result += tagSize * count; |
| 4584 | return result; |
| 4585 | } |
| 4586 | |
| 4587 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 4588 | asField:(GPBFieldDescriptor *)field { |
| 4589 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4590 | GPBDataType keyDataType = field.mapKeyDataType; |
| 4591 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4592 | NSDictionary *internal = _dictionary; |
| 4593 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4594 | NSNumber *aKey; |
| 4595 | while ((aKey = [keys nextObject])) { |
| 4596 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4597 | [outputStream writeInt32NoTag:tag]; |
| 4598 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4599 | int32_t unwrappedKey = [aKey intValue]; |
| 4600 | int32_t unwrappedValue = [aValue intValue]; |
| 4601 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4602 | msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4603 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 4604 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4605 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4606 | WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 4607 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | - (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 4611 | forKey:(GPBGenericValue *)key |
| 4612 | keyDataType:(GPBDataType)keyDataType { |
| 4613 | size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType); |
| 4614 | msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 4615 | NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 4616 | GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 4617 | WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType); |
| 4618 | WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 4619 | [outputStream release]; |
| 4620 | return data; |
| 4621 | } |
| 4622 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 4623 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 4624 | [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)]; |
| 4625 | } |
| 4626 | |
| 4627 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 4628 | [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) { |
| 4629 | #pragma unused(stop) |
| 4630 | block([NSString stringWithFormat:@"%d", key], @(value)); |
| 4631 | }]; |
| 4632 | } |
| 4633 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4634 | - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4635 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 4636 | if (wrapped && value) { |
| 4637 | int32_t result = [wrapped intValue]; |
| 4638 | if (!_validationFunc(result)) { |
| 4639 | result = kGPBUnrecognizedEnumeratorValue; |
| 4640 | } |
| 4641 | *value = result; |
| 4642 | } |
| 4643 | return (wrapped != NULL); |
| 4644 | } |
| 4645 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4646 | - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4647 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 4648 | if (wrapped && rawValue) { |
| 4649 | *rawValue = [wrapped intValue]; |
| 4650 | } |
| 4651 | return (wrapped != NULL); |
| 4652 | } |
| 4653 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4654 | - (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4655 | (void (^)(int32_t key, int32_t value, BOOL *stop))block { |
| 4656 | GPBEnumValidationFunc func = _validationFunc; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4657 | BOOL stop = NO; |
| 4658 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 4659 | NSNumber *aKey; |
| 4660 | while ((aKey = [keys nextObject])) { |
| 4661 | NSNumber *aValue = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4662 | int32_t unwrapped = [aValue intValue]; |
| 4663 | if (!func(unwrapped)) { |
| 4664 | unwrapped = kGPBUnrecognizedEnumeratorValue; |
| 4665 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4666 | block([aKey intValue], unwrapped, &stop); |
| 4667 | if (stop) { |
| 4668 | break; |
| 4669 | } |
| 4670 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary { |
| 4674 | if (otherDictionary) { |
| 4675 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 4676 | if (_autocreator) { |
| 4677 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | |
| 4682 | - (void)setRawValue:(int32_t)value forKey:(int32_t)key { |
| 4683 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 4684 | if (_autocreator) { |
| 4685 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4686 | } |
| 4687 | } |
| 4688 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4689 | - (void)removeEnumForKey:(int32_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4690 | [_dictionary removeObjectForKey:@(aKey)]; |
| 4691 | } |
| 4692 | |
| 4693 | - (void)removeAll { |
| 4694 | [_dictionary removeAllObjects]; |
| 4695 | } |
| 4696 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4697 | - (void)setEnum:(int32_t)value forKey:(int32_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4698 | if (!_validationFunc(value)) { |
| 4699 | [NSException raise:NSInvalidArgumentException |
| 4700 | format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)", |
| 4701 | value]; |
| 4702 | } |
| 4703 | |
| 4704 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 4705 | if (_autocreator) { |
| 4706 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4707 | } |
| 4708 | } |
| 4709 | |
| 4710 | @end |
| 4711 | |
| 4712 | #pragma mark - Int32 -> Object |
| 4713 | |
| 4714 | @implementation GPBInt32ObjectDictionary { |
| 4715 | @package |
| 4716 | NSMutableDictionary *_dictionary; |
| 4717 | } |
| 4718 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4719 | - (instancetype)init { |
| 4720 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 4721 | } |
| 4722 | |
| 4723 | - (instancetype)initWithObjects:(const id [])objects |
| 4724 | forKeys:(const int32_t [])keys |
| 4725 | count:(NSUInteger)count { |
| 4726 | self = [super init]; |
| 4727 | if (self) { |
| 4728 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 4729 | if (count && objects && keys) { |
| 4730 | for (NSUInteger i = 0; i < count; ++i) { |
| 4731 | if (!objects[i]) { |
| 4732 | [NSException raise:NSInvalidArgumentException |
| 4733 | format:@"Attempting to add nil object to a Dictionary"]; |
| 4734 | } |
| 4735 | [_dictionary setObject:objects[i] forKey:@(keys[i])]; |
| 4736 | } |
| 4737 | } |
| 4738 | } |
| 4739 | return self; |
| 4740 | } |
| 4741 | |
| 4742 | - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary { |
| 4743 | self = [self initWithObjects:NULL forKeys:NULL count:0]; |
| 4744 | if (self) { |
| 4745 | if (dictionary) { |
| 4746 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 4747 | } |
| 4748 | } |
| 4749 | return self; |
| 4750 | } |
| 4751 | |
| 4752 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 4753 | #pragma unused(numItems) |
| 4754 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 4755 | } |
| 4756 | |
| 4757 | - (void)dealloc { |
| 4758 | NSAssert(!_autocreator, |
| 4759 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 4760 | [self class], _autocreator); |
| 4761 | [_dictionary release]; |
| 4762 | [super dealloc]; |
| 4763 | } |
| 4764 | |
| 4765 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 4766 | return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self]; |
| 4767 | } |
| 4768 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4769 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4770 | if (self == other) { |
| 4771 | return YES; |
| 4772 | } |
| 4773 | if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) { |
| 4774 | return NO; |
| 4775 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4776 | GPBInt32ObjectDictionary *otherDictionary = other; |
| 4777 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | - (NSUInteger)hash { |
| 4781 | return _dictionary.count; |
| 4782 | } |
| 4783 | |
| 4784 | - (NSString *)description { |
| 4785 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 4786 | } |
| 4787 | |
| 4788 | - (NSUInteger)count { |
| 4789 | return _dictionary.count; |
| 4790 | } |
| 4791 | |
| 4792 | - (void)enumerateKeysAndObjectsUsingBlock: |
| 4793 | (void (^)(int32_t key, id object, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4794 | BOOL stop = NO; |
| 4795 | NSDictionary *internal = _dictionary; |
| 4796 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4797 | NSNumber *aKey; |
| 4798 | while ((aKey = [keys nextObject])) { |
| 4799 | id aObject = internal[aKey]; |
| 4800 | block([aKey intValue], aObject, &stop); |
| 4801 | if (stop) { |
| 4802 | break; |
| 4803 | } |
| 4804 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | - (BOOL)isInitialized { |
| 4808 | for (GPBMessage *msg in [_dictionary objectEnumerator]) { |
| 4809 | if (!msg.initialized) { |
| 4810 | return NO; |
| 4811 | } |
| 4812 | } |
| 4813 | return YES; |
| 4814 | } |
| 4815 | |
| 4816 | - (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 4817 | GPBInt32ObjectDictionary *newDict = |
| 4818 | [[GPBInt32ObjectDictionary alloc] init]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4819 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 4820 | id aKey; |
| 4821 | NSMutableDictionary *internalDict = newDict->_dictionary; |
| 4822 | while ((aKey = [keys nextObject])) { |
| 4823 | GPBMessage *msg = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4824 | GPBMessage *copiedMsg = [msg copyWithZone:zone]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4825 | [internalDict setObject:copiedMsg forKey:aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4826 | [copiedMsg release]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4827 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4828 | return newDict; |
| 4829 | } |
| 4830 | |
| 4831 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4832 | NSDictionary *internal = _dictionary; |
| 4833 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4834 | if (count == 0) { |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
| 4838 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4839 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4840 | size_t result = 0; |
| 4841 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4842 | NSNumber *aKey; |
| 4843 | while ((aKey = [keys nextObject])) { |
| 4844 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4845 | size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType); |
| 4846 | msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType); |
| 4847 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4848 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4849 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 4850 | result += tagSize * count; |
| 4851 | return result; |
| 4852 | } |
| 4853 | |
| 4854 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 4855 | asField:(GPBFieldDescriptor *)field { |
| 4856 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 4857 | GPBDataType keyDataType = field.mapKeyDataType; |
| 4858 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4859 | NSDictionary *internal = _dictionary; |
| 4860 | NSEnumerator *keys = [internal keyEnumerator]; |
| 4861 | NSNumber *aKey; |
| 4862 | while ((aKey = [keys nextObject])) { |
| 4863 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4864 | [outputStream writeInt32NoTag:tag]; |
| 4865 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4866 | int32_t unwrappedKey = [aKey intValue]; |
| 4867 | id unwrappedValue = aObject; |
| 4868 | size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4869 | msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4870 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 4871 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4872 | WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 4873 | WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 4874 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 4878 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 4879 | [_dictionary setObject:value->valueString forKey:@(key->valueInt32)]; |
| 4880 | } |
| 4881 | |
| 4882 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 4883 | [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) { |
| 4884 | #pragma unused(stop) |
| 4885 | block([NSString stringWithFormat:@"%d", key], object); |
| 4886 | }]; |
| 4887 | } |
| 4888 | |
| 4889 | - (id)objectForKey:(int32_t)key { |
| 4890 | id result = [_dictionary objectForKey:@(key)]; |
| 4891 | return result; |
| 4892 | } |
| 4893 | |
| 4894 | - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary { |
| 4895 | if (otherDictionary) { |
| 4896 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 4897 | if (_autocreator) { |
| 4898 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4899 | } |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | - (void)setObject:(id)object forKey:(int32_t)key { |
| 4904 | if (!object) { |
| 4905 | [NSException raise:NSInvalidArgumentException |
| 4906 | format:@"Attempting to add nil object to a Dictionary"]; |
| 4907 | } |
| 4908 | [_dictionary setObject:object forKey:@(key)]; |
| 4909 | if (_autocreator) { |
| 4910 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | - (void)removeObjectForKey:(int32_t)aKey { |
| 4915 | [_dictionary removeObjectForKey:@(aKey)]; |
| 4916 | } |
| 4917 | |
| 4918 | - (void)removeAll { |
| 4919 | [_dictionary removeAllObjects]; |
| 4920 | } |
| 4921 | |
| 4922 | @end |
| 4923 | |
| 4924 | //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t) |
| 4925 | // This block of code is generated, do not edit it directly. |
| 4926 | |
| 4927 | #pragma mark - UInt64 -> UInt32 |
| 4928 | |
| 4929 | @implementation GPBUInt64UInt32Dictionary { |
| 4930 | @package |
| 4931 | NSMutableDictionary *_dictionary; |
| 4932 | } |
| 4933 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4934 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4935 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4936 | } |
| 4937 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4938 | - (instancetype)initWithUInt32s:(const uint32_t [])values |
| 4939 | forKeys:(const uint64_t [])keys |
| 4940 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4941 | self = [super init]; |
| 4942 | if (self) { |
| 4943 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 4944 | if (count && values && keys) { |
| 4945 | for (NSUInteger i = 0; i < count; ++i) { |
| 4946 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 4947 | } |
| 4948 | } |
| 4949 | } |
| 4950 | return self; |
| 4951 | } |
| 4952 | |
| 4953 | - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4954 | self = [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4955 | if (self) { |
| 4956 | if (dictionary) { |
| 4957 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 4958 | } |
| 4959 | } |
| 4960 | return self; |
| 4961 | } |
| 4962 | |
| 4963 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 4964 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4965 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | - (void)dealloc { |
| 4969 | NSAssert(!_autocreator, |
| 4970 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 4971 | [self class], _autocreator); |
| 4972 | [_dictionary release]; |
| 4973 | [super dealloc]; |
| 4974 | } |
| 4975 | |
| 4976 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 4977 | return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 4978 | } |
| 4979 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4980 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4981 | if (self == other) { |
| 4982 | return YES; |
| 4983 | } |
| 4984 | if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) { |
| 4985 | return NO; |
| 4986 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 4987 | GPBUInt64UInt32Dictionary *otherDictionary = other; |
| 4988 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 4989 | } |
| 4990 | |
| 4991 | - (NSUInteger)hash { |
| 4992 | return _dictionary.count; |
| 4993 | } |
| 4994 | |
| 4995 | - (NSString *)description { |
| 4996 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 4997 | } |
| 4998 | |
| 4999 | - (NSUInteger)count { |
| 5000 | return _dictionary.count; |
| 5001 | } |
| 5002 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5003 | - (void)enumerateKeysAndUInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5004 | (void (^)(uint64_t key, uint32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5005 | BOOL stop = NO; |
| 5006 | NSDictionary *internal = _dictionary; |
| 5007 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5008 | NSNumber *aKey; |
| 5009 | while ((aKey = [keys nextObject])) { |
| 5010 | NSNumber *aValue = internal[aKey]; |
| 5011 | block([aKey unsignedLongLongValue], [aValue unsignedIntValue], &stop); |
| 5012 | if (stop) { |
| 5013 | break; |
| 5014 | } |
| 5015 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5019 | NSDictionary *internal = _dictionary; |
| 5020 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5021 | if (count == 0) { |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
| 5025 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5026 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5027 | size_t result = 0; |
| 5028 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5029 | NSNumber *aKey; |
| 5030 | while ((aKey = [keys nextObject])) { |
| 5031 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5032 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 5033 | msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType); |
| 5034 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5035 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5036 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 5037 | result += tagSize * count; |
| 5038 | return result; |
| 5039 | } |
| 5040 | |
| 5041 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 5042 | asField:(GPBFieldDescriptor *)field { |
| 5043 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5044 | GPBDataType keyDataType = field.mapKeyDataType; |
| 5045 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5046 | NSDictionary *internal = _dictionary; |
| 5047 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5048 | NSNumber *aKey; |
| 5049 | while ((aKey = [keys nextObject])) { |
| 5050 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5051 | [outputStream writeInt32NoTag:tag]; |
| 5052 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5053 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 5054 | uint32_t unwrappedValue = [aValue unsignedIntValue]; |
| 5055 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5056 | msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5057 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 5058 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5059 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5060 | WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 5061 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 5065 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 5066 | [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)]; |
| 5067 | } |
| 5068 | |
| 5069 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5070 | [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5071 | #pragma unused(stop) |
| 5072 | block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]); |
| 5073 | }]; |
| 5074 | } |
| 5075 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5076 | - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5077 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 5078 | if (wrapped && value) { |
| 5079 | *value = [wrapped unsignedIntValue]; |
| 5080 | } |
| 5081 | return (wrapped != NULL); |
| 5082 | } |
| 5083 | |
| 5084 | - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary { |
| 5085 | if (otherDictionary) { |
| 5086 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 5087 | if (_autocreator) { |
| 5088 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5089 | } |
| 5090 | } |
| 5091 | } |
| 5092 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5093 | - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5094 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 5095 | if (_autocreator) { |
| 5096 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5097 | } |
| 5098 | } |
| 5099 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5100 | - (void)removeUInt32ForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5101 | [_dictionary removeObjectForKey:@(aKey)]; |
| 5102 | } |
| 5103 | |
| 5104 | - (void)removeAll { |
| 5105 | [_dictionary removeAllObjects]; |
| 5106 | } |
| 5107 | |
| 5108 | @end |
| 5109 | |
| 5110 | #pragma mark - UInt64 -> Int32 |
| 5111 | |
| 5112 | @implementation GPBUInt64Int32Dictionary { |
| 5113 | @package |
| 5114 | NSMutableDictionary *_dictionary; |
| 5115 | } |
| 5116 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5117 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5118 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5119 | } |
| 5120 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5121 | - (instancetype)initWithInt32s:(const int32_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5122 | forKeys:(const uint64_t [])keys |
| 5123 | count:(NSUInteger)count { |
| 5124 | self = [super init]; |
| 5125 | if (self) { |
| 5126 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 5127 | if (count && values && keys) { |
| 5128 | for (NSUInteger i = 0; i < count; ++i) { |
| 5129 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 5130 | } |
| 5131 | } |
| 5132 | } |
| 5133 | return self; |
| 5134 | } |
| 5135 | |
| 5136 | - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5137 | self = [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5138 | if (self) { |
| 5139 | if (dictionary) { |
| 5140 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 5141 | } |
| 5142 | } |
| 5143 | return self; |
| 5144 | } |
| 5145 | |
| 5146 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 5147 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5148 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | - (void)dealloc { |
| 5152 | NSAssert(!_autocreator, |
| 5153 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 5154 | [self class], _autocreator); |
| 5155 | [_dictionary release]; |
| 5156 | [super dealloc]; |
| 5157 | } |
| 5158 | |
| 5159 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 5160 | return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 5161 | } |
| 5162 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5163 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5164 | if (self == other) { |
| 5165 | return YES; |
| 5166 | } |
| 5167 | if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) { |
| 5168 | return NO; |
| 5169 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5170 | GPBUInt64Int32Dictionary *otherDictionary = other; |
| 5171 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | - (NSUInteger)hash { |
| 5175 | return _dictionary.count; |
| 5176 | } |
| 5177 | |
| 5178 | - (NSString *)description { |
| 5179 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 5180 | } |
| 5181 | |
| 5182 | - (NSUInteger)count { |
| 5183 | return _dictionary.count; |
| 5184 | } |
| 5185 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5186 | - (void)enumerateKeysAndInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5187 | (void (^)(uint64_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5188 | BOOL stop = NO; |
| 5189 | NSDictionary *internal = _dictionary; |
| 5190 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5191 | NSNumber *aKey; |
| 5192 | while ((aKey = [keys nextObject])) { |
| 5193 | NSNumber *aValue = internal[aKey]; |
| 5194 | block([aKey unsignedLongLongValue], [aValue intValue], &stop); |
| 5195 | if (stop) { |
| 5196 | break; |
| 5197 | } |
| 5198 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5202 | NSDictionary *internal = _dictionary; |
| 5203 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5204 | if (count == 0) { |
| 5205 | return 0; |
| 5206 | } |
| 5207 | |
| 5208 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5209 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5210 | size_t result = 0; |
| 5211 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5212 | NSNumber *aKey; |
| 5213 | while ((aKey = [keys nextObject])) { |
| 5214 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5215 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 5216 | msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 5217 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5218 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5219 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 5220 | result += tagSize * count; |
| 5221 | return result; |
| 5222 | } |
| 5223 | |
| 5224 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 5225 | asField:(GPBFieldDescriptor *)field { |
| 5226 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5227 | GPBDataType keyDataType = field.mapKeyDataType; |
| 5228 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5229 | NSDictionary *internal = _dictionary; |
| 5230 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5231 | NSNumber *aKey; |
| 5232 | while ((aKey = [keys nextObject])) { |
| 5233 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5234 | [outputStream writeInt32NoTag:tag]; |
| 5235 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5236 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 5237 | int32_t unwrappedValue = [aValue intValue]; |
| 5238 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5239 | msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5240 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 5241 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5242 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5243 | WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 5244 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 5248 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 5249 | [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)]; |
| 5250 | } |
| 5251 | |
| 5252 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5253 | [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5254 | #pragma unused(stop) |
| 5255 | block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]); |
| 5256 | }]; |
| 5257 | } |
| 5258 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5259 | - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5260 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 5261 | if (wrapped && value) { |
| 5262 | *value = [wrapped intValue]; |
| 5263 | } |
| 5264 | return (wrapped != NULL); |
| 5265 | } |
| 5266 | |
| 5267 | - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary { |
| 5268 | if (otherDictionary) { |
| 5269 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 5270 | if (_autocreator) { |
| 5271 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5272 | } |
| 5273 | } |
| 5274 | } |
| 5275 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5276 | - (void)setInt32:(int32_t)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5277 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 5278 | if (_autocreator) { |
| 5279 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5280 | } |
| 5281 | } |
| 5282 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5283 | - (void)removeInt32ForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5284 | [_dictionary removeObjectForKey:@(aKey)]; |
| 5285 | } |
| 5286 | |
| 5287 | - (void)removeAll { |
| 5288 | [_dictionary removeAllObjects]; |
| 5289 | } |
| 5290 | |
| 5291 | @end |
| 5292 | |
| 5293 | #pragma mark - UInt64 -> UInt64 |
| 5294 | |
| 5295 | @implementation GPBUInt64UInt64Dictionary { |
| 5296 | @package |
| 5297 | NSMutableDictionary *_dictionary; |
| 5298 | } |
| 5299 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5300 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5301 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5302 | } |
| 5303 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5304 | - (instancetype)initWithUInt64s:(const uint64_t [])values |
| 5305 | forKeys:(const uint64_t [])keys |
| 5306 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5307 | self = [super init]; |
| 5308 | if (self) { |
| 5309 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 5310 | if (count && values && keys) { |
| 5311 | for (NSUInteger i = 0; i < count; ++i) { |
| 5312 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 5313 | } |
| 5314 | } |
| 5315 | } |
| 5316 | return self; |
| 5317 | } |
| 5318 | |
| 5319 | - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5320 | self = [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5321 | if (self) { |
| 5322 | if (dictionary) { |
| 5323 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 5324 | } |
| 5325 | } |
| 5326 | return self; |
| 5327 | } |
| 5328 | |
| 5329 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 5330 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5331 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | - (void)dealloc { |
| 5335 | NSAssert(!_autocreator, |
| 5336 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 5337 | [self class], _autocreator); |
| 5338 | [_dictionary release]; |
| 5339 | [super dealloc]; |
| 5340 | } |
| 5341 | |
| 5342 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 5343 | return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 5344 | } |
| 5345 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5346 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5347 | if (self == other) { |
| 5348 | return YES; |
| 5349 | } |
| 5350 | if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) { |
| 5351 | return NO; |
| 5352 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5353 | GPBUInt64UInt64Dictionary *otherDictionary = other; |
| 5354 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | - (NSUInteger)hash { |
| 5358 | return _dictionary.count; |
| 5359 | } |
| 5360 | |
| 5361 | - (NSString *)description { |
| 5362 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 5363 | } |
| 5364 | |
| 5365 | - (NSUInteger)count { |
| 5366 | return _dictionary.count; |
| 5367 | } |
| 5368 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5369 | - (void)enumerateKeysAndUInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5370 | (void (^)(uint64_t key, uint64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5371 | BOOL stop = NO; |
| 5372 | NSDictionary *internal = _dictionary; |
| 5373 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5374 | NSNumber *aKey; |
| 5375 | while ((aKey = [keys nextObject])) { |
| 5376 | NSNumber *aValue = internal[aKey]; |
| 5377 | block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], &stop); |
| 5378 | if (stop) { |
| 5379 | break; |
| 5380 | } |
| 5381 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5385 | NSDictionary *internal = _dictionary; |
| 5386 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5387 | if (count == 0) { |
| 5388 | return 0; |
| 5389 | } |
| 5390 | |
| 5391 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5392 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5393 | size_t result = 0; |
| 5394 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5395 | NSNumber *aKey; |
| 5396 | while ((aKey = [keys nextObject])) { |
| 5397 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5398 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 5399 | msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType); |
| 5400 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5401 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5402 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 5403 | result += tagSize * count; |
| 5404 | return result; |
| 5405 | } |
| 5406 | |
| 5407 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 5408 | asField:(GPBFieldDescriptor *)field { |
| 5409 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5410 | GPBDataType keyDataType = field.mapKeyDataType; |
| 5411 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5412 | NSDictionary *internal = _dictionary; |
| 5413 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5414 | NSNumber *aKey; |
| 5415 | while ((aKey = [keys nextObject])) { |
| 5416 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5417 | [outputStream writeInt32NoTag:tag]; |
| 5418 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5419 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 5420 | uint64_t unwrappedValue = [aValue unsignedLongLongValue]; |
| 5421 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5422 | msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5423 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 5424 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5425 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5426 | WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 5427 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 5431 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 5432 | [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)]; |
| 5433 | } |
| 5434 | |
| 5435 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5436 | [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5437 | #pragma unused(stop) |
| 5438 | block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]); |
| 5439 | }]; |
| 5440 | } |
| 5441 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5442 | - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5443 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 5444 | if (wrapped && value) { |
| 5445 | *value = [wrapped unsignedLongLongValue]; |
| 5446 | } |
| 5447 | return (wrapped != NULL); |
| 5448 | } |
| 5449 | |
| 5450 | - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary { |
| 5451 | if (otherDictionary) { |
| 5452 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 5453 | if (_autocreator) { |
| 5454 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5455 | } |
| 5456 | } |
| 5457 | } |
| 5458 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5459 | - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5460 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 5461 | if (_autocreator) { |
| 5462 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5463 | } |
| 5464 | } |
| 5465 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5466 | - (void)removeUInt64ForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5467 | [_dictionary removeObjectForKey:@(aKey)]; |
| 5468 | } |
| 5469 | |
| 5470 | - (void)removeAll { |
| 5471 | [_dictionary removeAllObjects]; |
| 5472 | } |
| 5473 | |
| 5474 | @end |
| 5475 | |
| 5476 | #pragma mark - UInt64 -> Int64 |
| 5477 | |
| 5478 | @implementation GPBUInt64Int64Dictionary { |
| 5479 | @package |
| 5480 | NSMutableDictionary *_dictionary; |
| 5481 | } |
| 5482 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5483 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5484 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5485 | } |
| 5486 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5487 | - (instancetype)initWithInt64s:(const int64_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5488 | forKeys:(const uint64_t [])keys |
| 5489 | count:(NSUInteger)count { |
| 5490 | self = [super init]; |
| 5491 | if (self) { |
| 5492 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 5493 | if (count && values && keys) { |
| 5494 | for (NSUInteger i = 0; i < count; ++i) { |
| 5495 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 5496 | } |
| 5497 | } |
| 5498 | } |
| 5499 | return self; |
| 5500 | } |
| 5501 | |
| 5502 | - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5503 | self = [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5504 | if (self) { |
| 5505 | if (dictionary) { |
| 5506 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 5507 | } |
| 5508 | } |
| 5509 | return self; |
| 5510 | } |
| 5511 | |
| 5512 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 5513 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5514 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | - (void)dealloc { |
| 5518 | NSAssert(!_autocreator, |
| 5519 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 5520 | [self class], _autocreator); |
| 5521 | [_dictionary release]; |
| 5522 | [super dealloc]; |
| 5523 | } |
| 5524 | |
| 5525 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 5526 | return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 5527 | } |
| 5528 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5529 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5530 | if (self == other) { |
| 5531 | return YES; |
| 5532 | } |
| 5533 | if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) { |
| 5534 | return NO; |
| 5535 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5536 | GPBUInt64Int64Dictionary *otherDictionary = other; |
| 5537 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | - (NSUInteger)hash { |
| 5541 | return _dictionary.count; |
| 5542 | } |
| 5543 | |
| 5544 | - (NSString *)description { |
| 5545 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 5546 | } |
| 5547 | |
| 5548 | - (NSUInteger)count { |
| 5549 | return _dictionary.count; |
| 5550 | } |
| 5551 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5552 | - (void)enumerateKeysAndInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5553 | (void (^)(uint64_t key, int64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5554 | BOOL stop = NO; |
| 5555 | NSDictionary *internal = _dictionary; |
| 5556 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5557 | NSNumber *aKey; |
| 5558 | while ((aKey = [keys nextObject])) { |
| 5559 | NSNumber *aValue = internal[aKey]; |
| 5560 | block([aKey unsignedLongLongValue], [aValue longLongValue], &stop); |
| 5561 | if (stop) { |
| 5562 | break; |
| 5563 | } |
| 5564 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5568 | NSDictionary *internal = _dictionary; |
| 5569 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5570 | if (count == 0) { |
| 5571 | return 0; |
| 5572 | } |
| 5573 | |
| 5574 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5575 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5576 | size_t result = 0; |
| 5577 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5578 | NSNumber *aKey; |
| 5579 | while ((aKey = [keys nextObject])) { |
| 5580 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5581 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 5582 | msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType); |
| 5583 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5584 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5585 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 5586 | result += tagSize * count; |
| 5587 | return result; |
| 5588 | } |
| 5589 | |
| 5590 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 5591 | asField:(GPBFieldDescriptor *)field { |
| 5592 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5593 | GPBDataType keyDataType = field.mapKeyDataType; |
| 5594 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5595 | NSDictionary *internal = _dictionary; |
| 5596 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5597 | NSNumber *aKey; |
| 5598 | while ((aKey = [keys nextObject])) { |
| 5599 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5600 | [outputStream writeInt32NoTag:tag]; |
| 5601 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5602 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 5603 | int64_t unwrappedValue = [aValue longLongValue]; |
| 5604 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5605 | msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5606 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 5607 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5608 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5609 | WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 5610 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 5614 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 5615 | [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)]; |
| 5616 | } |
| 5617 | |
| 5618 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5619 | [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5620 | #pragma unused(stop) |
| 5621 | block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]); |
| 5622 | }]; |
| 5623 | } |
| 5624 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5625 | - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5626 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 5627 | if (wrapped && value) { |
| 5628 | *value = [wrapped longLongValue]; |
| 5629 | } |
| 5630 | return (wrapped != NULL); |
| 5631 | } |
| 5632 | |
| 5633 | - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary { |
| 5634 | if (otherDictionary) { |
| 5635 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 5636 | if (_autocreator) { |
| 5637 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5638 | } |
| 5639 | } |
| 5640 | } |
| 5641 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5642 | - (void)setInt64:(int64_t)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5643 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 5644 | if (_autocreator) { |
| 5645 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5646 | } |
| 5647 | } |
| 5648 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5649 | - (void)removeInt64ForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5650 | [_dictionary removeObjectForKey:@(aKey)]; |
| 5651 | } |
| 5652 | |
| 5653 | - (void)removeAll { |
| 5654 | [_dictionary removeAllObjects]; |
| 5655 | } |
| 5656 | |
| 5657 | @end |
| 5658 | |
| 5659 | #pragma mark - UInt64 -> Bool |
| 5660 | |
| 5661 | @implementation GPBUInt64BoolDictionary { |
| 5662 | @package |
| 5663 | NSMutableDictionary *_dictionary; |
| 5664 | } |
| 5665 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5666 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5667 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5668 | } |
| 5669 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5670 | - (instancetype)initWithBools:(const BOOL [])values |
| 5671 | forKeys:(const uint64_t [])keys |
| 5672 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5673 | self = [super init]; |
| 5674 | if (self) { |
| 5675 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 5676 | if (count && values && keys) { |
| 5677 | for (NSUInteger i = 0; i < count; ++i) { |
| 5678 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 5679 | } |
| 5680 | } |
| 5681 | } |
| 5682 | return self; |
| 5683 | } |
| 5684 | |
| 5685 | - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5686 | self = [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5687 | if (self) { |
| 5688 | if (dictionary) { |
| 5689 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 5690 | } |
| 5691 | } |
| 5692 | return self; |
| 5693 | } |
| 5694 | |
| 5695 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 5696 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5697 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | - (void)dealloc { |
| 5701 | NSAssert(!_autocreator, |
| 5702 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 5703 | [self class], _autocreator); |
| 5704 | [_dictionary release]; |
| 5705 | [super dealloc]; |
| 5706 | } |
| 5707 | |
| 5708 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 5709 | return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self]; |
| 5710 | } |
| 5711 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5712 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5713 | if (self == other) { |
| 5714 | return YES; |
| 5715 | } |
| 5716 | if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) { |
| 5717 | return NO; |
| 5718 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5719 | GPBUInt64BoolDictionary *otherDictionary = other; |
| 5720 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | - (NSUInteger)hash { |
| 5724 | return _dictionary.count; |
| 5725 | } |
| 5726 | |
| 5727 | - (NSString *)description { |
| 5728 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 5729 | } |
| 5730 | |
| 5731 | - (NSUInteger)count { |
| 5732 | return _dictionary.count; |
| 5733 | } |
| 5734 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5735 | - (void)enumerateKeysAndBoolsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5736 | (void (^)(uint64_t key, BOOL value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5737 | BOOL stop = NO; |
| 5738 | NSDictionary *internal = _dictionary; |
| 5739 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5740 | NSNumber *aKey; |
| 5741 | while ((aKey = [keys nextObject])) { |
| 5742 | NSNumber *aValue = internal[aKey]; |
| 5743 | block([aKey unsignedLongLongValue], [aValue boolValue], &stop); |
| 5744 | if (stop) { |
| 5745 | break; |
| 5746 | } |
| 5747 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5751 | NSDictionary *internal = _dictionary; |
| 5752 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5753 | if (count == 0) { |
| 5754 | return 0; |
| 5755 | } |
| 5756 | |
| 5757 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5758 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5759 | size_t result = 0; |
| 5760 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5761 | NSNumber *aKey; |
| 5762 | while ((aKey = [keys nextObject])) { |
| 5763 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5764 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 5765 | msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType); |
| 5766 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5767 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5768 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 5769 | result += tagSize * count; |
| 5770 | return result; |
| 5771 | } |
| 5772 | |
| 5773 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 5774 | asField:(GPBFieldDescriptor *)field { |
| 5775 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5776 | GPBDataType keyDataType = field.mapKeyDataType; |
| 5777 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5778 | NSDictionary *internal = _dictionary; |
| 5779 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5780 | NSNumber *aKey; |
| 5781 | while ((aKey = [keys nextObject])) { |
| 5782 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5783 | [outputStream writeInt32NoTag:tag]; |
| 5784 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5785 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 5786 | BOOL unwrappedValue = [aValue boolValue]; |
| 5787 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5788 | msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5789 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 5790 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5791 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5792 | WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 5793 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 5797 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 5798 | [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)]; |
| 5799 | } |
| 5800 | |
| 5801 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5802 | [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5803 | #pragma unused(stop) |
| 5804 | block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false")); |
| 5805 | }]; |
| 5806 | } |
| 5807 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5808 | - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5809 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 5810 | if (wrapped && value) { |
| 5811 | *value = [wrapped boolValue]; |
| 5812 | } |
| 5813 | return (wrapped != NULL); |
| 5814 | } |
| 5815 | |
| 5816 | - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary { |
| 5817 | if (otherDictionary) { |
| 5818 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 5819 | if (_autocreator) { |
| 5820 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5821 | } |
| 5822 | } |
| 5823 | } |
| 5824 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5825 | - (void)setBool:(BOOL)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5826 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 5827 | if (_autocreator) { |
| 5828 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 5829 | } |
| 5830 | } |
| 5831 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5832 | - (void)removeBoolForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5833 | [_dictionary removeObjectForKey:@(aKey)]; |
| 5834 | } |
| 5835 | |
| 5836 | - (void)removeAll { |
| 5837 | [_dictionary removeAllObjects]; |
| 5838 | } |
| 5839 | |
| 5840 | @end |
| 5841 | |
| 5842 | #pragma mark - UInt64 -> Float |
| 5843 | |
| 5844 | @implementation GPBUInt64FloatDictionary { |
| 5845 | @package |
| 5846 | NSMutableDictionary *_dictionary; |
| 5847 | } |
| 5848 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5849 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5850 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5851 | } |
| 5852 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5853 | - (instancetype)initWithFloats:(const float [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5854 | forKeys:(const uint64_t [])keys |
| 5855 | count:(NSUInteger)count { |
| 5856 | self = [super init]; |
| 5857 | if (self) { |
| 5858 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 5859 | if (count && values && keys) { |
| 5860 | for (NSUInteger i = 0; i < count; ++i) { |
| 5861 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 5862 | } |
| 5863 | } |
| 5864 | } |
| 5865 | return self; |
| 5866 | } |
| 5867 | |
| 5868 | - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5869 | self = [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5870 | if (self) { |
| 5871 | if (dictionary) { |
| 5872 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 5873 | } |
| 5874 | } |
| 5875 | return self; |
| 5876 | } |
| 5877 | |
| 5878 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 5879 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5880 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5881 | } |
| 5882 | |
| 5883 | - (void)dealloc { |
| 5884 | NSAssert(!_autocreator, |
| 5885 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 5886 | [self class], _autocreator); |
| 5887 | [_dictionary release]; |
| 5888 | [super dealloc]; |
| 5889 | } |
| 5890 | |
| 5891 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 5892 | return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self]; |
| 5893 | } |
| 5894 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5895 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5896 | if (self == other) { |
| 5897 | return YES; |
| 5898 | } |
| 5899 | if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) { |
| 5900 | return NO; |
| 5901 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5902 | GPBUInt64FloatDictionary *otherDictionary = other; |
| 5903 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | - (NSUInteger)hash { |
| 5907 | return _dictionary.count; |
| 5908 | } |
| 5909 | |
| 5910 | - (NSString *)description { |
| 5911 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 5912 | } |
| 5913 | |
| 5914 | - (NSUInteger)count { |
| 5915 | return _dictionary.count; |
| 5916 | } |
| 5917 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5918 | - (void)enumerateKeysAndFloatsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5919 | (void (^)(uint64_t key, float value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5920 | BOOL stop = NO; |
| 5921 | NSDictionary *internal = _dictionary; |
| 5922 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5923 | NSNumber *aKey; |
| 5924 | while ((aKey = [keys nextObject])) { |
| 5925 | NSNumber *aValue = internal[aKey]; |
| 5926 | block([aKey unsignedLongLongValue], [aValue floatValue], &stop); |
| 5927 | if (stop) { |
| 5928 | break; |
| 5929 | } |
| 5930 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5931 | } |
| 5932 | |
| 5933 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5934 | NSDictionary *internal = _dictionary; |
| 5935 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5936 | if (count == 0) { |
| 5937 | return 0; |
| 5938 | } |
| 5939 | |
| 5940 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5941 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5942 | size_t result = 0; |
| 5943 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5944 | NSNumber *aKey; |
| 5945 | while ((aKey = [keys nextObject])) { |
| 5946 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5947 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 5948 | msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType); |
| 5949 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5950 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5951 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 5952 | result += tagSize * count; |
| 5953 | return result; |
| 5954 | } |
| 5955 | |
| 5956 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 5957 | asField:(GPBFieldDescriptor *)field { |
| 5958 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 5959 | GPBDataType keyDataType = field.mapKeyDataType; |
| 5960 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5961 | NSDictionary *internal = _dictionary; |
| 5962 | NSEnumerator *keys = [internal keyEnumerator]; |
| 5963 | NSNumber *aKey; |
| 5964 | while ((aKey = [keys nextObject])) { |
| 5965 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5966 | [outputStream writeInt32NoTag:tag]; |
| 5967 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5968 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 5969 | float unwrappedValue = [aValue floatValue]; |
| 5970 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5971 | msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5972 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 5973 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5974 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 5975 | WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 5976 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 5980 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 5981 | [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)]; |
| 5982 | } |
| 5983 | |
| 5984 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5985 | [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5986 | #pragma unused(stop) |
| 5987 | block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); |
| 5988 | }]; |
| 5989 | } |
| 5990 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 5991 | - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 5992 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 5993 | if (wrapped && value) { |
| 5994 | *value = [wrapped floatValue]; |
| 5995 | } |
| 5996 | return (wrapped != NULL); |
| 5997 | } |
| 5998 | |
| 5999 | - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary { |
| 6000 | if (otherDictionary) { |
| 6001 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 6002 | if (_autocreator) { |
| 6003 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6004 | } |
| 6005 | } |
| 6006 | } |
| 6007 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6008 | - (void)setFloat:(float)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6009 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 6010 | if (_autocreator) { |
| 6011 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6012 | } |
| 6013 | } |
| 6014 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6015 | - (void)removeFloatForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6016 | [_dictionary removeObjectForKey:@(aKey)]; |
| 6017 | } |
| 6018 | |
| 6019 | - (void)removeAll { |
| 6020 | [_dictionary removeAllObjects]; |
| 6021 | } |
| 6022 | |
| 6023 | @end |
| 6024 | |
| 6025 | #pragma mark - UInt64 -> Double |
| 6026 | |
| 6027 | @implementation GPBUInt64DoubleDictionary { |
| 6028 | @package |
| 6029 | NSMutableDictionary *_dictionary; |
| 6030 | } |
| 6031 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6032 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6033 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6034 | } |
| 6035 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6036 | - (instancetype)initWithDoubles:(const double [])values |
| 6037 | forKeys:(const uint64_t [])keys |
| 6038 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6039 | self = [super init]; |
| 6040 | if (self) { |
| 6041 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 6042 | if (count && values && keys) { |
| 6043 | for (NSUInteger i = 0; i < count; ++i) { |
| 6044 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 6045 | } |
| 6046 | } |
| 6047 | } |
| 6048 | return self; |
| 6049 | } |
| 6050 | |
| 6051 | - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6052 | self = [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6053 | if (self) { |
| 6054 | if (dictionary) { |
| 6055 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 6056 | } |
| 6057 | } |
| 6058 | return self; |
| 6059 | } |
| 6060 | |
| 6061 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 6062 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6063 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | - (void)dealloc { |
| 6067 | NSAssert(!_autocreator, |
| 6068 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 6069 | [self class], _autocreator); |
| 6070 | [_dictionary release]; |
| 6071 | [super dealloc]; |
| 6072 | } |
| 6073 | |
| 6074 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 6075 | return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self]; |
| 6076 | } |
| 6077 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6078 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6079 | if (self == other) { |
| 6080 | return YES; |
| 6081 | } |
| 6082 | if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) { |
| 6083 | return NO; |
| 6084 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6085 | GPBUInt64DoubleDictionary *otherDictionary = other; |
| 6086 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | - (NSUInteger)hash { |
| 6090 | return _dictionary.count; |
| 6091 | } |
| 6092 | |
| 6093 | - (NSString *)description { |
| 6094 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 6095 | } |
| 6096 | |
| 6097 | - (NSUInteger)count { |
| 6098 | return _dictionary.count; |
| 6099 | } |
| 6100 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6101 | - (void)enumerateKeysAndDoublesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6102 | (void (^)(uint64_t key, double value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6103 | BOOL stop = NO; |
| 6104 | NSDictionary *internal = _dictionary; |
| 6105 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6106 | NSNumber *aKey; |
| 6107 | while ((aKey = [keys nextObject])) { |
| 6108 | NSNumber *aValue = internal[aKey]; |
| 6109 | block([aKey unsignedLongLongValue], [aValue doubleValue], &stop); |
| 6110 | if (stop) { |
| 6111 | break; |
| 6112 | } |
| 6113 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6114 | } |
| 6115 | |
| 6116 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6117 | NSDictionary *internal = _dictionary; |
| 6118 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6119 | if (count == 0) { |
| 6120 | return 0; |
| 6121 | } |
| 6122 | |
| 6123 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6124 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6125 | size_t result = 0; |
| 6126 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6127 | NSNumber *aKey; |
| 6128 | while ((aKey = [keys nextObject])) { |
| 6129 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6130 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 6131 | msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType); |
| 6132 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6133 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6134 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 6135 | result += tagSize * count; |
| 6136 | return result; |
| 6137 | } |
| 6138 | |
| 6139 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 6140 | asField:(GPBFieldDescriptor *)field { |
| 6141 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6142 | GPBDataType keyDataType = field.mapKeyDataType; |
| 6143 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6144 | NSDictionary *internal = _dictionary; |
| 6145 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6146 | NSNumber *aKey; |
| 6147 | while ((aKey = [keys nextObject])) { |
| 6148 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6149 | [outputStream writeInt32NoTag:tag]; |
| 6150 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6151 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 6152 | double unwrappedValue = [aValue doubleValue]; |
| 6153 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6154 | msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6155 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 6156 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6157 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6158 | WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 6159 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 6163 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 6164 | [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)]; |
| 6165 | } |
| 6166 | |
| 6167 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6168 | [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6169 | #pragma unused(stop) |
| 6170 | block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); |
| 6171 | }]; |
| 6172 | } |
| 6173 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6174 | - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6175 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 6176 | if (wrapped && value) { |
| 6177 | *value = [wrapped doubleValue]; |
| 6178 | } |
| 6179 | return (wrapped != NULL); |
| 6180 | } |
| 6181 | |
| 6182 | - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary { |
| 6183 | if (otherDictionary) { |
| 6184 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 6185 | if (_autocreator) { |
| 6186 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6187 | } |
| 6188 | } |
| 6189 | } |
| 6190 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6191 | - (void)setDouble:(double)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6192 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 6193 | if (_autocreator) { |
| 6194 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6195 | } |
| 6196 | } |
| 6197 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6198 | - (void)removeDoubleForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6199 | [_dictionary removeObjectForKey:@(aKey)]; |
| 6200 | } |
| 6201 | |
| 6202 | - (void)removeAll { |
| 6203 | [_dictionary removeAllObjects]; |
| 6204 | } |
| 6205 | |
| 6206 | @end |
| 6207 | |
| 6208 | #pragma mark - UInt64 -> Enum |
| 6209 | |
| 6210 | @implementation GPBUInt64EnumDictionary { |
| 6211 | @package |
| 6212 | NSMutableDictionary *_dictionary; |
| 6213 | GPBEnumValidationFunc _validationFunc; |
| 6214 | } |
| 6215 | |
| 6216 | @synthesize validationFunc = _validationFunc; |
| 6217 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6218 | - (instancetype)init { |
| 6219 | return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 6220 | } |
| 6221 | |
| 6222 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 6223 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 6224 | } |
| 6225 | |
| 6226 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 6227 | rawValues:(const int32_t [])rawValues |
| 6228 | forKeys:(const uint64_t [])keys |
| 6229 | count:(NSUInteger)count { |
| 6230 | self = [super init]; |
| 6231 | if (self) { |
| 6232 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 6233 | _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 6234 | if (count && rawValues && keys) { |
| 6235 | for (NSUInteger i = 0; i < count; ++i) { |
| 6236 | [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])]; |
| 6237 | } |
| 6238 | } |
| 6239 | } |
| 6240 | return self; |
| 6241 | } |
| 6242 | |
| 6243 | - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary { |
| 6244 | self = [self initWithValidationFunction:dictionary.validationFunc |
| 6245 | rawValues:NULL |
| 6246 | forKeys:NULL |
| 6247 | count:0]; |
| 6248 | if (self) { |
| 6249 | if (dictionary) { |
| 6250 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 6251 | } |
| 6252 | } |
| 6253 | return self; |
| 6254 | } |
| 6255 | |
| 6256 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 6257 | capacity:(NSUInteger)numItems { |
| 6258 | #pragma unused(numItems) |
| 6259 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 6260 | } |
| 6261 | |
| 6262 | - (void)dealloc { |
| 6263 | NSAssert(!_autocreator, |
| 6264 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 6265 | [self class], _autocreator); |
| 6266 | [_dictionary release]; |
| 6267 | [super dealloc]; |
| 6268 | } |
| 6269 | |
| 6270 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 6271 | return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self]; |
| 6272 | } |
| 6273 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6274 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6275 | if (self == other) { |
| 6276 | return YES; |
| 6277 | } |
| 6278 | if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) { |
| 6279 | return NO; |
| 6280 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6281 | GPBUInt64EnumDictionary *otherDictionary = other; |
| 6282 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6283 | } |
| 6284 | |
| 6285 | - (NSUInteger)hash { |
| 6286 | return _dictionary.count; |
| 6287 | } |
| 6288 | |
| 6289 | - (NSString *)description { |
| 6290 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 6291 | } |
| 6292 | |
| 6293 | - (NSUInteger)count { |
| 6294 | return _dictionary.count; |
| 6295 | } |
| 6296 | |
| 6297 | - (void)enumerateKeysAndRawValuesUsingBlock: |
| 6298 | (void (^)(uint64_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6299 | BOOL stop = NO; |
| 6300 | NSDictionary *internal = _dictionary; |
| 6301 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6302 | NSNumber *aKey; |
| 6303 | while ((aKey = [keys nextObject])) { |
| 6304 | NSNumber *aValue = internal[aKey]; |
| 6305 | block([aKey unsignedLongLongValue], [aValue intValue], &stop); |
| 6306 | if (stop) { |
| 6307 | break; |
| 6308 | } |
| 6309 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6313 | NSDictionary *internal = _dictionary; |
| 6314 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6315 | if (count == 0) { |
| 6316 | return 0; |
| 6317 | } |
| 6318 | |
| 6319 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6320 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6321 | size_t result = 0; |
| 6322 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6323 | NSNumber *aKey; |
| 6324 | while ((aKey = [keys nextObject])) { |
| 6325 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6326 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 6327 | msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 6328 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6329 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6330 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 6331 | result += tagSize * count; |
| 6332 | return result; |
| 6333 | } |
| 6334 | |
| 6335 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 6336 | asField:(GPBFieldDescriptor *)field { |
| 6337 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6338 | GPBDataType keyDataType = field.mapKeyDataType; |
| 6339 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6340 | NSDictionary *internal = _dictionary; |
| 6341 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6342 | NSNumber *aKey; |
| 6343 | while ((aKey = [keys nextObject])) { |
| 6344 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6345 | [outputStream writeInt32NoTag:tag]; |
| 6346 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6347 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 6348 | int32_t unwrappedValue = [aValue intValue]; |
| 6349 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6350 | msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6351 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 6352 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6353 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6354 | WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 6355 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | - (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 6359 | forKey:(GPBGenericValue *)key |
| 6360 | keyDataType:(GPBDataType)keyDataType { |
| 6361 | size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType); |
| 6362 | msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 6363 | NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 6364 | GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 6365 | WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType); |
| 6366 | WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 6367 | [outputStream release]; |
| 6368 | return data; |
| 6369 | } |
| 6370 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 6371 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 6372 | [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)]; |
| 6373 | } |
| 6374 | |
| 6375 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 6376 | [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) { |
| 6377 | #pragma unused(stop) |
| 6378 | block([NSString stringWithFormat:@"%llu", key], @(value)); |
| 6379 | }]; |
| 6380 | } |
| 6381 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6382 | - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6383 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 6384 | if (wrapped && value) { |
| 6385 | int32_t result = [wrapped intValue]; |
| 6386 | if (!_validationFunc(result)) { |
| 6387 | result = kGPBUnrecognizedEnumeratorValue; |
| 6388 | } |
| 6389 | *value = result; |
| 6390 | } |
| 6391 | return (wrapped != NULL); |
| 6392 | } |
| 6393 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6394 | - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6395 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 6396 | if (wrapped && rawValue) { |
| 6397 | *rawValue = [wrapped intValue]; |
| 6398 | } |
| 6399 | return (wrapped != NULL); |
| 6400 | } |
| 6401 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6402 | - (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6403 | (void (^)(uint64_t key, int32_t value, BOOL *stop))block { |
| 6404 | GPBEnumValidationFunc func = _validationFunc; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6405 | BOOL stop = NO; |
| 6406 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 6407 | NSNumber *aKey; |
| 6408 | while ((aKey = [keys nextObject])) { |
| 6409 | NSNumber *aValue = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6410 | int32_t unwrapped = [aValue intValue]; |
| 6411 | if (!func(unwrapped)) { |
| 6412 | unwrapped = kGPBUnrecognizedEnumeratorValue; |
| 6413 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6414 | block([aKey unsignedLongLongValue], unwrapped, &stop); |
| 6415 | if (stop) { |
| 6416 | break; |
| 6417 | } |
| 6418 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6419 | } |
| 6420 | |
| 6421 | - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary { |
| 6422 | if (otherDictionary) { |
| 6423 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 6424 | if (_autocreator) { |
| 6425 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6426 | } |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | - (void)setRawValue:(int32_t)value forKey:(uint64_t)key { |
| 6431 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 6432 | if (_autocreator) { |
| 6433 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6434 | } |
| 6435 | } |
| 6436 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6437 | - (void)removeEnumForKey:(uint64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6438 | [_dictionary removeObjectForKey:@(aKey)]; |
| 6439 | } |
| 6440 | |
| 6441 | - (void)removeAll { |
| 6442 | [_dictionary removeAllObjects]; |
| 6443 | } |
| 6444 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6445 | - (void)setEnum:(int32_t)value forKey:(uint64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6446 | if (!_validationFunc(value)) { |
| 6447 | [NSException raise:NSInvalidArgumentException |
| 6448 | format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)", |
| 6449 | value]; |
| 6450 | } |
| 6451 | |
| 6452 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 6453 | if (_autocreator) { |
| 6454 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6455 | } |
| 6456 | } |
| 6457 | |
| 6458 | @end |
| 6459 | |
| 6460 | #pragma mark - UInt64 -> Object |
| 6461 | |
| 6462 | @implementation GPBUInt64ObjectDictionary { |
| 6463 | @package |
| 6464 | NSMutableDictionary *_dictionary; |
| 6465 | } |
| 6466 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6467 | - (instancetype)init { |
| 6468 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 6469 | } |
| 6470 | |
| 6471 | - (instancetype)initWithObjects:(const id [])objects |
| 6472 | forKeys:(const uint64_t [])keys |
| 6473 | count:(NSUInteger)count { |
| 6474 | self = [super init]; |
| 6475 | if (self) { |
| 6476 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 6477 | if (count && objects && keys) { |
| 6478 | for (NSUInteger i = 0; i < count; ++i) { |
| 6479 | if (!objects[i]) { |
| 6480 | [NSException raise:NSInvalidArgumentException |
| 6481 | format:@"Attempting to add nil object to a Dictionary"]; |
| 6482 | } |
| 6483 | [_dictionary setObject:objects[i] forKey:@(keys[i])]; |
| 6484 | } |
| 6485 | } |
| 6486 | } |
| 6487 | return self; |
| 6488 | } |
| 6489 | |
| 6490 | - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary { |
| 6491 | self = [self initWithObjects:NULL forKeys:NULL count:0]; |
| 6492 | if (self) { |
| 6493 | if (dictionary) { |
| 6494 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 6495 | } |
| 6496 | } |
| 6497 | return self; |
| 6498 | } |
| 6499 | |
| 6500 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 6501 | #pragma unused(numItems) |
| 6502 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 6503 | } |
| 6504 | |
| 6505 | - (void)dealloc { |
| 6506 | NSAssert(!_autocreator, |
| 6507 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 6508 | [self class], _autocreator); |
| 6509 | [_dictionary release]; |
| 6510 | [super dealloc]; |
| 6511 | } |
| 6512 | |
| 6513 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 6514 | return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self]; |
| 6515 | } |
| 6516 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6517 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6518 | if (self == other) { |
| 6519 | return YES; |
| 6520 | } |
| 6521 | if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) { |
| 6522 | return NO; |
| 6523 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6524 | GPBUInt64ObjectDictionary *otherDictionary = other; |
| 6525 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | - (NSUInteger)hash { |
| 6529 | return _dictionary.count; |
| 6530 | } |
| 6531 | |
| 6532 | - (NSString *)description { |
| 6533 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 6534 | } |
| 6535 | |
| 6536 | - (NSUInteger)count { |
| 6537 | return _dictionary.count; |
| 6538 | } |
| 6539 | |
| 6540 | - (void)enumerateKeysAndObjectsUsingBlock: |
| 6541 | (void (^)(uint64_t key, id object, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6542 | BOOL stop = NO; |
| 6543 | NSDictionary *internal = _dictionary; |
| 6544 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6545 | NSNumber *aKey; |
| 6546 | while ((aKey = [keys nextObject])) { |
| 6547 | id aObject = internal[aKey]; |
| 6548 | block([aKey unsignedLongLongValue], aObject, &stop); |
| 6549 | if (stop) { |
| 6550 | break; |
| 6551 | } |
| 6552 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6553 | } |
| 6554 | |
| 6555 | - (BOOL)isInitialized { |
| 6556 | for (GPBMessage *msg in [_dictionary objectEnumerator]) { |
| 6557 | if (!msg.initialized) { |
| 6558 | return NO; |
| 6559 | } |
| 6560 | } |
| 6561 | return YES; |
| 6562 | } |
| 6563 | |
| 6564 | - (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 6565 | GPBUInt64ObjectDictionary *newDict = |
| 6566 | [[GPBUInt64ObjectDictionary alloc] init]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6567 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 6568 | id aKey; |
| 6569 | NSMutableDictionary *internalDict = newDict->_dictionary; |
| 6570 | while ((aKey = [keys nextObject])) { |
| 6571 | GPBMessage *msg = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6572 | GPBMessage *copiedMsg = [msg copyWithZone:zone]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6573 | [internalDict setObject:copiedMsg forKey:aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6574 | [copiedMsg release]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6575 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6576 | return newDict; |
| 6577 | } |
| 6578 | |
| 6579 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6580 | NSDictionary *internal = _dictionary; |
| 6581 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6582 | if (count == 0) { |
| 6583 | return 0; |
| 6584 | } |
| 6585 | |
| 6586 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6587 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6588 | size_t result = 0; |
| 6589 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6590 | NSNumber *aKey; |
| 6591 | while ((aKey = [keys nextObject])) { |
| 6592 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6593 | size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType); |
| 6594 | msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType); |
| 6595 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6596 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6597 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 6598 | result += tagSize * count; |
| 6599 | return result; |
| 6600 | } |
| 6601 | |
| 6602 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 6603 | asField:(GPBFieldDescriptor *)field { |
| 6604 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6605 | GPBDataType keyDataType = field.mapKeyDataType; |
| 6606 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6607 | NSDictionary *internal = _dictionary; |
| 6608 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6609 | NSNumber *aKey; |
| 6610 | while ((aKey = [keys nextObject])) { |
| 6611 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6612 | [outputStream writeInt32NoTag:tag]; |
| 6613 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6614 | uint64_t unwrappedKey = [aKey unsignedLongLongValue]; |
| 6615 | id unwrappedValue = aObject; |
| 6616 | size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6617 | msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6618 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 6619 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6620 | WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6621 | WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 6622 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 6626 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 6627 | [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)]; |
| 6628 | } |
| 6629 | |
| 6630 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 6631 | [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) { |
| 6632 | #pragma unused(stop) |
| 6633 | block([NSString stringWithFormat:@"%llu", key], object); |
| 6634 | }]; |
| 6635 | } |
| 6636 | |
| 6637 | - (id)objectForKey:(uint64_t)key { |
| 6638 | id result = [_dictionary objectForKey:@(key)]; |
| 6639 | return result; |
| 6640 | } |
| 6641 | |
| 6642 | - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary { |
| 6643 | if (otherDictionary) { |
| 6644 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 6645 | if (_autocreator) { |
| 6646 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6647 | } |
| 6648 | } |
| 6649 | } |
| 6650 | |
| 6651 | - (void)setObject:(id)object forKey:(uint64_t)key { |
| 6652 | if (!object) { |
| 6653 | [NSException raise:NSInvalidArgumentException |
| 6654 | format:@"Attempting to add nil object to a Dictionary"]; |
| 6655 | } |
| 6656 | [_dictionary setObject:object forKey:@(key)]; |
| 6657 | if (_autocreator) { |
| 6658 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6659 | } |
| 6660 | } |
| 6661 | |
| 6662 | - (void)removeObjectForKey:(uint64_t)aKey { |
| 6663 | [_dictionary removeObjectForKey:@(aKey)]; |
| 6664 | } |
| 6665 | |
| 6666 | - (void)removeAll { |
| 6667 | [_dictionary removeAllObjects]; |
| 6668 | } |
| 6669 | |
| 6670 | @end |
| 6671 | |
| 6672 | //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t) |
| 6673 | // This block of code is generated, do not edit it directly. |
| 6674 | |
| 6675 | #pragma mark - Int64 -> UInt32 |
| 6676 | |
| 6677 | @implementation GPBInt64UInt32Dictionary { |
| 6678 | @package |
| 6679 | NSMutableDictionary *_dictionary; |
| 6680 | } |
| 6681 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6682 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6683 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6684 | } |
| 6685 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6686 | - (instancetype)initWithUInt32s:(const uint32_t [])values |
| 6687 | forKeys:(const int64_t [])keys |
| 6688 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6689 | self = [super init]; |
| 6690 | if (self) { |
| 6691 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 6692 | if (count && values && keys) { |
| 6693 | for (NSUInteger i = 0; i < count; ++i) { |
| 6694 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 6695 | } |
| 6696 | } |
| 6697 | } |
| 6698 | return self; |
| 6699 | } |
| 6700 | |
| 6701 | - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6702 | self = [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6703 | if (self) { |
| 6704 | if (dictionary) { |
| 6705 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 6706 | } |
| 6707 | } |
| 6708 | return self; |
| 6709 | } |
| 6710 | |
| 6711 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 6712 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6713 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | - (void)dealloc { |
| 6717 | NSAssert(!_autocreator, |
| 6718 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 6719 | [self class], _autocreator); |
| 6720 | [_dictionary release]; |
| 6721 | [super dealloc]; |
| 6722 | } |
| 6723 | |
| 6724 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 6725 | return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 6726 | } |
| 6727 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6728 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6729 | if (self == other) { |
| 6730 | return YES; |
| 6731 | } |
| 6732 | if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) { |
| 6733 | return NO; |
| 6734 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6735 | GPBInt64UInt32Dictionary *otherDictionary = other; |
| 6736 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | - (NSUInteger)hash { |
| 6740 | return _dictionary.count; |
| 6741 | } |
| 6742 | |
| 6743 | - (NSString *)description { |
| 6744 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 6745 | } |
| 6746 | |
| 6747 | - (NSUInteger)count { |
| 6748 | return _dictionary.count; |
| 6749 | } |
| 6750 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6751 | - (void)enumerateKeysAndUInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6752 | (void (^)(int64_t key, uint32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6753 | BOOL stop = NO; |
| 6754 | NSDictionary *internal = _dictionary; |
| 6755 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6756 | NSNumber *aKey; |
| 6757 | while ((aKey = [keys nextObject])) { |
| 6758 | NSNumber *aValue = internal[aKey]; |
| 6759 | block([aKey longLongValue], [aValue unsignedIntValue], &stop); |
| 6760 | if (stop) { |
| 6761 | break; |
| 6762 | } |
| 6763 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6767 | NSDictionary *internal = _dictionary; |
| 6768 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6769 | if (count == 0) { |
| 6770 | return 0; |
| 6771 | } |
| 6772 | |
| 6773 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6774 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6775 | size_t result = 0; |
| 6776 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6777 | NSNumber *aKey; |
| 6778 | while ((aKey = [keys nextObject])) { |
| 6779 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6780 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 6781 | msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType); |
| 6782 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6783 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6784 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 6785 | result += tagSize * count; |
| 6786 | return result; |
| 6787 | } |
| 6788 | |
| 6789 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 6790 | asField:(GPBFieldDescriptor *)field { |
| 6791 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6792 | GPBDataType keyDataType = field.mapKeyDataType; |
| 6793 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6794 | NSDictionary *internal = _dictionary; |
| 6795 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6796 | NSNumber *aKey; |
| 6797 | while ((aKey = [keys nextObject])) { |
| 6798 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6799 | [outputStream writeInt32NoTag:tag]; |
| 6800 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6801 | int64_t unwrappedKey = [aKey longLongValue]; |
| 6802 | uint32_t unwrappedValue = [aValue unsignedIntValue]; |
| 6803 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6804 | msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6805 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 6806 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6807 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6808 | WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 6809 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 6813 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 6814 | [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)]; |
| 6815 | } |
| 6816 | |
| 6817 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6818 | [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6819 | #pragma unused(stop) |
| 6820 | block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]); |
| 6821 | }]; |
| 6822 | } |
| 6823 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6824 | - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6825 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 6826 | if (wrapped && value) { |
| 6827 | *value = [wrapped unsignedIntValue]; |
| 6828 | } |
| 6829 | return (wrapped != NULL); |
| 6830 | } |
| 6831 | |
| 6832 | - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary { |
| 6833 | if (otherDictionary) { |
| 6834 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 6835 | if (_autocreator) { |
| 6836 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6837 | } |
| 6838 | } |
| 6839 | } |
| 6840 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6841 | - (void)setUInt32:(uint32_t)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6842 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 6843 | if (_autocreator) { |
| 6844 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 6845 | } |
| 6846 | } |
| 6847 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6848 | - (void)removeUInt32ForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6849 | [_dictionary removeObjectForKey:@(aKey)]; |
| 6850 | } |
| 6851 | |
| 6852 | - (void)removeAll { |
| 6853 | [_dictionary removeAllObjects]; |
| 6854 | } |
| 6855 | |
| 6856 | @end |
| 6857 | |
| 6858 | #pragma mark - Int64 -> Int32 |
| 6859 | |
| 6860 | @implementation GPBInt64Int32Dictionary { |
| 6861 | @package |
| 6862 | NSMutableDictionary *_dictionary; |
| 6863 | } |
| 6864 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6865 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6866 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6867 | } |
| 6868 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6869 | - (instancetype)initWithInt32s:(const int32_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6870 | forKeys:(const int64_t [])keys |
| 6871 | count:(NSUInteger)count { |
| 6872 | self = [super init]; |
| 6873 | if (self) { |
| 6874 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 6875 | if (count && values && keys) { |
| 6876 | for (NSUInteger i = 0; i < count; ++i) { |
| 6877 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 6878 | } |
| 6879 | } |
| 6880 | } |
| 6881 | return self; |
| 6882 | } |
| 6883 | |
| 6884 | - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6885 | self = [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6886 | if (self) { |
| 6887 | if (dictionary) { |
| 6888 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 6889 | } |
| 6890 | } |
| 6891 | return self; |
| 6892 | } |
| 6893 | |
| 6894 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 6895 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6896 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6897 | } |
| 6898 | |
| 6899 | - (void)dealloc { |
| 6900 | NSAssert(!_autocreator, |
| 6901 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 6902 | [self class], _autocreator); |
| 6903 | [_dictionary release]; |
| 6904 | [super dealloc]; |
| 6905 | } |
| 6906 | |
| 6907 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 6908 | return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 6909 | } |
| 6910 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6911 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6912 | if (self == other) { |
| 6913 | return YES; |
| 6914 | } |
| 6915 | if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) { |
| 6916 | return NO; |
| 6917 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6918 | GPBInt64Int32Dictionary *otherDictionary = other; |
| 6919 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | - (NSUInteger)hash { |
| 6923 | return _dictionary.count; |
| 6924 | } |
| 6925 | |
| 6926 | - (NSString *)description { |
| 6927 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 6928 | } |
| 6929 | |
| 6930 | - (NSUInteger)count { |
| 6931 | return _dictionary.count; |
| 6932 | } |
| 6933 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6934 | - (void)enumerateKeysAndInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6935 | (void (^)(int64_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6936 | BOOL stop = NO; |
| 6937 | NSDictionary *internal = _dictionary; |
| 6938 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6939 | NSNumber *aKey; |
| 6940 | while ((aKey = [keys nextObject])) { |
| 6941 | NSNumber *aValue = internal[aKey]; |
| 6942 | block([aKey longLongValue], [aValue intValue], &stop); |
| 6943 | if (stop) { |
| 6944 | break; |
| 6945 | } |
| 6946 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6947 | } |
| 6948 | |
| 6949 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6950 | NSDictionary *internal = _dictionary; |
| 6951 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6952 | if (count == 0) { |
| 6953 | return 0; |
| 6954 | } |
| 6955 | |
| 6956 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6957 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6958 | size_t result = 0; |
| 6959 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6960 | NSNumber *aKey; |
| 6961 | while ((aKey = [keys nextObject])) { |
| 6962 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6963 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 6964 | msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 6965 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6966 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6967 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 6968 | result += tagSize * count; |
| 6969 | return result; |
| 6970 | } |
| 6971 | |
| 6972 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 6973 | asField:(GPBFieldDescriptor *)field { |
| 6974 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 6975 | GPBDataType keyDataType = field.mapKeyDataType; |
| 6976 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6977 | NSDictionary *internal = _dictionary; |
| 6978 | NSEnumerator *keys = [internal keyEnumerator]; |
| 6979 | NSNumber *aKey; |
| 6980 | while ((aKey = [keys nextObject])) { |
| 6981 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6982 | [outputStream writeInt32NoTag:tag]; |
| 6983 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6984 | int64_t unwrappedKey = [aKey longLongValue]; |
| 6985 | int32_t unwrappedValue = [aValue intValue]; |
| 6986 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6987 | msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6988 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 6989 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 6990 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 6991 | WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 6992 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 6996 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 6997 | [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)]; |
| 6998 | } |
| 6999 | |
| 7000 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7001 | [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7002 | #pragma unused(stop) |
| 7003 | block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]); |
| 7004 | }]; |
| 7005 | } |
| 7006 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7007 | - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7008 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 7009 | if (wrapped && value) { |
| 7010 | *value = [wrapped intValue]; |
| 7011 | } |
| 7012 | return (wrapped != NULL); |
| 7013 | } |
| 7014 | |
| 7015 | - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary { |
| 7016 | if (otherDictionary) { |
| 7017 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 7018 | if (_autocreator) { |
| 7019 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7020 | } |
| 7021 | } |
| 7022 | } |
| 7023 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7024 | - (void)setInt32:(int32_t)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7025 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 7026 | if (_autocreator) { |
| 7027 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7028 | } |
| 7029 | } |
| 7030 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7031 | - (void)removeInt32ForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7032 | [_dictionary removeObjectForKey:@(aKey)]; |
| 7033 | } |
| 7034 | |
| 7035 | - (void)removeAll { |
| 7036 | [_dictionary removeAllObjects]; |
| 7037 | } |
| 7038 | |
| 7039 | @end |
| 7040 | |
| 7041 | #pragma mark - Int64 -> UInt64 |
| 7042 | |
| 7043 | @implementation GPBInt64UInt64Dictionary { |
| 7044 | @package |
| 7045 | NSMutableDictionary *_dictionary; |
| 7046 | } |
| 7047 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7048 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7049 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7050 | } |
| 7051 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7052 | - (instancetype)initWithUInt64s:(const uint64_t [])values |
| 7053 | forKeys:(const int64_t [])keys |
| 7054 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7055 | self = [super init]; |
| 7056 | if (self) { |
| 7057 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 7058 | if (count && values && keys) { |
| 7059 | for (NSUInteger i = 0; i < count; ++i) { |
| 7060 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 7061 | } |
| 7062 | } |
| 7063 | } |
| 7064 | return self; |
| 7065 | } |
| 7066 | |
| 7067 | - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7068 | self = [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7069 | if (self) { |
| 7070 | if (dictionary) { |
| 7071 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 7072 | } |
| 7073 | } |
| 7074 | return self; |
| 7075 | } |
| 7076 | |
| 7077 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 7078 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7079 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7080 | } |
| 7081 | |
| 7082 | - (void)dealloc { |
| 7083 | NSAssert(!_autocreator, |
| 7084 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 7085 | [self class], _autocreator); |
| 7086 | [_dictionary release]; |
| 7087 | [super dealloc]; |
| 7088 | } |
| 7089 | |
| 7090 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 7091 | return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 7092 | } |
| 7093 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7094 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7095 | if (self == other) { |
| 7096 | return YES; |
| 7097 | } |
| 7098 | if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) { |
| 7099 | return NO; |
| 7100 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7101 | GPBInt64UInt64Dictionary *otherDictionary = other; |
| 7102 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7103 | } |
| 7104 | |
| 7105 | - (NSUInteger)hash { |
| 7106 | return _dictionary.count; |
| 7107 | } |
| 7108 | |
| 7109 | - (NSString *)description { |
| 7110 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 7111 | } |
| 7112 | |
| 7113 | - (NSUInteger)count { |
| 7114 | return _dictionary.count; |
| 7115 | } |
| 7116 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7117 | - (void)enumerateKeysAndUInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7118 | (void (^)(int64_t key, uint64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7119 | BOOL stop = NO; |
| 7120 | NSDictionary *internal = _dictionary; |
| 7121 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7122 | NSNumber *aKey; |
| 7123 | while ((aKey = [keys nextObject])) { |
| 7124 | NSNumber *aValue = internal[aKey]; |
| 7125 | block([aKey longLongValue], [aValue unsignedLongLongValue], &stop); |
| 7126 | if (stop) { |
| 7127 | break; |
| 7128 | } |
| 7129 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7130 | } |
| 7131 | |
| 7132 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7133 | NSDictionary *internal = _dictionary; |
| 7134 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7135 | if (count == 0) { |
| 7136 | return 0; |
| 7137 | } |
| 7138 | |
| 7139 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7140 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7141 | size_t result = 0; |
| 7142 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7143 | NSNumber *aKey; |
| 7144 | while ((aKey = [keys nextObject])) { |
| 7145 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7146 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 7147 | msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType); |
| 7148 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7149 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7150 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 7151 | result += tagSize * count; |
| 7152 | return result; |
| 7153 | } |
| 7154 | |
| 7155 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 7156 | asField:(GPBFieldDescriptor *)field { |
| 7157 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7158 | GPBDataType keyDataType = field.mapKeyDataType; |
| 7159 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7160 | NSDictionary *internal = _dictionary; |
| 7161 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7162 | NSNumber *aKey; |
| 7163 | while ((aKey = [keys nextObject])) { |
| 7164 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7165 | [outputStream writeInt32NoTag:tag]; |
| 7166 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7167 | int64_t unwrappedKey = [aKey longLongValue]; |
| 7168 | uint64_t unwrappedValue = [aValue unsignedLongLongValue]; |
| 7169 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7170 | msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7171 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 7172 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7173 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7174 | WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 7175 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 7179 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 7180 | [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)]; |
| 7181 | } |
| 7182 | |
| 7183 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7184 | [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7185 | #pragma unused(stop) |
| 7186 | block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]); |
| 7187 | }]; |
| 7188 | } |
| 7189 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7190 | - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7191 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 7192 | if (wrapped && value) { |
| 7193 | *value = [wrapped unsignedLongLongValue]; |
| 7194 | } |
| 7195 | return (wrapped != NULL); |
| 7196 | } |
| 7197 | |
| 7198 | - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary { |
| 7199 | if (otherDictionary) { |
| 7200 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 7201 | if (_autocreator) { |
| 7202 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7203 | } |
| 7204 | } |
| 7205 | } |
| 7206 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7207 | - (void)setUInt64:(uint64_t)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7208 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 7209 | if (_autocreator) { |
| 7210 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7211 | } |
| 7212 | } |
| 7213 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7214 | - (void)removeUInt64ForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7215 | [_dictionary removeObjectForKey:@(aKey)]; |
| 7216 | } |
| 7217 | |
| 7218 | - (void)removeAll { |
| 7219 | [_dictionary removeAllObjects]; |
| 7220 | } |
| 7221 | |
| 7222 | @end |
| 7223 | |
| 7224 | #pragma mark - Int64 -> Int64 |
| 7225 | |
| 7226 | @implementation GPBInt64Int64Dictionary { |
| 7227 | @package |
| 7228 | NSMutableDictionary *_dictionary; |
| 7229 | } |
| 7230 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7231 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7232 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7233 | } |
| 7234 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7235 | - (instancetype)initWithInt64s:(const int64_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7236 | forKeys:(const int64_t [])keys |
| 7237 | count:(NSUInteger)count { |
| 7238 | self = [super init]; |
| 7239 | if (self) { |
| 7240 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 7241 | if (count && values && keys) { |
| 7242 | for (NSUInteger i = 0; i < count; ++i) { |
| 7243 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 7244 | } |
| 7245 | } |
| 7246 | } |
| 7247 | return self; |
| 7248 | } |
| 7249 | |
| 7250 | - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7251 | self = [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7252 | if (self) { |
| 7253 | if (dictionary) { |
| 7254 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 7255 | } |
| 7256 | } |
| 7257 | return self; |
| 7258 | } |
| 7259 | |
| 7260 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 7261 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7262 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | - (void)dealloc { |
| 7266 | NSAssert(!_autocreator, |
| 7267 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 7268 | [self class], _autocreator); |
| 7269 | [_dictionary release]; |
| 7270 | [super dealloc]; |
| 7271 | } |
| 7272 | |
| 7273 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 7274 | return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 7275 | } |
| 7276 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7277 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7278 | if (self == other) { |
| 7279 | return YES; |
| 7280 | } |
| 7281 | if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) { |
| 7282 | return NO; |
| 7283 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7284 | GPBInt64Int64Dictionary *otherDictionary = other; |
| 7285 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7286 | } |
| 7287 | |
| 7288 | - (NSUInteger)hash { |
| 7289 | return _dictionary.count; |
| 7290 | } |
| 7291 | |
| 7292 | - (NSString *)description { |
| 7293 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 7294 | } |
| 7295 | |
| 7296 | - (NSUInteger)count { |
| 7297 | return _dictionary.count; |
| 7298 | } |
| 7299 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7300 | - (void)enumerateKeysAndInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7301 | (void (^)(int64_t key, int64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7302 | BOOL stop = NO; |
| 7303 | NSDictionary *internal = _dictionary; |
| 7304 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7305 | NSNumber *aKey; |
| 7306 | while ((aKey = [keys nextObject])) { |
| 7307 | NSNumber *aValue = internal[aKey]; |
| 7308 | block([aKey longLongValue], [aValue longLongValue], &stop); |
| 7309 | if (stop) { |
| 7310 | break; |
| 7311 | } |
| 7312 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7313 | } |
| 7314 | |
| 7315 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7316 | NSDictionary *internal = _dictionary; |
| 7317 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7318 | if (count == 0) { |
| 7319 | return 0; |
| 7320 | } |
| 7321 | |
| 7322 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7323 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7324 | size_t result = 0; |
| 7325 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7326 | NSNumber *aKey; |
| 7327 | while ((aKey = [keys nextObject])) { |
| 7328 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7329 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 7330 | msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType); |
| 7331 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7332 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7333 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 7334 | result += tagSize * count; |
| 7335 | return result; |
| 7336 | } |
| 7337 | |
| 7338 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 7339 | asField:(GPBFieldDescriptor *)field { |
| 7340 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7341 | GPBDataType keyDataType = field.mapKeyDataType; |
| 7342 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7343 | NSDictionary *internal = _dictionary; |
| 7344 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7345 | NSNumber *aKey; |
| 7346 | while ((aKey = [keys nextObject])) { |
| 7347 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7348 | [outputStream writeInt32NoTag:tag]; |
| 7349 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7350 | int64_t unwrappedKey = [aKey longLongValue]; |
| 7351 | int64_t unwrappedValue = [aValue longLongValue]; |
| 7352 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7353 | msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7354 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 7355 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7356 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7357 | WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 7358 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7359 | } |
| 7360 | |
| 7361 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 7362 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 7363 | [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)]; |
| 7364 | } |
| 7365 | |
| 7366 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7367 | [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7368 | #pragma unused(stop) |
| 7369 | block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]); |
| 7370 | }]; |
| 7371 | } |
| 7372 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7373 | - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7374 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 7375 | if (wrapped && value) { |
| 7376 | *value = [wrapped longLongValue]; |
| 7377 | } |
| 7378 | return (wrapped != NULL); |
| 7379 | } |
| 7380 | |
| 7381 | - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary { |
| 7382 | if (otherDictionary) { |
| 7383 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 7384 | if (_autocreator) { |
| 7385 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7386 | } |
| 7387 | } |
| 7388 | } |
| 7389 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7390 | - (void)setInt64:(int64_t)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7391 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 7392 | if (_autocreator) { |
| 7393 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7394 | } |
| 7395 | } |
| 7396 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7397 | - (void)removeInt64ForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7398 | [_dictionary removeObjectForKey:@(aKey)]; |
| 7399 | } |
| 7400 | |
| 7401 | - (void)removeAll { |
| 7402 | [_dictionary removeAllObjects]; |
| 7403 | } |
| 7404 | |
| 7405 | @end |
| 7406 | |
| 7407 | #pragma mark - Int64 -> Bool |
| 7408 | |
| 7409 | @implementation GPBInt64BoolDictionary { |
| 7410 | @package |
| 7411 | NSMutableDictionary *_dictionary; |
| 7412 | } |
| 7413 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7414 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7415 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7416 | } |
| 7417 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7418 | - (instancetype)initWithBools:(const BOOL [])values |
| 7419 | forKeys:(const int64_t [])keys |
| 7420 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7421 | self = [super init]; |
| 7422 | if (self) { |
| 7423 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 7424 | if (count && values && keys) { |
| 7425 | for (NSUInteger i = 0; i < count; ++i) { |
| 7426 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 7427 | } |
| 7428 | } |
| 7429 | } |
| 7430 | return self; |
| 7431 | } |
| 7432 | |
| 7433 | - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7434 | self = [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7435 | if (self) { |
| 7436 | if (dictionary) { |
| 7437 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 7438 | } |
| 7439 | } |
| 7440 | return self; |
| 7441 | } |
| 7442 | |
| 7443 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 7444 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7445 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7446 | } |
| 7447 | |
| 7448 | - (void)dealloc { |
| 7449 | NSAssert(!_autocreator, |
| 7450 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 7451 | [self class], _autocreator); |
| 7452 | [_dictionary release]; |
| 7453 | [super dealloc]; |
| 7454 | } |
| 7455 | |
| 7456 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 7457 | return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self]; |
| 7458 | } |
| 7459 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7460 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7461 | if (self == other) { |
| 7462 | return YES; |
| 7463 | } |
| 7464 | if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) { |
| 7465 | return NO; |
| 7466 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7467 | GPBInt64BoolDictionary *otherDictionary = other; |
| 7468 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7469 | } |
| 7470 | |
| 7471 | - (NSUInteger)hash { |
| 7472 | return _dictionary.count; |
| 7473 | } |
| 7474 | |
| 7475 | - (NSString *)description { |
| 7476 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 7477 | } |
| 7478 | |
| 7479 | - (NSUInteger)count { |
| 7480 | return _dictionary.count; |
| 7481 | } |
| 7482 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7483 | - (void)enumerateKeysAndBoolsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7484 | (void (^)(int64_t key, BOOL value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7485 | BOOL stop = NO; |
| 7486 | NSDictionary *internal = _dictionary; |
| 7487 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7488 | NSNumber *aKey; |
| 7489 | while ((aKey = [keys nextObject])) { |
| 7490 | NSNumber *aValue = internal[aKey]; |
| 7491 | block([aKey longLongValue], [aValue boolValue], &stop); |
| 7492 | if (stop) { |
| 7493 | break; |
| 7494 | } |
| 7495 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7496 | } |
| 7497 | |
| 7498 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7499 | NSDictionary *internal = _dictionary; |
| 7500 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7501 | if (count == 0) { |
| 7502 | return 0; |
| 7503 | } |
| 7504 | |
| 7505 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7506 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7507 | size_t result = 0; |
| 7508 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7509 | NSNumber *aKey; |
| 7510 | while ((aKey = [keys nextObject])) { |
| 7511 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7512 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 7513 | msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType); |
| 7514 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7515 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7516 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 7517 | result += tagSize * count; |
| 7518 | return result; |
| 7519 | } |
| 7520 | |
| 7521 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 7522 | asField:(GPBFieldDescriptor *)field { |
| 7523 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7524 | GPBDataType keyDataType = field.mapKeyDataType; |
| 7525 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7526 | NSDictionary *internal = _dictionary; |
| 7527 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7528 | NSNumber *aKey; |
| 7529 | while ((aKey = [keys nextObject])) { |
| 7530 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7531 | [outputStream writeInt32NoTag:tag]; |
| 7532 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7533 | int64_t unwrappedKey = [aKey longLongValue]; |
| 7534 | BOOL unwrappedValue = [aValue boolValue]; |
| 7535 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7536 | msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7537 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 7538 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7539 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7540 | WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 7541 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7542 | } |
| 7543 | |
| 7544 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 7545 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 7546 | [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)]; |
| 7547 | } |
| 7548 | |
| 7549 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7550 | [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7551 | #pragma unused(stop) |
| 7552 | block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false")); |
| 7553 | }]; |
| 7554 | } |
| 7555 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7556 | - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7557 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 7558 | if (wrapped && value) { |
| 7559 | *value = [wrapped boolValue]; |
| 7560 | } |
| 7561 | return (wrapped != NULL); |
| 7562 | } |
| 7563 | |
| 7564 | - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary { |
| 7565 | if (otherDictionary) { |
| 7566 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 7567 | if (_autocreator) { |
| 7568 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7569 | } |
| 7570 | } |
| 7571 | } |
| 7572 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7573 | - (void)setBool:(BOOL)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7574 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 7575 | if (_autocreator) { |
| 7576 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7577 | } |
| 7578 | } |
| 7579 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7580 | - (void)removeBoolForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7581 | [_dictionary removeObjectForKey:@(aKey)]; |
| 7582 | } |
| 7583 | |
| 7584 | - (void)removeAll { |
| 7585 | [_dictionary removeAllObjects]; |
| 7586 | } |
| 7587 | |
| 7588 | @end |
| 7589 | |
| 7590 | #pragma mark - Int64 -> Float |
| 7591 | |
| 7592 | @implementation GPBInt64FloatDictionary { |
| 7593 | @package |
| 7594 | NSMutableDictionary *_dictionary; |
| 7595 | } |
| 7596 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7597 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7598 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7599 | } |
| 7600 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7601 | - (instancetype)initWithFloats:(const float [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7602 | forKeys:(const int64_t [])keys |
| 7603 | count:(NSUInteger)count { |
| 7604 | self = [super init]; |
| 7605 | if (self) { |
| 7606 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 7607 | if (count && values && keys) { |
| 7608 | for (NSUInteger i = 0; i < count; ++i) { |
| 7609 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 7610 | } |
| 7611 | } |
| 7612 | } |
| 7613 | return self; |
| 7614 | } |
| 7615 | |
| 7616 | - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7617 | self = [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7618 | if (self) { |
| 7619 | if (dictionary) { |
| 7620 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 7621 | } |
| 7622 | } |
| 7623 | return self; |
| 7624 | } |
| 7625 | |
| 7626 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 7627 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7628 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | - (void)dealloc { |
| 7632 | NSAssert(!_autocreator, |
| 7633 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 7634 | [self class], _autocreator); |
| 7635 | [_dictionary release]; |
| 7636 | [super dealloc]; |
| 7637 | } |
| 7638 | |
| 7639 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 7640 | return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self]; |
| 7641 | } |
| 7642 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7643 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7644 | if (self == other) { |
| 7645 | return YES; |
| 7646 | } |
| 7647 | if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) { |
| 7648 | return NO; |
| 7649 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7650 | GPBInt64FloatDictionary *otherDictionary = other; |
| 7651 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | - (NSUInteger)hash { |
| 7655 | return _dictionary.count; |
| 7656 | } |
| 7657 | |
| 7658 | - (NSString *)description { |
| 7659 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 7660 | } |
| 7661 | |
| 7662 | - (NSUInteger)count { |
| 7663 | return _dictionary.count; |
| 7664 | } |
| 7665 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7666 | - (void)enumerateKeysAndFloatsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7667 | (void (^)(int64_t key, float value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7668 | BOOL stop = NO; |
| 7669 | NSDictionary *internal = _dictionary; |
| 7670 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7671 | NSNumber *aKey; |
| 7672 | while ((aKey = [keys nextObject])) { |
| 7673 | NSNumber *aValue = internal[aKey]; |
| 7674 | block([aKey longLongValue], [aValue floatValue], &stop); |
| 7675 | if (stop) { |
| 7676 | break; |
| 7677 | } |
| 7678 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7679 | } |
| 7680 | |
| 7681 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7682 | NSDictionary *internal = _dictionary; |
| 7683 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7684 | if (count == 0) { |
| 7685 | return 0; |
| 7686 | } |
| 7687 | |
| 7688 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7689 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7690 | size_t result = 0; |
| 7691 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7692 | NSNumber *aKey; |
| 7693 | while ((aKey = [keys nextObject])) { |
| 7694 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7695 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 7696 | msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType); |
| 7697 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7698 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7699 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 7700 | result += tagSize * count; |
| 7701 | return result; |
| 7702 | } |
| 7703 | |
| 7704 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 7705 | asField:(GPBFieldDescriptor *)field { |
| 7706 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7707 | GPBDataType keyDataType = field.mapKeyDataType; |
| 7708 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7709 | NSDictionary *internal = _dictionary; |
| 7710 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7711 | NSNumber *aKey; |
| 7712 | while ((aKey = [keys nextObject])) { |
| 7713 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7714 | [outputStream writeInt32NoTag:tag]; |
| 7715 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7716 | int64_t unwrappedKey = [aKey longLongValue]; |
| 7717 | float unwrappedValue = [aValue floatValue]; |
| 7718 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7719 | msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7720 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 7721 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7722 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7723 | WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 7724 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7725 | } |
| 7726 | |
| 7727 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 7728 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 7729 | [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)]; |
| 7730 | } |
| 7731 | |
| 7732 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7733 | [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7734 | #pragma unused(stop) |
| 7735 | block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); |
| 7736 | }]; |
| 7737 | } |
| 7738 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7739 | - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7740 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 7741 | if (wrapped && value) { |
| 7742 | *value = [wrapped floatValue]; |
| 7743 | } |
| 7744 | return (wrapped != NULL); |
| 7745 | } |
| 7746 | |
| 7747 | - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary { |
| 7748 | if (otherDictionary) { |
| 7749 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 7750 | if (_autocreator) { |
| 7751 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7752 | } |
| 7753 | } |
| 7754 | } |
| 7755 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7756 | - (void)setFloat:(float)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7757 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 7758 | if (_autocreator) { |
| 7759 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7760 | } |
| 7761 | } |
| 7762 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7763 | - (void)removeFloatForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7764 | [_dictionary removeObjectForKey:@(aKey)]; |
| 7765 | } |
| 7766 | |
| 7767 | - (void)removeAll { |
| 7768 | [_dictionary removeAllObjects]; |
| 7769 | } |
| 7770 | |
| 7771 | @end |
| 7772 | |
| 7773 | #pragma mark - Int64 -> Double |
| 7774 | |
| 7775 | @implementation GPBInt64DoubleDictionary { |
| 7776 | @package |
| 7777 | NSMutableDictionary *_dictionary; |
| 7778 | } |
| 7779 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7780 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7781 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7782 | } |
| 7783 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7784 | - (instancetype)initWithDoubles:(const double [])values |
| 7785 | forKeys:(const int64_t [])keys |
| 7786 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7787 | self = [super init]; |
| 7788 | if (self) { |
| 7789 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 7790 | if (count && values && keys) { |
| 7791 | for (NSUInteger i = 0; i < count; ++i) { |
| 7792 | [_dictionary setObject:@(values[i]) forKey:@(keys[i])]; |
| 7793 | } |
| 7794 | } |
| 7795 | } |
| 7796 | return self; |
| 7797 | } |
| 7798 | |
| 7799 | - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7800 | self = [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7801 | if (self) { |
| 7802 | if (dictionary) { |
| 7803 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 7804 | } |
| 7805 | } |
| 7806 | return self; |
| 7807 | } |
| 7808 | |
| 7809 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 7810 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7811 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7812 | } |
| 7813 | |
| 7814 | - (void)dealloc { |
| 7815 | NSAssert(!_autocreator, |
| 7816 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 7817 | [self class], _autocreator); |
| 7818 | [_dictionary release]; |
| 7819 | [super dealloc]; |
| 7820 | } |
| 7821 | |
| 7822 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 7823 | return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self]; |
| 7824 | } |
| 7825 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7826 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7827 | if (self == other) { |
| 7828 | return YES; |
| 7829 | } |
| 7830 | if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) { |
| 7831 | return NO; |
| 7832 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7833 | GPBInt64DoubleDictionary *otherDictionary = other; |
| 7834 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7835 | } |
| 7836 | |
| 7837 | - (NSUInteger)hash { |
| 7838 | return _dictionary.count; |
| 7839 | } |
| 7840 | |
| 7841 | - (NSString *)description { |
| 7842 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 7843 | } |
| 7844 | |
| 7845 | - (NSUInteger)count { |
| 7846 | return _dictionary.count; |
| 7847 | } |
| 7848 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7849 | - (void)enumerateKeysAndDoublesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7850 | (void (^)(int64_t key, double value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7851 | BOOL stop = NO; |
| 7852 | NSDictionary *internal = _dictionary; |
| 7853 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7854 | NSNumber *aKey; |
| 7855 | while ((aKey = [keys nextObject])) { |
| 7856 | NSNumber *aValue = internal[aKey]; |
| 7857 | block([aKey longLongValue], [aValue doubleValue], &stop); |
| 7858 | if (stop) { |
| 7859 | break; |
| 7860 | } |
| 7861 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7865 | NSDictionary *internal = _dictionary; |
| 7866 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7867 | if (count == 0) { |
| 7868 | return 0; |
| 7869 | } |
| 7870 | |
| 7871 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7872 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7873 | size_t result = 0; |
| 7874 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7875 | NSNumber *aKey; |
| 7876 | while ((aKey = [keys nextObject])) { |
| 7877 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7878 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 7879 | msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType); |
| 7880 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7881 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7882 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 7883 | result += tagSize * count; |
| 7884 | return result; |
| 7885 | } |
| 7886 | |
| 7887 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 7888 | asField:(GPBFieldDescriptor *)field { |
| 7889 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 7890 | GPBDataType keyDataType = field.mapKeyDataType; |
| 7891 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7892 | NSDictionary *internal = _dictionary; |
| 7893 | NSEnumerator *keys = [internal keyEnumerator]; |
| 7894 | NSNumber *aKey; |
| 7895 | while ((aKey = [keys nextObject])) { |
| 7896 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7897 | [outputStream writeInt32NoTag:tag]; |
| 7898 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7899 | int64_t unwrappedKey = [aKey longLongValue]; |
| 7900 | double unwrappedValue = [aValue doubleValue]; |
| 7901 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7902 | msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7903 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 7904 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7905 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 7906 | WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 7907 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7908 | } |
| 7909 | |
| 7910 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 7911 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 7912 | [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)]; |
| 7913 | } |
| 7914 | |
| 7915 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7916 | [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7917 | #pragma unused(stop) |
| 7918 | block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); |
| 7919 | }]; |
| 7920 | } |
| 7921 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7922 | - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7923 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 7924 | if (wrapped && value) { |
| 7925 | *value = [wrapped doubleValue]; |
| 7926 | } |
| 7927 | return (wrapped != NULL); |
| 7928 | } |
| 7929 | |
| 7930 | - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary { |
| 7931 | if (otherDictionary) { |
| 7932 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 7933 | if (_autocreator) { |
| 7934 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7935 | } |
| 7936 | } |
| 7937 | } |
| 7938 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7939 | - (void)setDouble:(double)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7940 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 7941 | if (_autocreator) { |
| 7942 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 7943 | } |
| 7944 | } |
| 7945 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 7946 | - (void)removeDoubleForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7947 | [_dictionary removeObjectForKey:@(aKey)]; |
| 7948 | } |
| 7949 | |
| 7950 | - (void)removeAll { |
| 7951 | [_dictionary removeAllObjects]; |
| 7952 | } |
| 7953 | |
| 7954 | @end |
| 7955 | |
| 7956 | #pragma mark - Int64 -> Enum |
| 7957 | |
| 7958 | @implementation GPBInt64EnumDictionary { |
| 7959 | @package |
| 7960 | NSMutableDictionary *_dictionary; |
| 7961 | GPBEnumValidationFunc _validationFunc; |
| 7962 | } |
| 7963 | |
| 7964 | @synthesize validationFunc = _validationFunc; |
| 7965 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 7966 | - (instancetype)init { |
| 7967 | return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 7968 | } |
| 7969 | |
| 7970 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 7971 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 7972 | } |
| 7973 | |
| 7974 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 7975 | rawValues:(const int32_t [])rawValues |
| 7976 | forKeys:(const int64_t [])keys |
| 7977 | count:(NSUInteger)count { |
| 7978 | self = [super init]; |
| 7979 | if (self) { |
| 7980 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 7981 | _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 7982 | if (count && rawValues && keys) { |
| 7983 | for (NSUInteger i = 0; i < count; ++i) { |
| 7984 | [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])]; |
| 7985 | } |
| 7986 | } |
| 7987 | } |
| 7988 | return self; |
| 7989 | } |
| 7990 | |
| 7991 | - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary { |
| 7992 | self = [self initWithValidationFunction:dictionary.validationFunc |
| 7993 | rawValues:NULL |
| 7994 | forKeys:NULL |
| 7995 | count:0]; |
| 7996 | if (self) { |
| 7997 | if (dictionary) { |
| 7998 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 7999 | } |
| 8000 | } |
| 8001 | return self; |
| 8002 | } |
| 8003 | |
| 8004 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 8005 | capacity:(NSUInteger)numItems { |
| 8006 | #pragma unused(numItems) |
| 8007 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 8008 | } |
| 8009 | |
| 8010 | - (void)dealloc { |
| 8011 | NSAssert(!_autocreator, |
| 8012 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 8013 | [self class], _autocreator); |
| 8014 | [_dictionary release]; |
| 8015 | [super dealloc]; |
| 8016 | } |
| 8017 | |
| 8018 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 8019 | return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self]; |
| 8020 | } |
| 8021 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8022 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8023 | if (self == other) { |
| 8024 | return YES; |
| 8025 | } |
| 8026 | if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) { |
| 8027 | return NO; |
| 8028 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8029 | GPBInt64EnumDictionary *otherDictionary = other; |
| 8030 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | - (NSUInteger)hash { |
| 8034 | return _dictionary.count; |
| 8035 | } |
| 8036 | |
| 8037 | - (NSString *)description { |
| 8038 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 8039 | } |
| 8040 | |
| 8041 | - (NSUInteger)count { |
| 8042 | return _dictionary.count; |
| 8043 | } |
| 8044 | |
| 8045 | - (void)enumerateKeysAndRawValuesUsingBlock: |
| 8046 | (void (^)(int64_t key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8047 | BOOL stop = NO; |
| 8048 | NSDictionary *internal = _dictionary; |
| 8049 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8050 | NSNumber *aKey; |
| 8051 | while ((aKey = [keys nextObject])) { |
| 8052 | NSNumber *aValue = internal[aKey]; |
| 8053 | block([aKey longLongValue], [aValue intValue], &stop); |
| 8054 | if (stop) { |
| 8055 | break; |
| 8056 | } |
| 8057 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8058 | } |
| 8059 | |
| 8060 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8061 | NSDictionary *internal = _dictionary; |
| 8062 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8063 | if (count == 0) { |
| 8064 | return 0; |
| 8065 | } |
| 8066 | |
| 8067 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8068 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8069 | size_t result = 0; |
| 8070 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8071 | NSNumber *aKey; |
| 8072 | while ((aKey = [keys nextObject])) { |
| 8073 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8074 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 8075 | msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 8076 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8077 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8078 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 8079 | result += tagSize * count; |
| 8080 | return result; |
| 8081 | } |
| 8082 | |
| 8083 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 8084 | asField:(GPBFieldDescriptor *)field { |
| 8085 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8086 | GPBDataType keyDataType = field.mapKeyDataType; |
| 8087 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8088 | NSDictionary *internal = _dictionary; |
| 8089 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8090 | NSNumber *aKey; |
| 8091 | while ((aKey = [keys nextObject])) { |
| 8092 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8093 | [outputStream writeInt32NoTag:tag]; |
| 8094 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8095 | int64_t unwrappedKey = [aKey longLongValue]; |
| 8096 | int32_t unwrappedValue = [aValue intValue]; |
| 8097 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8098 | msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8099 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 8100 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8101 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8102 | WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 8103 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8104 | } |
| 8105 | |
| 8106 | - (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 8107 | forKey:(GPBGenericValue *)key |
| 8108 | keyDataType:(GPBDataType)keyDataType { |
| 8109 | size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType); |
| 8110 | msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 8111 | NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 8112 | GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 8113 | WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType); |
| 8114 | WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 8115 | [outputStream release]; |
| 8116 | return data; |
| 8117 | } |
| 8118 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 8119 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 8120 | [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)]; |
| 8121 | } |
| 8122 | |
| 8123 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 8124 | [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) { |
| 8125 | #pragma unused(stop) |
| 8126 | block([NSString stringWithFormat:@"%lld", key], @(value)); |
| 8127 | }]; |
| 8128 | } |
| 8129 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8130 | - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8131 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 8132 | if (wrapped && value) { |
| 8133 | int32_t result = [wrapped intValue]; |
| 8134 | if (!_validationFunc(result)) { |
| 8135 | result = kGPBUnrecognizedEnumeratorValue; |
| 8136 | } |
| 8137 | *value = result; |
| 8138 | } |
| 8139 | return (wrapped != NULL); |
| 8140 | } |
| 8141 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8142 | - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8143 | NSNumber *wrapped = [_dictionary objectForKey:@(key)]; |
| 8144 | if (wrapped && rawValue) { |
| 8145 | *rawValue = [wrapped intValue]; |
| 8146 | } |
| 8147 | return (wrapped != NULL); |
| 8148 | } |
| 8149 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8150 | - (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8151 | (void (^)(int64_t key, int32_t value, BOOL *stop))block { |
| 8152 | GPBEnumValidationFunc func = _validationFunc; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8153 | BOOL stop = NO; |
| 8154 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 8155 | NSNumber *aKey; |
| 8156 | while ((aKey = [keys nextObject])) { |
| 8157 | NSNumber *aValue = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8158 | int32_t unwrapped = [aValue intValue]; |
| 8159 | if (!func(unwrapped)) { |
| 8160 | unwrapped = kGPBUnrecognizedEnumeratorValue; |
| 8161 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8162 | block([aKey longLongValue], unwrapped, &stop); |
| 8163 | if (stop) { |
| 8164 | break; |
| 8165 | } |
| 8166 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8167 | } |
| 8168 | |
| 8169 | - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary { |
| 8170 | if (otherDictionary) { |
| 8171 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 8172 | if (_autocreator) { |
| 8173 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8174 | } |
| 8175 | } |
| 8176 | } |
| 8177 | |
| 8178 | - (void)setRawValue:(int32_t)value forKey:(int64_t)key { |
| 8179 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 8180 | if (_autocreator) { |
| 8181 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8182 | } |
| 8183 | } |
| 8184 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8185 | - (void)removeEnumForKey:(int64_t)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8186 | [_dictionary removeObjectForKey:@(aKey)]; |
| 8187 | } |
| 8188 | |
| 8189 | - (void)removeAll { |
| 8190 | [_dictionary removeAllObjects]; |
| 8191 | } |
| 8192 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8193 | - (void)setEnum:(int32_t)value forKey:(int64_t)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8194 | if (!_validationFunc(value)) { |
| 8195 | [NSException raise:NSInvalidArgumentException |
| 8196 | format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)", |
| 8197 | value]; |
| 8198 | } |
| 8199 | |
| 8200 | [_dictionary setObject:@(value) forKey:@(key)]; |
| 8201 | if (_autocreator) { |
| 8202 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8203 | } |
| 8204 | } |
| 8205 | |
| 8206 | @end |
| 8207 | |
| 8208 | #pragma mark - Int64 -> Object |
| 8209 | |
| 8210 | @implementation GPBInt64ObjectDictionary { |
| 8211 | @package |
| 8212 | NSMutableDictionary *_dictionary; |
| 8213 | } |
| 8214 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8215 | - (instancetype)init { |
| 8216 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 8217 | } |
| 8218 | |
| 8219 | - (instancetype)initWithObjects:(const id [])objects |
| 8220 | forKeys:(const int64_t [])keys |
| 8221 | count:(NSUInteger)count { |
| 8222 | self = [super init]; |
| 8223 | if (self) { |
| 8224 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 8225 | if (count && objects && keys) { |
| 8226 | for (NSUInteger i = 0; i < count; ++i) { |
| 8227 | if (!objects[i]) { |
| 8228 | [NSException raise:NSInvalidArgumentException |
| 8229 | format:@"Attempting to add nil object to a Dictionary"]; |
| 8230 | } |
| 8231 | [_dictionary setObject:objects[i] forKey:@(keys[i])]; |
| 8232 | } |
| 8233 | } |
| 8234 | } |
| 8235 | return self; |
| 8236 | } |
| 8237 | |
| 8238 | - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary { |
| 8239 | self = [self initWithObjects:NULL forKeys:NULL count:0]; |
| 8240 | if (self) { |
| 8241 | if (dictionary) { |
| 8242 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 8243 | } |
| 8244 | } |
| 8245 | return self; |
| 8246 | } |
| 8247 | |
| 8248 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 8249 | #pragma unused(numItems) |
| 8250 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 8251 | } |
| 8252 | |
| 8253 | - (void)dealloc { |
| 8254 | NSAssert(!_autocreator, |
| 8255 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 8256 | [self class], _autocreator); |
| 8257 | [_dictionary release]; |
| 8258 | [super dealloc]; |
| 8259 | } |
| 8260 | |
| 8261 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 8262 | return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self]; |
| 8263 | } |
| 8264 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8265 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8266 | if (self == other) { |
| 8267 | return YES; |
| 8268 | } |
| 8269 | if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) { |
| 8270 | return NO; |
| 8271 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8272 | GPBInt64ObjectDictionary *otherDictionary = other; |
| 8273 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8274 | } |
| 8275 | |
| 8276 | - (NSUInteger)hash { |
| 8277 | return _dictionary.count; |
| 8278 | } |
| 8279 | |
| 8280 | - (NSString *)description { |
| 8281 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 8282 | } |
| 8283 | |
| 8284 | - (NSUInteger)count { |
| 8285 | return _dictionary.count; |
| 8286 | } |
| 8287 | |
| 8288 | - (void)enumerateKeysAndObjectsUsingBlock: |
| 8289 | (void (^)(int64_t key, id object, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8290 | BOOL stop = NO; |
| 8291 | NSDictionary *internal = _dictionary; |
| 8292 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8293 | NSNumber *aKey; |
| 8294 | while ((aKey = [keys nextObject])) { |
| 8295 | id aObject = internal[aKey]; |
| 8296 | block([aKey longLongValue], aObject, &stop); |
| 8297 | if (stop) { |
| 8298 | break; |
| 8299 | } |
| 8300 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8301 | } |
| 8302 | |
| 8303 | - (BOOL)isInitialized { |
| 8304 | for (GPBMessage *msg in [_dictionary objectEnumerator]) { |
| 8305 | if (!msg.initialized) { |
| 8306 | return NO; |
| 8307 | } |
| 8308 | } |
| 8309 | return YES; |
| 8310 | } |
| 8311 | |
| 8312 | - (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 8313 | GPBInt64ObjectDictionary *newDict = |
| 8314 | [[GPBInt64ObjectDictionary alloc] init]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8315 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 8316 | id aKey; |
| 8317 | NSMutableDictionary *internalDict = newDict->_dictionary; |
| 8318 | while ((aKey = [keys nextObject])) { |
| 8319 | GPBMessage *msg = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8320 | GPBMessage *copiedMsg = [msg copyWithZone:zone]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8321 | [internalDict setObject:copiedMsg forKey:aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8322 | [copiedMsg release]; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8323 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8324 | return newDict; |
| 8325 | } |
| 8326 | |
| 8327 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8328 | NSDictionary *internal = _dictionary; |
| 8329 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8330 | if (count == 0) { |
| 8331 | return 0; |
| 8332 | } |
| 8333 | |
| 8334 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8335 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8336 | size_t result = 0; |
| 8337 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8338 | NSNumber *aKey; |
| 8339 | while ((aKey = [keys nextObject])) { |
| 8340 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8341 | size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType); |
| 8342 | msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType); |
| 8343 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8344 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8345 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 8346 | result += tagSize * count; |
| 8347 | return result; |
| 8348 | } |
| 8349 | |
| 8350 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 8351 | asField:(GPBFieldDescriptor *)field { |
| 8352 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8353 | GPBDataType keyDataType = field.mapKeyDataType; |
| 8354 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8355 | NSDictionary *internal = _dictionary; |
| 8356 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8357 | NSNumber *aKey; |
| 8358 | while ((aKey = [keys nextObject])) { |
| 8359 | id aObject = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8360 | [outputStream writeInt32NoTag:tag]; |
| 8361 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8362 | int64_t unwrappedKey = [aKey longLongValue]; |
| 8363 | id unwrappedValue = aObject; |
| 8364 | size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8365 | msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8366 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 8367 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8368 | WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8369 | WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 8370 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8371 | } |
| 8372 | |
| 8373 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 8374 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 8375 | [_dictionary setObject:value->valueString forKey:@(key->valueInt64)]; |
| 8376 | } |
| 8377 | |
| 8378 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 8379 | [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) { |
| 8380 | #pragma unused(stop) |
| 8381 | block([NSString stringWithFormat:@"%lld", key], object); |
| 8382 | }]; |
| 8383 | } |
| 8384 | |
| 8385 | - (id)objectForKey:(int64_t)key { |
| 8386 | id result = [_dictionary objectForKey:@(key)]; |
| 8387 | return result; |
| 8388 | } |
| 8389 | |
| 8390 | - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary { |
| 8391 | if (otherDictionary) { |
| 8392 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 8393 | if (_autocreator) { |
| 8394 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8395 | } |
| 8396 | } |
| 8397 | } |
| 8398 | |
| 8399 | - (void)setObject:(id)object forKey:(int64_t)key { |
| 8400 | if (!object) { |
| 8401 | [NSException raise:NSInvalidArgumentException |
| 8402 | format:@"Attempting to add nil object to a Dictionary"]; |
| 8403 | } |
| 8404 | [_dictionary setObject:object forKey:@(key)]; |
| 8405 | if (_autocreator) { |
| 8406 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8407 | } |
| 8408 | } |
| 8409 | |
| 8410 | - (void)removeObjectForKey:(int64_t)aKey { |
| 8411 | [_dictionary removeObjectForKey:@(aKey)]; |
| 8412 | } |
| 8413 | |
| 8414 | - (void)removeAll { |
| 8415 | [_dictionary removeAllObjects]; |
| 8416 | } |
| 8417 | |
| 8418 | @end |
| 8419 | |
| 8420 | //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT) |
| 8421 | // This block of code is generated, do not edit it directly. |
| 8422 | |
| 8423 | #pragma mark - String -> UInt32 |
| 8424 | |
| 8425 | @implementation GPBStringUInt32Dictionary { |
| 8426 | @package |
| 8427 | NSMutableDictionary *_dictionary; |
| 8428 | } |
| 8429 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8430 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8431 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8432 | } |
| 8433 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8434 | - (instancetype)initWithUInt32s:(const uint32_t [])values |
| 8435 | forKeys:(const NSString * [])keys |
| 8436 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8437 | self = [super init]; |
| 8438 | if (self) { |
| 8439 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 8440 | if (count && values && keys) { |
| 8441 | for (NSUInteger i = 0; i < count; ++i) { |
| 8442 | if (!keys[i]) { |
| 8443 | [NSException raise:NSInvalidArgumentException |
| 8444 | format:@"Attempting to add nil key to a Dictionary"]; |
| 8445 | } |
| 8446 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 8447 | } |
| 8448 | } |
| 8449 | } |
| 8450 | return self; |
| 8451 | } |
| 8452 | |
| 8453 | - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8454 | self = [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8455 | if (self) { |
| 8456 | if (dictionary) { |
| 8457 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 8458 | } |
| 8459 | } |
| 8460 | return self; |
| 8461 | } |
| 8462 | |
| 8463 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 8464 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8465 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8466 | } |
| 8467 | |
| 8468 | - (void)dealloc { |
| 8469 | NSAssert(!_autocreator, |
| 8470 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 8471 | [self class], _autocreator); |
| 8472 | [_dictionary release]; |
| 8473 | [super dealloc]; |
| 8474 | } |
| 8475 | |
| 8476 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 8477 | return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 8478 | } |
| 8479 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8480 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8481 | if (self == other) { |
| 8482 | return YES; |
| 8483 | } |
| 8484 | if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) { |
| 8485 | return NO; |
| 8486 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8487 | GPBStringUInt32Dictionary *otherDictionary = other; |
| 8488 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8489 | } |
| 8490 | |
| 8491 | - (NSUInteger)hash { |
| 8492 | return _dictionary.count; |
| 8493 | } |
| 8494 | |
| 8495 | - (NSString *)description { |
| 8496 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 8497 | } |
| 8498 | |
| 8499 | - (NSUInteger)count { |
| 8500 | return _dictionary.count; |
| 8501 | } |
| 8502 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8503 | - (void)enumerateKeysAndUInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8504 | (void (^)(NSString *key, uint32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8505 | BOOL stop = NO; |
| 8506 | NSDictionary *internal = _dictionary; |
| 8507 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8508 | NSString *aKey; |
| 8509 | while ((aKey = [keys nextObject])) { |
| 8510 | NSNumber *aValue = internal[aKey]; |
| 8511 | block(aKey, [aValue unsignedIntValue], &stop); |
| 8512 | if (stop) { |
| 8513 | break; |
| 8514 | } |
| 8515 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8516 | } |
| 8517 | |
| 8518 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8519 | NSDictionary *internal = _dictionary; |
| 8520 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8521 | if (count == 0) { |
| 8522 | return 0; |
| 8523 | } |
| 8524 | |
| 8525 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8526 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8527 | size_t result = 0; |
| 8528 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8529 | NSString *aKey; |
| 8530 | while ((aKey = [keys nextObject])) { |
| 8531 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8532 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 8533 | msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType); |
| 8534 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8535 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8536 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 8537 | result += tagSize * count; |
| 8538 | return result; |
| 8539 | } |
| 8540 | |
| 8541 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 8542 | asField:(GPBFieldDescriptor *)field { |
| 8543 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8544 | GPBDataType keyDataType = field.mapKeyDataType; |
| 8545 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8546 | NSDictionary *internal = _dictionary; |
| 8547 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8548 | NSString *aKey; |
| 8549 | while ((aKey = [keys nextObject])) { |
| 8550 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8551 | [outputStream writeInt32NoTag:tag]; |
| 8552 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8553 | NSString *unwrappedKey = aKey; |
| 8554 | uint32_t unwrappedValue = [aValue unsignedIntValue]; |
| 8555 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8556 | msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8557 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 8558 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8559 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8560 | WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 8561 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8562 | } |
| 8563 | |
| 8564 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 8565 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 8566 | [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString]; |
| 8567 | } |
| 8568 | |
| 8569 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8570 | [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8571 | #pragma unused(stop) |
| 8572 | block(key, [NSString stringWithFormat:@"%u", value]); |
| 8573 | }]; |
| 8574 | } |
| 8575 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8576 | - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8577 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 8578 | if (wrapped && value) { |
| 8579 | *value = [wrapped unsignedIntValue]; |
| 8580 | } |
| 8581 | return (wrapped != NULL); |
| 8582 | } |
| 8583 | |
| 8584 | - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary { |
| 8585 | if (otherDictionary) { |
| 8586 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 8587 | if (_autocreator) { |
| 8588 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8589 | } |
| 8590 | } |
| 8591 | } |
| 8592 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8593 | - (void)setUInt32:(uint32_t)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8594 | if (!key) { |
| 8595 | [NSException raise:NSInvalidArgumentException |
| 8596 | format:@"Attempting to add nil key to a Dictionary"]; |
| 8597 | } |
| 8598 | [_dictionary setObject:@(value) forKey:key]; |
| 8599 | if (_autocreator) { |
| 8600 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8601 | } |
| 8602 | } |
| 8603 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8604 | - (void)removeUInt32ForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8605 | [_dictionary removeObjectForKey:aKey]; |
| 8606 | } |
| 8607 | |
| 8608 | - (void)removeAll { |
| 8609 | [_dictionary removeAllObjects]; |
| 8610 | } |
| 8611 | |
| 8612 | @end |
| 8613 | |
| 8614 | #pragma mark - String -> Int32 |
| 8615 | |
| 8616 | @implementation GPBStringInt32Dictionary { |
| 8617 | @package |
| 8618 | NSMutableDictionary *_dictionary; |
| 8619 | } |
| 8620 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8621 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8622 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8623 | } |
| 8624 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8625 | - (instancetype)initWithInt32s:(const int32_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8626 | forKeys:(const NSString * [])keys |
| 8627 | count:(NSUInteger)count { |
| 8628 | self = [super init]; |
| 8629 | if (self) { |
| 8630 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 8631 | if (count && values && keys) { |
| 8632 | for (NSUInteger i = 0; i < count; ++i) { |
| 8633 | if (!keys[i]) { |
| 8634 | [NSException raise:NSInvalidArgumentException |
| 8635 | format:@"Attempting to add nil key to a Dictionary"]; |
| 8636 | } |
| 8637 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 8638 | } |
| 8639 | } |
| 8640 | } |
| 8641 | return self; |
| 8642 | } |
| 8643 | |
| 8644 | - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8645 | self = [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8646 | if (self) { |
| 8647 | if (dictionary) { |
| 8648 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 8649 | } |
| 8650 | } |
| 8651 | return self; |
| 8652 | } |
| 8653 | |
| 8654 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 8655 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8656 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8657 | } |
| 8658 | |
| 8659 | - (void)dealloc { |
| 8660 | NSAssert(!_autocreator, |
| 8661 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 8662 | [self class], _autocreator); |
| 8663 | [_dictionary release]; |
| 8664 | [super dealloc]; |
| 8665 | } |
| 8666 | |
| 8667 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 8668 | return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 8669 | } |
| 8670 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8671 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8672 | if (self == other) { |
| 8673 | return YES; |
| 8674 | } |
| 8675 | if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) { |
| 8676 | return NO; |
| 8677 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8678 | GPBStringInt32Dictionary *otherDictionary = other; |
| 8679 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | - (NSUInteger)hash { |
| 8683 | return _dictionary.count; |
| 8684 | } |
| 8685 | |
| 8686 | - (NSString *)description { |
| 8687 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 8688 | } |
| 8689 | |
| 8690 | - (NSUInteger)count { |
| 8691 | return _dictionary.count; |
| 8692 | } |
| 8693 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8694 | - (void)enumerateKeysAndInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8695 | (void (^)(NSString *key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8696 | BOOL stop = NO; |
| 8697 | NSDictionary *internal = _dictionary; |
| 8698 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8699 | NSString *aKey; |
| 8700 | while ((aKey = [keys nextObject])) { |
| 8701 | NSNumber *aValue = internal[aKey]; |
| 8702 | block(aKey, [aValue intValue], &stop); |
| 8703 | if (stop) { |
| 8704 | break; |
| 8705 | } |
| 8706 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8707 | } |
| 8708 | |
| 8709 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8710 | NSDictionary *internal = _dictionary; |
| 8711 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8712 | if (count == 0) { |
| 8713 | return 0; |
| 8714 | } |
| 8715 | |
| 8716 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8717 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8718 | size_t result = 0; |
| 8719 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8720 | NSString *aKey; |
| 8721 | while ((aKey = [keys nextObject])) { |
| 8722 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8723 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 8724 | msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 8725 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8726 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8727 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 8728 | result += tagSize * count; |
| 8729 | return result; |
| 8730 | } |
| 8731 | |
| 8732 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 8733 | asField:(GPBFieldDescriptor *)field { |
| 8734 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8735 | GPBDataType keyDataType = field.mapKeyDataType; |
| 8736 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8737 | NSDictionary *internal = _dictionary; |
| 8738 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8739 | NSString *aKey; |
| 8740 | while ((aKey = [keys nextObject])) { |
| 8741 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8742 | [outputStream writeInt32NoTag:tag]; |
| 8743 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8744 | NSString *unwrappedKey = aKey; |
| 8745 | int32_t unwrappedValue = [aValue intValue]; |
| 8746 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8747 | msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8748 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 8749 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8750 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8751 | WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 8752 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8753 | } |
| 8754 | |
| 8755 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 8756 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 8757 | [_dictionary setObject:@(value->valueInt32) forKey:key->valueString]; |
| 8758 | } |
| 8759 | |
| 8760 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8761 | [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8762 | #pragma unused(stop) |
| 8763 | block(key, [NSString stringWithFormat:@"%d", value]); |
| 8764 | }]; |
| 8765 | } |
| 8766 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8767 | - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8768 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 8769 | if (wrapped && value) { |
| 8770 | *value = [wrapped intValue]; |
| 8771 | } |
| 8772 | return (wrapped != NULL); |
| 8773 | } |
| 8774 | |
| 8775 | - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary { |
| 8776 | if (otherDictionary) { |
| 8777 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 8778 | if (_autocreator) { |
| 8779 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8780 | } |
| 8781 | } |
| 8782 | } |
| 8783 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8784 | - (void)setInt32:(int32_t)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8785 | if (!key) { |
| 8786 | [NSException raise:NSInvalidArgumentException |
| 8787 | format:@"Attempting to add nil key to a Dictionary"]; |
| 8788 | } |
| 8789 | [_dictionary setObject:@(value) forKey:key]; |
| 8790 | if (_autocreator) { |
| 8791 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8792 | } |
| 8793 | } |
| 8794 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8795 | - (void)removeInt32ForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8796 | [_dictionary removeObjectForKey:aKey]; |
| 8797 | } |
| 8798 | |
| 8799 | - (void)removeAll { |
| 8800 | [_dictionary removeAllObjects]; |
| 8801 | } |
| 8802 | |
| 8803 | @end |
| 8804 | |
| 8805 | #pragma mark - String -> UInt64 |
| 8806 | |
| 8807 | @implementation GPBStringUInt64Dictionary { |
| 8808 | @package |
| 8809 | NSMutableDictionary *_dictionary; |
| 8810 | } |
| 8811 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8812 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8813 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8814 | } |
| 8815 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8816 | - (instancetype)initWithUInt64s:(const uint64_t [])values |
| 8817 | forKeys:(const NSString * [])keys |
| 8818 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8819 | self = [super init]; |
| 8820 | if (self) { |
| 8821 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 8822 | if (count && values && keys) { |
| 8823 | for (NSUInteger i = 0; i < count; ++i) { |
| 8824 | if (!keys[i]) { |
| 8825 | [NSException raise:NSInvalidArgumentException |
| 8826 | format:@"Attempting to add nil key to a Dictionary"]; |
| 8827 | } |
| 8828 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 8829 | } |
| 8830 | } |
| 8831 | } |
| 8832 | return self; |
| 8833 | } |
| 8834 | |
| 8835 | - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8836 | self = [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8837 | if (self) { |
| 8838 | if (dictionary) { |
| 8839 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 8840 | } |
| 8841 | } |
| 8842 | return self; |
| 8843 | } |
| 8844 | |
| 8845 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 8846 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8847 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8848 | } |
| 8849 | |
| 8850 | - (void)dealloc { |
| 8851 | NSAssert(!_autocreator, |
| 8852 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 8853 | [self class], _autocreator); |
| 8854 | [_dictionary release]; |
| 8855 | [super dealloc]; |
| 8856 | } |
| 8857 | |
| 8858 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 8859 | return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 8860 | } |
| 8861 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8862 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8863 | if (self == other) { |
| 8864 | return YES; |
| 8865 | } |
| 8866 | if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) { |
| 8867 | return NO; |
| 8868 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8869 | GPBStringUInt64Dictionary *otherDictionary = other; |
| 8870 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8871 | } |
| 8872 | |
| 8873 | - (NSUInteger)hash { |
| 8874 | return _dictionary.count; |
| 8875 | } |
| 8876 | |
| 8877 | - (NSString *)description { |
| 8878 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 8879 | } |
| 8880 | |
| 8881 | - (NSUInteger)count { |
| 8882 | return _dictionary.count; |
| 8883 | } |
| 8884 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8885 | - (void)enumerateKeysAndUInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8886 | (void (^)(NSString *key, uint64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8887 | BOOL stop = NO; |
| 8888 | NSDictionary *internal = _dictionary; |
| 8889 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8890 | NSString *aKey; |
| 8891 | while ((aKey = [keys nextObject])) { |
| 8892 | NSNumber *aValue = internal[aKey]; |
| 8893 | block(aKey, [aValue unsignedLongLongValue], &stop); |
| 8894 | if (stop) { |
| 8895 | break; |
| 8896 | } |
| 8897 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8898 | } |
| 8899 | |
| 8900 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8901 | NSDictionary *internal = _dictionary; |
| 8902 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8903 | if (count == 0) { |
| 8904 | return 0; |
| 8905 | } |
| 8906 | |
| 8907 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8908 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8909 | size_t result = 0; |
| 8910 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8911 | NSString *aKey; |
| 8912 | while ((aKey = [keys nextObject])) { |
| 8913 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8914 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 8915 | msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType); |
| 8916 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8917 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8918 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 8919 | result += tagSize * count; |
| 8920 | return result; |
| 8921 | } |
| 8922 | |
| 8923 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 8924 | asField:(GPBFieldDescriptor *)field { |
| 8925 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 8926 | GPBDataType keyDataType = field.mapKeyDataType; |
| 8927 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8928 | NSDictionary *internal = _dictionary; |
| 8929 | NSEnumerator *keys = [internal keyEnumerator]; |
| 8930 | NSString *aKey; |
| 8931 | while ((aKey = [keys nextObject])) { |
| 8932 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8933 | [outputStream writeInt32NoTag:tag]; |
| 8934 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8935 | NSString *unwrappedKey = aKey; |
| 8936 | uint64_t unwrappedValue = [aValue unsignedLongLongValue]; |
| 8937 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8938 | msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8939 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 8940 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8941 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 8942 | WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 8943 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8944 | } |
| 8945 | |
| 8946 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 8947 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 8948 | [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString]; |
| 8949 | } |
| 8950 | |
| 8951 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8952 | [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8953 | #pragma unused(stop) |
| 8954 | block(key, [NSString stringWithFormat:@"%llu", value]); |
| 8955 | }]; |
| 8956 | } |
| 8957 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8958 | - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8959 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 8960 | if (wrapped && value) { |
| 8961 | *value = [wrapped unsignedLongLongValue]; |
| 8962 | } |
| 8963 | return (wrapped != NULL); |
| 8964 | } |
| 8965 | |
| 8966 | - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary { |
| 8967 | if (otherDictionary) { |
| 8968 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 8969 | if (_autocreator) { |
| 8970 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8971 | } |
| 8972 | } |
| 8973 | } |
| 8974 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8975 | - (void)setUInt64:(uint64_t)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8976 | if (!key) { |
| 8977 | [NSException raise:NSInvalidArgumentException |
| 8978 | format:@"Attempting to add nil key to a Dictionary"]; |
| 8979 | } |
| 8980 | [_dictionary setObject:@(value) forKey:key]; |
| 8981 | if (_autocreator) { |
| 8982 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 8983 | } |
| 8984 | } |
| 8985 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 8986 | - (void)removeUInt64ForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 8987 | [_dictionary removeObjectForKey:aKey]; |
| 8988 | } |
| 8989 | |
| 8990 | - (void)removeAll { |
| 8991 | [_dictionary removeAllObjects]; |
| 8992 | } |
| 8993 | |
| 8994 | @end |
| 8995 | |
| 8996 | #pragma mark - String -> Int64 |
| 8997 | |
| 8998 | @implementation GPBStringInt64Dictionary { |
| 8999 | @package |
| 9000 | NSMutableDictionary *_dictionary; |
| 9001 | } |
| 9002 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9003 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9004 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9005 | } |
| 9006 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9007 | - (instancetype)initWithInt64s:(const int64_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9008 | forKeys:(const NSString * [])keys |
| 9009 | count:(NSUInteger)count { |
| 9010 | self = [super init]; |
| 9011 | if (self) { |
| 9012 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 9013 | if (count && values && keys) { |
| 9014 | for (NSUInteger i = 0; i < count; ++i) { |
| 9015 | if (!keys[i]) { |
| 9016 | [NSException raise:NSInvalidArgumentException |
| 9017 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9018 | } |
| 9019 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 9020 | } |
| 9021 | } |
| 9022 | } |
| 9023 | return self; |
| 9024 | } |
| 9025 | |
| 9026 | - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9027 | self = [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9028 | if (self) { |
| 9029 | if (dictionary) { |
| 9030 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 9031 | } |
| 9032 | } |
| 9033 | return self; |
| 9034 | } |
| 9035 | |
| 9036 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 9037 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9038 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9039 | } |
| 9040 | |
| 9041 | - (void)dealloc { |
| 9042 | NSAssert(!_autocreator, |
| 9043 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 9044 | [self class], _autocreator); |
| 9045 | [_dictionary release]; |
| 9046 | [super dealloc]; |
| 9047 | } |
| 9048 | |
| 9049 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 9050 | return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 9051 | } |
| 9052 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9053 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9054 | if (self == other) { |
| 9055 | return YES; |
| 9056 | } |
| 9057 | if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) { |
| 9058 | return NO; |
| 9059 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9060 | GPBStringInt64Dictionary *otherDictionary = other; |
| 9061 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | - (NSUInteger)hash { |
| 9065 | return _dictionary.count; |
| 9066 | } |
| 9067 | |
| 9068 | - (NSString *)description { |
| 9069 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 9070 | } |
| 9071 | |
| 9072 | - (NSUInteger)count { |
| 9073 | return _dictionary.count; |
| 9074 | } |
| 9075 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9076 | - (void)enumerateKeysAndInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9077 | (void (^)(NSString *key, int64_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9078 | BOOL stop = NO; |
| 9079 | NSDictionary *internal = _dictionary; |
| 9080 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9081 | NSString *aKey; |
| 9082 | while ((aKey = [keys nextObject])) { |
| 9083 | NSNumber *aValue = internal[aKey]; |
| 9084 | block(aKey, [aValue longLongValue], &stop); |
| 9085 | if (stop) { |
| 9086 | break; |
| 9087 | } |
| 9088 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9089 | } |
| 9090 | |
| 9091 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9092 | NSDictionary *internal = _dictionary; |
| 9093 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9094 | if (count == 0) { |
| 9095 | return 0; |
| 9096 | } |
| 9097 | |
| 9098 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9099 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9100 | size_t result = 0; |
| 9101 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9102 | NSString *aKey; |
| 9103 | while ((aKey = [keys nextObject])) { |
| 9104 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9105 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 9106 | msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType); |
| 9107 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9108 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9109 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 9110 | result += tagSize * count; |
| 9111 | return result; |
| 9112 | } |
| 9113 | |
| 9114 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 9115 | asField:(GPBFieldDescriptor *)field { |
| 9116 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9117 | GPBDataType keyDataType = field.mapKeyDataType; |
| 9118 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9119 | NSDictionary *internal = _dictionary; |
| 9120 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9121 | NSString *aKey; |
| 9122 | while ((aKey = [keys nextObject])) { |
| 9123 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9124 | [outputStream writeInt32NoTag:tag]; |
| 9125 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9126 | NSString *unwrappedKey = aKey; |
| 9127 | int64_t unwrappedValue = [aValue longLongValue]; |
| 9128 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9129 | msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9130 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 9131 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9132 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9133 | WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 9134 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9135 | } |
| 9136 | |
| 9137 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 9138 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 9139 | [_dictionary setObject:@(value->valueInt64) forKey:key->valueString]; |
| 9140 | } |
| 9141 | |
| 9142 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9143 | [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9144 | #pragma unused(stop) |
| 9145 | block(key, [NSString stringWithFormat:@"%lld", value]); |
| 9146 | }]; |
| 9147 | } |
| 9148 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9149 | - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9150 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 9151 | if (wrapped && value) { |
| 9152 | *value = [wrapped longLongValue]; |
| 9153 | } |
| 9154 | return (wrapped != NULL); |
| 9155 | } |
| 9156 | |
| 9157 | - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary { |
| 9158 | if (otherDictionary) { |
| 9159 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 9160 | if (_autocreator) { |
| 9161 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9162 | } |
| 9163 | } |
| 9164 | } |
| 9165 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9166 | - (void)setInt64:(int64_t)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9167 | if (!key) { |
| 9168 | [NSException raise:NSInvalidArgumentException |
| 9169 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9170 | } |
| 9171 | [_dictionary setObject:@(value) forKey:key]; |
| 9172 | if (_autocreator) { |
| 9173 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9174 | } |
| 9175 | } |
| 9176 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9177 | - (void)removeInt64ForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9178 | [_dictionary removeObjectForKey:aKey]; |
| 9179 | } |
| 9180 | |
| 9181 | - (void)removeAll { |
| 9182 | [_dictionary removeAllObjects]; |
| 9183 | } |
| 9184 | |
| 9185 | @end |
| 9186 | |
| 9187 | #pragma mark - String -> Bool |
| 9188 | |
| 9189 | @implementation GPBStringBoolDictionary { |
| 9190 | @package |
| 9191 | NSMutableDictionary *_dictionary; |
| 9192 | } |
| 9193 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9194 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9195 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9196 | } |
| 9197 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9198 | - (instancetype)initWithBools:(const BOOL [])values |
| 9199 | forKeys:(const NSString * [])keys |
| 9200 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9201 | self = [super init]; |
| 9202 | if (self) { |
| 9203 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 9204 | if (count && values && keys) { |
| 9205 | for (NSUInteger i = 0; i < count; ++i) { |
| 9206 | if (!keys[i]) { |
| 9207 | [NSException raise:NSInvalidArgumentException |
| 9208 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9209 | } |
| 9210 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 9211 | } |
| 9212 | } |
| 9213 | } |
| 9214 | return self; |
| 9215 | } |
| 9216 | |
| 9217 | - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9218 | self = [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9219 | if (self) { |
| 9220 | if (dictionary) { |
| 9221 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 9222 | } |
| 9223 | } |
| 9224 | return self; |
| 9225 | } |
| 9226 | |
| 9227 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 9228 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9229 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9230 | } |
| 9231 | |
| 9232 | - (void)dealloc { |
| 9233 | NSAssert(!_autocreator, |
| 9234 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 9235 | [self class], _autocreator); |
| 9236 | [_dictionary release]; |
| 9237 | [super dealloc]; |
| 9238 | } |
| 9239 | |
| 9240 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 9241 | return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self]; |
| 9242 | } |
| 9243 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9244 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9245 | if (self == other) { |
| 9246 | return YES; |
| 9247 | } |
| 9248 | if (![other isKindOfClass:[GPBStringBoolDictionary class]]) { |
| 9249 | return NO; |
| 9250 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9251 | GPBStringBoolDictionary *otherDictionary = other; |
| 9252 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9253 | } |
| 9254 | |
| 9255 | - (NSUInteger)hash { |
| 9256 | return _dictionary.count; |
| 9257 | } |
| 9258 | |
| 9259 | - (NSString *)description { |
| 9260 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 9261 | } |
| 9262 | |
| 9263 | - (NSUInteger)count { |
| 9264 | return _dictionary.count; |
| 9265 | } |
| 9266 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9267 | - (void)enumerateKeysAndBoolsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9268 | (void (^)(NSString *key, BOOL value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9269 | BOOL stop = NO; |
| 9270 | NSDictionary *internal = _dictionary; |
| 9271 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9272 | NSString *aKey; |
| 9273 | while ((aKey = [keys nextObject])) { |
| 9274 | NSNumber *aValue = internal[aKey]; |
| 9275 | block(aKey, [aValue boolValue], &stop); |
| 9276 | if (stop) { |
| 9277 | break; |
| 9278 | } |
| 9279 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9280 | } |
| 9281 | |
| 9282 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9283 | NSDictionary *internal = _dictionary; |
| 9284 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9285 | if (count == 0) { |
| 9286 | return 0; |
| 9287 | } |
| 9288 | |
| 9289 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9290 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9291 | size_t result = 0; |
| 9292 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9293 | NSString *aKey; |
| 9294 | while ((aKey = [keys nextObject])) { |
| 9295 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9296 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 9297 | msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType); |
| 9298 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9299 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9300 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 9301 | result += tagSize * count; |
| 9302 | return result; |
| 9303 | } |
| 9304 | |
| 9305 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 9306 | asField:(GPBFieldDescriptor *)field { |
| 9307 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9308 | GPBDataType keyDataType = field.mapKeyDataType; |
| 9309 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9310 | NSDictionary *internal = _dictionary; |
| 9311 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9312 | NSString *aKey; |
| 9313 | while ((aKey = [keys nextObject])) { |
| 9314 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9315 | [outputStream writeInt32NoTag:tag]; |
| 9316 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9317 | NSString *unwrappedKey = aKey; |
| 9318 | BOOL unwrappedValue = [aValue boolValue]; |
| 9319 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9320 | msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9321 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 9322 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9323 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9324 | WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 9325 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9326 | } |
| 9327 | |
| 9328 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 9329 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 9330 | [_dictionary setObject:@(value->valueBool) forKey:key->valueString]; |
| 9331 | } |
| 9332 | |
| 9333 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9334 | [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9335 | #pragma unused(stop) |
| 9336 | block(key, (value ? @"true" : @"false")); |
| 9337 | }]; |
| 9338 | } |
| 9339 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9340 | - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9341 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 9342 | if (wrapped && value) { |
| 9343 | *value = [wrapped boolValue]; |
| 9344 | } |
| 9345 | return (wrapped != NULL); |
| 9346 | } |
| 9347 | |
| 9348 | - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary { |
| 9349 | if (otherDictionary) { |
| 9350 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 9351 | if (_autocreator) { |
| 9352 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9353 | } |
| 9354 | } |
| 9355 | } |
| 9356 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9357 | - (void)setBool:(BOOL)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9358 | if (!key) { |
| 9359 | [NSException raise:NSInvalidArgumentException |
| 9360 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9361 | } |
| 9362 | [_dictionary setObject:@(value) forKey:key]; |
| 9363 | if (_autocreator) { |
| 9364 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9365 | } |
| 9366 | } |
| 9367 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9368 | - (void)removeBoolForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9369 | [_dictionary removeObjectForKey:aKey]; |
| 9370 | } |
| 9371 | |
| 9372 | - (void)removeAll { |
| 9373 | [_dictionary removeAllObjects]; |
| 9374 | } |
| 9375 | |
| 9376 | @end |
| 9377 | |
| 9378 | #pragma mark - String -> Float |
| 9379 | |
| 9380 | @implementation GPBStringFloatDictionary { |
| 9381 | @package |
| 9382 | NSMutableDictionary *_dictionary; |
| 9383 | } |
| 9384 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9385 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9386 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9387 | } |
| 9388 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9389 | - (instancetype)initWithFloats:(const float [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9390 | forKeys:(const NSString * [])keys |
| 9391 | count:(NSUInteger)count { |
| 9392 | self = [super init]; |
| 9393 | if (self) { |
| 9394 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 9395 | if (count && values && keys) { |
| 9396 | for (NSUInteger i = 0; i < count; ++i) { |
| 9397 | if (!keys[i]) { |
| 9398 | [NSException raise:NSInvalidArgumentException |
| 9399 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9400 | } |
| 9401 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 9402 | } |
| 9403 | } |
| 9404 | } |
| 9405 | return self; |
| 9406 | } |
| 9407 | |
| 9408 | - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9409 | self = [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9410 | if (self) { |
| 9411 | if (dictionary) { |
| 9412 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 9413 | } |
| 9414 | } |
| 9415 | return self; |
| 9416 | } |
| 9417 | |
| 9418 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 9419 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9420 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9421 | } |
| 9422 | |
| 9423 | - (void)dealloc { |
| 9424 | NSAssert(!_autocreator, |
| 9425 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 9426 | [self class], _autocreator); |
| 9427 | [_dictionary release]; |
| 9428 | [super dealloc]; |
| 9429 | } |
| 9430 | |
| 9431 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 9432 | return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self]; |
| 9433 | } |
| 9434 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9435 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9436 | if (self == other) { |
| 9437 | return YES; |
| 9438 | } |
| 9439 | if (![other isKindOfClass:[GPBStringFloatDictionary class]]) { |
| 9440 | return NO; |
| 9441 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9442 | GPBStringFloatDictionary *otherDictionary = other; |
| 9443 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9444 | } |
| 9445 | |
| 9446 | - (NSUInteger)hash { |
| 9447 | return _dictionary.count; |
| 9448 | } |
| 9449 | |
| 9450 | - (NSString *)description { |
| 9451 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 9452 | } |
| 9453 | |
| 9454 | - (NSUInteger)count { |
| 9455 | return _dictionary.count; |
| 9456 | } |
| 9457 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9458 | - (void)enumerateKeysAndFloatsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9459 | (void (^)(NSString *key, float value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9460 | BOOL stop = NO; |
| 9461 | NSDictionary *internal = _dictionary; |
| 9462 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9463 | NSString *aKey; |
| 9464 | while ((aKey = [keys nextObject])) { |
| 9465 | NSNumber *aValue = internal[aKey]; |
| 9466 | block(aKey, [aValue floatValue], &stop); |
| 9467 | if (stop) { |
| 9468 | break; |
| 9469 | } |
| 9470 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9471 | } |
| 9472 | |
| 9473 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9474 | NSDictionary *internal = _dictionary; |
| 9475 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9476 | if (count == 0) { |
| 9477 | return 0; |
| 9478 | } |
| 9479 | |
| 9480 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9481 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9482 | size_t result = 0; |
| 9483 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9484 | NSString *aKey; |
| 9485 | while ((aKey = [keys nextObject])) { |
| 9486 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9487 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 9488 | msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType); |
| 9489 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9490 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9491 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 9492 | result += tagSize * count; |
| 9493 | return result; |
| 9494 | } |
| 9495 | |
| 9496 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 9497 | asField:(GPBFieldDescriptor *)field { |
| 9498 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9499 | GPBDataType keyDataType = field.mapKeyDataType; |
| 9500 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9501 | NSDictionary *internal = _dictionary; |
| 9502 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9503 | NSString *aKey; |
| 9504 | while ((aKey = [keys nextObject])) { |
| 9505 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9506 | [outputStream writeInt32NoTag:tag]; |
| 9507 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9508 | NSString *unwrappedKey = aKey; |
| 9509 | float unwrappedValue = [aValue floatValue]; |
| 9510 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9511 | msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9512 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 9513 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9514 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9515 | WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 9516 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9517 | } |
| 9518 | |
| 9519 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 9520 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 9521 | [_dictionary setObject:@(value->valueFloat) forKey:key->valueString]; |
| 9522 | } |
| 9523 | |
| 9524 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9525 | [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9526 | #pragma unused(stop) |
| 9527 | block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]); |
| 9528 | }]; |
| 9529 | } |
| 9530 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9531 | - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9532 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 9533 | if (wrapped && value) { |
| 9534 | *value = [wrapped floatValue]; |
| 9535 | } |
| 9536 | return (wrapped != NULL); |
| 9537 | } |
| 9538 | |
| 9539 | - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary { |
| 9540 | if (otherDictionary) { |
| 9541 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 9542 | if (_autocreator) { |
| 9543 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9544 | } |
| 9545 | } |
| 9546 | } |
| 9547 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9548 | - (void)setFloat:(float)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9549 | if (!key) { |
| 9550 | [NSException raise:NSInvalidArgumentException |
| 9551 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9552 | } |
| 9553 | [_dictionary setObject:@(value) forKey:key]; |
| 9554 | if (_autocreator) { |
| 9555 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9556 | } |
| 9557 | } |
| 9558 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9559 | - (void)removeFloatForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9560 | [_dictionary removeObjectForKey:aKey]; |
| 9561 | } |
| 9562 | |
| 9563 | - (void)removeAll { |
| 9564 | [_dictionary removeAllObjects]; |
| 9565 | } |
| 9566 | |
| 9567 | @end |
| 9568 | |
| 9569 | #pragma mark - String -> Double |
| 9570 | |
| 9571 | @implementation GPBStringDoubleDictionary { |
| 9572 | @package |
| 9573 | NSMutableDictionary *_dictionary; |
| 9574 | } |
| 9575 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9576 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9577 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9578 | } |
| 9579 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9580 | - (instancetype)initWithDoubles:(const double [])values |
| 9581 | forKeys:(const NSString * [])keys |
| 9582 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9583 | self = [super init]; |
| 9584 | if (self) { |
| 9585 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 9586 | if (count && values && keys) { |
| 9587 | for (NSUInteger i = 0; i < count; ++i) { |
| 9588 | if (!keys[i]) { |
| 9589 | [NSException raise:NSInvalidArgumentException |
| 9590 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9591 | } |
| 9592 | [_dictionary setObject:@(values[i]) forKey:keys[i]]; |
| 9593 | } |
| 9594 | } |
| 9595 | } |
| 9596 | return self; |
| 9597 | } |
| 9598 | |
| 9599 | - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9600 | self = [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9601 | if (self) { |
| 9602 | if (dictionary) { |
| 9603 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 9604 | } |
| 9605 | } |
| 9606 | return self; |
| 9607 | } |
| 9608 | |
| 9609 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 9610 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9611 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9612 | } |
| 9613 | |
| 9614 | - (void)dealloc { |
| 9615 | NSAssert(!_autocreator, |
| 9616 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 9617 | [self class], _autocreator); |
| 9618 | [_dictionary release]; |
| 9619 | [super dealloc]; |
| 9620 | } |
| 9621 | |
| 9622 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 9623 | return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self]; |
| 9624 | } |
| 9625 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9626 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9627 | if (self == other) { |
| 9628 | return YES; |
| 9629 | } |
| 9630 | if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) { |
| 9631 | return NO; |
| 9632 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9633 | GPBStringDoubleDictionary *otherDictionary = other; |
| 9634 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9635 | } |
| 9636 | |
| 9637 | - (NSUInteger)hash { |
| 9638 | return _dictionary.count; |
| 9639 | } |
| 9640 | |
| 9641 | - (NSString *)description { |
| 9642 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 9643 | } |
| 9644 | |
| 9645 | - (NSUInteger)count { |
| 9646 | return _dictionary.count; |
| 9647 | } |
| 9648 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9649 | - (void)enumerateKeysAndDoublesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9650 | (void (^)(NSString *key, double value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9651 | BOOL stop = NO; |
| 9652 | NSDictionary *internal = _dictionary; |
| 9653 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9654 | NSString *aKey; |
| 9655 | while ((aKey = [keys nextObject])) { |
| 9656 | NSNumber *aValue = internal[aKey]; |
| 9657 | block(aKey, [aValue doubleValue], &stop); |
| 9658 | if (stop) { |
| 9659 | break; |
| 9660 | } |
| 9661 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9662 | } |
| 9663 | |
| 9664 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9665 | NSDictionary *internal = _dictionary; |
| 9666 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9667 | if (count == 0) { |
| 9668 | return 0; |
| 9669 | } |
| 9670 | |
| 9671 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9672 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9673 | size_t result = 0; |
| 9674 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9675 | NSString *aKey; |
| 9676 | while ((aKey = [keys nextObject])) { |
| 9677 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9678 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 9679 | msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType); |
| 9680 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9681 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9682 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 9683 | result += tagSize * count; |
| 9684 | return result; |
| 9685 | } |
| 9686 | |
| 9687 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 9688 | asField:(GPBFieldDescriptor *)field { |
| 9689 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9690 | GPBDataType keyDataType = field.mapKeyDataType; |
| 9691 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9692 | NSDictionary *internal = _dictionary; |
| 9693 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9694 | NSString *aKey; |
| 9695 | while ((aKey = [keys nextObject])) { |
| 9696 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9697 | [outputStream writeInt32NoTag:tag]; |
| 9698 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9699 | NSString *unwrappedKey = aKey; |
| 9700 | double unwrappedValue = [aValue doubleValue]; |
| 9701 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9702 | msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9703 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 9704 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9705 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9706 | WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 9707 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9708 | } |
| 9709 | |
| 9710 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 9711 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 9712 | [_dictionary setObject:@(value->valueDouble) forKey:key->valueString]; |
| 9713 | } |
| 9714 | |
| 9715 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9716 | [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9717 | #pragma unused(stop) |
| 9718 | block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]); |
| 9719 | }]; |
| 9720 | } |
| 9721 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9722 | - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9723 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 9724 | if (wrapped && value) { |
| 9725 | *value = [wrapped doubleValue]; |
| 9726 | } |
| 9727 | return (wrapped != NULL); |
| 9728 | } |
| 9729 | |
| 9730 | - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary { |
| 9731 | if (otherDictionary) { |
| 9732 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 9733 | if (_autocreator) { |
| 9734 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9735 | } |
| 9736 | } |
| 9737 | } |
| 9738 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9739 | - (void)setDouble:(double)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9740 | if (!key) { |
| 9741 | [NSException raise:NSInvalidArgumentException |
| 9742 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9743 | } |
| 9744 | [_dictionary setObject:@(value) forKey:key]; |
| 9745 | if (_autocreator) { |
| 9746 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9747 | } |
| 9748 | } |
| 9749 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9750 | - (void)removeDoubleForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9751 | [_dictionary removeObjectForKey:aKey]; |
| 9752 | } |
| 9753 | |
| 9754 | - (void)removeAll { |
| 9755 | [_dictionary removeAllObjects]; |
| 9756 | } |
| 9757 | |
| 9758 | @end |
| 9759 | |
| 9760 | #pragma mark - String -> Enum |
| 9761 | |
| 9762 | @implementation GPBStringEnumDictionary { |
| 9763 | @package |
| 9764 | NSMutableDictionary *_dictionary; |
| 9765 | GPBEnumValidationFunc _validationFunc; |
| 9766 | } |
| 9767 | |
| 9768 | @synthesize validationFunc = _validationFunc; |
| 9769 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9770 | - (instancetype)init { |
| 9771 | return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 9772 | } |
| 9773 | |
| 9774 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 9775 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 9776 | } |
| 9777 | |
| 9778 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 9779 | rawValues:(const int32_t [])rawValues |
| 9780 | forKeys:(const NSString * [])keys |
| 9781 | count:(NSUInteger)count { |
| 9782 | self = [super init]; |
| 9783 | if (self) { |
| 9784 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 9785 | _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 9786 | if (count && rawValues && keys) { |
| 9787 | for (NSUInteger i = 0; i < count; ++i) { |
| 9788 | if (!keys[i]) { |
| 9789 | [NSException raise:NSInvalidArgumentException |
| 9790 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9791 | } |
| 9792 | [_dictionary setObject:@(rawValues[i]) forKey:keys[i]]; |
| 9793 | } |
| 9794 | } |
| 9795 | } |
| 9796 | return self; |
| 9797 | } |
| 9798 | |
| 9799 | - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary { |
| 9800 | self = [self initWithValidationFunction:dictionary.validationFunc |
| 9801 | rawValues:NULL |
| 9802 | forKeys:NULL |
| 9803 | count:0]; |
| 9804 | if (self) { |
| 9805 | if (dictionary) { |
| 9806 | [_dictionary addEntriesFromDictionary:dictionary->_dictionary]; |
| 9807 | } |
| 9808 | } |
| 9809 | return self; |
| 9810 | } |
| 9811 | |
| 9812 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 9813 | capacity:(NSUInteger)numItems { |
| 9814 | #pragma unused(numItems) |
| 9815 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 9816 | } |
| 9817 | |
| 9818 | - (void)dealloc { |
| 9819 | NSAssert(!_autocreator, |
| 9820 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 9821 | [self class], _autocreator); |
| 9822 | [_dictionary release]; |
| 9823 | [super dealloc]; |
| 9824 | } |
| 9825 | |
| 9826 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 9827 | return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self]; |
| 9828 | } |
| 9829 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9830 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9831 | if (self == other) { |
| 9832 | return YES; |
| 9833 | } |
| 9834 | if (![other isKindOfClass:[GPBStringEnumDictionary class]]) { |
| 9835 | return NO; |
| 9836 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9837 | GPBStringEnumDictionary *otherDictionary = other; |
| 9838 | return [_dictionary isEqual:otherDictionary->_dictionary]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9839 | } |
| 9840 | |
| 9841 | - (NSUInteger)hash { |
| 9842 | return _dictionary.count; |
| 9843 | } |
| 9844 | |
| 9845 | - (NSString *)description { |
| 9846 | return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary]; |
| 9847 | } |
| 9848 | |
| 9849 | - (NSUInteger)count { |
| 9850 | return _dictionary.count; |
| 9851 | } |
| 9852 | |
| 9853 | - (void)enumerateKeysAndRawValuesUsingBlock: |
| 9854 | (void (^)(NSString *key, int32_t value, BOOL *stop))block { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9855 | BOOL stop = NO; |
| 9856 | NSDictionary *internal = _dictionary; |
| 9857 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9858 | NSString *aKey; |
| 9859 | while ((aKey = [keys nextObject])) { |
| 9860 | NSNumber *aValue = internal[aKey]; |
| 9861 | block(aKey, [aValue intValue], &stop); |
| 9862 | if (stop) { |
| 9863 | break; |
| 9864 | } |
| 9865 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9866 | } |
| 9867 | |
| 9868 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9869 | NSDictionary *internal = _dictionary; |
| 9870 | NSUInteger count = internal.count; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9871 | if (count == 0) { |
| 9872 | return 0; |
| 9873 | } |
| 9874 | |
| 9875 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9876 | GPBDataType keyDataType = field.mapKeyDataType; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9877 | size_t result = 0; |
| 9878 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9879 | NSString *aKey; |
| 9880 | while ((aKey = [keys nextObject])) { |
| 9881 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9882 | size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType); |
| 9883 | msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType); |
| 9884 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9885 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9886 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 9887 | result += tagSize * count; |
| 9888 | return result; |
| 9889 | } |
| 9890 | |
| 9891 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 9892 | asField:(GPBFieldDescriptor *)field { |
| 9893 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 9894 | GPBDataType keyDataType = field.mapKeyDataType; |
| 9895 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9896 | NSDictionary *internal = _dictionary; |
| 9897 | NSEnumerator *keys = [internal keyEnumerator]; |
| 9898 | NSString *aKey; |
| 9899 | while ((aKey = [keys nextObject])) { |
| 9900 | NSNumber *aValue = internal[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9901 | [outputStream writeInt32NoTag:tag]; |
| 9902 | // Write the size of the message. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9903 | NSString *unwrappedKey = aKey; |
| 9904 | int32_t unwrappedValue = [aValue intValue]; |
| 9905 | size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9906 | msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9907 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 9908 | // Write the fields. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9909 | WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType); |
| 9910 | WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType); |
| 9911 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9912 | } |
| 9913 | |
| 9914 | - (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 9915 | forKey:(GPBGenericValue *)key |
| 9916 | keyDataType:(GPBDataType)keyDataType { |
| 9917 | size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType); |
| 9918 | msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 9919 | NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 9920 | GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 9921 | WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType); |
| 9922 | WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 9923 | [outputStream release]; |
| 9924 | return data; |
| 9925 | } |
| 9926 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 9927 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 9928 | [_dictionary setObject:@(value->valueEnum) forKey:key->valueString]; |
| 9929 | } |
| 9930 | |
| 9931 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 9932 | [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) { |
| 9933 | #pragma unused(stop) |
| 9934 | block(key, @(value)); |
| 9935 | }]; |
| 9936 | } |
| 9937 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9938 | - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9939 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 9940 | if (wrapped && value) { |
| 9941 | int32_t result = [wrapped intValue]; |
| 9942 | if (!_validationFunc(result)) { |
| 9943 | result = kGPBUnrecognizedEnumeratorValue; |
| 9944 | } |
| 9945 | *value = result; |
| 9946 | } |
| 9947 | return (wrapped != NULL); |
| 9948 | } |
| 9949 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9950 | - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9951 | NSNumber *wrapped = [_dictionary objectForKey:key]; |
| 9952 | if (wrapped && rawValue) { |
| 9953 | *rawValue = [wrapped intValue]; |
| 9954 | } |
| 9955 | return (wrapped != NULL); |
| 9956 | } |
| 9957 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9958 | - (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9959 | (void (^)(NSString *key, int32_t value, BOOL *stop))block { |
| 9960 | GPBEnumValidationFunc func = _validationFunc; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9961 | BOOL stop = NO; |
| 9962 | NSEnumerator *keys = [_dictionary keyEnumerator]; |
| 9963 | NSString *aKey; |
| 9964 | while ((aKey = [keys nextObject])) { |
| 9965 | NSNumber *aValue = _dictionary[aKey]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9966 | int32_t unwrapped = [aValue intValue]; |
| 9967 | if (!func(unwrapped)) { |
| 9968 | unwrapped = kGPBUnrecognizedEnumeratorValue; |
| 9969 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9970 | block(aKey, unwrapped, &stop); |
| 9971 | if (stop) { |
| 9972 | break; |
| 9973 | } |
| 9974 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9975 | } |
| 9976 | |
| 9977 | - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary { |
| 9978 | if (otherDictionary) { |
| 9979 | [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary]; |
| 9980 | if (_autocreator) { |
| 9981 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9982 | } |
| 9983 | } |
| 9984 | } |
| 9985 | |
| 9986 | - (void)setRawValue:(int32_t)value forKey:(NSString *)key { |
| 9987 | if (!key) { |
| 9988 | [NSException raise:NSInvalidArgumentException |
| 9989 | format:@"Attempting to add nil key to a Dictionary"]; |
| 9990 | } |
| 9991 | [_dictionary setObject:@(value) forKey:key]; |
| 9992 | if (_autocreator) { |
| 9993 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 9994 | } |
| 9995 | } |
| 9996 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 9997 | - (void)removeEnumForKey:(NSString *)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9998 | [_dictionary removeObjectForKey:aKey]; |
| 9999 | } |
| 10000 | |
| 10001 | - (void)removeAll { |
| 10002 | [_dictionary removeAllObjects]; |
| 10003 | } |
| 10004 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10005 | - (void)setEnum:(int32_t)value forKey:(NSString *)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10006 | if (!key) { |
| 10007 | [NSException raise:NSInvalidArgumentException |
| 10008 | format:@"Attempting to add nil key to a Dictionary"]; |
| 10009 | } |
| 10010 | if (!_validationFunc(value)) { |
| 10011 | [NSException raise:NSInvalidArgumentException |
| 10012 | format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)", |
| 10013 | value]; |
| 10014 | } |
| 10015 | |
| 10016 | [_dictionary setObject:@(value) forKey:key]; |
| 10017 | if (_autocreator) { |
| 10018 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10019 | } |
| 10020 | } |
| 10021 | |
| 10022 | @end |
| 10023 | |
| 10024 | //%PDDM-EXPAND-END (5 expansions) |
| 10025 | |
| 10026 | |
| 10027 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t) |
| 10028 | // This block of code is generated, do not edit it directly. |
| 10029 | |
| 10030 | #pragma mark - Bool -> UInt32 |
| 10031 | |
| 10032 | @implementation GPBBoolUInt32Dictionary { |
| 10033 | @package |
| 10034 | uint32_t _values[2]; |
| 10035 | BOOL _valueSet[2]; |
| 10036 | } |
| 10037 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10038 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10039 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10040 | } |
| 10041 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10042 | - (instancetype)initWithUInt32s:(const uint32_t [])values |
| 10043 | forKeys:(const BOOL [])keys |
| 10044 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10045 | self = [super init]; |
| 10046 | if (self) { |
| 10047 | for (NSUInteger i = 0; i < count; ++i) { |
| 10048 | int idx = keys[i] ? 1 : 0; |
| 10049 | _values[idx] = values[i]; |
| 10050 | _valueSet[idx] = YES; |
| 10051 | } |
| 10052 | } |
| 10053 | return self; |
| 10054 | } |
| 10055 | |
| 10056 | - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10057 | self = [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10058 | if (self) { |
| 10059 | if (dictionary) { |
| 10060 | for (int i = 0; i < 2; ++i) { |
| 10061 | if (dictionary->_valueSet[i]) { |
| 10062 | _values[i] = dictionary->_values[i]; |
| 10063 | _valueSet[i] = YES; |
| 10064 | } |
| 10065 | } |
| 10066 | } |
| 10067 | } |
| 10068 | return self; |
| 10069 | } |
| 10070 | |
| 10071 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 10072 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10073 | return [self initWithUInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10074 | } |
| 10075 | |
| 10076 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 10077 | - (void)dealloc { |
| 10078 | NSAssert(!_autocreator, |
| 10079 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 10080 | [self class], _autocreator); |
| 10081 | [super dealloc]; |
| 10082 | } |
| 10083 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 10084 | |
| 10085 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 10086 | return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 10087 | } |
| 10088 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10089 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10090 | if (self == other) { |
| 10091 | return YES; |
| 10092 | } |
| 10093 | if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) { |
| 10094 | return NO; |
| 10095 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10096 | GPBBoolUInt32Dictionary *otherDictionary = other; |
| 10097 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 10098 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10099 | return NO; |
| 10100 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10101 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 10102 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10103 | return NO; |
| 10104 | } |
| 10105 | return YES; |
| 10106 | } |
| 10107 | |
| 10108 | - (NSUInteger)hash { |
| 10109 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10110 | } |
| 10111 | |
| 10112 | - (NSString *)description { |
| 10113 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 10114 | if (_valueSet[0]) { |
| 10115 | [result appendFormat:@"NO: %u", _values[0]]; |
| 10116 | } |
| 10117 | if (_valueSet[1]) { |
| 10118 | [result appendFormat:@"YES: %u", _values[1]]; |
| 10119 | } |
| 10120 | [result appendString:@" }"]; |
| 10121 | return result; |
| 10122 | } |
| 10123 | |
| 10124 | - (NSUInteger)count { |
| 10125 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10126 | } |
| 10127 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10128 | - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10129 | int idx = (key ? 1 : 0); |
| 10130 | if (_valueSet[idx]) { |
| 10131 | if (value) { |
| 10132 | *value = _values[idx]; |
| 10133 | } |
| 10134 | return YES; |
| 10135 | } |
| 10136 | return NO; |
| 10137 | } |
| 10138 | |
| 10139 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 10140 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 10141 | int idx = (key->valueBool ? 1 : 0); |
| 10142 | _values[idx] = value->valueUInt32; |
| 10143 | _valueSet[idx] = YES; |
| 10144 | } |
| 10145 | |
| 10146 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 10147 | if (_valueSet[0]) { |
| 10148 | block(@"false", [NSString stringWithFormat:@"%u", _values[0]]); |
| 10149 | } |
| 10150 | if (_valueSet[1]) { |
| 10151 | block(@"true", [NSString stringWithFormat:@"%u", _values[1]]); |
| 10152 | } |
| 10153 | } |
| 10154 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10155 | - (void)enumerateKeysAndUInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10156 | (void (^)(BOOL key, uint32_t value, BOOL *stop))block { |
| 10157 | BOOL stop = NO; |
| 10158 | if (_valueSet[0]) { |
| 10159 | block(NO, _values[0], &stop); |
| 10160 | } |
| 10161 | if (!stop && _valueSet[1]) { |
| 10162 | block(YES, _values[1], &stop); |
| 10163 | } |
| 10164 | } |
| 10165 | |
| 10166 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 10167 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10168 | NSUInteger count = 0; |
| 10169 | size_t result = 0; |
| 10170 | for (int i = 0; i < 2; ++i) { |
| 10171 | if (_valueSet[i]) { |
| 10172 | ++count; |
| 10173 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10174 | msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10175 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 10176 | } |
| 10177 | } |
| 10178 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 10179 | result += tagSize * count; |
| 10180 | return result; |
| 10181 | } |
| 10182 | |
| 10183 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 10184 | asField:(GPBFieldDescriptor *)field { |
| 10185 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10186 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 10187 | for (int i = 0; i < 2; ++i) { |
| 10188 | if (_valueSet[i]) { |
| 10189 | // Write the tag. |
| 10190 | [outputStream writeInt32NoTag:tag]; |
| 10191 | // Write the size of the message. |
| 10192 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10193 | msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10194 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 10195 | // Write the fields. |
| 10196 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10197 | WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 10198 | } |
| 10199 | } |
| 10200 | } |
| 10201 | |
| 10202 | - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary { |
| 10203 | if (otherDictionary) { |
| 10204 | for (int i = 0; i < 2; ++i) { |
| 10205 | if (otherDictionary->_valueSet[i]) { |
| 10206 | _valueSet[i] = YES; |
| 10207 | _values[i] = otherDictionary->_values[i]; |
| 10208 | } |
| 10209 | } |
| 10210 | if (_autocreator) { |
| 10211 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10212 | } |
| 10213 | } |
| 10214 | } |
| 10215 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10216 | - (void)setUInt32:(uint32_t)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10217 | int idx = (key ? 1 : 0); |
| 10218 | _values[idx] = value; |
| 10219 | _valueSet[idx] = YES; |
| 10220 | if (_autocreator) { |
| 10221 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10222 | } |
| 10223 | } |
| 10224 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10225 | - (void)removeUInt32ForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10226 | _valueSet[aKey ? 1 : 0] = NO; |
| 10227 | } |
| 10228 | |
| 10229 | - (void)removeAll { |
| 10230 | _valueSet[0] = NO; |
| 10231 | _valueSet[1] = NO; |
| 10232 | } |
| 10233 | |
| 10234 | @end |
| 10235 | |
| 10236 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t) |
| 10237 | // This block of code is generated, do not edit it directly. |
| 10238 | |
| 10239 | #pragma mark - Bool -> Int32 |
| 10240 | |
| 10241 | @implementation GPBBoolInt32Dictionary { |
| 10242 | @package |
| 10243 | int32_t _values[2]; |
| 10244 | BOOL _valueSet[2]; |
| 10245 | } |
| 10246 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10247 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10248 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10249 | } |
| 10250 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10251 | - (instancetype)initWithInt32s:(const int32_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10252 | forKeys:(const BOOL [])keys |
| 10253 | count:(NSUInteger)count { |
| 10254 | self = [super init]; |
| 10255 | if (self) { |
| 10256 | for (NSUInteger i = 0; i < count; ++i) { |
| 10257 | int idx = keys[i] ? 1 : 0; |
| 10258 | _values[idx] = values[i]; |
| 10259 | _valueSet[idx] = YES; |
| 10260 | } |
| 10261 | } |
| 10262 | return self; |
| 10263 | } |
| 10264 | |
| 10265 | - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10266 | self = [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10267 | if (self) { |
| 10268 | if (dictionary) { |
| 10269 | for (int i = 0; i < 2; ++i) { |
| 10270 | if (dictionary->_valueSet[i]) { |
| 10271 | _values[i] = dictionary->_values[i]; |
| 10272 | _valueSet[i] = YES; |
| 10273 | } |
| 10274 | } |
| 10275 | } |
| 10276 | } |
| 10277 | return self; |
| 10278 | } |
| 10279 | |
| 10280 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 10281 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10282 | return [self initWithInt32s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10283 | } |
| 10284 | |
| 10285 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 10286 | - (void)dealloc { |
| 10287 | NSAssert(!_autocreator, |
| 10288 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 10289 | [self class], _autocreator); |
| 10290 | [super dealloc]; |
| 10291 | } |
| 10292 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 10293 | |
| 10294 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 10295 | return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 10296 | } |
| 10297 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10298 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10299 | if (self == other) { |
| 10300 | return YES; |
| 10301 | } |
| 10302 | if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) { |
| 10303 | return NO; |
| 10304 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10305 | GPBBoolInt32Dictionary *otherDictionary = other; |
| 10306 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 10307 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10308 | return NO; |
| 10309 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10310 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 10311 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10312 | return NO; |
| 10313 | } |
| 10314 | return YES; |
| 10315 | } |
| 10316 | |
| 10317 | - (NSUInteger)hash { |
| 10318 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10319 | } |
| 10320 | |
| 10321 | - (NSString *)description { |
| 10322 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 10323 | if (_valueSet[0]) { |
| 10324 | [result appendFormat:@"NO: %d", _values[0]]; |
| 10325 | } |
| 10326 | if (_valueSet[1]) { |
| 10327 | [result appendFormat:@"YES: %d", _values[1]]; |
| 10328 | } |
| 10329 | [result appendString:@" }"]; |
| 10330 | return result; |
| 10331 | } |
| 10332 | |
| 10333 | - (NSUInteger)count { |
| 10334 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10335 | } |
| 10336 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10337 | - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10338 | int idx = (key ? 1 : 0); |
| 10339 | if (_valueSet[idx]) { |
| 10340 | if (value) { |
| 10341 | *value = _values[idx]; |
| 10342 | } |
| 10343 | return YES; |
| 10344 | } |
| 10345 | return NO; |
| 10346 | } |
| 10347 | |
| 10348 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 10349 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 10350 | int idx = (key->valueBool ? 1 : 0); |
| 10351 | _values[idx] = value->valueInt32; |
| 10352 | _valueSet[idx] = YES; |
| 10353 | } |
| 10354 | |
| 10355 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 10356 | if (_valueSet[0]) { |
| 10357 | block(@"false", [NSString stringWithFormat:@"%d", _values[0]]); |
| 10358 | } |
| 10359 | if (_valueSet[1]) { |
| 10360 | block(@"true", [NSString stringWithFormat:@"%d", _values[1]]); |
| 10361 | } |
| 10362 | } |
| 10363 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10364 | - (void)enumerateKeysAndInt32sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10365 | (void (^)(BOOL key, int32_t value, BOOL *stop))block { |
| 10366 | BOOL stop = NO; |
| 10367 | if (_valueSet[0]) { |
| 10368 | block(NO, _values[0], &stop); |
| 10369 | } |
| 10370 | if (!stop && _valueSet[1]) { |
| 10371 | block(YES, _values[1], &stop); |
| 10372 | } |
| 10373 | } |
| 10374 | |
| 10375 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 10376 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10377 | NSUInteger count = 0; |
| 10378 | size_t result = 0; |
| 10379 | for (int i = 0; i < 2; ++i) { |
| 10380 | if (_valueSet[i]) { |
| 10381 | ++count; |
| 10382 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10383 | msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10384 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 10385 | } |
| 10386 | } |
| 10387 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 10388 | result += tagSize * count; |
| 10389 | return result; |
| 10390 | } |
| 10391 | |
| 10392 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 10393 | asField:(GPBFieldDescriptor *)field { |
| 10394 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10395 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 10396 | for (int i = 0; i < 2; ++i) { |
| 10397 | if (_valueSet[i]) { |
| 10398 | // Write the tag. |
| 10399 | [outputStream writeInt32NoTag:tag]; |
| 10400 | // Write the size of the message. |
| 10401 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10402 | msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10403 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 10404 | // Write the fields. |
| 10405 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10406 | WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 10407 | } |
| 10408 | } |
| 10409 | } |
| 10410 | |
| 10411 | - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary { |
| 10412 | if (otherDictionary) { |
| 10413 | for (int i = 0; i < 2; ++i) { |
| 10414 | if (otherDictionary->_valueSet[i]) { |
| 10415 | _valueSet[i] = YES; |
| 10416 | _values[i] = otherDictionary->_values[i]; |
| 10417 | } |
| 10418 | } |
| 10419 | if (_autocreator) { |
| 10420 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10421 | } |
| 10422 | } |
| 10423 | } |
| 10424 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10425 | - (void)setInt32:(int32_t)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10426 | int idx = (key ? 1 : 0); |
| 10427 | _values[idx] = value; |
| 10428 | _valueSet[idx] = YES; |
| 10429 | if (_autocreator) { |
| 10430 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10431 | } |
| 10432 | } |
| 10433 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10434 | - (void)removeInt32ForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10435 | _valueSet[aKey ? 1 : 0] = NO; |
| 10436 | } |
| 10437 | |
| 10438 | - (void)removeAll { |
| 10439 | _valueSet[0] = NO; |
| 10440 | _valueSet[1] = NO; |
| 10441 | } |
| 10442 | |
| 10443 | @end |
| 10444 | |
| 10445 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t) |
| 10446 | // This block of code is generated, do not edit it directly. |
| 10447 | |
| 10448 | #pragma mark - Bool -> UInt64 |
| 10449 | |
| 10450 | @implementation GPBBoolUInt64Dictionary { |
| 10451 | @package |
| 10452 | uint64_t _values[2]; |
| 10453 | BOOL _valueSet[2]; |
| 10454 | } |
| 10455 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10456 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10457 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10458 | } |
| 10459 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10460 | - (instancetype)initWithUInt64s:(const uint64_t [])values |
| 10461 | forKeys:(const BOOL [])keys |
| 10462 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10463 | self = [super init]; |
| 10464 | if (self) { |
| 10465 | for (NSUInteger i = 0; i < count; ++i) { |
| 10466 | int idx = keys[i] ? 1 : 0; |
| 10467 | _values[idx] = values[i]; |
| 10468 | _valueSet[idx] = YES; |
| 10469 | } |
| 10470 | } |
| 10471 | return self; |
| 10472 | } |
| 10473 | |
| 10474 | - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10475 | self = [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10476 | if (self) { |
| 10477 | if (dictionary) { |
| 10478 | for (int i = 0; i < 2; ++i) { |
| 10479 | if (dictionary->_valueSet[i]) { |
| 10480 | _values[i] = dictionary->_values[i]; |
| 10481 | _valueSet[i] = YES; |
| 10482 | } |
| 10483 | } |
| 10484 | } |
| 10485 | } |
| 10486 | return self; |
| 10487 | } |
| 10488 | |
| 10489 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 10490 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10491 | return [self initWithUInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10492 | } |
| 10493 | |
| 10494 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 10495 | - (void)dealloc { |
| 10496 | NSAssert(!_autocreator, |
| 10497 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 10498 | [self class], _autocreator); |
| 10499 | [super dealloc]; |
| 10500 | } |
| 10501 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 10502 | |
| 10503 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 10504 | return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 10505 | } |
| 10506 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10507 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10508 | if (self == other) { |
| 10509 | return YES; |
| 10510 | } |
| 10511 | if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) { |
| 10512 | return NO; |
| 10513 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10514 | GPBBoolUInt64Dictionary *otherDictionary = other; |
| 10515 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 10516 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10517 | return NO; |
| 10518 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10519 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 10520 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10521 | return NO; |
| 10522 | } |
| 10523 | return YES; |
| 10524 | } |
| 10525 | |
| 10526 | - (NSUInteger)hash { |
| 10527 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10528 | } |
| 10529 | |
| 10530 | - (NSString *)description { |
| 10531 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 10532 | if (_valueSet[0]) { |
| 10533 | [result appendFormat:@"NO: %llu", _values[0]]; |
| 10534 | } |
| 10535 | if (_valueSet[1]) { |
| 10536 | [result appendFormat:@"YES: %llu", _values[1]]; |
| 10537 | } |
| 10538 | [result appendString:@" }"]; |
| 10539 | return result; |
| 10540 | } |
| 10541 | |
| 10542 | - (NSUInteger)count { |
| 10543 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10544 | } |
| 10545 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10546 | - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10547 | int idx = (key ? 1 : 0); |
| 10548 | if (_valueSet[idx]) { |
| 10549 | if (value) { |
| 10550 | *value = _values[idx]; |
| 10551 | } |
| 10552 | return YES; |
| 10553 | } |
| 10554 | return NO; |
| 10555 | } |
| 10556 | |
| 10557 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 10558 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 10559 | int idx = (key->valueBool ? 1 : 0); |
| 10560 | _values[idx] = value->valueUInt64; |
| 10561 | _valueSet[idx] = YES; |
| 10562 | } |
| 10563 | |
| 10564 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 10565 | if (_valueSet[0]) { |
| 10566 | block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]); |
| 10567 | } |
| 10568 | if (_valueSet[1]) { |
| 10569 | block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]); |
| 10570 | } |
| 10571 | } |
| 10572 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10573 | - (void)enumerateKeysAndUInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10574 | (void (^)(BOOL key, uint64_t value, BOOL *stop))block { |
| 10575 | BOOL stop = NO; |
| 10576 | if (_valueSet[0]) { |
| 10577 | block(NO, _values[0], &stop); |
| 10578 | } |
| 10579 | if (!stop && _valueSet[1]) { |
| 10580 | block(YES, _values[1], &stop); |
| 10581 | } |
| 10582 | } |
| 10583 | |
| 10584 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 10585 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10586 | NSUInteger count = 0; |
| 10587 | size_t result = 0; |
| 10588 | for (int i = 0; i < 2; ++i) { |
| 10589 | if (_valueSet[i]) { |
| 10590 | ++count; |
| 10591 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10592 | msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10593 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 10594 | } |
| 10595 | } |
| 10596 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 10597 | result += tagSize * count; |
| 10598 | return result; |
| 10599 | } |
| 10600 | |
| 10601 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 10602 | asField:(GPBFieldDescriptor *)field { |
| 10603 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10604 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 10605 | for (int i = 0; i < 2; ++i) { |
| 10606 | if (_valueSet[i]) { |
| 10607 | // Write the tag. |
| 10608 | [outputStream writeInt32NoTag:tag]; |
| 10609 | // Write the size of the message. |
| 10610 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10611 | msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10612 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 10613 | // Write the fields. |
| 10614 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10615 | WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 10616 | } |
| 10617 | } |
| 10618 | } |
| 10619 | |
| 10620 | - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary { |
| 10621 | if (otherDictionary) { |
| 10622 | for (int i = 0; i < 2; ++i) { |
| 10623 | if (otherDictionary->_valueSet[i]) { |
| 10624 | _valueSet[i] = YES; |
| 10625 | _values[i] = otherDictionary->_values[i]; |
| 10626 | } |
| 10627 | } |
| 10628 | if (_autocreator) { |
| 10629 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10630 | } |
| 10631 | } |
| 10632 | } |
| 10633 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10634 | - (void)setUInt64:(uint64_t)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10635 | int idx = (key ? 1 : 0); |
| 10636 | _values[idx] = value; |
| 10637 | _valueSet[idx] = YES; |
| 10638 | if (_autocreator) { |
| 10639 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10640 | } |
| 10641 | } |
| 10642 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10643 | - (void)removeUInt64ForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10644 | _valueSet[aKey ? 1 : 0] = NO; |
| 10645 | } |
| 10646 | |
| 10647 | - (void)removeAll { |
| 10648 | _valueSet[0] = NO; |
| 10649 | _valueSet[1] = NO; |
| 10650 | } |
| 10651 | |
| 10652 | @end |
| 10653 | |
| 10654 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t) |
| 10655 | // This block of code is generated, do not edit it directly. |
| 10656 | |
| 10657 | #pragma mark - Bool -> Int64 |
| 10658 | |
| 10659 | @implementation GPBBoolInt64Dictionary { |
| 10660 | @package |
| 10661 | int64_t _values[2]; |
| 10662 | BOOL _valueSet[2]; |
| 10663 | } |
| 10664 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10665 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10666 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10667 | } |
| 10668 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10669 | - (instancetype)initWithInt64s:(const int64_t [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10670 | forKeys:(const BOOL [])keys |
| 10671 | count:(NSUInteger)count { |
| 10672 | self = [super init]; |
| 10673 | if (self) { |
| 10674 | for (NSUInteger i = 0; i < count; ++i) { |
| 10675 | int idx = keys[i] ? 1 : 0; |
| 10676 | _values[idx] = values[i]; |
| 10677 | _valueSet[idx] = YES; |
| 10678 | } |
| 10679 | } |
| 10680 | return self; |
| 10681 | } |
| 10682 | |
| 10683 | - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10684 | self = [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10685 | if (self) { |
| 10686 | if (dictionary) { |
| 10687 | for (int i = 0; i < 2; ++i) { |
| 10688 | if (dictionary->_valueSet[i]) { |
| 10689 | _values[i] = dictionary->_values[i]; |
| 10690 | _valueSet[i] = YES; |
| 10691 | } |
| 10692 | } |
| 10693 | } |
| 10694 | } |
| 10695 | return self; |
| 10696 | } |
| 10697 | |
| 10698 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 10699 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10700 | return [self initWithInt64s:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10701 | } |
| 10702 | |
| 10703 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 10704 | - (void)dealloc { |
| 10705 | NSAssert(!_autocreator, |
| 10706 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 10707 | [self class], _autocreator); |
| 10708 | [super dealloc]; |
| 10709 | } |
| 10710 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 10711 | |
| 10712 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 10713 | return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self]; |
| 10714 | } |
| 10715 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10716 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10717 | if (self == other) { |
| 10718 | return YES; |
| 10719 | } |
| 10720 | if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) { |
| 10721 | return NO; |
| 10722 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10723 | GPBBoolInt64Dictionary *otherDictionary = other; |
| 10724 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 10725 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10726 | return NO; |
| 10727 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10728 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 10729 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10730 | return NO; |
| 10731 | } |
| 10732 | return YES; |
| 10733 | } |
| 10734 | |
| 10735 | - (NSUInteger)hash { |
| 10736 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10737 | } |
| 10738 | |
| 10739 | - (NSString *)description { |
| 10740 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 10741 | if (_valueSet[0]) { |
| 10742 | [result appendFormat:@"NO: %lld", _values[0]]; |
| 10743 | } |
| 10744 | if (_valueSet[1]) { |
| 10745 | [result appendFormat:@"YES: %lld", _values[1]]; |
| 10746 | } |
| 10747 | [result appendString:@" }"]; |
| 10748 | return result; |
| 10749 | } |
| 10750 | |
| 10751 | - (NSUInteger)count { |
| 10752 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10753 | } |
| 10754 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10755 | - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10756 | int idx = (key ? 1 : 0); |
| 10757 | if (_valueSet[idx]) { |
| 10758 | if (value) { |
| 10759 | *value = _values[idx]; |
| 10760 | } |
| 10761 | return YES; |
| 10762 | } |
| 10763 | return NO; |
| 10764 | } |
| 10765 | |
| 10766 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 10767 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 10768 | int idx = (key->valueBool ? 1 : 0); |
| 10769 | _values[idx] = value->valueInt64; |
| 10770 | _valueSet[idx] = YES; |
| 10771 | } |
| 10772 | |
| 10773 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 10774 | if (_valueSet[0]) { |
| 10775 | block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]); |
| 10776 | } |
| 10777 | if (_valueSet[1]) { |
| 10778 | block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]); |
| 10779 | } |
| 10780 | } |
| 10781 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10782 | - (void)enumerateKeysAndInt64sUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10783 | (void (^)(BOOL key, int64_t value, BOOL *stop))block { |
| 10784 | BOOL stop = NO; |
| 10785 | if (_valueSet[0]) { |
| 10786 | block(NO, _values[0], &stop); |
| 10787 | } |
| 10788 | if (!stop && _valueSet[1]) { |
| 10789 | block(YES, _values[1], &stop); |
| 10790 | } |
| 10791 | } |
| 10792 | |
| 10793 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 10794 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10795 | NSUInteger count = 0; |
| 10796 | size_t result = 0; |
| 10797 | for (int i = 0; i < 2; ++i) { |
| 10798 | if (_valueSet[i]) { |
| 10799 | ++count; |
| 10800 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10801 | msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10802 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 10803 | } |
| 10804 | } |
| 10805 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 10806 | result += tagSize * count; |
| 10807 | return result; |
| 10808 | } |
| 10809 | |
| 10810 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 10811 | asField:(GPBFieldDescriptor *)field { |
| 10812 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 10813 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 10814 | for (int i = 0; i < 2; ++i) { |
| 10815 | if (_valueSet[i]) { |
| 10816 | // Write the tag. |
| 10817 | [outputStream writeInt32NoTag:tag]; |
| 10818 | // Write the size of the message. |
| 10819 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10820 | msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 10821 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 10822 | // Write the fields. |
| 10823 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 10824 | WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 10825 | } |
| 10826 | } |
| 10827 | } |
| 10828 | |
| 10829 | - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary { |
| 10830 | if (otherDictionary) { |
| 10831 | for (int i = 0; i < 2; ++i) { |
| 10832 | if (otherDictionary->_valueSet[i]) { |
| 10833 | _valueSet[i] = YES; |
| 10834 | _values[i] = otherDictionary->_values[i]; |
| 10835 | } |
| 10836 | } |
| 10837 | if (_autocreator) { |
| 10838 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10839 | } |
| 10840 | } |
| 10841 | } |
| 10842 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10843 | - (void)setInt64:(int64_t)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10844 | int idx = (key ? 1 : 0); |
| 10845 | _values[idx] = value; |
| 10846 | _valueSet[idx] = YES; |
| 10847 | if (_autocreator) { |
| 10848 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 10849 | } |
| 10850 | } |
| 10851 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10852 | - (void)removeInt64ForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10853 | _valueSet[aKey ? 1 : 0] = NO; |
| 10854 | } |
| 10855 | |
| 10856 | - (void)removeAll { |
| 10857 | _valueSet[0] = NO; |
| 10858 | _valueSet[1] = NO; |
| 10859 | } |
| 10860 | |
| 10861 | @end |
| 10862 | |
| 10863 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL) |
| 10864 | // This block of code is generated, do not edit it directly. |
| 10865 | |
| 10866 | #pragma mark - Bool -> Bool |
| 10867 | |
| 10868 | @implementation GPBBoolBoolDictionary { |
| 10869 | @package |
| 10870 | BOOL _values[2]; |
| 10871 | BOOL _valueSet[2]; |
| 10872 | } |
| 10873 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10874 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10875 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10876 | } |
| 10877 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10878 | - (instancetype)initWithBools:(const BOOL [])values |
| 10879 | forKeys:(const BOOL [])keys |
| 10880 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10881 | self = [super init]; |
| 10882 | if (self) { |
| 10883 | for (NSUInteger i = 0; i < count; ++i) { |
| 10884 | int idx = keys[i] ? 1 : 0; |
| 10885 | _values[idx] = values[i]; |
| 10886 | _valueSet[idx] = YES; |
| 10887 | } |
| 10888 | } |
| 10889 | return self; |
| 10890 | } |
| 10891 | |
| 10892 | - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10893 | self = [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10894 | if (self) { |
| 10895 | if (dictionary) { |
| 10896 | for (int i = 0; i < 2; ++i) { |
| 10897 | if (dictionary->_valueSet[i]) { |
| 10898 | _values[i] = dictionary->_values[i]; |
| 10899 | _valueSet[i] = YES; |
| 10900 | } |
| 10901 | } |
| 10902 | } |
| 10903 | } |
| 10904 | return self; |
| 10905 | } |
| 10906 | |
| 10907 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 10908 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10909 | return [self initWithBools:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10910 | } |
| 10911 | |
| 10912 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 10913 | - (void)dealloc { |
| 10914 | NSAssert(!_autocreator, |
| 10915 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 10916 | [self class], _autocreator); |
| 10917 | [super dealloc]; |
| 10918 | } |
| 10919 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 10920 | |
| 10921 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 10922 | return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self]; |
| 10923 | } |
| 10924 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10925 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10926 | if (self == other) { |
| 10927 | return YES; |
| 10928 | } |
| 10929 | if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) { |
| 10930 | return NO; |
| 10931 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10932 | GPBBoolBoolDictionary *otherDictionary = other; |
| 10933 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 10934 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10935 | return NO; |
| 10936 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10937 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 10938 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10939 | return NO; |
| 10940 | } |
| 10941 | return YES; |
| 10942 | } |
| 10943 | |
| 10944 | - (NSUInteger)hash { |
| 10945 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10946 | } |
| 10947 | |
| 10948 | - (NSString *)description { |
| 10949 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 10950 | if (_valueSet[0]) { |
| 10951 | [result appendFormat:@"NO: %d", _values[0]]; |
| 10952 | } |
| 10953 | if (_valueSet[1]) { |
| 10954 | [result appendFormat:@"YES: %d", _values[1]]; |
| 10955 | } |
| 10956 | [result appendString:@" }"]; |
| 10957 | return result; |
| 10958 | } |
| 10959 | |
| 10960 | - (NSUInteger)count { |
| 10961 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 10962 | } |
| 10963 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10964 | - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10965 | int idx = (key ? 1 : 0); |
| 10966 | if (_valueSet[idx]) { |
| 10967 | if (value) { |
| 10968 | *value = _values[idx]; |
| 10969 | } |
| 10970 | return YES; |
| 10971 | } |
| 10972 | return NO; |
| 10973 | } |
| 10974 | |
| 10975 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 10976 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 10977 | int idx = (key->valueBool ? 1 : 0); |
| 10978 | _values[idx] = value->valueBool; |
| 10979 | _valueSet[idx] = YES; |
| 10980 | } |
| 10981 | |
| 10982 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 10983 | if (_valueSet[0]) { |
| 10984 | block(@"false", (_values[0] ? @"true" : @"false")); |
| 10985 | } |
| 10986 | if (_valueSet[1]) { |
| 10987 | block(@"true", (_values[1] ? @"true" : @"false")); |
| 10988 | } |
| 10989 | } |
| 10990 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 10991 | - (void)enumerateKeysAndBoolsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 10992 | (void (^)(BOOL key, BOOL value, BOOL *stop))block { |
| 10993 | BOOL stop = NO; |
| 10994 | if (_valueSet[0]) { |
| 10995 | block(NO, _values[0], &stop); |
| 10996 | } |
| 10997 | if (!stop && _valueSet[1]) { |
| 10998 | block(YES, _values[1], &stop); |
| 10999 | } |
| 11000 | } |
| 11001 | |
| 11002 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 11003 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11004 | NSUInteger count = 0; |
| 11005 | size_t result = 0; |
| 11006 | for (int i = 0; i < 2; ++i) { |
| 11007 | if (_valueSet[i]) { |
| 11008 | ++count; |
| 11009 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11010 | msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11011 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 11012 | } |
| 11013 | } |
| 11014 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 11015 | result += tagSize * count; |
| 11016 | return result; |
| 11017 | } |
| 11018 | |
| 11019 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 11020 | asField:(GPBFieldDescriptor *)field { |
| 11021 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11022 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 11023 | for (int i = 0; i < 2; ++i) { |
| 11024 | if (_valueSet[i]) { |
| 11025 | // Write the tag. |
| 11026 | [outputStream writeInt32NoTag:tag]; |
| 11027 | // Write the size of the message. |
| 11028 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11029 | msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11030 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 11031 | // Write the fields. |
| 11032 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11033 | WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 11034 | } |
| 11035 | } |
| 11036 | } |
| 11037 | |
| 11038 | - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary { |
| 11039 | if (otherDictionary) { |
| 11040 | for (int i = 0; i < 2; ++i) { |
| 11041 | if (otherDictionary->_valueSet[i]) { |
| 11042 | _valueSet[i] = YES; |
| 11043 | _values[i] = otherDictionary->_values[i]; |
| 11044 | } |
| 11045 | } |
| 11046 | if (_autocreator) { |
| 11047 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11048 | } |
| 11049 | } |
| 11050 | } |
| 11051 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11052 | - (void)setBool:(BOOL)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11053 | int idx = (key ? 1 : 0); |
| 11054 | _values[idx] = value; |
| 11055 | _valueSet[idx] = YES; |
| 11056 | if (_autocreator) { |
| 11057 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11058 | } |
| 11059 | } |
| 11060 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11061 | - (void)removeBoolForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11062 | _valueSet[aKey ? 1 : 0] = NO; |
| 11063 | } |
| 11064 | |
| 11065 | - (void)removeAll { |
| 11066 | _valueSet[0] = NO; |
| 11067 | _valueSet[1] = NO; |
| 11068 | } |
| 11069 | |
| 11070 | @end |
| 11071 | |
| 11072 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float) |
| 11073 | // This block of code is generated, do not edit it directly. |
| 11074 | |
| 11075 | #pragma mark - Bool -> Float |
| 11076 | |
| 11077 | @implementation GPBBoolFloatDictionary { |
| 11078 | @package |
| 11079 | float _values[2]; |
| 11080 | BOOL _valueSet[2]; |
| 11081 | } |
| 11082 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11083 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11084 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11085 | } |
| 11086 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11087 | - (instancetype)initWithFloats:(const float [])values |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11088 | forKeys:(const BOOL [])keys |
| 11089 | count:(NSUInteger)count { |
| 11090 | self = [super init]; |
| 11091 | if (self) { |
| 11092 | for (NSUInteger i = 0; i < count; ++i) { |
| 11093 | int idx = keys[i] ? 1 : 0; |
| 11094 | _values[idx] = values[i]; |
| 11095 | _valueSet[idx] = YES; |
| 11096 | } |
| 11097 | } |
| 11098 | return self; |
| 11099 | } |
| 11100 | |
| 11101 | - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11102 | self = [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11103 | if (self) { |
| 11104 | if (dictionary) { |
| 11105 | for (int i = 0; i < 2; ++i) { |
| 11106 | if (dictionary->_valueSet[i]) { |
| 11107 | _values[i] = dictionary->_values[i]; |
| 11108 | _valueSet[i] = YES; |
| 11109 | } |
| 11110 | } |
| 11111 | } |
| 11112 | } |
| 11113 | return self; |
| 11114 | } |
| 11115 | |
| 11116 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 11117 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11118 | return [self initWithFloats:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11119 | } |
| 11120 | |
| 11121 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 11122 | - (void)dealloc { |
| 11123 | NSAssert(!_autocreator, |
| 11124 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 11125 | [self class], _autocreator); |
| 11126 | [super dealloc]; |
| 11127 | } |
| 11128 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 11129 | |
| 11130 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 11131 | return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self]; |
| 11132 | } |
| 11133 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11134 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11135 | if (self == other) { |
| 11136 | return YES; |
| 11137 | } |
| 11138 | if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) { |
| 11139 | return NO; |
| 11140 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11141 | GPBBoolFloatDictionary *otherDictionary = other; |
| 11142 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 11143 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11144 | return NO; |
| 11145 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11146 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 11147 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11148 | return NO; |
| 11149 | } |
| 11150 | return YES; |
| 11151 | } |
| 11152 | |
| 11153 | - (NSUInteger)hash { |
| 11154 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 11155 | } |
| 11156 | |
| 11157 | - (NSString *)description { |
| 11158 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 11159 | if (_valueSet[0]) { |
| 11160 | [result appendFormat:@"NO: %f", _values[0]]; |
| 11161 | } |
| 11162 | if (_valueSet[1]) { |
| 11163 | [result appendFormat:@"YES: %f", _values[1]]; |
| 11164 | } |
| 11165 | [result appendString:@" }"]; |
| 11166 | return result; |
| 11167 | } |
| 11168 | |
| 11169 | - (NSUInteger)count { |
| 11170 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 11171 | } |
| 11172 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11173 | - (BOOL)getFloat:(float *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11174 | int idx = (key ? 1 : 0); |
| 11175 | if (_valueSet[idx]) { |
| 11176 | if (value) { |
| 11177 | *value = _values[idx]; |
| 11178 | } |
| 11179 | return YES; |
| 11180 | } |
| 11181 | return NO; |
| 11182 | } |
| 11183 | |
| 11184 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 11185 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 11186 | int idx = (key->valueBool ? 1 : 0); |
| 11187 | _values[idx] = value->valueFloat; |
| 11188 | _valueSet[idx] = YES; |
| 11189 | } |
| 11190 | |
| 11191 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 11192 | if (_valueSet[0]) { |
| 11193 | block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]); |
| 11194 | } |
| 11195 | if (_valueSet[1]) { |
| 11196 | block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]); |
| 11197 | } |
| 11198 | } |
| 11199 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11200 | - (void)enumerateKeysAndFloatsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11201 | (void (^)(BOOL key, float value, BOOL *stop))block { |
| 11202 | BOOL stop = NO; |
| 11203 | if (_valueSet[0]) { |
| 11204 | block(NO, _values[0], &stop); |
| 11205 | } |
| 11206 | if (!stop && _valueSet[1]) { |
| 11207 | block(YES, _values[1], &stop); |
| 11208 | } |
| 11209 | } |
| 11210 | |
| 11211 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 11212 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11213 | NSUInteger count = 0; |
| 11214 | size_t result = 0; |
| 11215 | for (int i = 0; i < 2; ++i) { |
| 11216 | if (_valueSet[i]) { |
| 11217 | ++count; |
| 11218 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11219 | msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11220 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 11221 | } |
| 11222 | } |
| 11223 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 11224 | result += tagSize * count; |
| 11225 | return result; |
| 11226 | } |
| 11227 | |
| 11228 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 11229 | asField:(GPBFieldDescriptor *)field { |
| 11230 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11231 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 11232 | for (int i = 0; i < 2; ++i) { |
| 11233 | if (_valueSet[i]) { |
| 11234 | // Write the tag. |
| 11235 | [outputStream writeInt32NoTag:tag]; |
| 11236 | // Write the size of the message. |
| 11237 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11238 | msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11239 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 11240 | // Write the fields. |
| 11241 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11242 | WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 11243 | } |
| 11244 | } |
| 11245 | } |
| 11246 | |
| 11247 | - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary { |
| 11248 | if (otherDictionary) { |
| 11249 | for (int i = 0; i < 2; ++i) { |
| 11250 | if (otherDictionary->_valueSet[i]) { |
| 11251 | _valueSet[i] = YES; |
| 11252 | _values[i] = otherDictionary->_values[i]; |
| 11253 | } |
| 11254 | } |
| 11255 | if (_autocreator) { |
| 11256 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11257 | } |
| 11258 | } |
| 11259 | } |
| 11260 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11261 | - (void)setFloat:(float)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11262 | int idx = (key ? 1 : 0); |
| 11263 | _values[idx] = value; |
| 11264 | _valueSet[idx] = YES; |
| 11265 | if (_autocreator) { |
| 11266 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11267 | } |
| 11268 | } |
| 11269 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11270 | - (void)removeFloatForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11271 | _valueSet[aKey ? 1 : 0] = NO; |
| 11272 | } |
| 11273 | |
| 11274 | - (void)removeAll { |
| 11275 | _valueSet[0] = NO; |
| 11276 | _valueSet[1] = NO; |
| 11277 | } |
| 11278 | |
| 11279 | @end |
| 11280 | |
| 11281 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double) |
| 11282 | // This block of code is generated, do not edit it directly. |
| 11283 | |
| 11284 | #pragma mark - Bool -> Double |
| 11285 | |
| 11286 | @implementation GPBBoolDoubleDictionary { |
| 11287 | @package |
| 11288 | double _values[2]; |
| 11289 | BOOL _valueSet[2]; |
| 11290 | } |
| 11291 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11292 | - (instancetype)init { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11293 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11294 | } |
| 11295 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11296 | - (instancetype)initWithDoubles:(const double [])values |
| 11297 | forKeys:(const BOOL [])keys |
| 11298 | count:(NSUInteger)count { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11299 | self = [super init]; |
| 11300 | if (self) { |
| 11301 | for (NSUInteger i = 0; i < count; ++i) { |
| 11302 | int idx = keys[i] ? 1 : 0; |
| 11303 | _values[idx] = values[i]; |
| 11304 | _valueSet[idx] = YES; |
| 11305 | } |
| 11306 | } |
| 11307 | return self; |
| 11308 | } |
| 11309 | |
| 11310 | - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11311 | self = [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11312 | if (self) { |
| 11313 | if (dictionary) { |
| 11314 | for (int i = 0; i < 2; ++i) { |
| 11315 | if (dictionary->_valueSet[i]) { |
| 11316 | _values[i] = dictionary->_values[i]; |
| 11317 | _valueSet[i] = YES; |
| 11318 | } |
| 11319 | } |
| 11320 | } |
| 11321 | } |
| 11322 | return self; |
| 11323 | } |
| 11324 | |
| 11325 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 11326 | #pragma unused(numItems) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11327 | return [self initWithDoubles:NULL forKeys:NULL count:0]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11328 | } |
| 11329 | |
| 11330 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 11331 | - (void)dealloc { |
| 11332 | NSAssert(!_autocreator, |
| 11333 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 11334 | [self class], _autocreator); |
| 11335 | [super dealloc]; |
| 11336 | } |
| 11337 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 11338 | |
| 11339 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 11340 | return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self]; |
| 11341 | } |
| 11342 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11343 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11344 | if (self == other) { |
| 11345 | return YES; |
| 11346 | } |
| 11347 | if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) { |
| 11348 | return NO; |
| 11349 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11350 | GPBBoolDoubleDictionary *otherDictionary = other; |
| 11351 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 11352 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11353 | return NO; |
| 11354 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11355 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 11356 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11357 | return NO; |
| 11358 | } |
| 11359 | return YES; |
| 11360 | } |
| 11361 | |
| 11362 | - (NSUInteger)hash { |
| 11363 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 11364 | } |
| 11365 | |
| 11366 | - (NSString *)description { |
| 11367 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 11368 | if (_valueSet[0]) { |
| 11369 | [result appendFormat:@"NO: %lf", _values[0]]; |
| 11370 | } |
| 11371 | if (_valueSet[1]) { |
| 11372 | [result appendFormat:@"YES: %lf", _values[1]]; |
| 11373 | } |
| 11374 | [result appendString:@" }"]; |
| 11375 | return result; |
| 11376 | } |
| 11377 | |
| 11378 | - (NSUInteger)count { |
| 11379 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 11380 | } |
| 11381 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11382 | - (BOOL)getDouble:(double *)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11383 | int idx = (key ? 1 : 0); |
| 11384 | if (_valueSet[idx]) { |
| 11385 | if (value) { |
| 11386 | *value = _values[idx]; |
| 11387 | } |
| 11388 | return YES; |
| 11389 | } |
| 11390 | return NO; |
| 11391 | } |
| 11392 | |
| 11393 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 11394 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 11395 | int idx = (key->valueBool ? 1 : 0); |
| 11396 | _values[idx] = value->valueDouble; |
| 11397 | _valueSet[idx] = YES; |
| 11398 | } |
| 11399 | |
| 11400 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 11401 | if (_valueSet[0]) { |
| 11402 | block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]); |
| 11403 | } |
| 11404 | if (_valueSet[1]) { |
| 11405 | block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]); |
| 11406 | } |
| 11407 | } |
| 11408 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11409 | - (void)enumerateKeysAndDoublesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11410 | (void (^)(BOOL key, double value, BOOL *stop))block { |
| 11411 | BOOL stop = NO; |
| 11412 | if (_valueSet[0]) { |
| 11413 | block(NO, _values[0], &stop); |
| 11414 | } |
| 11415 | if (!stop && _valueSet[1]) { |
| 11416 | block(YES, _values[1], &stop); |
| 11417 | } |
| 11418 | } |
| 11419 | |
| 11420 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 11421 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11422 | NSUInteger count = 0; |
| 11423 | size_t result = 0; |
| 11424 | for (int i = 0; i < 2; ++i) { |
| 11425 | if (_valueSet[i]) { |
| 11426 | ++count; |
| 11427 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11428 | msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11429 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 11430 | } |
| 11431 | } |
| 11432 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 11433 | result += tagSize * count; |
| 11434 | return result; |
| 11435 | } |
| 11436 | |
| 11437 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 11438 | asField:(GPBFieldDescriptor *)field { |
| 11439 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11440 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 11441 | for (int i = 0; i < 2; ++i) { |
| 11442 | if (_valueSet[i]) { |
| 11443 | // Write the tag. |
| 11444 | [outputStream writeInt32NoTag:tag]; |
| 11445 | // Write the size of the message. |
| 11446 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11447 | msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11448 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 11449 | // Write the fields. |
| 11450 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11451 | WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 11452 | } |
| 11453 | } |
| 11454 | } |
| 11455 | |
| 11456 | - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary { |
| 11457 | if (otherDictionary) { |
| 11458 | for (int i = 0; i < 2; ++i) { |
| 11459 | if (otherDictionary->_valueSet[i]) { |
| 11460 | _valueSet[i] = YES; |
| 11461 | _values[i] = otherDictionary->_values[i]; |
| 11462 | } |
| 11463 | } |
| 11464 | if (_autocreator) { |
| 11465 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11466 | } |
| 11467 | } |
| 11468 | } |
| 11469 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11470 | - (void)setDouble:(double)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11471 | int idx = (key ? 1 : 0); |
| 11472 | _values[idx] = value; |
| 11473 | _valueSet[idx] = YES; |
| 11474 | if (_autocreator) { |
| 11475 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11476 | } |
| 11477 | } |
| 11478 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11479 | - (void)removeDoubleForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11480 | _valueSet[aKey ? 1 : 0] = NO; |
| 11481 | } |
| 11482 | |
| 11483 | - (void)removeAll { |
| 11484 | _valueSet[0] = NO; |
| 11485 | _valueSet[1] = NO; |
| 11486 | } |
| 11487 | |
| 11488 | @end |
| 11489 | |
| 11490 | //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id) |
| 11491 | // This block of code is generated, do not edit it directly. |
| 11492 | |
| 11493 | #pragma mark - Bool -> Object |
| 11494 | |
| 11495 | @implementation GPBBoolObjectDictionary { |
| 11496 | @package |
| 11497 | id _values[2]; |
| 11498 | } |
| 11499 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11500 | - (instancetype)init { |
| 11501 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 11502 | } |
| 11503 | |
| 11504 | - (instancetype)initWithObjects:(const id [])objects |
| 11505 | forKeys:(const BOOL [])keys |
| 11506 | count:(NSUInteger)count { |
| 11507 | self = [super init]; |
| 11508 | if (self) { |
| 11509 | for (NSUInteger i = 0; i < count; ++i) { |
| 11510 | if (!objects[i]) { |
| 11511 | [NSException raise:NSInvalidArgumentException |
| 11512 | format:@"Attempting to add nil object to a Dictionary"]; |
| 11513 | } |
| 11514 | int idx = keys[i] ? 1 : 0; |
| 11515 | [_values[idx] release]; |
| 11516 | _values[idx] = (id)[objects[i] retain]; |
| 11517 | } |
| 11518 | } |
| 11519 | return self; |
| 11520 | } |
| 11521 | |
| 11522 | - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary { |
| 11523 | self = [self initWithObjects:NULL forKeys:NULL count:0]; |
| 11524 | if (self) { |
| 11525 | if (dictionary) { |
| 11526 | _values[0] = [dictionary->_values[0] retain]; |
| 11527 | _values[1] = [dictionary->_values[1] retain]; |
| 11528 | } |
| 11529 | } |
| 11530 | return self; |
| 11531 | } |
| 11532 | |
| 11533 | - (instancetype)initWithCapacity:(NSUInteger)numItems { |
| 11534 | #pragma unused(numItems) |
| 11535 | return [self initWithObjects:NULL forKeys:NULL count:0]; |
| 11536 | } |
| 11537 | |
| 11538 | - (void)dealloc { |
| 11539 | NSAssert(!_autocreator, |
| 11540 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 11541 | [self class], _autocreator); |
| 11542 | [_values[0] release]; |
| 11543 | [_values[1] release]; |
| 11544 | [super dealloc]; |
| 11545 | } |
| 11546 | |
| 11547 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 11548 | return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self]; |
| 11549 | } |
| 11550 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11551 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11552 | if (self == other) { |
| 11553 | return YES; |
| 11554 | } |
| 11555 | if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) { |
| 11556 | return NO; |
| 11557 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11558 | GPBBoolObjectDictionary *otherDictionary = other; |
| 11559 | if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) || |
| 11560 | ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11561 | return NO; |
| 11562 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11563 | if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) || |
| 11564 | ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11565 | return NO; |
| 11566 | } |
| 11567 | return YES; |
| 11568 | } |
| 11569 | |
| 11570 | - (NSUInteger)hash { |
| 11571 | return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0); |
| 11572 | } |
| 11573 | |
| 11574 | - (NSString *)description { |
| 11575 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 11576 | if ((_values[0] != nil)) { |
| 11577 | [result appendFormat:@"NO: %@", _values[0]]; |
| 11578 | } |
| 11579 | if ((_values[1] != nil)) { |
| 11580 | [result appendFormat:@"YES: %@", _values[1]]; |
| 11581 | } |
| 11582 | [result appendString:@" }"]; |
| 11583 | return result; |
| 11584 | } |
| 11585 | |
| 11586 | - (NSUInteger)count { |
| 11587 | return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0); |
| 11588 | } |
| 11589 | |
| 11590 | - (id)objectForKey:(BOOL)key { |
| 11591 | return _values[key ? 1 : 0]; |
| 11592 | } |
| 11593 | |
| 11594 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 11595 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 11596 | int idx = (key->valueBool ? 1 : 0); |
| 11597 | [_values[idx] release]; |
| 11598 | _values[idx] = [value->valueString retain]; |
| 11599 | } |
| 11600 | |
| 11601 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 11602 | if (_values[0] != nil) { |
| 11603 | block(@"false", _values[0]); |
| 11604 | } |
| 11605 | if ((_values[1] != nil)) { |
| 11606 | block(@"true", _values[1]); |
| 11607 | } |
| 11608 | } |
| 11609 | |
| 11610 | - (void)enumerateKeysAndObjectsUsingBlock: |
| 11611 | (void (^)(BOOL key, id object, BOOL *stop))block { |
| 11612 | BOOL stop = NO; |
| 11613 | if (_values[0] != nil) { |
| 11614 | block(NO, _values[0], &stop); |
| 11615 | } |
| 11616 | if (!stop && (_values[1] != nil)) { |
| 11617 | block(YES, _values[1], &stop); |
| 11618 | } |
| 11619 | } |
| 11620 | |
| 11621 | - (BOOL)isInitialized { |
| 11622 | if (_values[0] && ![_values[0] isInitialized]) { |
| 11623 | return NO; |
| 11624 | } |
| 11625 | if (_values[1] && ![_values[1] isInitialized]) { |
| 11626 | return NO; |
| 11627 | } |
| 11628 | return YES; |
| 11629 | } |
| 11630 | |
| 11631 | - (instancetype)deepCopyWithZone:(NSZone *)zone { |
| 11632 | GPBBoolObjectDictionary *newDict = |
| 11633 | [[GPBBoolObjectDictionary alloc] init]; |
| 11634 | for (int i = 0; i < 2; ++i) { |
| 11635 | if (_values[i] != nil) { |
| 11636 | newDict->_values[i] = [_values[i] copyWithZone:zone]; |
| 11637 | } |
| 11638 | } |
| 11639 | return newDict; |
| 11640 | } |
| 11641 | |
| 11642 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 11643 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11644 | NSUInteger count = 0; |
| 11645 | size_t result = 0; |
| 11646 | for (int i = 0; i < 2; ++i) { |
| 11647 | if (_values[i] != nil) { |
| 11648 | ++count; |
| 11649 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11650 | msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11651 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 11652 | } |
| 11653 | } |
| 11654 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 11655 | result += tagSize * count; |
| 11656 | return result; |
| 11657 | } |
| 11658 | |
| 11659 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 11660 | asField:(GPBFieldDescriptor *)field { |
| 11661 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11662 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 11663 | for (int i = 0; i < 2; ++i) { |
| 11664 | if (_values[i] != nil) { |
| 11665 | // Write the tag. |
| 11666 | [outputStream writeInt32NoTag:tag]; |
| 11667 | // Write the size of the message. |
| 11668 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11669 | msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11670 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 11671 | // Write the fields. |
| 11672 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11673 | WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 11674 | } |
| 11675 | } |
| 11676 | } |
| 11677 | |
| 11678 | - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary { |
| 11679 | if (otherDictionary) { |
| 11680 | for (int i = 0; i < 2; ++i) { |
| 11681 | if (otherDictionary->_values[i] != nil) { |
| 11682 | [_values[i] release]; |
| 11683 | _values[i] = [otherDictionary->_values[i] retain]; |
| 11684 | } |
| 11685 | } |
| 11686 | if (_autocreator) { |
| 11687 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11688 | } |
| 11689 | } |
| 11690 | } |
| 11691 | |
| 11692 | - (void)setObject:(id)object forKey:(BOOL)key { |
| 11693 | if (!object) { |
| 11694 | [NSException raise:NSInvalidArgumentException |
| 11695 | format:@"Attempting to add nil object to a Dictionary"]; |
| 11696 | } |
| 11697 | int idx = (key ? 1 : 0); |
| 11698 | [_values[idx] release]; |
| 11699 | _values[idx] = [object retain]; |
| 11700 | if (_autocreator) { |
| 11701 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11702 | } |
| 11703 | } |
| 11704 | |
| 11705 | - (void)removeObjectForKey:(BOOL)aKey { |
| 11706 | int idx = (aKey ? 1 : 0); |
| 11707 | [_values[idx] release]; |
| 11708 | _values[idx] = nil; |
| 11709 | } |
| 11710 | |
| 11711 | - (void)removeAll { |
| 11712 | for (int i = 0; i < 2; ++i) { |
| 11713 | [_values[i] release]; |
| 11714 | _values[i] = nil; |
| 11715 | } |
| 11716 | } |
| 11717 | |
| 11718 | @end |
| 11719 | |
| 11720 | //%PDDM-EXPAND-END (8 expansions) |
| 11721 | |
| 11722 | #pragma mark - Bool -> Enum |
| 11723 | |
| 11724 | @implementation GPBBoolEnumDictionary { |
| 11725 | @package |
| 11726 | GPBEnumValidationFunc _validationFunc; |
| 11727 | int32_t _values[2]; |
| 11728 | BOOL _valueSet[2]; |
| 11729 | } |
| 11730 | |
| 11731 | @synthesize validationFunc = _validationFunc; |
| 11732 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11733 | - (instancetype)init { |
| 11734 | return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0]; |
| 11735 | } |
| 11736 | |
| 11737 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func { |
| 11738 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 11739 | } |
| 11740 | |
| 11741 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 11742 | rawValues:(const int32_t [])rawValues |
| 11743 | forKeys:(const BOOL [])keys |
| 11744 | count:(NSUInteger)count { |
| 11745 | self = [super init]; |
| 11746 | if (self) { |
| 11747 | _validationFunc = (func != NULL ? func : DictDefault_IsValidValue); |
| 11748 | for (NSUInteger i = 0; i < count; ++i) { |
| 11749 | int idx = keys[i] ? 1 : 0; |
| 11750 | _values[idx] = rawValues[i]; |
| 11751 | _valueSet[idx] = YES; |
| 11752 | } |
| 11753 | } |
| 11754 | return self; |
| 11755 | } |
| 11756 | |
| 11757 | - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary { |
| 11758 | self = [self initWithValidationFunction:dictionary.validationFunc |
| 11759 | rawValues:NULL |
| 11760 | forKeys:NULL |
| 11761 | count:0]; |
| 11762 | if (self) { |
| 11763 | if (dictionary) { |
| 11764 | for (int i = 0; i < 2; ++i) { |
| 11765 | if (dictionary->_valueSet[i]) { |
| 11766 | _values[i] = dictionary->_values[i]; |
| 11767 | _valueSet[i] = YES; |
| 11768 | } |
| 11769 | } |
| 11770 | } |
| 11771 | } |
| 11772 | return self; |
| 11773 | } |
| 11774 | |
| 11775 | - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func |
| 11776 | capacity:(NSUInteger)numItems { |
| 11777 | #pragma unused(numItems) |
| 11778 | return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0]; |
| 11779 | } |
| 11780 | |
| 11781 | #if !defined(NS_BLOCK_ASSERTIONS) |
| 11782 | - (void)dealloc { |
| 11783 | NSAssert(!_autocreator, |
| 11784 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 11785 | [self class], _autocreator); |
| 11786 | [super dealloc]; |
| 11787 | } |
| 11788 | #endif // !defined(NS_BLOCK_ASSERTIONS) |
| 11789 | |
| 11790 | - (instancetype)copyWithZone:(NSZone *)zone { |
| 11791 | return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self]; |
| 11792 | } |
| 11793 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11794 | - (BOOL)isEqual:(id)other { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11795 | if (self == other) { |
| 11796 | return YES; |
| 11797 | } |
| 11798 | if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) { |
| 11799 | return NO; |
| 11800 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11801 | GPBBoolEnumDictionary *otherDictionary = other; |
| 11802 | if ((_valueSet[0] != otherDictionary->_valueSet[0]) || |
| 11803 | (_valueSet[1] != otherDictionary->_valueSet[1])) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11804 | return NO; |
| 11805 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11806 | if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) || |
| 11807 | (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11808 | return NO; |
| 11809 | } |
| 11810 | return YES; |
| 11811 | } |
| 11812 | |
| 11813 | - (NSUInteger)hash { |
| 11814 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 11815 | } |
| 11816 | |
| 11817 | - (NSString *)description { |
| 11818 | NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self]; |
| 11819 | if (_valueSet[0]) { |
| 11820 | [result appendFormat:@"NO: %d", _values[0]]; |
| 11821 | } |
| 11822 | if (_valueSet[1]) { |
| 11823 | [result appendFormat:@"YES: %d", _values[1]]; |
| 11824 | } |
| 11825 | [result appendString:@" }"]; |
| 11826 | return result; |
| 11827 | } |
| 11828 | |
| 11829 | - (NSUInteger)count { |
| 11830 | return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0); |
| 11831 | } |
| 11832 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11833 | - (BOOL)getEnum:(int32_t*)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11834 | int idx = (key ? 1 : 0); |
| 11835 | if (_valueSet[idx]) { |
| 11836 | if (value) { |
| 11837 | int32_t result = _values[idx]; |
| 11838 | if (!_validationFunc(result)) { |
| 11839 | result = kGPBUnrecognizedEnumeratorValue; |
| 11840 | } |
| 11841 | *value = result; |
| 11842 | } |
| 11843 | return YES; |
| 11844 | } |
| 11845 | return NO; |
| 11846 | } |
| 11847 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11848 | - (BOOL)getRawValue:(int32_t*)rawValue forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11849 | int idx = (key ? 1 : 0); |
| 11850 | if (_valueSet[idx]) { |
| 11851 | if (rawValue) { |
| 11852 | *rawValue = _values[idx]; |
| 11853 | } |
| 11854 | return YES; |
| 11855 | } |
| 11856 | return NO; |
| 11857 | } |
| 11858 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11859 | - (void)enumerateKeysAndRawValuesUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11860 | (void (^)(BOOL key, int32_t value, BOOL *stop))block { |
| 11861 | BOOL stop = NO; |
| 11862 | if (_valueSet[0]) { |
| 11863 | block(NO, _values[0], &stop); |
| 11864 | } |
| 11865 | if (!stop && _valueSet[1]) { |
| 11866 | block(YES, _values[1], &stop); |
| 11867 | } |
| 11868 | } |
| 11869 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11870 | - (void)enumerateKeysAndEnumsUsingBlock: |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11871 | (void (^)(BOOL key, int32_t rawValue, BOOL *stop))block { |
| 11872 | BOOL stop = NO; |
| 11873 | GPBEnumValidationFunc func = _validationFunc; |
| 11874 | int32_t validatedValue; |
| 11875 | if (_valueSet[0]) { |
| 11876 | validatedValue = _values[0]; |
| 11877 | if (!func(validatedValue)) { |
| 11878 | validatedValue = kGPBUnrecognizedEnumeratorValue; |
| 11879 | } |
| 11880 | block(NO, validatedValue, &stop); |
| 11881 | } |
| 11882 | if (!stop && _valueSet[1]) { |
| 11883 | validatedValue = _values[1]; |
| 11884 | if (!func(validatedValue)) { |
| 11885 | validatedValue = kGPBUnrecognizedEnumeratorValue; |
| 11886 | } |
| 11887 | block(YES, validatedValue, &stop); |
| 11888 | } |
| 11889 | } |
| 11890 | |
| 11891 | //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool) |
| 11892 | // This block of code is generated, do not edit it directly. |
| 11893 | |
| 11894 | - (NSData *)serializedDataForUnknownValue:(int32_t)value |
| 11895 | forKey:(GPBGenericValue *)key |
| 11896 | keyDataType:(GPBDataType)keyDataType { |
| 11897 | size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType); |
| 11898 | msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 11899 | NSMutableData *data = [NSMutableData dataWithLength:msgSize]; |
| 11900 | GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data]; |
| 11901 | WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType); |
| 11902 | WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum); |
| 11903 | [outputStream release]; |
| 11904 | return data; |
| 11905 | } |
| 11906 | |
| 11907 | //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool) |
| 11908 | |
| 11909 | - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field { |
| 11910 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11911 | NSUInteger count = 0; |
| 11912 | size_t result = 0; |
| 11913 | for (int i = 0; i < 2; ++i) { |
| 11914 | if (_valueSet[i]) { |
| 11915 | ++count; |
| 11916 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11917 | msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11918 | result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize; |
| 11919 | } |
| 11920 | } |
| 11921 | size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage); |
| 11922 | result += tagSize * count; |
| 11923 | return result; |
| 11924 | } |
| 11925 | |
| 11926 | - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream |
| 11927 | asField:(GPBFieldDescriptor *)field { |
| 11928 | GPBDataType valueDataType = GPBGetFieldDataType(field); |
| 11929 | uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited); |
| 11930 | for (int i = 0; i < 2; ++i) { |
| 11931 | if (_valueSet[i]) { |
| 11932 | // Write the tag. |
| 11933 | [outputStream writeInt32NoTag:tag]; |
| 11934 | // Write the size of the message. |
| 11935 | size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11936 | msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType); |
| 11937 | [outputStream writeInt32NoTag:(int32_t)msgSize]; |
| 11938 | // Write the fields. |
| 11939 | WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool); |
| 11940 | WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType); |
| 11941 | } |
| 11942 | } |
| 11943 | } |
| 11944 | |
| 11945 | - (void)enumerateForTextFormat:(void (^)(id keyObj, id valueObj))block { |
| 11946 | if (_valueSet[0]) { |
| 11947 | block(@"false", @(_values[0])); |
| 11948 | } |
| 11949 | if (_valueSet[1]) { |
| 11950 | block(@"true", @(_values[1])); |
| 11951 | } |
| 11952 | } |
| 11953 | |
| 11954 | - (void)setGPBGenericValue:(GPBGenericValue *)value |
| 11955 | forGPBGenericValueKey:(GPBGenericValue *)key { |
| 11956 | int idx = (key->valueBool ? 1 : 0); |
| 11957 | _values[idx] = value->valueInt32; |
| 11958 | _valueSet[idx] = YES; |
| 11959 | } |
| 11960 | |
| 11961 | - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary { |
| 11962 | if (otherDictionary) { |
| 11963 | for (int i = 0; i < 2; ++i) { |
| 11964 | if (otherDictionary->_valueSet[i]) { |
| 11965 | _valueSet[i] = YES; |
| 11966 | _values[i] = otherDictionary->_values[i]; |
| 11967 | } |
| 11968 | } |
| 11969 | if (_autocreator) { |
| 11970 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11971 | } |
| 11972 | } |
| 11973 | } |
| 11974 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11975 | - (void)setEnum:(int32_t)value forKey:(BOOL)key { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11976 | if (!_validationFunc(value)) { |
| 11977 | [NSException raise:NSInvalidArgumentException |
| 11978 | format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)", |
| 11979 | value]; |
| 11980 | } |
| 11981 | int idx = (key ? 1 : 0); |
| 11982 | _values[idx] = value; |
| 11983 | _valueSet[idx] = YES; |
| 11984 | if (_autocreator) { |
| 11985 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11986 | } |
| 11987 | } |
| 11988 | |
| 11989 | - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key { |
| 11990 | int idx = (key ? 1 : 0); |
| 11991 | _values[idx] = rawValue; |
| 11992 | _valueSet[idx] = YES; |
| 11993 | if (_autocreator) { |
| 11994 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 11995 | } |
| 11996 | } |
| 11997 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 11998 | - (void)removeEnumForKey:(BOOL)aKey { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 11999 | _valueSet[aKey ? 1 : 0] = NO; |
| 12000 | } |
| 12001 | |
| 12002 | - (void)removeAll { |
| 12003 | _valueSet[0] = NO; |
| 12004 | _valueSet[1] = NO; |
| 12005 | } |
| 12006 | |
| 12007 | @end |
| 12008 | |
| 12009 | #pragma mark - NSDictionary Subclass |
| 12010 | |
| 12011 | @implementation GPBAutocreatedDictionary { |
| 12012 | NSMutableDictionary *_dictionary; |
| 12013 | } |
| 12014 | |
| 12015 | - (void)dealloc { |
| 12016 | NSAssert(!_autocreator, |
| 12017 | @"%@: Autocreator must be cleared before release, autocreator: %@", |
| 12018 | [self class], _autocreator); |
| 12019 | [_dictionary release]; |
| 12020 | [super dealloc]; |
| 12021 | } |
| 12022 | |
| 12023 | #pragma mark Required NSDictionary overrides |
| 12024 | |
| 12025 | - (instancetype)initWithObjects:(const id [])objects |
| 12026 | forKeys:(const id<NSCopying> [])keys |
| 12027 | count:(NSUInteger)count { |
| 12028 | self = [super init]; |
| 12029 | if (self) { |
| 12030 | _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects |
| 12031 | forKeys:keys |
| 12032 | count:count]; |
| 12033 | } |
| 12034 | return self; |
| 12035 | } |
| 12036 | |
| 12037 | - (NSUInteger)count { |
| 12038 | return [_dictionary count]; |
| 12039 | } |
| 12040 | |
| 12041 | - (id)objectForKey:(id)aKey { |
| 12042 | return [_dictionary objectForKey:aKey]; |
| 12043 | } |
| 12044 | |
| 12045 | - (NSEnumerator *)keyEnumerator { |
| 12046 | if (_dictionary == nil) { |
| 12047 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 12048 | } |
| 12049 | return [_dictionary keyEnumerator]; |
| 12050 | } |
| 12051 | |
| 12052 | #pragma mark Required NSMutableDictionary overrides |
| 12053 | |
| 12054 | // Only need to call GPBAutocreatedDictionaryModified() when adding things |
| 12055 | // since we only autocreate empty dictionaries. |
| 12056 | |
| 12057 | - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey { |
| 12058 | if (_dictionary == nil) { |
| 12059 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 12060 | } |
| 12061 | [_dictionary setObject:anObject forKey:aKey]; |
| 12062 | if (_autocreator) { |
| 12063 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 12064 | } |
| 12065 | } |
| 12066 | |
| 12067 | - (void)removeObjectForKey:(id)aKey { |
| 12068 | [_dictionary removeObjectForKey:aKey]; |
| 12069 | } |
| 12070 | |
| 12071 | #pragma mark Extra things hooked |
| 12072 | |
| 12073 | - (id)copyWithZone:(NSZone *)zone { |
| 12074 | if (_dictionary == nil) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12075 | return [[NSMutableDictionary allocWithZone:zone] init]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 12076 | } |
| 12077 | return [_dictionary copyWithZone:zone]; |
| 12078 | } |
| 12079 | |
| 12080 | - (id)mutableCopyWithZone:(NSZone *)zone { |
| 12081 | if (_dictionary == nil) { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12082 | return [[NSMutableDictionary allocWithZone:zone] init]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 12083 | } |
| 12084 | return [_dictionary mutableCopyWithZone:zone]; |
| 12085 | } |
| 12086 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12087 | // Not really needed, but subscripting is likely common enough it doesn't hurt |
| 12088 | // to ensure it goes directly to the real NSMutableDictionary. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 12089 | - (id)objectForKeyedSubscript:(id)key { |
| 12090 | return [_dictionary objectForKeyedSubscript:key]; |
| 12091 | } |
| 12092 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12093 | // Not really needed, but subscripting is likely common enough it doesn't hurt |
| 12094 | // to ensure it goes directly to the real NSMutableDictionary. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 12095 | - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key { |
| 12096 | if (_dictionary == nil) { |
| 12097 | _dictionary = [[NSMutableDictionary alloc] init]; |
| 12098 | } |
| 12099 | [_dictionary setObject:obj forKeyedSubscript:key]; |
| 12100 | if (_autocreator) { |
| 12101 | GPBAutocreatedDictionaryModified(_autocreator, self); |
| 12102 | } |
| 12103 | } |
| 12104 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12105 | - (void)enumerateKeysAndObjectsUsingBlock:(void (NS_NOESCAPE ^)(id key, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 12106 | id obj, |
| 12107 | BOOL *stop))block { |
| 12108 | [_dictionary enumerateKeysAndObjectsUsingBlock:block]; |
| 12109 | } |
| 12110 | |
| 12111 | - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12112 | usingBlock:(void (NS_NOESCAPE ^)(id key, |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 12113 | id obj, |
| 12114 | BOOL *stop))block { |
| 12115 | [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block]; |
| 12116 | } |
| 12117 | |
| 12118 | @end |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 12119 | |
| 12120 | #pragma clang diagnostic pop |