Skip to content

Commit fd040eb

Browse files
committed
simplify selfRefUntil
1 parent a98d879 commit fd040eb

File tree

8 files changed

+28
-47
lines changed

8 files changed

+28
-47
lines changed

src/handle.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ namespace uvcpp {
5050

5151
template<typename E>
5252
void on(EventCallback<E, Derived> &&callback) {
53-
if (std::is_same<E, EvClose>::value) {
54-
Resource<T, Derived>::template once<E>(
55-
std::forward<EventCallback<E, Derived>>(callback));
56-
} else {
57-
Resource<T, Derived>::template on<E>(
58-
std::forward<EventCallback<E, Derived>>(callback));
59-
}
53+
static_assert(!std::is_same<E, EvClose>::value,
54+
"EvClose is not allowed to be registered with 'on'");
55+
56+
Resource<T, Derived>::template on<E>(
57+
std::forward<EventCallback<E, Derived>>(callback));
6058
}
6159

6260
private:

src/resource.hpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,17 @@ namespace uvcpp {
8484
template<typename E, typename =
8585
std::enable_if_t<std::is_base_of<Event, E>::value, E>>
8686
void selfRefUntil() {
87-
auto index = getEventTypeIndex<E, CallbackType::ONCE>();
88-
if (index >= selfRefs_.size()) {
89-
selfRefs_.resize(index + 1);
90-
}
91-
if (!selfRefs_[index]) {
92-
selfRefs_[index] = shared_from_this();
93-
}
87+
once<E>([_ = shared_from_this()](const auto &e, auto &h){});
9488
}
9589

9690
template<typename E, typename =
9791
std::enable_if_t<std::is_base_of<Event, E>::value, E>>
9892
void on(EventCallback<E, Derived> &&callback) {
99-
const auto cbType =
100-
(std::is_same<E, EvError>::value ||
101-
std::is_same<E, EvRef>::value ||
102-
std::is_same<E, EvDestroy>::value) ?
103-
CallbackType::ONCE :
104-
CallbackType::ALWAYS;
105-
106-
registerCallback<E, cbType>(
93+
static_assert(
94+
!std::is_same<E, EvRef>::value && !std::is_same<E, EvDestroy>::value,
95+
"EvRef/EvDestroy is not allowed to be registered with 'on'");
96+
97+
registerCallback<E, CallbackType::ALWAYS>(
10798
std::forward<EventCallback<E, Derived>>(callback));
10899
}
109100

@@ -117,17 +108,11 @@ namespace uvcpp {
117108
template<typename E, typename =
118109
std::enable_if_t<std::is_base_of<Event, E>::value, E>>
119110
void publish(E &&event) {
120-
if (!std::is_same<E, EvError>::value &&
121-
!std::is_same<E, EvRef>::value &&
111+
if (!std::is_same<E, EvRef>::value &&
122112
!std::is_same<E, EvDestroy>::value) {
123113
doCallback<E, CallbackType::ALWAYS>(std::forward<E>(event));
124114
}
125115
doCallback<E, CallbackType::ONCE>(std::forward<E>(event));
126-
127-
auto selfRefIndex = getEventTypeIndex<E, CallbackType::ONCE>();
128-
if (selfRefIndex < selfRefs_.size()) {
129-
selfRefs_[selfRefIndex].reset();
130-
}
131116
}
132117

133118
private:
@@ -182,7 +167,6 @@ namespace uvcpp {
182167
T resource_;
183168
std::vector<std::vector<std::unique_ptr<ICallback>>> callbacks_;
184169
std::vector<std::vector<std::unique_ptr<ICallback>>> onceCallbacks_;
185-
std::vector<std::shared_ptr<Resource<T, Derived>>> selfRefs_;
186170
};
187171
} /* end of namspace: uvcpp */
188172

test/uvcpp/fs_event.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ TEST(FsEvent, TestFileChange) {
1414
auto fsEvent = FsEvent::create(loop);
1515
ASSERT_TRUE(!!fsEvent);
1616

17-
fsEvent->on<EvError>([](const auto &e, auto &fsEvent) {
17+
fsEvent->once<EvError>([](const auto &e, auto &fsEvent) {
1818
FAIL() << "fsEvent failed with status: " << e.status << ", msg: " << e.message;
1919
});
20-
fsEvent->on<EvClose>([fe = fsEvent](const auto &e, auto &fsEvent) {
20+
fsEvent->once<EvClose>([fe = fsEvent](const auto &e, auto &fsEvent) {
2121
LOG_D("fsEvent closed: %li", fe.use_count());
2222
});
2323
fsEvent->once<EvDestroy>([&destroyed, fe = &fsEvent](const auto &e, auto &fsEvent) {

test/uvcpp/pipe.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ TEST(Pipe, Connection) {
3636
++destroyCount;
3737
});
3838

39-
server->on<EvError>([](const auto &e, auto &pipe) {
39+
server->once<EvError>([](const auto &e, auto &pipe) {
4040
FAIL() << "server failed with status: " << e.status;
4141
});
42-
client->on<EvError>([](const auto &e, auto &pipe) {
42+
client->once<EvError>([](const auto &e, auto &pipe) {
4343
FAIL() << "client failed with status: " << e.status;
4444
});
45-
client->on<EvClose>([](const auto &e, auto &handle) {
45+
client->once<EvClose>([](const auto &e, auto &handle) {
4646
LOG_D("client closed: isValid=%d", handle.isValid());
4747
});
4848

@@ -131,7 +131,7 @@ TEST(Pipe, ImmediateClose) {
131131
ASSERT_TRUE(!!server);
132132
ASSERT_TRUE(!!client);
133133

134-
client->on<EvError>([&](const auto &e, auto &client){
134+
client->once<EvError>([&](const auto &e, auto &client){
135135
LOG_E("error: %s", e.message.c_str());
136136
client.close();
137137
server->close();

test/uvcpp/prepare.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ TEST(Prepare, LoopCount) {
99

1010
auto prepare = Prepare::create(loop);
1111

12-
prepare->on<EvError>([](const auto &e, auto &prepare) {
12+
prepare->once<EvError>([](const auto &e, auto &prepare) {
1313
FAIL() << "prepare failed with status: " << e.status;
1414
});
15-
prepare->on<EvClose>([](const auto &e, auto &prepare) {
15+
prepare->once<EvClose>([](const auto &e, auto &prepare) {
1616
LOG_D("prepare closed");
1717
});
1818

test/uvcpp/req.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ TEST(Req, DNSRequestResolveLocalHost) {
99

1010
auto req = DNSRequest::create(loop);
1111
req->selfRefUntil<EvDNSRequestFinish>();
12-
req->on<EvError>([](const auto &e, auto &r) {
12+
req->once<EvError>([](const auto &e, auto &r) {
1313
FAIL() << "failed with status: " << e.status;
1414
});
1515

@@ -31,7 +31,7 @@ TEST(Req, DNSRequest0000) {
3131

3232
auto req = DNSRequest::create(loop);
3333
req->selfRefUntil<EvDNSRequestFinish>();
34-
req->on<EvError>([](const auto &e, auto &r) {
34+
req->once<EvError>([](const auto &e, auto &r) {
3535
FAIL() << "failed with status: " << e.status;
3636
});
3737

test/uvcpp/tcp.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ TEST(Tcp, Connection) {
3636
++destroyCount;
3737
});
3838

39-
server->on<EvError>([](const auto &e, auto &tcp) {
39+
server->once<EvError>([](const auto &e, auto &tcp) {
4040
FAIL() << "server failed with status: " << e.status;
4141
});
42-
client->on<EvError>([](const auto &e, auto &tcp) {
42+
client->once<EvError>([](const auto &e, auto &tcp) {
4343
FAIL() << "client failed with status: " << e.status;
4444
});
45-
client->on<EvClose>([](const auto &e, auto &handle) {
45+
client->once<EvClose>([](const auto &e, auto &handle) {
4646
LOG_D("client closed: isValid=%d", handle.isValid());
4747
});
4848

@@ -137,7 +137,7 @@ TEST(Tcp, ImmediateClose) {
137137
ASSERT_TRUE(!!server);
138138
ASSERT_TRUE(!!client);
139139

140-
client->on<EvError>([&](const auto &e, auto &client){
140+
client->once<EvError>([&](const auto &e, auto &client){
141141
LOG_E("error: %s", e.message.c_str());
142142
client.close();
143143
server->close();

test/uvcpp/timer.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ TEST(Timer, RepeatShared) {
5454
// though 'on<>' instead of 'once<>' is used, when it is called,
5555
// the callback itself gets deleted, so the Timer object will be released,
5656
// and EvDestroy event will be fired
57-
timer->on<EvClose>([sharedTimer = timer](const auto &e, auto &timer) {
57+
timer->once<EvClose>([sharedTimer = timer](const auto &e, auto &timer) {
5858
LOG_D("timer closed: %li", sharedTimer.use_count());
5959
});
6060
timer->once<EvDestroy>([&destroyed, t = &timer](const auto &e, auto &timer) {
6161
LOG_D("timer destroyed: %li", t->use_count());
6262
destroyed = true;
6363
});
6464

65-
auto count = 0;
66-
timer->on<EvTimer>([&count, t = &timer](const auto &e, auto &timer) {
65+
timer->on<EvTimer>([t = &timer](const auto &e, auto &timer) {
6766
LOG_D("timer event: %li", t->use_count());
6867
timer.stop();
6968
timer.close();

0 commit comments

Comments
 (0)