-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_shell_executor.h
92 lines (83 loc) · 2.31 KB
/
async_shell_executor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef __ASYNC_SHELL_EXECUTOR_H
#define __ASYNC_SHELL_EXECUTOR_H
#include "prefix.h"
#include <stdio.h>
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
COROUTINE_TOOL_NAMESPACE_START
class AsyncExecutor
{
public:
struct promise_type
{
AsyncExecutor get_return_object() { return AsyncExecutor(handle_type::from_promise(*this)); }
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
std::suspend_always yield_value(const char* p_msg)
{
p_msg_ = p_msg;
return {};
}
void unhandled_exception() { exception_ = std::current_exception(); }
void return_value(int code) { return_code_ = code; }
const char* p_msg_;
std::exception_ptr exception_;
int return_code_;
};
using handle_type = std::coroutine_handle<promise_type>;
AsyncExecutor(handle_type handle) : handle_(handle) {}
AsyncExecutor(const AsyncExecutor& rhs) : handle_(rhs.handle_) {}
AsyncExecutor(AsyncExecutor&& rhs) : handle_(rhs.handle_) { rhs.handle_ = nullptr; }
AsyncExecutor& operator=(const AsyncExecutor& rhs)
{
if (handle_ != nullptr)
{
handle_.destroy();
}
handle_ = rhs.handle_;
return *this;
}
AsyncExecutor& operator=(AsyncExecutor&& rhs)
{
if (handle_ != nullptr)
{
handle_.destroy();
}
handle_ = rhs.handle_;
rhs.handle_ = nullptr;
return *this;
}
~AsyncExecutor()
{
if (handle_ != nullptr)
handle_.destroy();
}
explicit operator bool()
{
handle_.resume();
return !handle_.done();
}
int Code() { return handle_.promise().return_code_; }
static AsyncExecutor Run(const char* command)
{
FILE* fp = NULL;
fp = popen(command, "r");
if (fp == NULL)
co_return -1;
static constexpr size_t buffer_size = 128;
char buf[buffer_size];
while (fgets(buf, buffer_size, fp) != NULL)
co_yield buf;
co_return pclose(fp);
}
const char* operator()()
{
return handle_.promise().p_msg_;
}
protected:
handle_type handle_;
};
COROUTINE_TOOL_NAMESPACE_END
#endif