61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
#pragma once
|
|
#include "char_types.hpp"
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#if defined(BASALT_OS_WINDOWS)
|
|
#include <Windows.h>
|
|
#else
|
|
// Nothing was included in POSIX
|
|
#endif
|
|
|
|
namespace basalt::shared::pipe_operator {
|
|
|
|
class PipeOperator {
|
|
public:
|
|
#if defined(BASALT_OS_WINDOWS)
|
|
using Handle = HANDLE;
|
|
static constexpr Handle BAD_PIPE_HANDLE = INVALID_HANDLE_VALUE;
|
|
#else
|
|
using Handle = int;
|
|
static constexpr Handle BAD_PIPE_HANDLE = -1;
|
|
#endif
|
|
|
|
public:
|
|
PipeOperator(const char_types::BSStringView& name);
|
|
~PipeOperator();
|
|
// Delete copy ctor
|
|
PipeOperator(const PipeOperator&) = delete;
|
|
PipeOperator& operator=(const PipeOperator&) = delete;
|
|
// Implement move ctor
|
|
PipeOperator(PipeOperator&&) noexcept;
|
|
PipeOperator& operator=(PipeOperator&& other) noexcept;
|
|
|
|
public:
|
|
void Read(void* buffer, size_t size);
|
|
void Write(const void* buffer, size_t size);
|
|
|
|
template<typename TPod>
|
|
void ReadPod(TPod& buffer) {
|
|
Read(&buffer, sizeof(TPod));
|
|
}
|
|
template<typename TPod>
|
|
void WritePod(const TPod& buffer) {
|
|
Write(&buffer, sizeof(TPod));
|
|
}
|
|
void ReadString(std::string& buffer) {
|
|
size_t length = 0;
|
|
ReadPod(length);
|
|
buffer.resize(length);
|
|
Read(buffer.data(), length);
|
|
}
|
|
void WriteString(std::string_view& buffer) {
|
|
WritePod(buffer.size());
|
|
Write(buffer.data(), buffer.size());
|
|
}
|
|
|
|
private:
|
|
Handle m_Handle;
|
|
};
|
|
} // namespace basalt::shared::pipe_operator
|