提交 d33e91dd 编写于 作者: X xuyong

make_shared使用规范说明

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