10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ #if DEPLOYMENT_RUNTIME_SWIFT
14
+ import CoreFoundation
15
+ #else
13
16
@_exported import Foundation // Clang module
14
17
import _SwiftCoreFoundationOverlayShims
18
+ #endif
15
19
16
20
/// A `Measurement` is a model type that holds a `Double` value associated with a `Unit`.
17
21
///
@@ -25,13 +29,13 @@ public struct Measurement<UnitType : Unit> : ReferenceConvertible, Comparable, E
25
29
26
30
/// The value component of the `Measurement`.
27
31
public var value : Double
28
-
32
+
29
33
/// Create a `Measurement` given a specified value and unit.
30
34
public init ( value: Double , unit: UnitType ) {
31
35
self . value = value
32
36
self . unit = unit
33
37
}
34
-
38
+
35
39
public var hashValue : Int {
36
40
return Int ( bitPattern: __CFHashDouble ( value) )
37
41
}
@@ -42,11 +46,11 @@ extension Measurement : CustomStringConvertible, CustomDebugStringConvertible, C
42
46
public var description : String {
43
47
return " \( value) \( unit. symbol) "
44
48
}
45
-
49
+
46
50
public var debugDescription : String {
47
51
return " \( value) \( unit. symbol) "
48
52
}
49
-
53
+
50
54
public var customMirror : Mirror {
51
55
var c : [ ( label: String ? , value: Any ) ] = [ ]
52
56
c. append ( ( label: " value " , value: value) )
@@ -76,7 +80,7 @@ extension Measurement where UnitType : Dimension {
76
80
}
77
81
}
78
82
}
79
-
83
+
80
84
/// Converts the measurement to the specified unit.
81
85
///
82
86
/// - parameter otherUnit: A unit of the same `Dimension`.
@@ -85,7 +89,7 @@ extension Measurement where UnitType : Dimension {
85
89
}
86
90
87
91
/// Add two measurements of the same Dimension.
88
- ///
92
+ ///
89
93
/// If the `unit` of the `lhs` and `rhs` are `isEqual`, then this returns the result of adding the `value` of each `Measurement`. If they are not equal, then this will convert both to the base unit of the `Dimension` and return the result as a `Measurement` of that base unit.
90
94
/// - returns: The result of adding the two measurements.
91
95
public static func + ( lhs: Measurement < UnitType > , rhs: Measurement < UnitType > ) -> Measurement < UnitType > {
@@ -170,7 +174,7 @@ extension Measurement {
170
174
return lhs. value == rhs. value
171
175
} else {
172
176
if let lhsDimensionalUnit = lhs. unit as? Dimension ,
173
- let rhsDimensionalUnit = rhs. unit as? Dimension {
177
+ let rhsDimensionalUnit = rhs. unit as? Dimension {
174
178
if type ( of: lhsDimensionalUnit) . baseUnit ( ) == type ( of: rhsDimensionalUnit) . baseUnit ( ) {
175
179
let lhsValueInTermsOfBase = lhsDimensionalUnit. converter. baseUnitValue ( fromValue: lhs. value)
176
180
let rhsValueInTermsOfBase = rhsDimensionalUnit. converter. baseUnitValue ( fromValue: rhs. value)
@@ -188,7 +192,7 @@ extension Measurement {
188
192
return lhs. value < rhs. value
189
193
} else {
190
194
if let lhsDimensionalUnit = lhs. unit as? Dimension ,
191
- let rhsDimensionalUnit = rhs. unit as? Dimension {
195
+ let rhsDimensionalUnit = rhs. unit as? Dimension {
192
196
if type ( of: lhsDimensionalUnit) . baseUnit ( ) == type ( of: rhsDimensionalUnit) . baseUnit ( ) {
193
197
let lhsValueInTermsOfBase = lhsDimensionalUnit. converter. baseUnitValue ( fromValue: lhs. value)
194
198
let rhsValueInTermsOfBase = rhsDimensionalUnit. converter. baseUnitValue ( fromValue: rhs. value)
@@ -202,17 +206,23 @@ extension Measurement {
202
206
203
207
// Implementation note: similar to NSArray, NSDictionary, etc., NSMeasurement's import as an ObjC generic type is suppressed by the importer. Eventually we will need a more general purpose mechanism to correctly import generic types.
204
208
209
+ #if DEPLOYMENT_RUNTIME_SWIFT
210
+ internal typealias MeasurementBridgeType = _ObjectTypeBridgeable
211
+ #else
212
+ internal typealias MeasurementBridgeType = _ObjectiveCBridgeable
213
+ #endif
214
+
205
215
@available ( OSX 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
206
- extension Measurement : _ObjectiveCBridgeable {
216
+ extension Measurement : MeasurementBridgeType {
207
217
@_semantics ( " convertToObjectiveC " )
208
218
public func _bridgeToObjectiveC( ) -> NSMeasurement {
209
219
return NSMeasurement ( doubleValue: value, unit: unit)
210
220
}
211
-
221
+
212
222
public static func _forceBridgeFromObjectiveC( _ source: NSMeasurement , result: inout Measurement ? ) {
213
223
result = Measurement ( value: source. doubleValue, unit: source. unit as! UnitType )
214
224
}
215
-
225
+
216
226
public static func _conditionallyBridgeFromObjectiveC( _ source: NSMeasurement , result: inout Measurement ? ) -> Bool {
217
227
if let u = source. unit as? UnitType {
218
228
result = Measurement ( value: source. doubleValue, unit: u)
@@ -221,7 +231,7 @@ extension Measurement : _ObjectiveCBridgeable {
221
231
return false
222
232
}
223
233
}
224
-
234
+
225
235
public static func _unconditionallyBridgeFromObjectiveC( _ source: NSMeasurement ? ) -> Measurement {
226
236
let u = source!. unit as! UnitType
227
237
return Measurement ( value: source!. doubleValue, unit: u)
@@ -233,7 +243,11 @@ extension NSMeasurement : _HasCustomAnyHashableRepresentation {
233
243
// Must be @nonobjc to avoid infinite recursion during bridging.
234
244
@nonobjc
235
245
public func _toCustomAnyHashable( ) -> AnyHashable ? {
246
+ #if DEPLOYMENT_RUNTIME_SWIFT
247
+ return AnyHashable ( Measurement . _unconditionallyBridgeFromObjectiveC ( self ) )
248
+ #else
236
249
return AnyHashable ( self as Measurement )
250
+ #endif
237
251
}
238
252
}
239
253
0 commit comments