Implement C++ bindings for pipe io client

This commit is contained in:
Kristóf Tóth 2019-04-08 15:16:11 +02:00
parent 11813a9454
commit 586c8b8503
2 changed files with 91 additions and 0 deletions

72
clients/c_cpp/pipe_io.hpp Normal file
View File

@ -0,0 +1,72 @@
extern "C" {
#include "pipe_io.h"
}
#include <string>
#include <iostream>
class PipeReader {
public:
PipeReader(const char* pipe_path)
:preader(new_pipe_reader(pipe_path)) {}
virtual ~PipeReader() {
close_pipe_reader(&this->preader);
}
PipeReader(PipeReader&) = delete;
PipeReader(PipeReader&&) = delete;
PipeReader& operator=(const PipeReader&) = delete;
PipeReader& operator=(const PipeReader&&) = delete;
void run() {
while (const char* linebuf = this->recv_message()) {
std::string msg(linebuf);
this->handle_message(msg);
}
}
const char* recv_message() {
return recv_msg_pipe_reader(&this->preader);
}
virtual void handle_message(std::string msg) {}
private:
pipe_reader preader;
};
class PipeWriter {
public:
PipeWriter(const char* pipe_path)
:pwriter(new_pipe_writer(pipe_path)) {}
virtual ~PipeWriter() {
close_pipe_writer(&this->pwriter);
}
PipeWriter(PipeReader&) = delete;
PipeWriter(PipeReader&&) = delete;
PipeWriter& operator=(const PipeReader&) = delete;
PipeWriter& operator=(const PipeReader&&) = delete;
void send_message(std::string msg) {
send_msg_pipe_writer(&this->pwriter, msg.c_str());
}
private:
pipe_writer pwriter;
};
class PipeIO : public PipeReader, public PipeWriter {
public:
PipeIO(const char* in_pipe_path, const char* out_pipe_path)
:PipeReader(in_pipe_path), PipeWriter(out_pipe_path) {}
PipeIO(PipeReader&) = delete;
PipeIO(PipeReader&&) = delete;
PipeIO& operator=(const PipeReader&) = delete;
PipeIO& operator=(const PipeReader&&) = delete;
};

19
clients/c_cpp/test.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
#include <string>
#include <fstream>
#include "pipe_io.hpp"
class EchoPipeIO : public PipeIO {
using PipeIO::PipeIO;
virtual void handle_message(std::string msg) override {
this->send_message(msg);
}
};
int main() {
EchoPipeIO echo_pipe_io("in", "out");
echo_pipe_io.run();
}