Skip to content

Commit

Permalink
Enable default cetl::pmr::PmrInterfaceDeleter ctor. #verification #…
Browse files Browse the repository at this point in the history
…docs #sonar
  • Loading branch information
serges147 committed Jun 18, 2024
1 parent 5aa0a5b commit db5062a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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

0 comments on commit db5062a

Please sign in to comment.