Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable default cetl::pmr::PmrInterfaceDeleter ctor. #128

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cetlvast/suites/unittest/pmr/test_pmr_interface_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,17 @@ TEST_F(TestPmrInterfacePtr, make_unique_myobj_ctor_throws)
#endif
}

TEST_F(TestPmrInterfacePtr, initially_empty_with_default_deleter)
{
cetl::pmr::polymorphic_allocator<MyObject> alloc{get_mr()};

// 1. Create initially empty interface pointer.
cetl::pmr::InterfacePtr<INamed> obj0;
EXPECT_THAT(obj0, IsNull());

// 2. Now assign a new instance.
obj0 = cetl::pmr::InterfaceFactory::make_unique<INamed>(alloc, "obj0");
EXPECT_THAT(obj0, NotNull());
}

} // namespace
15 changes: 13 additions & 2 deletions include/cetl/pmr/interface_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ template <typename Interface>
class PmrInterfaceDeleter final
{
public:
/// Constructs empty no-operation deleter.
///
/// Useful for initially empty `InterfacePtr` instance without deleter attached.
///
PmrInterfaceDeleter() = default;

/// Constructs a Concrete type-erased deleter for the given interface type.
///
/// @tparam PmrAllocator The type of the polymorphic allocator to use for deallocation.
Expand All @@ -51,7 +57,12 @@ class PmrInterfaceDeleter final
///
void operator()(Interface* ptr) noexcept
{
deleter_(ptr);
CETL_DEBUG_ASSERT((nullptr != ptr) == static_cast<bool>(deleter_), "Empty deleter is fine for null ptr!");

if ((nullptr != ptr) && static_cast<bool>(deleter_))
{
deleter_(ptr);
}
}

// Below convertor constructor is only possible with enabled PMR at `function`.
Expand Down Expand Up @@ -143,7 +154,7 @@ class InterfaceFactory final
using Concrete = typename PmrAllocator::value_type;

public:
ConcreteRaii(PmrAllocator& pmr_allocator)
explicit ConcreteRaii(PmrAllocator& pmr_allocator)
: concrete_{pmr_allocator.allocate(1)}
, constructed_{false}
, pmr_allocator_{pmr_allocator}
Expand Down
Loading