未验证 提交 5d00e377 编写于 作者: O openharmony_ci 提交者: Gitee

!2641 make_shared规范性整改

Merge pull request !2641 from honghecun/master
...@@ -101,18 +101,25 @@ C++ ...@@ -101,18 +101,25 @@ C++
Call the **AddEventListener** API of the **HiSysEventManager** class to add a listener for system events. Call the **AddEventListener** API of the **HiSysEventManager** class to add a listener for system events.
``` ```
auto demoListener = std::make_shared<DemoListener>(); std::shared_ptr<DemoListener> demoListener = nullptr;
// Add a ListenerRule object based on the event tag, with RuleType left unspecified (in this case, ruleType is defaulted to WHOLE_WORD). try {
ListenerRule tagRule("dfx"); demoListener = std::make_shared<DemoListener>();
// Add a ListenerRule object based on the event tag, with RuleType set as REGULAR. } catch(...) {
ListenerRule regRule("dfx.*", RuleType::REGULAR); // Catch exception thrown by make_shared
// Add a ListenerRule object based on the event domain and event name, with RuleType set as PREFIX. }
ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); if (demoListener != nullptr) {
std::vector<ListenerRule> sysRules; // Add a ListenerRule object based on the event tag, with RuleType left unspecified (in this case, ruleType is defaulted to WHOLE_WORD).
sysRules.push_back(tagRule); ListenerRule tagRule("dfx");
sysRules.push_back(regRule); // Add a ListenerRule object based on the event tag, with RuleType set as REGULAR.
sysRules.push_back(domainNameRule); ListenerRule regRule("dfx.*", RuleType::REGULAR);
HiSysEventManager::AddEventListener(demoListener, sysRules); // Add a ListenerRule object based on the event domain and event name, with RuleType set as PREFIX.
ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX);
std::vector<ListenerRule> sysRules;
sysRules.push_back(tagRule);
sysRules.push_back(regRule);
sysRules.push_back(domainNameRule);
HiSysEventManager::AddEventListener(demoListener, sysRules);
}
``` ```
2. Configure the **BUILD.gn** file. 2. Configure the **BUILD.gn** file.
......
...@@ -86,10 +86,17 @@ C++ ...@@ -86,10 +86,17 @@ C++
} // namespace OHOS } // namespace OHOS
// Invoke the query callback API to obtain system events. // Invoke the query callback API to obtain system events.
auto queryCallBack = std::make_shared<HiSysEventToolQuery>(); std::shared_ptr<HiSysEventToolQuery> queryCallBack = nullptr;
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); try {
std::vector<QueryRule> mRules; queryCallBack = std::make_shared<HiSysEventToolQuery>();
HiSysEventManager::QueryHiSysEvent(args, mRules, queryCallBack); } catch(...) {
// Catch exception thrown by make_shared
}
if (queryCallBack != nullptr) {
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents);
std::vector<QueryRule> rules;
HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack);
}
``` ```
2. Modify the **BUILD.gn** file. 2. Modify the **BUILD.gn** file.
......
...@@ -103,18 +103,25 @@ HiSysEvent提供了跨进程订阅机制,开发者可以通过注册订阅接 ...@@ -103,18 +103,25 @@ HiSysEvent提供了跨进程订阅机制,开发者可以通过注册订阅接
通过HiSysEventManager类提供的AddEventListener接口注册回调对象,完成对HiSysEvent的订阅: 通过HiSysEventManager类提供的AddEventListener接口注册回调对象,完成对HiSysEvent的订阅:
``` ```
auto demoListener = std::make_shared<DemoListener>(); std::shared_ptr<DemoListener> demoListener = nullptr;
// 事件标签规则订阅,规则类型为默认的全词匹配类型 try {
ListenerRule tagRule("dfx"); demoListener = std::make_shared<DemoListener>();
// 事件标签规则订阅,规则类型为正则匹配类型 } catch(...) {
ListenerRule regRule("dfx.*", RuleType::REGULAR); // 智能指针获取失败异常处理
// 事件领域及事件名称规则订阅,规则类型为前缀匹配类型 }
ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); if (demoListener != nullptr) {
std::vector<ListenerRule> sysRules; // 事件标签规则订阅,规则类型为默认的全词匹配类型
sysRules.push_back(tagRule); ListenerRule tagRule("dfx");
sysRules.push_back(regRule); // 事件标签规则订阅,规则类型为正则匹配类型
sysRules.push_back(domainNameRule); ListenerRule regRule("dfx.*", RuleType::REGULAR);
HiSysEventManager::AddEventListener(demoListener, sysRules); // 事件领域及事件名称规则订阅,规则类型为前缀匹配类型
ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX);
std::vector<ListenerRule> sysRules;
sysRules.push_back(tagRule);
sysRules.push_back(regRule);
sysRules.push_back(domainNameRule);
HiSysEventManager::AddEventListener(demoListener, sysRules);
}
``` ```
2. 编译配置 2. 编译配置
......
...@@ -86,10 +86,17 @@ C++接口实例。 ...@@ -86,10 +86,17 @@ C++接口实例。
} // namespace OHOS } // namespace OHOS
// 调用查询接口获取HiSysEvent事件 // 调用查询接口获取HiSysEvent事件
auto queryCallBack = std::make_shared<HiSysEventToolQuery>(); std::shared_ptr<HiSysEventToolQuery> queryCallBack = nullptr;
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); try {
std::vector<QueryRule> mRules; queryCallBack = std::make_shared<HiSysEventToolQuery>();
HiSysEventManager::QueryHiSysEvent(args, mRules, queryCallBack); } catch (...) {
// 智能指针获取失败处理
}
if (queryCallBack != nullptr) {
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents);
std::vector<QueryRule> rules;
HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack);
}
``` ```
2. 编译设置: 2. 编译设置:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册