1+ //
2+ // EventType.swift
3+ // SwiftState
4+ //
5+ // Created by Yasuhiro Inami on 2014/08/05.
6+ // Copyright (c) 2014年 Yasuhiro Inami. All rights reserved.
7+ //
8+
9+ public protocol EventType : Hashable { }
10+
11+ // MARK: _Event (internal)
12+
13+ internal enum _Event < E: EventType > : Hashable
14+ {
15+ case Some( E )
16+ case Any // represents any `Some(E)` events but not `.None`
17+ case None // default internal value for `addRoute()` without event
18+
19+ internal var hashValue : Int
20+ {
21+ switch self {
22+ case . Some( let x) : return x. hashValue
23+ case . Any: return - 4611686018427387904
24+ case . None: return - 4611686018427387905
25+ }
26+ }
27+
28+ internal var value : E ?
29+ {
30+ switch self {
31+ case . Some( let x) : return x
32+ default : return nil
33+ }
34+ }
35+ }
36+
37+ internal func == < E: Hashable > ( lhs: _Event < E > , rhs: _Event < E > ) -> Bool
38+ {
39+ return lhs. hashValue == rhs. hashValue
40+ }
41+
42+ internal func == < E: Hashable > ( lhs: _Event < E > , rhs: E ) -> Bool
43+ {
44+ return lhs. hashValue == rhs. hashValue
45+ }
46+
47+ internal func == < E: Hashable > ( lhs: E , rhs: _Event < E > ) -> Bool
48+ {
49+ return lhs. hashValue == rhs. hashValue
50+ }
51+
52+ // MARK: Event (public)
53+
54+ /// `EventType` wrapper for handling`.Any` event.
55+ public enum Event < E: EventType > : Equatable
56+ {
57+ case Some( E )
58+ case Any
59+
60+ public var value : E ?
61+ {
62+ switch self {
63+ case . Some( let x) : return x
64+ default : return nil
65+ }
66+ }
67+
68+ internal func _toInternal( ) -> _Event < E >
69+ {
70+ switch self {
71+ case . Some( let x) : return . Some( x)
72+ case . Any: return . Any
73+ }
74+ }
75+ }
76+
77+ public func == < E: EventType > ( lhs: Event < E > , rhs: Event < E > ) -> Bool
78+ {
79+ switch ( lhs, rhs) {
80+ case let ( . Some( x1) , . Some( x2) ) where x1 == x2:
81+ return true
82+ case ( . Any, . Any) :
83+ return true
84+ default :
85+ return false
86+ }
87+ }
88+
89+ // MARK: NoEvent
90+
91+ /// Useful for creating StateMachine without events, i.e. `Machine<MyState, NoEvent>`.
92+ public enum NoEvent : EventType
93+ {
94+ public var hashValue : Int
95+ {
96+ return 0
97+ }
98+ }
99+
100+ public func == ( lhs: NoEvent , rhs: NoEvent ) -> Bool
101+ {
102+ return true
103+ }
0 commit comments