proxy handlers forwarding events to all subordinate handlers v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 12 Jun 2021 18:34:19 +0200
branchv_0
changeset 4 7230e1ea0b07
parent 3 68026fe3aaf5
child 5 15c3221e66af
proxy handlers forwarding events to all subordinate handlers
src/XMLDocumentConstructor.h
src/lib/ASN1ContentHandler.h
src/lib/ASN1Reader.h
src/lib/BasicASN1Reader.h
src/lib/DOMBuildingSAXContentHandler.h
src/lib/GenericASN1ContentHandler.h
src/lib/SAXContentHandler.h
--- a/src/XMLDocumentConstructor.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/XMLDocumentConstructor.h	Sat Jun 12 18:34:19 2021 +0200
@@ -16,6 +16,8 @@
  */
 #pragma once
 
+#include <memory>
+
 #include <libxml++-2.6/libxml++/libxml++.h>
 
 #include "lib/BasicASN1Reader.h"
@@ -41,6 +43,13 @@
 	void process() {
 
 		relpipe::in::asn1::lib::BasicASN1Reader reader;
+		std::shared_ptr<relpipe::in::asn1::lib::GenericASN1ContentHandler> asn1handler = make_shared<relpipe::in::asn1::lib::GenericASN1ContentHandler>();
+		std::shared_ptr<relpipe::in::asn1::lib::DOMBuildingSAXContentHandler> saxHandler = make_shared<relpipe::in::asn1::lib::DOMBuildingSAXContentHandler>(parser->get_document());
+
+		asn1handler->addHandler(saxHandler);
+		reader.addHandler(asn1handler);
+
+
 		reader.write(nullptr, 0); // FIXME: transfer data from input to reader + use DOMBuildingSAXContentHandler
 		reader.close();
 
--- a/src/lib/ASN1ContentHandler.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/lib/ASN1ContentHandler.h	Sat Jun 12 18:34:19 2021 +0200
@@ -16,14 +16,53 @@
  */
 #pragma once
 
+#include <memory>
+#include <vector>
+
 namespace relpipe {
 namespace in {
 namespace asn1 {
 namespace lib {
 
 class ASN1ContentHandler {
+public:
+
+	virtual ~ASN1ContentHandler() = default;
+
+	virtual void abc() = 0; // FIXME: remove dummy method
+	virtual void def(int a) = 0; // FIXME: remove dummy method
+	virtual void ghi(int a, int b) = 0; // FIXME: remove dummy method
+
 };
 
+class ASN1ContentHandlerProxy : public ASN1ContentHandler {
+private:
+	std::vector<std::shared_ptr<ASN1ContentHandler>> handlers;
+public:
+
+	void addHandler(std::shared_ptr<ASN1ContentHandler> handler) {
+		handlers.push_back(handler);
+	}
+
+#define handler for (auto ___h : handlers) ___h
+
+	void abc() override {
+		handler->abc();
+	}
+
+	void def(int a) override {
+		handler->def(a);
+	}
+
+	void ghi(int a, int b) override {
+		handler->ghi(a, b);
+	}
+
+#undef handler
+
+};
+
+
 }
 }
 }
--- a/src/lib/ASN1Reader.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/lib/ASN1Reader.h	Sat Jun 12 18:34:19 2021 +0200
@@ -29,8 +29,13 @@
 class ASN1Reader : public AbstractParser {
 public:
 	virtual ~ASN1Reader() = default;
-	virtual void addHandler(std::shared_ptr<ASN1ContentHandler> handler) = 0;
-	virtual void process() = 0;
+
+	virtual void addHandler(std::shared_ptr<ASN1ContentHandler> handler) {
+		handlers.addHandler(handler);
+	};
+
+protected:
+	ASN1ContentHandlerProxy handlers;
 };
 
 }
--- a/src/lib/BasicASN1Reader.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/lib/BasicASN1Reader.h	Sat Jun 12 18:34:19 2021 +0200
@@ -17,6 +17,7 @@
 #pragma once
 
 #include <memory>
+#include <vector>
 
 #include "ASN1Reader.h"
 
@@ -29,19 +30,6 @@
  * Reads ASN.1 data encoded as BER (DER, CER).
  */
 class BasicASN1Reader : public ASN1Reader {
-public:
-	virtual ~BasicASN1Reader() = default;
-
-	virtual void addHandler(std::shared_ptr<ASN1ContentHandler> handler) {
-	};
-
-	virtual void process() {
-	};
-
-	static ASN1Reader* create(std::istream& input) {
-		return new BasicASN1Reader();
-	}
-
 private:
 
 	enum class State {
@@ -57,11 +45,11 @@
 	void update() override {
 
 		if (state == State::A) {
-			
+
 		} else if (state == State::B) {
-			
+
 		} else if (state == State::C) {
-			
+
 		}
 
 
--- a/src/lib/DOMBuildingSAXContentHandler.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/lib/DOMBuildingSAXContentHandler.h	Sat Jun 12 18:34:19 2021 +0200
@@ -29,14 +29,19 @@
 private:
 	xmlpp::Document* document;
 
+public:
+
 	DOMBuildingSAXContentHandler(xmlpp::Document* document) : document(document) {
 	}
 
-public:
+	void abc() override {
+	};
 
-	static DOMBuildingSAXContentHandler* create(xmlpp::Document* document) {
-		return new DOMBuildingSAXContentHandler(document);
-	}
+	void def(int a) override {
+	};
+
+	void ghi(int a, int b) override {
+	};
 
 };
 
--- a/src/lib/GenericASN1ContentHandler.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/lib/GenericASN1ContentHandler.h	Sat Jun 12 18:34:19 2021 +0200
@@ -16,12 +16,40 @@
  */
 #pragma once
 
+#include "ASN1ContentHandler.h"
+#include "SAXContentHandler.h"
+
 namespace relpipe {
 namespace in {
 namespace asn1 {
 namespace lib {
 
+/**
+ * Converts ASN.1 events to XML SAX events.
+ * 
+ * Does not need to know anything about the model/schema – works with generic events (nodes).
+ */
 class GenericASN1ContentHandler : public ASN1ContentHandler {
+private:
+	SAXContentHandlerProxy handlers;
+public:
+
+	void addHandler(std::shared_ptr<SAXContentHandler> handler) {
+		handlers.addHandler(handler);
+	}
+
+	virtual ~GenericASN1ContentHandler() {
+	}
+
+	void abc() override {
+	};
+
+	void def(int a) override {
+	};
+
+	void ghi(int a, int b) override {
+	};
+
 };
 
 }
--- a/src/lib/SAXContentHandler.h	Sun Mar 14 19:58:35 2021 +0100
+++ b/src/lib/SAXContentHandler.h	Sat Jun 12 18:34:19 2021 +0200
@@ -26,6 +26,37 @@
 
 	virtual ~SAXContentHandler() = default;
 
+	virtual void abc() = 0; // FIXME: remove dummy method
+	virtual void def(int a) = 0; // FIXME: remove dummy method
+	virtual void ghi(int a, int b) = 0; // FIXME: remove dummy method
+
+};
+
+class SAXContentHandlerProxy : public SAXContentHandler {
+private:
+	std::vector<std::shared_ptr<SAXContentHandler>> handlers;
+public:
+
+	void addHandler(std::shared_ptr<SAXContentHandler> handler) {
+		handlers.push_back(handler);
+	}
+
+#define handler for (auto ___h : handlers) ___h
+
+	void abc() override {
+		handler->abc();
+	}
+
+	void def(int a) override {
+		handler->def(a);
+	}
+
+	void ghi(int a, int b) override {
+		handler->ghi(a, b);
+	}
+
+#undef handler
+
 };
 
 }