-
Notifications
You must be signed in to change notification settings - Fork 9
/
L0UUID.m
118 lines (92 loc) · 2.43 KB
/
L0UUID.m
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
//
// L0UUID.m
// Orma
//
// Created by ∞ on 12/08/08.
// Copyright 2008 Emanuele Vulcano. All rights reserved.
//
#import "L0UUID.h"
@implementation L0UUID
- (id) init {
CFUUIDRef ref = CFUUIDCreate(kCFAllocatorDefault);
self = [self initWithUUID:ref];
CFRelease(ref);
return self;
}
- (id) initWithUUID:(CFUUIDRef) u {
if (self = [super init])
uuid = CFMakeCollectable(CFRetain(u));
return self;
}
- (id) initWithString:(NSString*) string {
CFUUIDRef ref = CFUUIDCreateFromString(kCFAllocatorDefault, (CFStringRef) string);
self = [self initWithUUID:ref];
CFRelease(ref);
return self;
}
- (id) initWithData:(NSData*) data {
return [self initWithBytes:(CFUUIDBytes*) [data bytes]];
}
- (id) initWithBytes:(CFUUIDBytes*) bytes {
CFUUIDRef ref = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, *bytes);
self = [self initWithUUID:ref];
CFRelease(ref);
return self;
}
- (CFUUIDRef) CFUUID {
return uuid;
}
- (NSString*) stringValue {
NSString* r = [NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, uuid)) autorelease];
return r;
}
- (NSData*) dataValue {
CFUUIDBytes b = CFUUIDGetUUIDBytes(uuid);
NSData* d = [[NSData alloc] initWithBytes:&b length:sizeof(CFUUIDBytes)];
return [d autorelease];
}
- (CFUUIDBytes) UUIDBytes {
return CFUUIDGetUUIDBytes(uuid);
}
- (BOOL) isEqual:(id) o {
BOOL isOK = [o isKindOfClass:[self class]] &&
CFEqual([o CFUUID], [self CFUUID]);
// NSLog(@"Will test for equality: self %@ with %@ result %d", self, o, isOK);
return isOK;
}
- (NSUInteger) hash {
return [[self class] hash] ^ CFHash(uuid);
}
- (void) dealloc {
CFRelease(uuid);
[super dealloc];
}
+ (id) UUID {
return [[[self alloc] init] autorelease];
}
+ (id) UUIDWithUUID:(CFUUIDRef) uuid {
return [[[self alloc] initWithUUID:uuid] autorelease];
}
+ (id) UUIDWithString:(NSString*) string {
return [[[self alloc] initWithString:string] autorelease];
}
+ (id) UUIDWithBytes:(CFUUIDBytes*) bytes {
return [[[self alloc] initWithBytes:bytes] autorelease];
}
+ (id) UUIDWithData:(NSData*) data {
return [[[self alloc] initWithData:data] autorelease];
}
- (id) copyWithZone:(NSZone*) zone {
return [[[self class] allocWithZone:zone] initWithUUID:[self CFUUID]];
}
- (id) copy {
return [self retain];
}
- (void) encodeWithCoder:(NSCoder*) c {
[c encodeObject:self.dataValue forKey:@"UUIDData"];
}
- (id) initWithCoder:(NSCoder*) c {
NSData* d = [c decodeObjectForKey:@"UUIDData"];
return [self initWithData:d];
}
@end