77 lines
2.5 KiB
C++
77 lines
2.5 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 read_pod(TPod& buffer) {
|
|
read(&buffer, sizeof(TPod));
|
|
}
|
|
template<typename TPod>
|
|
void write_pod(const TPod& buffer) {
|
|
write(&buffer, sizeof(TPod));
|
|
}
|
|
void read_string(std::string& 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(std::string::value_type));
|
|
}
|
|
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));
|
|
}
|
|
|
|
private:
|
|
Handle m_Handle;
|
|
};
|
|
} // namespace basalt::shared::pipe_operator
|