1 /* 2 * Copyright (c) 2023 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 */ 15 16 #ifndef I_COOPERATE_STATE_H 17 #define I_COOPERATE_STATE_H 18 19 #include <memory> 20 21 #include "cooperate_context.h" 22 #include "cooperate_events.h" 23 24 namespace OHOS { 25 namespace Msdp { 26 namespace DeviceStatus { 27 namespace Cooperate { 28 class ICooperateState { 29 public: 30 ICooperateState() = default; 31 virtual ~ICooperateState() = default; 32 33 virtual void OnEvent(Context &context, const CooperateEvent &event) = 0; 34 virtual void OnEnterState(Context &context) = 0; 35 virtual void OnLeaveState(Context &context) = 0; 36 37 protected: 38 class ICooperateStep { 39 public: 40 ICooperateStep(ICooperateState &parent, std::shared_ptr<ICooperateStep> prev); 41 virtual ~ICooperateStep() = default; 42 43 virtual void OnEvent(Context &context, const CooperateEvent &event) = 0; 44 virtual void OnProgress(Context &context, const CooperateEvent &event) = 0; 45 virtual void OnReset(Context &context, const CooperateEvent &event) = 0; 46 47 void SetNext(std::shared_ptr<ICooperateStep> next); 48 49 protected: 50 void Switch(std::shared_ptr<ICooperateStep> step); 51 void Proceed(Context &context, const CooperateEvent &event); 52 void Reset(Context &context, const CooperateEvent &event); 53 54 ICooperateState &parent_; 55 std::shared_ptr<ICooperateStep> prev_ { nullptr }; 56 std::shared_ptr<ICooperateStep> next_ { nullptr }; 57 }; 58 59 void Switch(std::shared_ptr<ICooperateStep> step); 60 61 std::shared_ptr<ICooperateStep> current_ { nullptr }; 62 }; 63 } // namespace Cooperate 64 } // namespace DeviceStatus 65 } // namespace Msdp 66 } // namespace OHOS 67 #endif // I_COOPERATE_STATE_H 68