1
0
Files
BasaltMeter/BasaltPresenter/Shared/basalt/pipe_operator.hpp

61 lines
1.6 KiB
C++
Raw Normal View History

2026-01-06 21:24:47 +08:00
#pragma once
2026-01-06 16:27:19 +08:00
#include "char_types.hpp"
2026-01-08 19:23:19 +08:00
#include <string>
2025-11-27 20:48:35 +08:00
#include <string_view>
#if defined(BASALT_OS_WINDOWS)
#include <Windows.h>
#else
2026-01-08 19:23:19 +08:00
// Nothing was included in POSIX
2025-11-27 20:48:35 +08:00
#endif
2026-01-08 19:23:19 +08:00
namespace basalt::shared::pipe_operator {
2025-11-27 20:48:35 +08:00
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:
2026-01-08 19:23:19 +08:00
PipeOperator(const char_types::BSStringView& name);
2025-11-27 20:48:35 +08:00
~PipeOperator();
// Delete copy ctor
PipeOperator(const PipeOperator&) = delete;
PipeOperator& operator=(const PipeOperator&) = delete;
// Implement move ctor
PipeOperator(PipeOperator&&) noexcept;
PipeOperator& operator=(PipeOperator&& other) noexcept;
2026-01-08 19:23:19 +08:00
public:
2025-11-27 20:48:35 +08:00
void Read(void* buffer, size_t size);
void Write(const void* buffer, size_t size);
2026-01-08 19:23:19 +08:00
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());
}
2025-11-27 20:48:35 +08:00
private:
Handle m_Handle;
};
2026-01-08 19:23:19 +08:00
} // namespace basalt::shared::pipe_operator