/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef INTELL_VOICE_GENERIC_FACTORY_H #define INTELL_VOICE_GENERIC_FACTORY_H #include namespace OHOS { namespace IntellVoiceUtils { template class UniquePtrFactory { public: using UniqueProductPtr = std::unique_ptr; template static UniqueProductPtr CreateInstance(Args && ...args) { T *instance = new(std::nothrow) T {}; if (instance == nullptr) { return UniqueProductPtr {nullptr, nullptr}; } if (!instance->Init(std::forward(args)...)) { delete instance; return UniqueProductPtr {nullptr, nullptr}; } return UniqueProductPtr(instance, DestroyInstance); } private: static void DestroyInstance(T *ptr) { delete ptr; } }; template using UniqueProductType = typename UniquePtrFactory::UniqueProductPtr; template class SptrFactory { public: using SptrProductPtr = OHOS::sptr; template static SptrProductPtr CreateInstance(Args && ...args) { B *instance = new(std::nothrow) D {}; if (instance == nullptr) { return SptrProductPtr { nullptr }; } if (!instance->Init(std::forward(args)...)) { delete instance; return SptrProductPtr { nullptr }; } return SptrProductPtr(instance); } }; template using SptrProductType = typename SptrFactory::SptrProductPtr; } } #endif