1/*
2 * Copyright (c) 2022 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_PTR_H
17#define IAM_PTR_H
18
19#include <memory>
20
21#include "refbase.h"
22
23namespace OHOS {
24namespace UserIam {
25namespace Common {
26template <typename T>
27static inline std::shared_ptr<T> SptrToStdSharedPtr(sptr<T> &other)
28{
29    return std::shared_ptr<T>(other.GetRefPtr(), [other](T *) mutable { other = nullptr; });
30}
31
32template <typename T, typename... Args>
33static inline std::shared_ptr<T> MakeShared(Args &&... args)
34{
35    try {
36        return std::make_shared<T>(std::forward<Args>(args)...);
37    } catch (...) {
38        return nullptr;
39    }
40}
41
42template <typename T, typename... Args>
43static inline std::unique_ptr<T> MakeUnique(Args &&... args)
44{
45    try {
46        return std::make_unique<T>(std::forward<Args>(args)...);
47    } catch (...) {
48        return nullptr;
49    }
50}
51} // namespace Common
52} // namespace UserIam
53} // namespace OHOS
54
55#endif // IAM_PTR_H