Skip to content

Commit e108742

Browse files
committed
refactor: disallow non-clearable once events
1 parent d180a7b commit e108742

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

include/ereignis/event/event.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace ereignis
6767

6868
public:
6969
std::size_t add(listener);
70-
void once(listener);
70+
void once(listener::callback);
7171

7272
public:
7373
template <typename... Rs>

include/ereignis/event/event.inl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ namespace ereignis
5353
template <typename R, typename... Ts>
5454
struct listener<R(Ts...), true>
5555
{
56-
std::move_only_function<R(Ts...)> func;
56+
using callback = std::move_only_function<R(Ts...)>;
5757

5858
public:
59+
callback func;
5960
bool clearable{true};
6061
};
6162

@@ -110,20 +111,20 @@ namespace ereignis
110111
}
111112

112113
template <auto Id, typename R, typename... Ts>
113-
void event<Id, R(Ts...)>::once(listener l)
114+
void event<Id, R(Ts...)>::once(listener::callback cb)
114115
{
115116
auto lock = std::lock_guard{m_mutex};
116117
auto id = m_counter++;
117118

118-
l.func = [this, state = std::make_tuple(id, std::move(l.func))]<typename... Us>(Us &&...args) mutable
119+
auto wrapper = [this, state = std::make_tuple(id, std::move(cb))]<typename... Us>(Us &&...args) mutable
119120
{
120-
auto [id, func] = std::move(state);
121+
auto [id, cb] = std::move(state);
121122

122123
remove(id);
123-
return std::invoke(func, std::forward<Us>(args)...);
124+
return std::invoke(cb, std::forward<Us>(args)...);
124125
};
125126

126-
m_listeners.emplace(id, std::make_shared<listener>(std::move(l)));
127+
m_listeners.emplace(id, std::make_shared<listener>(std::move(wrapper)));
127128
}
128129

129130
template <auto Id, typename R, typename... Ts>

0 commit comments

Comments
 (0)