diff --git a/src/uvw/resource.hpp b/src/uvw/resource.hpp index 8a7e24bb..f525a024 100644 --- a/src/uvw/resource.hpp +++ b/src/uvw/resource.hpp @@ -5,7 +5,6 @@ #include #include #include "emitter.hpp" -#include "self.hpp" #include "loop.hpp" @@ -19,14 +18,14 @@ namespace uvw { * It mainly acts as a wrapper around a libuv's data structure. */ template -class Resource: public Emitter, public Self { +class Resource: public Emitter, public std::enable_shared_from_this { template friend class Resource; protected: explicit Resource(std::shared_ptr ref) : Emitter{}, - Self{}, + std::enable_shared_from_this{}, pLoop{std::move(ref)}, resource{} { @@ -47,6 +46,18 @@ protected: return reinterpret_cast(&resource); } + void leak() noexcept { + ptr = this->shared_from_this(); + } + + void reset() noexcept { + ptr.reset(); + } + + bool self() const noexcept { + return static_cast(ptr); + } + public: Resource(const Resource &) = delete; Resource(Resource &&) = delete; @@ -65,6 +76,7 @@ public: Loop& loop() const noexcept { return *pLoop; } private: + std::shared_ptr ptr{nullptr}; std::shared_ptr pLoop; U resource; }; diff --git a/src/uvw/self.hpp b/src/uvw/self.hpp deleted file mode 100644 index c924a79f..00000000 --- a/src/uvw/self.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - - -#include - - -namespace uvw { - - -template -struct Self: std::enable_shared_from_this { - void leak() noexcept { - ptr = this->shared_from_this(); - } - - void reset() noexcept { - ptr.reset(); - } - - bool self() const noexcept { - return static_cast(ptr); - } - -private: - std::shared_ptr ptr{nullptr}; -}; - - -} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1e5ee14b..7e84185f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,7 +26,6 @@ set(TARGET_CHECK check) set(TARGET_IDLE idle) set(TARGET_LOOP loop) set(TARGET_PREPARE prepare) -set(TARGET_SELF self) set(TARGET_WORK work) # Test TARGET_MAIN @@ -77,14 +76,6 @@ target_include_directories(${TARGET_PREPARE} PRIVATE ${COMMON_INCLUDE_DIRS}) target_link_libraries(${TARGET_PREPARE} PRIVATE ${COMMON_LINK_LIBS}) add_test(NAME ${TARGET_PREPARE} COMMAND ${TARGET_PREPARE}) -# Test TARGET_SELF - -set(TARGET_SELF_SOURCES uvw/self.cpp) -add_executable(${TARGET_SELF} ${TARGET_SELF_SOURCES}) -target_include_directories(${TARGET_SELF} PRIVATE ${COMMON_INCLUDE_DIRS}) -target_link_libraries(${TARGET_SELF} PRIVATE ${COMMON_LINK_LIBS}) -add_test(NAME ${TARGET_SELF} COMMAND ${TARGET_SELF}) - # Test TARGET_WORK set(TARGET_WORK_SOURCES uvw/work.cpp) diff --git a/test/uvw/self.cpp b/test/uvw/self.cpp deleted file mode 100644 index 1e7c3e24..00000000 --- a/test/uvw/self.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - - -struct S: uvw::Self { }; - - -TEST(Self, Basics) { - std::shared_ptr self = std::make_shared(); - - ASSERT_TRUE(self.unique()); - ASSERT_FALSE(self->self()); - - self->leak(); - - ASSERT_FALSE(self.unique()); - ASSERT_TRUE(self->self()); - - self->reset(); - - ASSERT_TRUE(self.unique()); - ASSERT_FALSE(self->self()); -}