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 FILEMGMT_COMMON_FILE_UTILS_H
17 #define FILEMGMT_COMMON_FILE_UTILS_H
18 
19 #include <memory>
20 
21 #include "filemgmt_libhilog.h"
22 
23 namespace OHOS::FileManagement {
24 template<typename T, typename... Args>
CreateSharedPtr(Args &&...args)25 std::shared_ptr<T> CreateSharedPtr(Args&&... args)
26 {
27     std::shared_ptr<T> sPtr = nullptr;
28     try {
29         sPtr = std::make_shared<T>(std::forward<Args>(args)...);
30     } catch (const std::bad_alloc&) {
31         return nullptr;
32     }
33     return sPtr;
34 };
35 
36 template<typename T, typename... Args>
CreateUniquePtr(Args &&...args)37 std::unique_ptr<T> CreateUniquePtr(Args&&... args)
38 {
39     std::unique_ptr<T> uPtr = nullptr;
40     try {
41         uPtr = std::make_unique<T>(std::forward<Args>(args)...);
42     } catch (const std::bad_alloc&) {
43         return nullptr;
44     }
45     return uPtr;
46 }
47 
48 } // namespace OHOS::FileManagement
49 #endif // FILEMGMT_COMMON_FILE_UTILS_H
50