16 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
18 #elif defined(_WIN32) || defined(_WIN64)
31 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
32 int result = pipe(_pipe);
34 throwex SystemException(
"Failed to create a new pipe!");
35 #elif defined(_WIN32) || defined(_WIN64)
36 if (!CreatePipe(&_pipe[0], &_pipe[1],
nullptr, 0))
37 throwex SystemException(
"Failed to create a new pipe!");
48 catch (
const SystemException& ex)
50 fatality(SystemException(ex.string()));
54 void*
reader() const noexcept
56 return (
void*)(size_t)_pipe[0];
59 void*
writer() const noexcept
61 return (
void*)(size_t)_pipe[1];
71 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
72 return (_pipe[0] >= 0);
73 #elif defined(_WIN32) || defined(_WIN64)
74 return (_pipe[0] != INVALID_HANDLE_VALUE);
80 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
81 return (_pipe[1] >= 0);
82 #elif defined(_WIN32) || defined(_WIN64)
83 return (_pipe[1] != INVALID_HANDLE_VALUE);
87 size_t Read(
void* buffer,
size_t size)
89 if ((buffer ==
nullptr) || (size == 0))
94 throwex SystemException(
"Cannot read from the closed pipe!");
95 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
96 ssize_t result = read(_pipe[0], buffer, size);
98 throwex SystemException(
"Cannot read from the pipe!");
99 return (
size_t)result;
100 #elif defined(_WIN32) || defined(_WIN64)
102 if (!ReadFile(_pipe[0], buffer, (DWORD)size, &result,
nullptr))
103 if (GetLastError() != ERROR_BROKEN_PIPE)
104 throwex SystemException(
"Cannot read from the pipe!");
105 return (
size_t)result;
109 size_t Write(
const void* buffer,
size_t size)
111 if ((buffer ==
nullptr) || (size == 0))
116 throwex SystemException(
"Cannot write into the closed pipe!");
117 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
118 ssize_t result = write(_pipe[1], buffer, size);
120 throwex SystemException(
"Cannot write into the pipe!");
121 return (
size_t)result;
122 #elif defined(_WIN32) || defined(_WIN64)
124 if (!WriteFile(_pipe[1], buffer, (DWORD)size, &result,
nullptr))
125 if (GetLastError() != ERROR_BROKEN_PIPE)
126 throwex SystemException(
"Cannot write into the pipe!");
127 return (
size_t)result;
135 throwex SystemException(
"Pipe is not opened for reading!");
136 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
137 int result = close(_pipe[0]);
139 throwex SystemException(
"Cannot close the read pipe endpoint!");
141 #elif defined(_WIN32) || defined(_WIN64)
142 if (!CloseHandle(_pipe[0]))
143 throwex SystemException(
"Cannot close the read pipe endpoint!");
144 _pipe[0] = INVALID_HANDLE_VALUE;
152 throwex SystemException(
"Pipe is not opened for writing!");
153 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
154 int result = close(_pipe[1]);
156 throwex SystemException(
"Cannot close the write pipe endpoint!");
158 #elif defined(_WIN32) || defined(_WIN64)
159 if (!CloseHandle(_pipe[1]))
160 throwex SystemException(
"Cannot close the write pipe endpoint!");
161 _pipe[1] = INVALID_HANDLE_VALUE;
174 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
176 #elif defined(_WIN32) || defined(_WIN64)
187 static_assert((StorageSize >=
sizeof(Impl)),
"Pipe::StorageSize must be increased!");
188 static_assert(((StorageAlign %
alignof(Impl)) == 0),
"Pipe::StorageAlign must be adjusted!");
191 new(&_storage)Impl();
197 reinterpret_cast<Impl*
>(&_storage)->~Impl();
207 size_t Pipe::Read(
void* buffer,
size_t size) {
return impl().Read(buffer, size); }
208 size_t Pipe::Write(
const void* buffer,
size_t size) {
return impl().Write(buffer, size); }
217 swap(_storage, pipe._storage);
bool IsPipeOpened() const noexcept
Is pipe opened for reading or writing?
void CloseWrite()
Close the write pipe endpoint.
virtual size_t Write(const void *buffer, size_t size)=0
Write a byte buffer base method.
size_t Read(void *buffer, size_t size) override
Read a bytes buffer from the pipe.
void Close()
Close all pipe endpoints.
bool IsPipeWriteOpened() const noexcept
Is pipe opened for writing?
bool IsPipeReadOpened() const noexcept
Is pipe opened for reading?
void * reader() const noexcept
Get the native read endpoint handler.
void * writer() const noexcept
Get the native write endpoint handler.
void CloseRead()
Close the read pipe endpoint.
void swap(Pipe &pipe) noexcept
Swap two instances.
Aligned storage validator.
#define throwex
Throw extended exception macro.
Fatal abort execution definition.
#define fatality(...)
Fatal abort execution extended macro.
C++ Common project definitions.
void swap(FileCache &cache1, FileCache &cache2) noexcept
Aligned storage validator definition.