mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00: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:
parent
699d294ac1
commit
ac9a0f0dbf
1 changed files with 24 additions and 6 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue