src/lib/AbstractParser.h
branchv_0
changeset 1 2179f13227f4
child 3 68026fe3aaf5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/AbstractParser.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,91 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 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 <cstring>
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class AbstractParserImpl;
+
+class AbstractParser {
+private:
+	friend AbstractParserImpl;
+	AbstractParserImpl* implementation;
+public:
+	virtual ~AbstractParser();
+	void write(const char* buffer, const size_t length);
+	void rollback();
+protected:
+	AbstractParser();
+
+	/**
+	 * Is thrown from read() and peak() methods if there are not enough data.
+	 * Interrupts current update() cycle and causes rollback.
+	 */
+	class ReadBufferUnderflowException {
+		// TODO: common super-class for exceptions, hidden implementation
+	};
+
+	/**
+	 * May be thrown from the update() method in order to cancel currenty cycle and do explicit rollback.
+	 * Same data will be processed in the next cycle.
+	 */
+	class ExplicitRollbackException {
+		// TODO: common super-class for exceptions, hidden implementation
+	};
+
+	/**
+	 * May be called from the update() method in order to explicitly confirm that read data was successfully processed.
+	 * Such data will not be processed again during the next cycle (even if ReadBufferUnderflowException occurs later in this cycle).
+	 
+	 * Explicit commit() call is useful when we did some demanding work (so we are not willing to do it again in case of ReadBufferUnderflowException);
+	 * and is necessary when we already published some outputs or did other non-idempotent operation or caused some other significant side effects.
+	 
+	 * If there is no commit() called and update() just finishes, commit() is called implicitly.
+	 * 
+	 * Note: There is no accessible rollback() method – throw ExplicitRollbackException instead.
+	 */
+	void commit();
+
+	/**
+	 * 
+	 * @param buffer
+	 * @param length
+	 */
+	void read(char* buffer, const size_t length);
+
+	/**
+	 * 
+	 * @param buffer
+	 * @param length
+	 */
+	void peek(char* buffer, const size_t length);
+
+	/**
+	 *  
+	 */
+	virtual void update() = 0;
+};
+
+}
+}
+}
+}