blob: 5754b07b220ebc2f8526f26303981c6cbfdb9749 (
plain)
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
|
#pragma once
#if !defined(__unix) && !defined(__APPLE__)
#error "This file can only be included on unix."
#endif
#include "../../Bitmask.h"
#include <sys/types.h>
#include <functional>
namespace cru::platform::unix {
class UnixFileDescriptor {
public:
UnixFileDescriptor();
/**
* \param close Default to POSIX close function.
*/
explicit UnixFileDescriptor(int descriptor, bool auto_close = true,
std::function<int(int)> close = {});
~UnixFileDescriptor();
UnixFileDescriptor(const UnixFileDescriptor& other) = delete;
UnixFileDescriptor& operator=(const UnixFileDescriptor& other) = delete;
UnixFileDescriptor(UnixFileDescriptor&& other) noexcept;
UnixFileDescriptor& operator=(UnixFileDescriptor&& other) noexcept;
bool IsValid() const;
void EnsureValid() const;
int GetValue() const;
void Close();
bool IsAutoClose() const { return auto_close_; }
void SetAutoClose(bool auto_close) { this->auto_close_ = auto_close; }
explicit operator bool() const { return this->IsValid(); }
operator int() const { return this->GetValue(); }
/**
* If O_NONBLOCK is set and EAGAIN or EWOULDBLOCK is returned, -1 is returned.
*/
ssize_t Read(void* buffer, size_t size);
/**
* If O_NONBLOCK is set and EAGAIN or EWOULDBLOCK is returned, -1 is returned.
*/
ssize_t Write(const void* buffer, size_t size);
void SetFileDescriptorFlags(int flags);
private:
bool DoClose();
private:
int descriptor_;
bool auto_close_;
std::function<int(int)> close_;
};
struct UniDirectionalUnixPipeResult {
UnixFileDescriptor read;
UnixFileDescriptor write;
};
namespace details {
struct UnixPipeFlagTag;
}
using UnixPipeFlag = Bitmask<details::UnixPipeFlagTag>;
struct UnixPipeFlags {
constexpr static auto NonBlock = UnixPipeFlag::FromOffset(1);
};
UniDirectionalUnixPipeResult OpenUniDirectionalPipe(UnixPipeFlag flags = {});
} // namespace cru::platform::unix
|