Skip to content

Commit fefc4d9

Browse files
committed
Merge pull request #16 from cakper/swift/1.2
Migrate SwiftState to Swift 1.2
2 parents 653f1a1 + f6e4f50 commit fefc4d9

File tree

6 files changed

+27
-38
lines changed

6 files changed

+27
-38
lines changed

SwiftState/HierarchicalStateMachine.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class HierarchicalStateMachine<S: StateType, E: StateEventType>: StateMac
6161
{
6262
assert(state is HSM.State, "HSM state must be String.")
6363

64-
let components = split(state as HSM.State, { $0 == "." }, maxSplit: 1)
64+
let components = split(state as! HSM.State, { $0 == "." }, maxSplit: 1)
6565

6666
switch components.count {
6767
case 2:
@@ -85,7 +85,7 @@ public class HierarchicalStateMachine<S: StateType, E: StateEventType>: StateMac
8585
let (submachine, substate) = self._submachineTupleForState(self._state)
8686

8787
if let submachine = submachine {
88-
self._state = "\(submachine.name).\(submachine.state)" as State
88+
self._state = "\(submachine.name).\(submachine.state)" as! State
8989
}
9090

9191
return self._state
@@ -147,7 +147,7 @@ public class HierarchicalStateMachine<S: StateType, E: StateEventType>: StateMac
147147
// try changing submachine-internal state
148148
if fromSubmachine != nil && toSubmachine != nil && fromSubmachine === toSubmachine {
149149

150-
if toSubmachine!.canTryState(toSubstate, forEvent: event as HSM.Event) {
150+
if toSubmachine!.canTryState(toSubstate, forEvent: event as! HSM.Event) {
151151

152152
//
153153
// NOTE:

SwiftState/StateMachine.swift

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ public class StateMachineRouteID<S: StateType, E: StateEventType>
2626
self.transition = transition
2727
self.routeKey = routeKey
2828
self.event = event
29+
30+
self.bundledRouteIDs = nil
2931
}
3032

3133
private init(bundledRouteIDs: [StateMachineRouteID<S, E>]?)
3234
{
3335
self.bundledRouteIDs = bundledRouteIDs
36+
37+
self.transition = nil
38+
self.routeKey = nil
39+
self.event = nil
3440
}
3541
}
3642

@@ -48,11 +54,16 @@ public class StateMachineHandlerID<S: StateType, E: StateEventType>
4854
{
4955
self.transition = transition
5056
self.handlerKey = handlerKey
57+
58+
self.bundledHandlerIDs = nil
5159
}
5260

5361
private init(bundledHandlerIDs: [StateMachineHandlerID<S, E>]?)
5462
{
5563
self.bundledHandlerIDs = bundledHandlerIDs
64+
65+
self.transition = nil
66+
self.handlerKey = nil
5667
}
5768
}
5869

@@ -339,11 +350,6 @@ public class StateMachine<S: StateType, E: StateEventType>
339350
return self.addRoute(route)
340351
}
341352

342-
public func addRoute(transition: Transition, condition: @autoclosure () -> Bool) -> RouteID
343-
{
344-
return self.addRoute(transition, condition: { t in condition() })
345-
}
346-
347353
public func addRoute(route: Route) -> RouteID
348354
{
349355
return self._addRoute(route)
@@ -389,11 +395,6 @@ public class StateMachine<S: StateType, E: StateEventType>
389395
return self.addRoute(route, handler: handler)
390396
}
391397

392-
public func addRoute(transition: Transition, condition: @autoclosure () -> Bool, handler: Handler) -> (RouteID, HandlerID)
393-
{
394-
return self.addRoute(transition, condition: { t in condition() }, handler: handler)
395-
}
396-
397398
public func addRoute(route: Route, handler: Handler) -> (RouteID, HandlerID)
398399
{
399400
let transition = route.transition
@@ -621,11 +622,6 @@ public class StateMachine<S: StateType, E: StateEventType>
621622
return self.addRouteChain(routeChain, handler: handler)
622623
}
623624

624-
public func addRouteChain(chain: TransitionChain, condition: @autoclosure () -> Bool, handler: Handler) -> (RouteID, HandlerID)
625-
{
626-
return self.addRouteChain(chain, condition: { t in condition() }, handler: handler)
627-
}
628-
629625
public func addRouteChain(chain: RouteChain, handler: Handler) -> (RouteID, HandlerID)
630626
{
631627
var routeIDs: [RouteID] = []
@@ -787,11 +783,6 @@ public class StateMachine<S: StateType, E: StateEventType>
787783
return self.addRouteEvent(event, routes: routes)
788784
}
789785

790-
public func addRouteEvent(event: Event, transitions: [Transition], condition: @autoclosure () -> Bool) -> [RouteID]
791-
{
792-
return self.addRouteEvent(event, transitions: transitions, condition: { t in condition() })
793-
}
794-
795786
public func addRouteEvent(event: Event, routes: [Route]) -> [RouteID]
796787
{
797788
var routeIDs: [RouteID] = []
@@ -818,11 +809,6 @@ public class StateMachine<S: StateType, E: StateEventType>
818809

819810
return (routeIDs, handlerID)
820811
}
821-
822-
public func addRouteEvent(event: Event, transitions: [Transition], condition: @autoclosure () -> Bool, handler: Handler) -> ([RouteID], HandlerID)
823-
{
824-
return self.addRouteEvent(event, transitions: transitions, condition: { t in condition() }, handler: handler)
825-
}
826812

827813
public func addRouteEvent(event: Event, routes: [Route], handler: Handler) -> ([RouteID], HandlerID)
828814
{

SwiftState/StateRoute.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public struct StateRoute<S: StateType>
2222
self.condition = condition
2323
}
2424

25-
public init(transition: Transition, condition: @autoclosure () -> Bool)
26-
{
27-
self.init(transition: transition, condition: { t in condition() })
28-
}
29-
3025
public func toTransition() -> Transition
3126
{
3227
return self.transition

SwiftStateTests/StateMachineChainTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ class StateMachineChainTests: _TestCase
5050
var invokeCount = 0
5151

5252
// add 0 => 1 => 2
53-
machine.addRouteChain(.State0 => .State1 => .State2, condition: flag) { context in
53+
machine.addRouteChain(.State0 => .State1 => .State2, condition: { _ -> Bool in
54+
return flag
55+
}) { (context) -> Void in
5456
invokeCount++
5557
return
5658
}
57-
59+
5860
// tryState 0 => 1 => 2
5961
machine <- .State1
6062
machine <- .State2

SwiftStateTests/StateMachineTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class StateMachineTests: _TestCase
103103
var flag = false
104104

105105
// add 0 => 1
106-
machine.addRoute(.State0 => .State1, condition: flag)
106+
machine.addRoute(.State0 => .State1, condition: { _ -> Bool in
107+
return flag
108+
})
107109

108110
XCTAssertFalse(machine.hasRoute(.State0 => .State1))
109111

@@ -161,7 +163,9 @@ class StateMachineTests: _TestCase
161163
machine.addRoute(.State0 => .State1)
162164

163165
// add 0 => 1 with condition + conditionalHandler
164-
machine.addRoute(.State0 => .State1, condition: flag) { context in
166+
machine.addRoute(.State0 => .State1, condition: { _ -> Bool in
167+
return flag
168+
}) { context in
165169
returnedTransition = context.transition
166170
}
167171

SwiftStateTests/StateRouteTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class StateRouteTests: _TestCase
1818
XCTAssertEqual(route.transition.toState, MyState.State1)
1919
XCTAssertTrue(route.condition == nil)
2020

21-
let route2 = StateRoute<MyState>(transition: .State1 => .State2, condition: false)
21+
let route2 = StateRoute<MyState>(transition: .State1 => .State2, condition: { _ -> Bool in
22+
return false
23+
})
2224
XCTAssertEqual(route2.transition.fromState, MyState.State1)
2325
XCTAssertEqual(route2.transition.toState, MyState.State2)
2426
XCTAssertTrue(route2.condition != nil)

0 commit comments

Comments
 (0)