src/SubProcess.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 25 Apr 2021 18:47:57 +0200
branchv_0
changeset 89 25a11859975b
parent 67 0766d298eb1c
permissions -rw-r--r--
streamlet examples: QR: rename qr to qr-decode + simplify Makefile

/**
 * Relational pipes
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <vector>
#include <map>
#include <string>
#include <sstream>

#include <relpipe/writer/typedefs.h>
#include <relpipe/writer/RelpipeWriterException.h>

/**
 * TODO: move to a separate library → can be used later also in relpipe-tr-streamlet
 */
class SubProcess {
public:

	class Message {
	public:
		int code;
		std::vector<relpipe::writer::string_t> parameters;

		Message() {
		}

		Message(int code) : code(code) {
		}

		Message(int code, std::vector<relpipe::writer::string_t> parameters) : code(code), parameters(parameters) {
		}

		Message(int code, relpipe::writer::string_t p1) : code(code), parameters({p1}) {
		}

		Message(int code, relpipe::writer::string_t p1, relpipe::writer::string_t p2) : code(code), parameters({p1, p2}) {
		}

		Message(int code, relpipe::writer::string_t p1, relpipe::writer::string_t p2, relpipe::writer::string_t p3) : code(code), parameters({p1, p2, p3}) {
		}

		relpipe::writer::string_t toString() {
			std::wstringstream s;
			s << L"Message(code: " << code << L", parameters: ";
			for (int i = 0; i < parameters.size(); i++) {
				if (i < parameters.size() - 1) s << parameters[i] << L", ";
				else s << parameters[i];
			}
			s << L")";
			return s.str();
		}
	};

	class Exception : public relpipe::writer::RelpipeWriterException {
	public:

		Exception(std::wstring message) : relpipe::writer::RelpipeWriterException(message) {
		}

	};

	static SubProcess* create(std::vector<relpipe::writer::string_t> commandLine, std::map<relpipe::writer::string_t, relpipe::writer::string_t> environment, bool dropErrorOutput = true);

	virtual Message read() = 0;
	virtual void write(Message message) = 0;
	virtual int wait() = 0;

	virtual ~SubProcess() = default;

};