• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 OHOS_FILEMGMT_BACKUP_TOOLS_OP_H
17 #define OHOS_FILEMGMT_BACKUP_TOOLS_OP_H
18 
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 namespace OHOS::FileManagement::Backup {
26 class ToolsOp {
27 public:
28     using CRefVStrView = const std::vector<std::string_view> &;
29     struct CmdInfo {
30         std::string paramName;
31         bool repeatable = false;
32     };
33 
34     struct Descriptor {
35         // 命令名,必填
36         std::vector<std::string_view> opName;
37         // 参数,选填
38         std::vector<CmdInfo> argList;
39         // 命令帮助语句,选填
40         std::function<std::string()> funcGenHelpMsg;
41         // 命令执行主体,必填
42         std::function<int(std::map<std::string, std::vector<std::string>> &args)> funcExec;
43     };
44 
45     /**
46      * @brief 构造一个操作
47      *
48      * @param desc 操作具体信息
49      */
ToolsOp(Descriptor && desc)50     explicit ToolsOp(Descriptor &&desc) : desc_(std::move(desc)) {}
51 
52     /**
53      * @brief 获取当前操作的名称。操作由多条字符串构成时,之间由空格隔开
54      *
55      * @return const std::string 当前操作的名称
56      */
57     const std::string GetName() const;
58 
59     /**
60      * @brief 获取当前操作的参数
61      *
62      * @return std::vector<CmdInfo> 当前参数的向量
63      */
GetParams()64     const std::vector<CmdInfo> GetParams() const
65     {
66         return desc_.argList;
67     }
68 
69     /**
70      * @brief 获取当前操作的原始具体信息
71      *
72      * @return const Descriptor& 当前操作的原始具体信息
73      */
GetDescriptor()74     const Descriptor &GetDescriptor() const
75     {
76         return desc_;
77     }
78 
79     /**
80      * @brief 获取所有操作
81      *
82      * @return const std::vector<ToolsOp>& 所有操作
83      */
GetAllOperations()84     static const std::vector<ToolsOp> &GetAllOperations()
85     {
86         return ToolsOp::opsAvailable_;
87     }
88 
89     /**
90      * @brief 注册一个操作
91      *
92      * @param op 操作
93      * @return true 注册成功
94      * @return false 注册失败
95      */
96     static bool Register(ToolsOp &&op);
97 
98     /**
99      * @brief 将当前操作与主函数给定的操作相匹配(大小写敏感)
100      *
101      * @param op 给定操作
102      * @return true 匹配成功
103      * @return false 匹配失败
104      */
105     bool TryMatch(CRefVStrView op) const;
106 
107     /**
108      * @brief 使用主函数给定的参数表执行当前操作
109      *
110      * @param args 给定参数表
111      * @return int 错误码(0 表示成功,非零表示失败)
112      */
113     int Execute(std::map<std::string, std::vector<std::string>> mapArg) const;
114 
115 private:
116     Descriptor desc_;
117     static inline std::vector<ToolsOp> opsAvailable_;
118 };
119 } // namespace OHOS::FileManagement::Backup
120 
121 #endif // OHOS_FILEMGMT_BACKUP_TOOLS_OP_H