1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
|
/** NSValue.m - Object encapsulation for C types.
Copyright (C) 1993, 1994, 1996, 1999 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: Mar 1995
Updated by: Richard Frith-Macdonald <rfm@gnu.org>
Date: Jan 2001
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
<title>NSValue class reference</title>
$Date$ $Revision$
*/
#import "common.h"
#import "typeEncodingHelper.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSCoder.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSException.h"
#import "Foundation/NSMapTable.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSData.h"
#import "Foundation/NSGeometry.h"
#import "GSPThread.h"
@interface GSPlaceholderValue : NSValue
@end
@class GSValue;
@interface GSValue : NSObject // Help the compiler
@end
@class GSNonretainedObjectValue;
@interface GSNonretainedObjectValue : NSObject // Help the compiler
@end
@class GSPointValue;
@interface GSPointValue : NSObject // Help the compiler
@end
@class GSPointerValue;
@interface GSPointerValue : NSObject // Help the compiler
@end
@class GSRangeValue;
@interface GSRangeValue : NSObject // Help the compiler
@end
@class GSRectValue;
@interface GSRectValue : NSObject // Help the compiler
@end
@class GSSizeValue;
@interface GSSizeValue : NSObject // Help the compiler
@end
@class NSDataStatic; // Needed for decoding.
@interface NSDataStatic : NSData // Help the compiler
@end
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
@class GSEdgeInsetsValueValue;
@interface GSEdgeInsetsValue : NSObject // Help the compiler
@end
#endif
static Class abstractClass;
static Class concreteClass;
static Class nonretainedObjectValueClass;
static Class pointValueClass;
static Class pointerValueClass;
static Class rangeValueClass;
static Class rectValueClass;
static Class sizeValueClass;
static Class GSPlaceholderValueClass;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
static Class edgeInsetsValueClass;
#endif
static GSPlaceholderValue *defaultPlaceholderValue;
static NSMapTable *placeholderMap;
static gs_mutex_t placeholderLock = GS_MUTEX_INIT_STATIC;
@implementation NSValue
+ (void) atExit
{
id o;
/* The default placeholder array overrides -dealloc so we must get rid of
* it directly.
*/
o = defaultPlaceholderValue;
defaultPlaceholderValue = nil;
NSDeallocateObject(o);
/* Deallocate all the placeholders in the map before destroying it.
*/
GS_MUTEX_LOCK(placeholderLock);
if (placeholderMap)
{
NSMapEnumerator mEnum = NSEnumerateMapTable(placeholderMap);
Class c;
id o;
while (NSNextMapEnumeratorPair(&mEnum, (void *)&c, (void *)&o))
{
NSDeallocateObject(o);
}
NSEndMapTableEnumeration(&mEnum);
DESTROY(placeholderMap);
}
GS_MUTEX_UNLOCK(placeholderLock);
}
+ (void) initialize
{
if (self == [NSValue class])
{
abstractClass = self;
[abstractClass setVersion: 3]; // Version 3
concreteClass = [GSValue class];
nonretainedObjectValueClass = [GSNonretainedObjectValue class];
pointValueClass = [GSPointValue class];
pointerValueClass = [GSPointerValue class];
rangeValueClass = [GSRangeValue class];
rectValueClass = [GSRectValue class];
sizeValueClass = [GSSizeValue class];
GSPlaceholderValueClass = [GSPlaceholderValue class];
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
edgeInsetsValueClass = [GSEdgeInsetsValue class];
#endif
/*
* Set up infrastructure for placeholder values.
*/
defaultPlaceholderValue = (GSPlaceholderValue*)
NSAllocateObject(GSPlaceholderValueClass, 0, NSDefaultMallocZone());
placeholderMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
NSNonRetainedObjectMapValueCallBacks, 0);
[self registerAtExit];
}
}
+ (id) allocWithZone: (NSZone*)z
{
if (self == abstractClass)
{
if (z == NSDefaultMallocZone() || z == 0)
{
/*
* As a special case, we can return a placeholder for a value
* in the default malloc zone extremely efficiently.
*/
return defaultPlaceholderValue;
}
else
{
id obj;
/*
* For anything other than the default zone, we need to
* locate the correct placeholder in the (lock protected)
* table of placeholders.
*/
GS_MUTEX_LOCK(placeholderLock);
obj = (id)NSMapGet(placeholderMap, (void*)z);
if (obj == nil && NO == [NSObject isExiting])
{
/*
* There is no placeholder object for this zone, so we
* create a new one and use that.
*/
obj = (id)NSAllocateObject(GSPlaceholderValueClass, 0, z);
NSMapInsert(placeholderMap, (void*)z, (void*)obj);
}
GS_MUTEX_UNLOCK(placeholderLock);
return obj;
}
}
else
{
return NSAllocateObject(self, 0, z);
}
}
// NSCopying - always a simple retain.
- (id) copy
{
return RETAIN(self);
}
- (id) copyWithZone: (NSZone *)zone
{
return RETAIN(self);
}
/* Returns the concrete class associated with the type encoding */
+ (Class) valueClassWithObjCType: (const char *)type
{
Class theClass = concreteClass;
/* Let someone else deal with this error */
if (!type)
return theClass;
/* Try for an exact type match.
*/
if (strcmp(@encode(id), type) == 0)
theClass = nonretainedObjectValueClass;
else if (strcmp(@encode(NSPoint), type) == 0)
theClass = pointValueClass;
else if (strcmp(@encode(void *), type) == 0)
theClass = pointerValueClass;
else if (strcmp(@encode(NSRange), type) == 0)
theClass = rangeValueClass;
else if (strcmp(@encode(NSRect), type) == 0)
theClass = rectValueClass;
else if (strcmp(@encode(NSSize), type) == 0)
theClass = sizeValueClass;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
else if (strcmp(@encode(NSEdgeInsets), type) == 0)
theClass = edgeInsetsValueClass;
#endif
/* Try for equivalent types match.
*/
else if (GSSelectorTypesMatch(@encode(id), type))
theClass = nonretainedObjectValueClass;
else if (GSSelectorTypesMatch(@encode(NSPoint), type))
theClass = pointValueClass;
else if (GSSelectorTypesMatch(@encode(void *), type))
theClass = pointerValueClass;
else if (GSSelectorTypesMatch(@encode(NSRange), type))
theClass = rangeValueClass;
else if (GSSelectorTypesMatch(@encode(NSRect), type))
theClass = rectValueClass;
else if (GSSelectorTypesMatch(@encode(NSSize), type))
theClass = sizeValueClass;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
else if (GSSelectorTypesMatch(@encode(NSEdgeInsets), type))
theClass = edgeInsetsValueClass;
#endif
return theClass;
}
// Allocating and Initializing
+ (NSValue*) value: (const void *)value
withObjCType: (const char *)type
{
Class theClass = [self valueClassWithObjCType: type];
NSValue *theObj;
theObj = [theClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: value objCType: type];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithBytes: (const void *)value
objCType: (const char *)type
{
Class theClass = [self valueClassWithObjCType: type];
NSValue *theObj;
theObj = [theClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: value objCType: type];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithNonretainedObject: (id)anObject
{
NSValue *theObj;
theObj = [nonretainedObjectValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &anObject objCType: @encode(id)];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithPoint: (NSPoint)point
{
NSValue *theObj;
theObj = [pointValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &point objCType: @encode(NSPoint)];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithPointer: (const void *)pointer
{
NSValue *theObj;
theObj = [pointerValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &pointer objCType: @encode(void*)];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithRange: (NSRange)range
{
NSValue *theObj;
theObj = [rangeValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &range objCType: @encode(NSRange)];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithRect: (NSRect)rect
{
NSValue *theObj;
theObj = [rectValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &rect objCType: @encode(NSRect)];
return AUTORELEASE(theObj);
}
+ (NSValue*) valueWithSize: (NSSize)size
{
NSValue *theObj;
theObj = [sizeValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &size objCType: @encode(NSSize)];
return AUTORELEASE(theObj);
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
+ (NSValue*) valueWithEdgeInsets: (NSEdgeInsets)insets
{
NSValue *theObj;
theObj = [edgeInsetsValueClass allocWithZone: NSDefaultMallocZone()];
theObj = [theObj initWithBytes: &insets objCType: @encode(NSEdgeInsets)];
return AUTORELEASE(theObj);
}
#endif
+ (NSValue*) valueFromString: (NSString *)string
{
NSDictionary *dict = [string propertyList];
if (dict == nil)
return nil;
if ([dict objectForKey: @"location"])
{
NSRange range;
range = NSMakeRange([[dict objectForKey: @"location"] intValue],
[[dict objectForKey: @"length"] intValue]);
return [abstractClass valueWithRange: range];
}
else if ([dict objectForKey: @"width"] && [dict objectForKey: @"x"])
{
NSRect rect;
rect = NSMakeRect([[dict objectForKey: @"x"] floatValue],
[[dict objectForKey: @"y"] floatValue],
[[dict objectForKey: @"width"] floatValue],
[[dict objectForKey: @"height"] floatValue]);
return [abstractClass valueWithRect: rect];
}
else if ([dict objectForKey: @"width"])
{
NSSize size;
size = NSMakeSize([[dict objectForKey: @"width"] floatValue],
[[dict objectForKey: @"height"] floatValue]);
return [abstractClass valueWithSize: size];
}
else if ([dict objectForKey: @"x"])
{
NSPoint point;
point = NSMakePoint([[dict objectForKey: @"x"] floatValue],
[[dict objectForKey: @"y"] floatValue]);
return [abstractClass valueWithPoint: point];
}
return nil;
}
- (id) initWithBytes: (const void*)data objCType: (const char*)type
{
[self subclassResponsibility: _cmd];
return nil;
}
// Accessing Data
/* All the rest of these methods must be implemented by a subclass */
- (void) getValue: (void *)value
{
[self subclassResponsibility: _cmd];
}
- (BOOL) isEqual: (id)other
{
if ([other isKindOfClass: [self class]])
{
return [self isEqualToValue: other];
}
return NO;
}
- (BOOL) isEqualToValue: (NSValue*)other
{
[self subclassResponsibility: _cmd];
return NO;
}
- (const char *) objCType
{
[self subclassResponsibility: _cmd];
return 0;
}
- (id) nonretainedObjectValue
{
[self subclassResponsibility: _cmd];
return 0;
}
- (void *) pointerValue
{
[self subclassResponsibility: _cmd];
return 0;
}
- (NSRange) rangeValue
{
[self subclassResponsibility: _cmd];
return NSMakeRange(0,0);
}
- (NSRect) rectValue
{
[self subclassResponsibility: _cmd];
return NSMakeRect(0,0,0,0);
}
- (NSSize) sizeValue
{
[self subclassResponsibility: _cmd];
return NSMakeSize(0,0);
}
- (NSPoint) pointValue
{
[self subclassResponsibility: _cmd];
return NSMakePoint(0,0);
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
- (NSEdgeInsets) edgeInsetsValue
{
[self subclassResponsibility: _cmd];
return NSEdgeInsetsMake(0,0,0,0);
}
#endif
- (Class) classForCoder
{
return abstractClass;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
NSUInteger tsize;
unsigned size;
const char *data;
const char *objctype = [self objCType];
NSMutableData *d;
size = strlen(objctype)+1;
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
[coder encodeArrayOfObjCType: @encode(signed char) count: size at: objctype];
if (strncmp(CGSIZE_ENCODING_PREFIX, objctype, strlen(CGSIZE_ENCODING_PREFIX)) == 0)
{
NSSize v = [self sizeValue];
[coder encodeValueOfObjCType: objctype at: &v];
return;
}
else if (strncmp(CGPOINT_ENCODING_PREFIX, objctype, strlen(CGPOINT_ENCODING_PREFIX)) == 0)
{
NSPoint v = [self pointValue];
[coder encodeValueOfObjCType: objctype at: &v];
return;
}
else if (strncmp(CGRECT_ENCODING_PREFIX, objctype, strlen(CGRECT_ENCODING_PREFIX)) == 0)
{
NSRect v = [self rectValue];
[coder encodeValueOfObjCType: objctype at: &v];
return;
}
else if (strncmp(NSRANGE_ENCODING_PREFIX, objctype, strlen(NSRANGE_ENCODING_PREFIX)) == 0)
{
NSRange v = [self rangeValue];
[coder encodeValueOfObjCType: objctype at: &v];
return;
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
else if (strncmp(NSINSETS_ENCODING_PREFIX, objctype, strlen(NSINSETS_ENCODING_PREFIX)) == 0)
{
NSEdgeInsets v = [self edgeInsetsValue];
[coder encodeValueOfObjCType: objctype at: &v];
return;
}
#endif
NSGetSizeAndAlignment(objctype, &tsize, NULL);
data = (void *)NSZoneMalloc([self zone], tsize);
[self getValue: (void*)data];
d = [NSMutableData new];
[d serializeDataAt: data ofObjCType: objctype context: nil];
size = [d length];
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
NSZoneFree(NSDefaultMallocZone(), (void*)data);
data = [d bytes];
[coder encodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
RELEASE(d);
}
- (id) initWithCoder: (NSCoder *)coder
{
char type[64];
const char *objctype;
Class c;
id o;
NSUInteger tsize;
unsigned size;
int ver;
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
/*
* For almost all type encodings, we can use space on the stack,
* but to handle exceptionally large ones (possibly some huge structs)
* we have a strategy of allocating and deallocating heap space too.
*/
if (size <= 64)
{
objctype = type;
}
else
{
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
}
[coder decodeArrayOfObjCType: @encode(signed char)
count: size
at: (void*)objctype];
if (strncmp(CGSIZE_ENCODING_PREFIX, objctype, strlen(CGSIZE_ENCODING_PREFIX)) == 0)
c = [abstractClass valueClassWithObjCType: @encode(NSSize)];
else if (strncmp(CGPOINT_ENCODING_PREFIX, objctype, strlen(CGPOINT_ENCODING_PREFIX)) == 0)
c = [abstractClass valueClassWithObjCType: @encode(NSPoint)];
else if (strncmp(CGRECT_ENCODING_PREFIX, objctype, strlen(CGRECT_ENCODING_PREFIX)) == 0)
c = [abstractClass valueClassWithObjCType: @encode(NSRect)];
else if (strncmp(NSRANGE_ENCODING_PREFIX, objctype, strlen(NSRANGE_ENCODING_PREFIX)) == 0)
c = [abstractClass valueClassWithObjCType: @encode(NSRange)];
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
else if (strncmp(NSINSETS_ENCODING_PREFIX, objctype, strlen(NSINSETS_ENCODING_PREFIX)) == 0)
c = [abstractClass valueClassWithObjCType: @encode(NSEdgeInsets)];
#endif
else
c = [abstractClass valueClassWithObjCType: objctype];
o = [c allocWithZone: [coder objectZone]];
ver = [coder versionForClassName: @"NSValue"];
if (ver > 2)
{
if (c == pointValueClass)
{
NSPoint v;
[coder decodeValueOfObjCType: @encode(NSPoint) at: &v];
DESTROY(self);
return [o initWithBytes: &v objCType: @encode(NSPoint)];
}
else if (c == sizeValueClass)
{
NSSize v;
[coder decodeValueOfObjCType: @encode(NSSize) at: &v];
DESTROY(self);
return [o initWithBytes: &v objCType: @encode(NSSize)];
}
else if (c == rangeValueClass)
{
NSRange v;
[coder decodeValueOfObjCType: @encode(NSRange) at: &v];
DESTROY(self);
return [o initWithBytes: &v objCType: @encode(NSRange)];
}
else if (c == rectValueClass)
{
NSRect v;
[coder decodeValueOfObjCType: @encode(NSRect) at: &v];
DESTROY(self);
return [o initWithBytes: &v objCType: @encode(NSRect)];
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
else if (c == edgeInsetsValueClass)
{
NSEdgeInsets v;
[coder decodeValueOfObjCType: @encode(NSEdgeInsets) at: &v];
DESTROY(self);
return [o initWithBytes: &v objCType: @encode(NSEdgeInsets)];
}
#endif
}
if (ver < 2)
{
if (ver < 1)
{
if (c == pointValueClass)
{
NSPoint v;
[coder decodeValueOfObjCType: @encode(NSPoint) at: &v];
o = [o initWithBytes: &v objCType: @encode(NSPoint)];
}
else if (c == sizeValueClass)
{
NSSize v;
[coder decodeValueOfObjCType: @encode(NSSize) at: &v];
o = [o initWithBytes: &v objCType: @encode(NSSize)];
}
else if (c == rangeValueClass)
{
NSRange v;
[coder decodeValueOfObjCType: @encode(NSRange) at: &v];
o = [o initWithBytes: &v objCType: @encode(NSRange)];
}
else if (c == rectValueClass)
{
NSRect v;
[coder decodeValueOfObjCType: @encode(NSRect) at: &v];
o = [o initWithBytes: &v objCType: @encode(NSRect)];
}
#if OS_API_VERSION(MAC_OS_X_VERSION_10_10, GS_API_LATEST)
else if (c == edgeInsetsValueClass)
{
NSEdgeInsets v;
[coder decodeValueOfObjCType: @encode(NSEdgeInsets) at: &v];
o = [o initWithBytes: &v objCType: @encode(NSEdgeInsets)];
}
#endif
else
{
unsigned char *data;
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
[coder decodeArrayOfObjCType: @encode(unsigned char)
count: size
at: (void*)data];
o = [o initWithBytes: data objCType: objctype];
NSZoneFree(NSDefaultMallocZone(), data);
}
}
else
{
NSData *d;
unsigned cursor = 0;
/*
* For performance, decode small values directly onto the stack,
* For larger values we allocate and deallocate heap space.
*/
NSGetSizeAndAlignment(objctype, &tsize, NULL);
if (tsize <= 64)
{
unsigned char data[tsize];
[coder decodeValueOfObjCType: @encode(id) at: &d];
[d deserializeDataAt: data
ofObjCType: objctype
atCursor: &cursor
context: nil];
o = [o initWithBytes: data objCType: objctype];
RELEASE(d);
}
else
{
unsigned char *data;
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), tsize);
[coder decodeValueOfObjCType: @encode(id) at: &d];
[d deserializeDataAt: data
ofObjCType: objctype
atCursor: &cursor
context: nil];
o = [o initWithBytes: data objCType: objctype];
RELEASE(d);
NSZoneFree(NSDefaultMallocZone(), data);
}
}
}
else
{
static NSData *d = nil;
unsigned cursor = 0;
if (d == nil)
{
d = [NSDataStatic allocWithZone: NSDefaultMallocZone()];
}
/*
* For performance, decode small values directly onto the stack,
* For larger values we allocate and deallocate heap space.
*/
NSGetSizeAndAlignment(objctype, &tsize, NULL);
if (tsize <= 64)
{
unsigned char data[tsize];
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
{
unsigned char serialized[size];
[coder decodeArrayOfObjCType: @encode(unsigned char)
count: size
at: (void*)serialized];
d = [d initWithBytesNoCopy: (void*)serialized
length: size
freeWhenDone: NO];
[d deserializeDataAt: data
ofObjCType: objctype
atCursor: &cursor
context: nil];
}
o = [o initWithBytes: data objCType: objctype];
}
else
{
void *data;
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), tsize);
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
{
void *serialized;
serialized = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
[coder decodeArrayOfObjCType: @encode(unsigned char)
count: size
at: serialized];
d = [d initWithBytesNoCopy: serialized length: size];
[d deserializeDataAt: data
ofObjCType: objctype
atCursor: &cursor
context: nil];
NSZoneFree(NSDefaultMallocZone(), serialized);
}
o = [o initWithBytes: data objCType: objctype];
NSZoneFree(NSDefaultMallocZone(), data);
}
}
if (objctype != type)
{
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
}
DESTROY(self);
self = o;
return self;
}
@end
@implementation GSPlaceholderValue
- (id) autorelease
{
NSWarnLog(@"-autorelease sent to uninitialised value");
return self; // placeholders never get released.
}
- (void) dealloc
{
GSNOSUPERDEALLOC; // placeholders never get deallocated.
}
- (void) getValue: (void*)data
{
[NSException raise: NSInternalInconsistencyException
format: @"attempt to use uninitialised value"];
}
- (id) initWithBytes: (const void*)data objCType: (const char*)type
{
Class c = [abstractClass valueClassWithObjCType: type];
self = (id)NSAllocateObject(c, 0, [self zone]);
return [self initWithBytes: data objCType: type];
}
- (const char*) objCType
{
[NSException raise: NSInternalInconsistencyException
format: @"attempt to use uninitialised value"];
return 0;
}
- (oneway void) release
{
NSWarnLog(@"-release sent to uninitialised value");
return; // placeholders never get released.
}
- (id) retain
{
NSWarnLog(@"-retain sent to uninitialised value");
return self; // placeholders never get retained.
}
@end
|