From 586c8b850398e91f1da0579f3029802cd824dbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 8 Apr 2019 15:16:11 +0200 Subject: [PATCH] Implement C++ bindings for pipe io client --- clients/c_cpp/pipe_io.hpp | 72 +++++++++++++++++++++++++++++++++++++++ clients/c_cpp/test.cpp | 19 +++++++++++ 2 files changed, 91 insertions(+) create mode 100644 clients/c_cpp/pipe_io.hpp create mode 100644 clients/c_cpp/test.cpp diff --git a/clients/c_cpp/pipe_io.hpp b/clients/c_cpp/pipe_io.hpp new file mode 100644 index 0000000..57ffd8d --- /dev/null +++ b/clients/c_cpp/pipe_io.hpp @@ -0,0 +1,72 @@ +extern "C" { +#include "pipe_io.h" +} +#include +#include + + +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; +}; diff --git a/clients/c_cpp/test.cpp b/clients/c_cpp/test.cpp new file mode 100644 index 0000000..4878391 --- /dev/null +++ b/clients/c_cpp/test.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#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(); +}