44 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Swift
		
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Swift
		
	
	
| // State.swift
 | |
| // The base class for implementing states in a StateMachine
 | |
| // Ported from Android implementation
 | |
| 
 | |
| import Foundation
 | |
| 
 | |
| /// Base class for implementing states in a StateMachine
 | |
| open class State: IState {
 | |
|     
 | |
|     /// Initialize a new state
 | |
|     public init() {}
 | |
|     
 | |
|     /// Called when a state is entered
 | |
|     /// - Parameters:
 | |
|     ///   - from: State we are coming from
 | |
|     ///   - params: Optional parameters for state transition
 | |
|     open func enter(from: IState?, params: Any?) {
 | |
|         // Default implementation does nothing
 | |
|     }
 | |
|     
 | |
|     /// Called when a state is exited
 | |
|     /// - Parameter to: State we are transitioning to
 | |
|     open func exit(to: IState?) {
 | |
|         // Default implementation does nothing
 | |
|     }
 | |
|     
 | |
|     /// Process a message sent to the state machine
 | |
|     /// - Parameter msg: The message to process
 | |
|     /// - Returns: HANDLED if processing has completed or NOT_HANDLED if the message wasn't processed
 | |
|     open func processMessage(_ msg: Message) -> Bool {
 | |
|         return false
 | |
|     }
 | |
|     
 | |
|     /// Name of State for debugging purposes
 | |
|     /// This default implementation returns the class name
 | |
|     open var name: String {
 | |
|         let className = String(describing: type(of: self))
 | |
|         if let lastDotIndex = className.lastIndex(of: ".") {
 | |
|             return String(className[className.index(after: lastDotIndex)...])
 | |
|         }
 | |
|         return className
 | |
|     }
 | |
| }
 |