/** * Copyright (c) 2021-2022 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 PANDA_RUNTIME_MEM_PANDA_SMART_POINTERS_H #define PANDA_RUNTIME_MEM_PANDA_SMART_POINTERS_H #include #include "libpandabase/concepts.h" #include "runtime/include/mem/allocator.h" namespace panda { template struct DefaultPandaDelete { constexpr DefaultPandaDelete() noexcept = default; template , void *> = nullptr> // NOLINTNEXTLINE(google-explicit-constructor, readability-named-parameter) DefaultPandaDelete(const DefaultPandaDelete &) noexcept { } void operator()(T *ptr) const noexcept { static_assert(!std::is_void_v, "Incorrect (void) type for DefaultPandaDelete"); static_assert(sizeof(T) > 0, "Incorrect (incomplete) type for DefaultPandaDelete"); mem::InternalAllocator<>::GetInternalAllocatorFromRuntime()->Delete(ptr); } }; template // NOLINTNEXTLINE(modernize-avoid-c-arrays) struct DefaultPandaDelete { constexpr DefaultPandaDelete() noexcept = default; // NOLINTNEXTLINE(modernize-avoid-c-arrays) template , void *> = nullptr> // NOLINTNEXTLINE(google-explicit-constructor, readability-named-parameter, modernize-avoid-c-arrays) DefaultPandaDelete(const DefaultPandaDelete &) noexcept { } template // NOLINTNEXTLINE(google-explicit-constructor, readability-named-parameter, modernize-avoid-c-arrays) std::enable_if_t> operator()(U *ptr) const noexcept { static_assert(!std::is_void_v, "Incorrect (void) type for DefaultPandaDelete"); static_assert(sizeof(T) > 0, "Incorrect (incomplete) type for DefaultPandaDelete"); mem::InternalAllocator<>::GetInternalAllocatorFromRuntime()->DeleteArray(ptr); } }; template > using PandaUniquePtr = std::unique_ptr; template using PandaUniquePtrIf = std::enable_if_t>; template inline PandaUniquePtrIf> MakePandaUnique(Args &&... args) { return PandaUniquePtr { mem::InternalAllocator<>::GetInternalAllocatorFromRuntime()->New(std::forward(args)...)}; } template inline PandaUniquePtrIf> MakePandaUnique(size_t size) { return PandaUniquePtr {mem::InternalAllocator<>::GetInternalAllocatorFromRuntime()->New(size)}; } template inline PandaUniquePtrIf> MakePandaUnique(Args &&... args) = delete; template using PandaSharedPtrIf = std::enable_if_t>; template inline PandaSharedPtrIf> MakePandaShared(Args &&... args) { return std::shared_ptr( mem::InternalAllocator<>::GetInternalAllocatorFromRuntime()->New(std::forward(args)...), DefaultPandaDelete()); } template inline PandaSharedPtrIf> MakePandaShared(size_t size) { return std::shared_ptr(mem::InternalAllocator<>::GetInternalAllocatorFromRuntime()->New(size), DefaultPandaDelete()); } template inline PandaSharedPtrIf> MakePandaShared(Args &&... args) = delete; } // namespace panda #endif // PANDA_RUNTIME_MEM_PANDA_SMART_POINTERS_H