112e714ceSopenharmony_ci/* 212e714ceSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 312e714ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 412e714ceSopenharmony_ci * you may not use this file except in compliance with the License. 512e714ceSopenharmony_ci * You may obtain a copy of the License at 612e714ceSopenharmony_ci * 712e714ceSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 812e714ceSopenharmony_ci * 912e714ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1012e714ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1112e714ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1212e714ceSopenharmony_ci * See the License for the specific language governing permissions and 1312e714ceSopenharmony_ci * limitations under the License. 1412e714ceSopenharmony_ci */ 1512e714ceSopenharmony_ci 1612e714ceSopenharmony_ci#ifndef NEURAL_NETWORK_RUNTIME_UTILS_H 1712e714ceSopenharmony_ci#define NEURAL_NETWORK_RUNTIME_UTILS_H 1812e714ceSopenharmony_ci 1912e714ceSopenharmony_ci#include <string> 2012e714ceSopenharmony_ci#include <memory> 2112e714ceSopenharmony_ci 2212e714ceSopenharmony_ci#include "log.h" 2312e714ceSopenharmony_ci 2412e714ceSopenharmony_cinamespace OHOS { 2512e714ceSopenharmony_cinamespace NeuralNetworkRuntime { 2612e714ceSopenharmony_citemplate<typename T, typename... Args> 2712e714ceSopenharmony_cistd::shared_ptr<T> CreateSharedPtr(Args&&... args) 2812e714ceSopenharmony_ci{ 2912e714ceSopenharmony_ci std::shared_ptr<T> tPtr = nullptr; 3012e714ceSopenharmony_ci try { 3112e714ceSopenharmony_ci tPtr = std::make_shared<T>(args...); 3212e714ceSopenharmony_ci } catch (const std::bad_alloc& except) { 3312e714ceSopenharmony_ci LOGW("Create a new shared pointer failed. Error: %s", except.what()); 3412e714ceSopenharmony_ci return nullptr; 3512e714ceSopenharmony_ci } 3612e714ceSopenharmony_ci return tPtr; 3712e714ceSopenharmony_ci}; 3812e714ceSopenharmony_ci 3912e714ceSopenharmony_citemplate<typename T, typename... Args> 4012e714ceSopenharmony_cistd::unique_ptr<T> CreateUniquePtr(Args&&... args) 4112e714ceSopenharmony_ci{ 4212e714ceSopenharmony_ci std::unique_ptr<T> tPtr = nullptr; 4312e714ceSopenharmony_ci try { 4412e714ceSopenharmony_ci tPtr = std::make_unique<T>(args...); 4512e714ceSopenharmony_ci } catch (const std::bad_alloc& except) { 4612e714ceSopenharmony_ci LOGW("Create a new unique pointer failed. Error: %s", except.what()); 4712e714ceSopenharmony_ci return nullptr; 4812e714ceSopenharmony_ci } 4912e714ceSopenharmony_ci return tPtr; 5012e714ceSopenharmony_ci} 5112e714ceSopenharmony_ci 5212e714ceSopenharmony_cistd::string GenUniqueName(const std::string&, const std::string&, const std::string&); 5312e714ceSopenharmony_ci 5412e714ceSopenharmony_ci} // namespace NeuralNetworkRuntime 5512e714ceSopenharmony_ci} // namespace OHOS 5612e714ceSopenharmony_ci#endif // NEURAL_NETWORK_RUNTIME_UTILS_H 57