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 #include "i_cooperate_state.h"
17
18 namespace OHOS {
19 namespace Msdp {
20 namespace DeviceStatus {
21 namespace Cooperate {
22
ICooperateStep(ICooperateState & parent,std::shared_ptr<ICooperateStep> prev)23 ICooperateState::ICooperateStep::ICooperateStep(ICooperateState &parent, std::shared_ptr<ICooperateStep> prev)
24 : parent_(parent), prev_(prev)
25 {}
26
Switch(std::shared_ptr<ICooperateStep> step)27 void ICooperateState::Switch(std::shared_ptr<ICooperateStep> step)
28 {
29 if (step != nullptr) {
30 current_ = step;
31 }
32 }
33
SetNext(std::shared_ptr<ICooperateStep> next)34 void ICooperateState::ICooperateStep::SetNext(std::shared_ptr<ICooperateStep> next)
35 {
36 if (next != nullptr) {
37 next_ = next;
38 } else if (next_ != nullptr) {
39 next_->SetNext(nullptr);
40 next_ = nullptr;
41 }
42 }
43
Switch(std::shared_ptr<ICooperateStep> step)44 void ICooperateState::ICooperateStep::Switch(std::shared_ptr<ICooperateStep> step)
45 {
46 parent_.Switch(step);
47 }
48
Proceed(Context & context,const CooperateEvent & event)49 void ICooperateState::ICooperateStep::Proceed(Context &context, const CooperateEvent &event)
50 {
51 if (next_ != nullptr) {
52 Switch(next_);
53 next_->OnProgress(context, event);
54 }
55 }
56
Reset(Context & context,const CooperateEvent & event)57 void ICooperateState::ICooperateStep::Reset(Context &context, const CooperateEvent &event)
58 {
59 if (prev_ != nullptr) {
60 Switch(prev_);
61 prev_->OnReset(context, event);
62 }
63 }
64 } // namespace Cooperate
65 } // namespace DeviceStatus
66 } // namespace Msdp
67 } // namespace OHOS
68