wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef DEVICE_STATE_MACHINE_H
#define DEVICE_STATE_MACHINE_H
 
#include <atomic>
#include <functional>
#include <mutex>
#include <vector>
 
#include "device_state.h"
 
/**
 * DeviceStateMachine - Manages device state transitions with validation
 * 
 * This class ensures strict state transition rules and provides a callback mechanism
 * for components to react to state changes.
 */
class DeviceStateMachine {
public:
    DeviceStateMachine();
    ~DeviceStateMachine() = default;
 
    // Delete copy constructor and assignment operator
    DeviceStateMachine(const DeviceStateMachine&) = delete;
    DeviceStateMachine& operator=(const DeviceStateMachine&) = delete;
 
    /**
     * Get the current device state
     */
    DeviceState GetState() const { return current_state_.load(); }
 
    /**
     * Attempt to transition to a new state
     * @param new_state The target state
     * @return true if transition was successful, false if invalid transition
     */
    bool TransitionTo(DeviceState new_state);
 
    /**
     * Check if transition to target state is valid from current state
     */
    bool CanTransitionTo(DeviceState target) const;
 
    /**
     * State change callback type
     * Parameters: old_state, new_state
     */
    using StateCallback = std::function<void(DeviceState, DeviceState)>;
 
    /**
     * Add a state change listener (observer pattern)
     * Callback is invoked in the context of the caller of TransitionTo()
     * @return listener id for removal
     */
    int AddStateChangeListener(StateCallback callback);
 
    /**
     * Remove a state change listener by id
     */
    void RemoveStateChangeListener(int listener_id);
 
    /**
     * Get state name string for logging
     */
    static const char* GetStateName(DeviceState state);
 
private:
    std::atomic<DeviceState> current_state_{kDeviceStateUnknown};
    std::vector<std::pair<int, StateCallback>> listeners_;
    int next_listener_id_{0};
    std::mutex mutex_;
 
    /**
     * Check if transition from source to target is valid
     */
    bool IsValidTransition(DeviceState from, DeviceState to) const;
 
    /**
     * Notify callback of state change
     */
    void NotifyStateChange(DeviceState old_state, DeviceState new_state);
};
 
#endif // DEVICE_STATE_MACHINE_H