1ea497e4dSopenharmony_ci/* 2ea497e4dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3ea497e4dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4ea497e4dSopenharmony_ci * you may not use this file except in compliance with the License. 5ea497e4dSopenharmony_ci * You may obtain a copy of the License at 6ea497e4dSopenharmony_ci * 7ea497e4dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8ea497e4dSopenharmony_ci * 9ea497e4dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10ea497e4dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11ea497e4dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12ea497e4dSopenharmony_ci * See the License for the specific language governing permissions and 13ea497e4dSopenharmony_ci * limitations under the License. 14ea497e4dSopenharmony_ci */ 15ea497e4dSopenharmony_ci 16ea497e4dSopenharmony_ci#ifndef IAM_PTR_H 17ea497e4dSopenharmony_ci#define IAM_PTR_H 18ea497e4dSopenharmony_ci 19ea497e4dSopenharmony_ci#include <memory> 20ea497e4dSopenharmony_ci 21ea497e4dSopenharmony_ci#include "refbase.h" 22ea497e4dSopenharmony_ci 23ea497e4dSopenharmony_cinamespace OHOS { 24ea497e4dSopenharmony_cinamespace UserIam { 25ea497e4dSopenharmony_cinamespace Common { 26ea497e4dSopenharmony_citemplate <typename T> 27ea497e4dSopenharmony_cistatic inline std::shared_ptr<T> SptrToStdSharedPtr(sptr<T> &other) 28ea497e4dSopenharmony_ci{ 29ea497e4dSopenharmony_ci return std::shared_ptr<T>(other.GetRefPtr(), [other](T *) mutable { other = nullptr; }); 30ea497e4dSopenharmony_ci} 31ea497e4dSopenharmony_ci 32ea497e4dSopenharmony_citemplate <typename T, typename... Args> 33ea497e4dSopenharmony_cistatic inline std::shared_ptr<T> MakeShared(Args &&...args) 34ea497e4dSopenharmony_ci{ 35ea497e4dSopenharmony_ci try { 36ea497e4dSopenharmony_ci return std::make_shared<T>(std::forward<Args>(args)...); 37ea497e4dSopenharmony_ci } catch (...) { 38ea497e4dSopenharmony_ci return nullptr; 39ea497e4dSopenharmony_ci } 40ea497e4dSopenharmony_ci} 41ea497e4dSopenharmony_ci 42ea497e4dSopenharmony_citemplate <typename T, typename... Args> 43ea497e4dSopenharmony_cistatic inline std::unique_ptr<T> MakeUnique(Args &&...args) 44ea497e4dSopenharmony_ci{ 45ea497e4dSopenharmony_ci try { 46ea497e4dSopenharmony_ci return std::make_unique<T>(std::forward<Args>(args)...); 47ea497e4dSopenharmony_ci } catch (...) { 48ea497e4dSopenharmony_ci return nullptr; 49ea497e4dSopenharmony_ci } 50ea497e4dSopenharmony_ci} 51ea497e4dSopenharmony_ci} // namespace Common 52ea497e4dSopenharmony_ci} // namespace UserIam 53ea497e4dSopenharmony_ci} // namespace OHOS 54ea497e4dSopenharmony_ci 55ea497e4dSopenharmony_ci#endif // IAM_PTR_H 56