15bebb993Sopenharmony_ci/*
25bebb993Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License.
55bebb993Sopenharmony_ci * You may obtain a copy of the License at
65bebb993Sopenharmony_ci *
75bebb993Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85bebb993Sopenharmony_ci *
95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and
135bebb993Sopenharmony_ci * limitations under the License.
145bebb993Sopenharmony_ci */
155bebb993Sopenharmony_ci
165bebb993Sopenharmony_ci#ifndef FILEMGMT_COMMON_FILE_UTILS_H
175bebb993Sopenharmony_ci#define FILEMGMT_COMMON_FILE_UTILS_H
185bebb993Sopenharmony_ci
195bebb993Sopenharmony_ci#include <memory>
205bebb993Sopenharmony_ci
215bebb993Sopenharmony_ci#include "filemgmt_libhilog.h"
225bebb993Sopenharmony_ci
235bebb993Sopenharmony_cinamespace OHOS::FileManagement {
245bebb993Sopenharmony_citemplate<typename T, typename... Args>
255bebb993Sopenharmony_cistd::shared_ptr<T> CreateSharedPtr(Args&&... args)
265bebb993Sopenharmony_ci{
275bebb993Sopenharmony_ci    std::shared_ptr<T> sPtr = nullptr;
285bebb993Sopenharmony_ci    try {
295bebb993Sopenharmony_ci        sPtr = std::make_shared<T>(std::forward<Args>(args)...);
305bebb993Sopenharmony_ci    } catch (const std::bad_alloc&) {
315bebb993Sopenharmony_ci        return nullptr;
325bebb993Sopenharmony_ci    }
335bebb993Sopenharmony_ci    return sPtr;
345bebb993Sopenharmony_ci};
355bebb993Sopenharmony_ci
365bebb993Sopenharmony_citemplate<typename T, typename... Args>
375bebb993Sopenharmony_cistd::unique_ptr<T> CreateUniquePtr(Args&&... args)
385bebb993Sopenharmony_ci{
395bebb993Sopenharmony_ci    std::unique_ptr<T> uPtr = nullptr;
405bebb993Sopenharmony_ci    try {
415bebb993Sopenharmony_ci        uPtr = std::make_unique<T>(std::forward<Args>(args)...);
425bebb993Sopenharmony_ci    } catch (const std::bad_alloc&) {
435bebb993Sopenharmony_ci        return nullptr;
445bebb993Sopenharmony_ci    }
455bebb993Sopenharmony_ci    return uPtr;
465bebb993Sopenharmony_ci}
475bebb993Sopenharmony_ci
485bebb993Sopenharmony_ci} // namespace OHOS::FileManagement
495bebb993Sopenharmony_ci#endif // FILEMGMT_COMMON_FILE_UTILS_H
50