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

77 lines
2.5 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:
2026-01-08 19:37:25 +08:00
void read(void* buffer, size_t size);
void write(const void* buffer, size_t size);
2025-11-27 20:48:35 +08:00
2026-01-08 19:23:19 +08:00
template<typename TPod>
2026-01-08 19:37:25 +08:00
void read_pod(TPod& buffer) {
read(&buffer, sizeof(TPod));
2026-01-08 19:23:19 +08:00
}
template<typename TPod>
2026-01-08 19:37:25 +08:00
void write_pod(const TPod& buffer) {
write(&buffer, sizeof(TPod));
2026-01-08 19:23:19 +08:00
}
2026-01-08 19:37:25 +08:00
void read_string(std::string& buffer) {
2026-01-10 17:10:14 +08:00
std::uint32_t raw_length = 0;
read_pod(raw_length);
auto length = static_cast<std::size_t>(raw_length);
2026-01-08 19:23:19 +08:00
buffer.resize(length);
2026-01-10 17:10:14 +08:00
read(buffer.data(), length * sizeof(std::string::value_type));
2026-01-08 19:23:19 +08:00
}
2026-01-10 17:10:14 +08:00
void write_string(const std::string_view& buffer) {
auto length = buffer.size();
auto raw_length = static_cast<std::uint32_t>(length);
write_pod(raw_length);
write(buffer.data(),length * sizeof(std::string_view::value_type));
}
void read_bsstring(char_types::BSString& buffer) {
std::uint32_t raw_length = 0;
read_pod(raw_length);
auto length = static_cast<std::size_t>(raw_length);
buffer.resize(length);
read(buffer.data(), length * sizeof(char_types::BSString::value_type));
}
void write_bsstring(const char_types::BSStringView& buffer) {
auto length = buffer.size();
auto raw_length = static_cast<std::uint32_t>(length);
write_pod(raw_length);
write(buffer.data(), length * sizeof(char_types::BSStringView::value_type));
2026-01-08 19:23:19 +08:00
}
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