Skip to content

Commit

Permalink
iox-eclipse-iceoryx#218 Make unique_ptr settable and comparable to nu…
Browse files Browse the repository at this point in the history
…llptr.

Signed-off-by: Ithier Jeff (CC-AD/EYF1) <[email protected]>
  • Loading branch information
orecham committed Aug 6, 2020
1 parent 227cb69 commit 6853d62
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
29 changes: 25 additions & 4 deletions iceoryx_utils/include/iceoryx_utils/cxx/unique_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class unique_ptr{

using ptr_t = T*;

unique_ptr() = delete;

///
/// @brief An empty pointer that does nothing.
///
unique_ptr(std::nullptr_t) noexcept
{};

///
/// @brief unique_ptr Creates an empty unique ptr that owns nothing. Can be passed ownership later.
///
Expand All @@ -46,7 +54,7 @@ class unique_ptr{
unique_ptr(ptr_t ptr, const function_ref<void(ptr_t const)> deleter) noexcept;

///
/// @brief unique_ptr Creates an empty unique pointer that points to a specific memory location.
/// @brief unique_ptr Creates an empty unique pointer that points to an allocated memory location.
/// @details The managed object is initially undefined thus must be defined before accessing.
/// @param allocation The allocation of memory where managed object will reside once created.
/// @param deleter The deleter function for cleaning up the managed object.
Expand All @@ -72,12 +80,25 @@ class unique_ptr{
/// Return the stored pointer.
ptr_t operator->() noexcept;

explicit operator bool() const noexcept
{ return get() == ptr_t() ? false : true; }

///
/// @brief operator = Reset to empty pointer when setting to nullptr.
/// @return An empty unique pointer.
///
unique_ptr& operator=(std::nullptr_t) noexcept
{
reset();
return *this;
}

///
/// @brief get Retrieve the underlying raw pointer.
/// @details The unique_ptr retains ownership, therefore the "borrowed" pointer must not be deleted.
/// @return Pointer to managed object or nullptr if none owned.
///
ptr_t get() noexcept;
ptr_t get() const noexcept;

///
/// @brief release Releases ownership of the underlying pointer.
Expand All @@ -87,10 +108,10 @@ class unique_ptr{

///
/// @brief reset Reset the unique_ptr instance's owned object to the one given.
/// @details Any previously owned objects will be deleted.
/// @details Any previously owned objects will be deleted. If no pointer given, resets ot an empty pointer.
/// @param ptr Pointer to object to take ownership on.
///
void reset(ptr_t ptr) noexcept;
void reset(ptr_t ptr = ptr_t()) noexcept;

///
/// @brief swap Swaps object ownership with another unique_ptr.
Expand Down
41 changes: 40 additions & 1 deletion iceoryx_utils/include/iceoryx_utils/internal/cxx/unique_ptr.inl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ T* unique_ptr<T>::operator->() noexcept


template<typename T>
T* unique_ptr<T>::get() noexcept
T* unique_ptr<T>::get() const noexcept
{
return m_ptr;
}
Expand Down Expand Up @@ -78,6 +78,45 @@ void unique_ptr<T>::swap(unique_ptr<T>& other) noexcept
other.reset(release());
}


// Comparison Operators

template<typename T, typename U>
bool operator==(const unique_ptr<T>& x, const unique_ptr<U>& y)
{
return x.get() == y.get();
}

template<typename T>
bool operator==(const unique_ptr<T>& x, std::nullptr_t)
{
return !x;
}

template<typename T>
bool operator==(std::nullptr_t, const unique_ptr<T>& x)
{
return !x;
}

template<typename T, typename U>
bool operator!=(const unique_ptr<T>& x, const unique_ptr<U>& y)
{
return x.get() != y.get();
}

template<typename T>
bool operator!=(const unique_ptr<T>& x, std::nullptr_t)
{
return (bool)x;
}

template<typename T>
bool operator!=(std::nullptr_t, const unique_ptr<T>& x)
{
return (bool)x;
}

} // namespace iox
} // namespace popo

Expand Down
3 changes: 0 additions & 3 deletions iceoryx_utils/test/moduletests/test_cxx_unique_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ TEST_F(UniquePtrTest, CanBeConstructedWithUndefinedBlob)
ptr->x = 10.0;
ptr->y = 77.77;
ptr->z = 50.50;

EXPECT_EQ(10.0, ptr->x);
EXPECT_EQ(77.77, ptr->y);
EXPECT_EQ(50.50, ptr->z);

}

TEST_F(UniquePtrTest, CanBeResetToPointToUndefinedBlob)
Expand All @@ -70,7 +68,6 @@ TEST_F(UniquePtrTest, CanBeResetToPointToUndefinedBlob)
ptr->x = 10.0;
ptr->y = 77.77;
ptr->z = 50.50;

EXPECT_EQ(10.0, ptr->x);
EXPECT_EQ(77.77, ptr->y);
EXPECT_EQ(50.50, ptr->z);
Expand Down

0 comments on commit 6853d62

Please sign in to comment.