diff --git a/iceoryx_utils/include/iceoryx_utils/cxx/unique_ptr.hpp b/iceoryx_utils/include/iceoryx_utils/cxx/unique_ptr.hpp index 545f125bb9..5cfc3f1e98 100644 --- a/iceoryx_utils/include/iceoryx_utils/cxx/unique_ptr.hpp +++ b/iceoryx_utils/include/iceoryx_utils/cxx/unique_ptr.hpp @@ -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. /// @@ -46,7 +54,7 @@ class unique_ptr{ unique_ptr(ptr_t ptr, const function_ref 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. @@ -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. @@ -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. diff --git a/iceoryx_utils/include/iceoryx_utils/internal/cxx/unique_ptr.inl b/iceoryx_utils/include/iceoryx_utils/internal/cxx/unique_ptr.inl index ad81026ced..ad1a35cfa5 100644 --- a/iceoryx_utils/include/iceoryx_utils/internal/cxx/unique_ptr.inl +++ b/iceoryx_utils/include/iceoryx_utils/internal/cxx/unique_ptr.inl @@ -44,7 +44,7 @@ T* unique_ptr::operator->() noexcept template -T* unique_ptr::get() noexcept +T* unique_ptr::get() const noexcept { return m_ptr; } @@ -78,6 +78,45 @@ void unique_ptr::swap(unique_ptr& other) noexcept other.reset(release()); } + +// Comparison Operators + +template +bool operator==(const unique_ptr& x, const unique_ptr& y) +{ + return x.get() == y.get(); +} + +template +bool operator==(const unique_ptr& x, std::nullptr_t) +{ + return !x; +} + +template +bool operator==(std::nullptr_t, const unique_ptr& x) +{ + return !x; +} + +template +bool operator!=(const unique_ptr& x, const unique_ptr& y) +{ + return x.get() != y.get(); +} + +template +bool operator!=(const unique_ptr& x, std::nullptr_t) +{ + return (bool)x; +} + +template +bool operator!=(std::nullptr_t, const unique_ptr& x) +{ + return (bool)x; +} + } // namespace iox } // namespace popo diff --git a/iceoryx_utils/test/moduletests/test_cxx_unique_ptr.cpp b/iceoryx_utils/test/moduletests/test_cxx_unique_ptr.cpp index 4cf8d23f38..40ae959e92 100644 --- a/iceoryx_utils/test/moduletests/test_cxx_unique_ptr.cpp +++ b/iceoryx_utils/test/moduletests/test_cxx_unique_ptr.cpp @@ -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) @@ -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);