Skip to content

Implement NSMeasurement #1425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ There is no _Complete_ status for test coverage because there are always additio
| `NSConcreteValue` | N/A | N/A | For internal use only |
| `NSSpecialValue` | N/A | N/A | For internal use only |
| `NSValue` | Complete | Substantial | |
| `NSMeasurement` | Unimplemented | None | |
| `NSMeasurement` | Complete | None | |
| `Measurement` | Complete | None | |
| `UnitConverter` | Complete | Incomplete | |
| `UnitConverterLinear` | Complete | Incomplete | |
Expand Down
65 changes: 58 additions & 7 deletions Foundation/NSMeasurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,72 @@ open class NSMeasurement : NSObject, NSCopying, NSSecureCoding {
self.unit = unit
}

open func canBeConverted(to unit: Unit) -> Bool { NSUnimplemented() }
open func canBeConverted(to otherUnit: Unit) -> Bool {
return otherUnit.isKind(of: type(of: unit))
}

open func converting(to unit: Unit) -> Measurement<Unit> { NSUnimplemented() }
open func converting(to otherUnit: Unit) -> Measurement<Unit> {
if canBeConverted(to: otherUnit) {
if unit.isEqual(otherUnit) {
return Measurement(value: doubleValue, unit: otherUnit)
} else {
guard let sdim = unit as? Dimension,
let udim = otherUnit as? Dimension else {
fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: unit)) rhs: \(type(of: otherUnit))")
}
let valueInTermsOfBase = unit.converter.baseUnitValue(fromValue: value)
if otherUnit.isEqual(type(of: unit).baseUnit()) {
return Measurement(value: valueInTermsOfBase, unit: otherUnit)
} else {
let otherValueFromTermsOfBase = otherUnit.converter.value(fromBaseUnitValue: valueInTermsOfBase)
return Measurement(value: otherValueFromTermsOfBase, unit: otherUnit)
}
}
} else {
fatalError("Cannot convert measurements of differing unit types! self: \(type(of: unit)) unit: \(type(of: otherUnit))")
}
}

open func adding(_ measurement: Measurement<Unit>) -> Measurement<Unit> { NSUnimplemented() }
open func adding(_ rhs: Measurement<Unit>) -> Measurement<Unit> {
if self.unit.isEqual(rhs.unit) {
return Measurement(value: self.doubleValue + rhs.doubleValue, unit: self.unit)
} else {
let selfValueInTermsOfBase = self.unit.converter.baseUnitValue(fromValue: self.doubleValue)
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.doubleValue)
return Measurement(value: selfValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: self.unit).baseUnit())
}
}

open func subtracting(_ measurement: Measurement<Unit>) -> Measurement<Unit> { NSUnimplemented() }
open func subtracting(_ rhs: Measurement<Unit>) -> Measurement<Unit> {
if self.unit.isEqual(rhs.unit) {
return Measurement(value: self.doubleValue - rhs.doubleValue, unit: self.unit)
} else {
let selfValueInTermsOfBase = self.unit.converter.baseUnitValue(fromValue: self.doubleValue)
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
return Measurement(value: selfValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: self.unit).baseUnit())
}
}

open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() }
open func copy(with zone: NSZone? = nil) -> Any { return self }

open class var supportsSecureCoding: Bool { return true }

open func encode(with aCoder: NSCoder) { NSUnimplemented() }
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.doubleValue, forKey:"NS.dblval")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NS.value

aCoder.encode(self.unit, forKey:"NS.unit")
}

public required init?(coder aDecoder: NSCoder) { NSUnimplemented() }
public required init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let doubleValue = aDecoder.decodeDouble(forKey: "NS.dblval")
let unit = aDecoder.decodeObject(forKey: "NS.unit")
self.init(coefficient: coefficient, constant: constant)
}
}

extension NSMeasurement : _StructTypeBridgeable {
Expand Down