aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base/platform/unix
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/base/platform/unix')
-rw-r--r--include/cru/base/platform/unix/PosixSpawnSubProcess.h6
-rw-r--r--include/cru/base/platform/unix/UnixFile.h61
-rw-r--r--include/cru/base/platform/unix/UnixFileStream.h8
-rw-r--r--include/cru/base/platform/unix/UnixPipe.h68
4 files changed, 66 insertions, 77 deletions
diff --git a/include/cru/base/platform/unix/PosixSpawnSubProcess.h b/include/cru/base/platform/unix/PosixSpawnSubProcess.h
index 5f2200f5..c691984b 100644
--- a/include/cru/base/platform/unix/PosixSpawnSubProcess.h
+++ b/include/cru/base/platform/unix/PosixSpawnSubProcess.h
@@ -9,7 +9,7 @@
#include "../../io/AutoReadStream.h"
#include "UnixFileStream.h"
-#include "UnixPipe.h"
+#include "UnixFile.h"
#include <spawn.h>
@@ -34,10 +34,6 @@ class PosixSpawnSubProcessImpl : public Object,
pid_t pid_;
int exit_code_;
- UnixPipe stdin_pipe_;
- UnixPipe stdout_pipe_;
- UnixPipe stderr_pipe_;
-
std::unique_ptr<UnixFileStream> stdin_stream_;
std::unique_ptr<UnixFileStream> stdout_stream_;
std::unique_ptr<UnixFileStream> stderr_stream_;
diff --git a/include/cru/base/platform/unix/UnixFile.h b/include/cru/base/platform/unix/UnixFile.h
new file mode 100644
index 00000000..63545428
--- /dev/null
+++ b/include/cru/base/platform/unix/UnixFile.h
@@ -0,0 +1,61 @@
+#pragma once
+
+#if !defined(__unix)
+#error "This file can only be included on unix."
+#endif
+
+#include "../../Bitmask.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;
+ 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(); }
+
+ 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
diff --git a/include/cru/base/platform/unix/UnixFileStream.h b/include/cru/base/platform/unix/UnixFileStream.h
index 0709a326..b97c22de 100644
--- a/include/cru/base/platform/unix/UnixFileStream.h
+++ b/include/cru/base/platform/unix/UnixFileStream.h
@@ -5,6 +5,7 @@
#endif
#include "../../io/Stream.h"
+#include "UnixFile.h"
namespace cru::platform::unix {
class UnixFileStream : public io::Stream {
@@ -13,8 +14,8 @@ class UnixFileStream : public io::Stream {
public:
UnixFileStream(const char* path, int oflag, mode_t mode = 0660);
- UnixFileStream(int fd, bool can_seek, bool can_read, bool can_write,
- bool auto_close);
+ UnixFileStream(UnixFileDescriptor fd, bool can_seek, bool can_read,
+ bool can_write);
~UnixFileStream() override;
public:
@@ -31,7 +32,6 @@ class UnixFileStream : public io::Stream {
void DoClose();
private:
- int file_descriptor_; // -1 for no file descriptor
- bool auto_close_;
+ UnixFileDescriptor file_descriptor_;
};
} // namespace cru::platform::unix
diff --git a/include/cru/base/platform/unix/UnixPipe.h b/include/cru/base/platform/unix/UnixPipe.h
deleted file mode 100644
index 830ddf62..00000000
--- a/include/cru/base/platform/unix/UnixPipe.h
+++ /dev/null
@@ -1,68 +0,0 @@
-#pragma once
-
-#if !defined(__unix)
-#error "This file can only be included on unix."
-#endif
-
-#include "../../Base.h"
-#include "../../Bitmask.h"
-
-namespace cru::platform::unix {
-namespace details {
-struct UnixPipeFlagTag;
-}
-using UnixPipeFlag = Bitmask<details::UnixPipeFlagTag>;
-struct UnixPipeFlags {
- constexpr static auto NonBlock = UnixPipeFlag::FromOffset(1);
-};
-
-/**
- * @brief an unix pipe, commonly for communication of parent process and child
- * process.
- *
- * There are two types of pipes sorted by its usage. For stdin, parent process
- * SEND data to child process. For stdout and stderr, parent process RECEIVE
- * data from child process. Each pipe has two ends, one for read and the other
- * for write. But for send and receive, they are reversed. It is a little
- * confused to consider which end should be used by parent and which end should
- * be used by child. So this class help you make it clear. You specify SEND or
- * RECEIVE, and this class give you a parent used end and a child used end.
- *
- * This class will only close the end used by parent when it is destructed. It
- * is the user's duty to close the one used by child.
- */
-class UnixPipe : public Object {
- private:
- constexpr static auto kLogTag = u"cru::platform::unix::UnixPipe";
-
- public:
- enum class Usage {
- Send,
- Receive,
- };
-
- explicit UnixPipe(Usage usage, bool auto_close, UnixPipeFlag flags = {});
-
- CRU_DELETE_COPY(UnixPipe)
- CRU_DELETE_MOVE(UnixPipe)
-
- ~UnixPipe();
-
- /**
- * @brief aka, the one used by parent process.
- */
- int GetSelfFileDescriptor();
-
- /**
- * @brief aka, the one used by child process.
- */
- int GetOtherFileDescriptor();
-
- private:
- Usage usage_;
- bool auto_close_;
- UnixPipeFlag flags_;
- int read_fd_;
- int write_fd_;
-};
-} // namespace cru::platform::unix