CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
pipe.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_SYSTEM_PIPE_H
10#define CPPCOMMON_SYSTEM_PIPE_H
11
12#include "common/reader.h"
13#include "common/writer.h"
14#include "errors/exceptions.h"
15
16#include <memory>
17
18namespace CppCommon {
19
21
30class Pipe : public Reader, public Writer
31{
32public:
33 Pipe();
34 Pipe(const Pipe&) = delete;
35 Pipe(Pipe&& pipe) = delete;
36 virtual ~Pipe();
37
38 Pipe& operator=(const Pipe&) = delete;
39 Pipe& operator=(Pipe&& pipe) = delete;
40
42 void* reader() const noexcept;
44 void* writer() const noexcept;
45
47 bool IsPipeOpened() const noexcept;
49 bool IsPipeReadOpened() const noexcept;
51 bool IsPipeWriteOpened() const noexcept;
52
54
62 size_t Read(void* buffer, size_t size) override;
63
64 using Reader::ReadAllBytes;
65 using Reader::ReadAllText;
66 using Reader::ReadAllLines;
67
69
77 size_t Write(const void* buffer, size_t size) override;
78
79 using Writer::Write;
80
82 void CloseRead();
84 void CloseWrite();
85
87 void Close();
88
90 void swap(Pipe& pipe) noexcept;
91 friend void swap(Pipe& pipe1, Pipe& pipe2) noexcept;
92
93private:
94 class Impl;
95
96 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
97 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
98
99 static const size_t StorageSize = 16;
100#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
101 static const size_t StorageAlign = 4;
102#else
103 static const size_t StorageAlign = 8;
104#endif
105 alignas(StorageAlign) std::byte _storage[StorageSize];
106};
107
110} // namespace CppCommon
111
112#include "pipe.inl"
113
114#endif // CPPCOMMON_SYSTEM_PIPE_H
Pipe.
Definition pipe.h:31
bool IsPipeOpened() const noexcept
Is pipe opened for reading or writing?
Definition pipe.cpp:203
Pipe(const Pipe &)=delete
void CloseWrite()
Close the write pipe endpoint.
Definition pipe.cpp:211
friend void swap(Pipe &pipe1, Pipe &pipe2) noexcept
Definition pipe.inl:11
size_t Read(void *buffer, size_t size) override
Read a bytes buffer from the pipe.
Definition pipe.cpp:207
void Close()
Close all pipe endpoints.
Definition pipe.cpp:212
bool IsPipeWriteOpened() const noexcept
Is pipe opened for writing?
Definition pipe.cpp:205
std::vector< std::string > ReadAllLines()
Read all text lines.
Definition reader.cpp:38
bool IsPipeReadOpened() const noexcept
Is pipe opened for reading?
Definition pipe.cpp:204
size_t Write(const void *buffer, size_t size) override
Write a byte buffer into the pipe.
Definition pipe.cpp:208
Pipe(Pipe &&pipe)=delete
Pipe & operator=(Pipe &&pipe)=delete
void * reader() const noexcept
Get the native read endpoint handler.
Definition pipe.cpp:200
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
Definition reader.cpp:15
void * writer() const noexcept
Get the native write endpoint handler.
Definition pipe.cpp:201
void CloseRead()
Close the read pipe endpoint.
Definition pipe.cpp:210
std::string ReadAllText()
Read all text.
Definition reader.cpp:32
virtual ~Pipe()
Definition pipe.cpp:194
Pipe & operator=(const Pipe &)=delete
Reader interface.
Definition reader.h:24
Writer interface.
Definition writer.h:23
Exceptions definition.
C++ Common project definitions.
Pipe inline implementation.
Reader interface definition.
Writer interface definition.