Refactor ComponentIdentifier, remove TypeIdentifiable
This commit is contained in:
parent
a1617ce390
commit
7c98d5af88
|
|
@ -5,22 +5,12 @@
|
|||
// Created by Christian Treffs on 08.10.17.
|
||||
//
|
||||
|
||||
public protocol Component: class, TypeIdentifiable {}
|
||||
public typealias ComponentIdentifier = ObjectIdentifier
|
||||
public protocol Component: class {
|
||||
static var identifier: ComponentIdentifier { get }
|
||||
var identifier: ComponentIdentifier { get }
|
||||
}
|
||||
|
||||
public extension Component {
|
||||
static var identifier: ComponentIdentifier { return Self.typeObjectIdentifier }
|
||||
var identifier: ComponentIdentifier { return self.typeObjectIdentifier }
|
||||
}
|
||||
|
||||
/// TypeIdentifiable
|
||||
/// Identifies an object by it's meta type.
|
||||
public protocol TypeIdentifiable {
|
||||
static var typeObjectIdentifier: ObjectIdentifier { get }
|
||||
var typeObjectIdentifier: ObjectIdentifier { get }
|
||||
}
|
||||
|
||||
public extension TypeIdentifiable {
|
||||
static var typeObjectIdentifier: ObjectIdentifier { return ObjectIdentifier(Self.self) }
|
||||
var typeObjectIdentifier: ObjectIdentifier { return Self.typeObjectIdentifier }
|
||||
static var identifier: ComponentIdentifier { return ComponentIdentifier(Self.self) }
|
||||
@inlinable var identifier: ComponentIdentifier { return Self.identifier }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// ComponentIdentifier.swift
|
||||
//
|
||||
//
|
||||
// Created by Christian Treffs on 20.08.19.
|
||||
//
|
||||
|
||||
/// Identifies a component by it's meta type
|
||||
public struct ComponentIdentifier {
|
||||
@usableFromInline let objectIdentifier: ObjectIdentifier
|
||||
|
||||
init<T>(_ type: T.Type) where T: Component {
|
||||
self.objectIdentifier = ObjectIdentifier(type)
|
||||
}
|
||||
}
|
||||
|
||||
extension ComponentIdentifier: Equatable { }
|
||||
extension ComponentIdentifier: Hashable { }
|
||||
|
|
@ -11,13 +11,16 @@ import XCTest
|
|||
class ComponentTests: XCTestCase {
|
||||
|
||||
func testComponentIdentifier() {
|
||||
let p1 = Position(x: 1, y: 2)
|
||||
XCTAssert(p1.identifier == Position.identifier)
|
||||
|
||||
let v1 = Velocity(a: 3.14)
|
||||
XCTAssert(v1.identifier == Velocity.identifier)
|
||||
XCTAssert(v1.identifier != p1.identifier)
|
||||
XCTAssert(Velocity.identifier != Position.identifier)
|
||||
XCTAssertEqual(Position.identifier, Position.identifier)
|
||||
XCTAssertEqual(Velocity.identifier, Velocity.identifier)
|
||||
XCTAssertNotEqual(Velocity.identifier, Position.identifier)
|
||||
|
||||
let p1 = Position(x: 1, y: 2)
|
||||
let v1 = Velocity(a: 3.14)
|
||||
XCTAssertEqual(p1.identifier, Position.identifier)
|
||||
XCTAssertEqual(v1.identifier, Velocity.identifier)
|
||||
XCTAssertNotEqual(v1.identifier, p1.identifier)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue