mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-28 01:11:15 -03:00
Rework iothread_perform for void return types
Need to use a template specialization so we don't try to create a variable of type void
This commit is contained in:
@@ -29,13 +29,31 @@ void iothread_drain_all(void);
|
||||
int iothread_perform(std::function<void(void)> &&func,
|
||||
std::function<void(void)> &&completion = std::function<void(void)>());
|
||||
|
||||
// Variant that allows computing a value in func, and then passing it to the completion handler
|
||||
// Template helpers
|
||||
template<typename T>
|
||||
int iothread_perform(std::function<T(void)> &&handler, std::function<void(T)> &&completion) {
|
||||
T *result = new T();
|
||||
return iothread_perform([=](){ *result = handler(); },
|
||||
[=](){ completion(std::move(*result)); delete result; }
|
||||
);
|
||||
struct _iothread_trampoline {
|
||||
template<typename HANDLER, typename COMPLETION>
|
||||
static int perform(const HANDLER &handler, const COMPLETION &completion) {
|
||||
T *result = new T();
|
||||
return iothread_perform([=](){ *result = handler(); },
|
||||
[=](){ completion(std::move(*result)); delete result; });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Void specialization
|
||||
template<>
|
||||
struct _iothread_trampoline<void> {
|
||||
template<typename HANDLER, typename COMPLETION>
|
||||
static int perform(const HANDLER &handler, const COMPLETION &completion) {
|
||||
return iothread_perform(handler, completion);
|
||||
}
|
||||
};
|
||||
|
||||
// Variant that allows computing a value in func, and then passing it to the completion handler
|
||||
template<typename HANDLER, typename COMPLETION>
|
||||
int iothread_perform(const HANDLER &handler, const COMPLETION &completion) {
|
||||
return _iothread_trampoline<decltype(handler())>::perform(handler, completion);
|
||||
}
|
||||
|
||||
/// Legacy templates
|
||||
|
||||
Reference in New Issue
Block a user