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 IAM_STATE_MACHINE_H
17 #define IAM_STATE_MACHINE_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "nocopyable.h"
24 
25 #include "thread_handler.h"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace FaceAuth {
30 class FiniteStateMachine {
31 public:
32     using Action = std::function<void(FiniteStateMachine &, uint32_t)>;
33     class Builder;
34     virtual ~FiniteStateMachine() = default;
35     virtual void Schedule(uint32_t event) = 0;
36     virtual uint32_t GetCurrentState() const = 0;
37     virtual uint32_t EnsureCurrentState() = 0;
38     virtual void SetThreadHandler(const std::shared_ptr<ThreadHandler> &threadHandler) = 0;
39     virtual const std::string &GetMachineName() const = 0;
40 };
41 
42 class FiniteStateMachine::Builder {
43 public:
44     static std::shared_ptr<Builder> New(const std::string &name, uint32_t initialState);
45     virtual ~Builder() = default;
46     virtual std::shared_ptr<Builder> MakeTransition(uint32_t state, uint32_t event, uint32_t nextState,
47         const FiniteStateMachine::Action &action) = 0;
48 
49     virtual std::shared_ptr<Builder> MakeTransition(uint32_t state, uint32_t event, uint32_t nextState) = 0;
50 
51     virtual std::shared_ptr<Builder> MakeOnStateEnter(uint32_t state, const FiniteStateMachine::Action &action) = 0;
52 
53     virtual std::shared_ptr<Builder> MakeOnStateLeave(uint32_t state, const FiniteStateMachine::Action &action) = 0;
54 
55     virtual std::shared_ptr<FiniteStateMachine> Build() = 0;
56 };
57 } // namespace FaceAuth
58 } // namespace UserIam
59 } // namespace OHOS
60 
61 #endif // IAM_STATE_MACHINE_H