NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
pipe.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_SYSTEM_PIPE_HPP__
2#define NEFORCE_CORE_SYSTEM_PIPE_HPP__
3
10
12NEFORCE_BEGIN_NAMESPACE__
13
19
24struct pipe_exception final : system_exception {
25 explicit pipe_exception(const char* info = "Pipe Operation Failed.", const char* type = static_type,
26 const int code = 0) noexcept :
27 system_exception(info, type, code) {}
28
29 explicit pipe_exception(const exception& e) :
30 system_exception(e) {}
31
32 ~pipe_exception() override = default;
33 static constexpr auto static_type = "pipe_exception";
34};
35 // Exceptions
37
43
50class NEFORCE_API pipe {
51public:
52 using native_handle_type = _NEFORCE native_handle_type;
53
54private:
55#ifdef NEFORCE_PLATFORM_WINDOWS
56 native_handle_type read_handle_ = nullptr;
57 native_handle_type write_handle_ = nullptr;
58#else
59 native_handle_type fds_[2] = {-1, -1};
60#endif
61
62public:
68 pipe() noexcept = default;
69
75 explicit pipe(bool inheritable);
76
82 ~pipe();
83
84 pipe(const pipe&) = delete;
85 pipe& operator=(const pipe&) = delete;
86
90 pipe(pipe&& other) noexcept;
91
95 pipe& operator=(pipe&& other) noexcept;
96
104 static void ignore_sigpipe() noexcept;
105
112 int read(void* buffer, size_t size) noexcept;
113
118 NEFORCE_NODISCARD string read_available();
119
126 int write(const void* data, size_t size) noexcept;
127
131 void close_read() noexcept;
132
136 void close_write() noexcept;
137
141 void close() noexcept;
142
147 NEFORCE_NODISCARD bool is_valid() const noexcept;
148
152 NEFORCE_NODISCARD native_handle_type native_read_handle() const noexcept {
153#ifdef NEFORCE_PLATFORM_WINDOWS
154 return read_handle_;
155#else
156 return fds_[0];
157#endif
158 }
159
163 NEFORCE_NODISCARD native_handle_type native_write_handle() const noexcept {
164#ifdef NEFORCE_PLATFORM_WINDOWS
165 return write_handle_;
166#else
167 return fds_[1];
168#endif
169 }
170
174 NEFORCE_NODISCARD native_handle_type detach_read_handle() noexcept;
175
179 NEFORCE_NODISCARD native_handle_type detach_write_handle() noexcept;
180};
181 // Pipe
183
184NEFORCE_END_NAMESPACE__
185#endif // NEFORCE_CORE_SYSTEM_PIPE_HPP__
bool is_valid() const noexcept
检查管道是否有效
int read(void *buffer, size_t size) noexcept
从管道读取数据
native_handle_type detach_write_handle() noexcept
分离写端句柄(调用者负责关闭)
void close() noexcept
关闭管道的所有端
native_handle_type detach_read_handle() noexcept
分离读端句柄(调用者负责关闭)
int write(const void *data, size_t size) noexcept
向管道写入数据
native_handle_type native_write_handle() const noexcept
获取写端句柄
static void ignore_sigpipe() noexcept
忽略 SIGPIPE 信号
native_handle_type native_read_handle() const noexcept
获取读端句柄
void close_write() noexcept
关闭管道的写端
void close_read() noexcept
关闭管道的读端
pipe() noexcept=default
默认构造函数
string read_available()
从管道读取所有可用数据(非阻塞)
constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
字符串类型别名和实用函数
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * type() const noexcept
获取异常类型
int code() const noexcept
获取异常码