removed class Self

This commit is contained in:
Michele Caini 2016-08-09 15:43:37 +02:00
parent c66780db2e
commit fc7a63bc63
4 changed files with 15 additions and 64 deletions

View File

@ -5,7 +5,6 @@
#include <memory>
#include <uv.h>
#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<typename T, typename U>
class Resource: public Emitter<T>, public Self<T> {
class Resource: public Emitter<T>, public std::enable_shared_from_this<T> {
template<typename, typename>
friend class Resource;
protected:
explicit Resource(std::shared_ptr<Loop> ref)
: Emitter<T>{},
Self<T>{},
std::enable_shared_from_this<T>{},
pLoop{std::move(ref)},
resource{}
{
@ -47,6 +46,18 @@ protected:
return reinterpret_cast<const R*>(&resource);
}
void leak() noexcept {
ptr = this->shared_from_this();
}
void reset() noexcept {
ptr.reset();
}
bool self() const noexcept {
return static_cast<bool>(ptr);
}
public:
Resource(const Resource &) = delete;
Resource(Resource &&) = delete;
@ -65,6 +76,7 @@ public:
Loop& loop() const noexcept { return *pLoop; }
private:
std::shared_ptr<void> ptr{nullptr};
std::shared_ptr<Loop> pLoop;
U resource;
};

View File

@ -1,29 +0,0 @@
#pragma once
#include <memory>
namespace uvw {
template<typename T>
struct Self: std::enable_shared_from_this<T> {
void leak() noexcept {
ptr = this->shared_from_this();
}
void reset() noexcept {
ptr.reset();
}
bool self() const noexcept {
return static_cast<bool>(ptr);
}
private:
std::shared_ptr<void> ptr{nullptr};
};
}

View File

@ -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)

View File

@ -1,23 +0,0 @@
#include <gtest/gtest.h>
#include <uvw.hpp>
struct S: uvw::Self<S> { };
TEST(Self, Basics) {
std::shared_ptr<S> self = std::make_shared<S>();
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());
}