-
Notifications
You must be signed in to change notification settings - Fork 986
Expand file tree
/
Copy pathExecuTorchBackendOption.mm
More file actions
137 lines (123 loc) · 3.7 KB
/
ExecuTorchBackendOption.mm
File metadata and controls
137 lines (123 loc) · 3.7 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "ExecuTorchBackendOption.h"
@implementation ExecuTorchBackendOption {
NSString *_key;
ExecuTorchBackendOptionType _type;
BOOL _boolValue;
NSInteger _intValue;
NSString *_stringValue;
}
- (instancetype)initWithKey:(NSString *)key
booleanValue:(BOOL)value {
self = [super init];
if (self) {
_key = [key copy];
_type = ExecuTorchBackendOptionTypeBoolean;
_boolValue = value;
}
return self;
}
- (instancetype)initWithKey:(NSString *)key
integerValue:(NSInteger)value {
self = [super init];
if (self) {
_key = [key copy];
_type = ExecuTorchBackendOptionTypeInteger;
_intValue = value;
}
return self;
}
- (instancetype)initWithKey:(NSString *)key
stringValue:(NSString *)value {
self = [super init];
if (self) {
_key = [key copy];
_type = ExecuTorchBackendOptionTypeString;
_stringValue = [value copy];
}
return self;
}
+ (instancetype)optionWithKey:(NSString *)key
booleanValue:(BOOL)value {
return [[self alloc] initWithKey:key booleanValue:value];
}
+ (instancetype)optionWithKey:(NSString *)key
integerValue:(NSInteger)value {
return [[self alloc] initWithKey:key integerValue:value];
}
+ (instancetype)optionWithKey:(NSString *)key
stringValue:(NSString *)value {
return [[self alloc] initWithKey:key stringValue:value];
}
#pragma mark - NSObject
- (NSString *)description {
switch (_type) {
case ExecuTorchBackendOptionTypeBoolean:
return [NSString stringWithFormat:@"<%@ %@=%@ (bool)>",
NSStringFromClass([self class]), _key, _boolValue ? @"true" : @"false"];
case ExecuTorchBackendOptionTypeInteger:
return [NSString stringWithFormat:@"<%@ %@=%ld (int)>",
NSStringFromClass([self class]), _key, (long)_intValue];
case ExecuTorchBackendOptionTypeString:
return [NSString stringWithFormat:@"<%@ %@=%@ (string)>",
NSStringFromClass([self class]), _key,
_stringValue ? [NSString stringWithFormat:@"\"%@\"", _stringValue] : @"(null)"];
}
return [super description];
}
- (NSString *)debugDescription {
return [self description];
}
- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
}
if (![object isKindOfClass:[ExecuTorchBackendOption class]]) {
return NO;
}
ExecuTorchBackendOption *other = (ExecuTorchBackendOption *)object;
if (_type != other.type || ![_key isEqualToString:other.key]) {
return NO;
}
switch (_type) {
case ExecuTorchBackendOptionTypeBoolean:
return _boolValue == other.boolValue;
case ExecuTorchBackendOptionTypeInteger:
return _intValue == other.intValue;
case ExecuTorchBackendOptionTypeString: {
// Both are non-null when type is String (init enforces it), but be
// defensive in case of subclass/manual misuse.
NSString *otherString = other.stringValue;
if (_stringValue == otherString) {
return YES;
}
if (_stringValue == nil || otherString == nil) {
return NO;
}
return [_stringValue isEqualToString:otherString];
}
}
return NO;
}
- (NSUInteger)hash {
NSUInteger h = _key.hash ^ (NSUInteger)_type;
switch (_type) {
case ExecuTorchBackendOptionTypeBoolean:
h ^= (NSUInteger)(_boolValue ? 1 : 0);
break;
case ExecuTorchBackendOptionTypeInteger:
h ^= (NSUInteger)_intValue;
break;
case ExecuTorchBackendOptionTypeString:
h ^= _stringValue.hash;
break;
}
return h;
}
@end