1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * Description: state machine function realization. 15 * Author: lijianzhao 16 * Create: 2022-01-25 17 */ 18 19 #ifndef SERVICE_SRC_SESSION_SRC_UTILS_INCLUDE_STATE_MACHINE_H 20 #define SERVICE_SRC_SESSION_SRC_UTILS_INCLUDE_STATE_MACHINE_H 21 22 #include <queue> 23 #include <memory> 24 #include "handler.h" 25 #include "utils.h" 26 27 namespace OHOS { 28 namespace CastEngine { 29 namespace CastEngineService { 30 class State { 31 public: parentState_(parentState)32 explicit State(const std::shared_ptr<State> &parentState = nullptr) : parentState_(parentState) {} GetParentState()33 std::shared_ptr<State> GetParentState() 34 { 35 return parentState_; 36 } 37 38 protected: 39 virtual ~State() = default; Enter()40 virtual void Enter() {} Exit()41 virtual void Exit() {} 42 virtual bool HandleMessage(const Message &msg) = 0; 43 44 private: 45 friend class StateMachine; 46 std::shared_ptr<State> parentState_; 47 DISALLOW_EVIL_CONSTRUCTORS(State); 48 }; 49 50 class StateMachine : public Handler { 51 public: 52 StateMachine() = default; 53 54 protected: 55 ~StateMachine() override = default; 56 void HandleMessage(const Message &msg) override; 57 void TransferState(const std::shared_ptr<State> &state); 58 void DeferMessage(const Message &msg); 59 60 private: 61 void ProcessDeferredMessages(); 62 std::shared_ptr<State> state_; 63 std::queue<Message> deferredQueue_; 64 DISALLOW_EVIL_CONSTRUCTORS(StateMachine); 65 }; 66 } // namespace CastEngineService 67 } // namespace CastEngine 68 } // namespace OHOS 69 70 #endif 71