src/lib/ASN1ContentHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Jun 2021 21:41:59 +0200
branchv_0
changeset 16 bb457bb5b515
parent 15 95ca127ba816
child 17 f5281ab3e68f
permissions -rw-r--r--
some support for UTF8String and PrintableString

/**
 * 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 <memory>
#include <vector>

namespace relpipe {
namespace in {
namespace asn1 {
namespace lib {

class ASN1ContentHandler {
public:

	enum class CollectionType : uint64_t {
		Constructed, // TODO: special event?
		Sequence = 16,
		Set = 17
	};

	enum class StringType : uint64_t {
		OctetString,
		UTF8String,
		NumericString,
		PrintableString,
		T61String,
		VideotexString,
		IA5String,
		GraphicString,
		VisibleString,
		GeneralString,
		UniversalString,
		CharacterString,
		BMPString,
	};

	virtual ~ASN1ContentHandler() = default;

	virtual void writeStreamStart() = 0;
	virtual void writeStreamEnd() = 0;

	virtual void writeCollectionStart(CollectionType type) = 0;
	virtual void writeCollectionEnd() = 0;
	virtual void writeBoolean(bool value) = 0;
	virtual void writeNull() = 0;
	virtual void writeInteger(int64_t value) = 0;
	virtual void writeString(StringType type, std::string value) = 0;
	// virtual void writeOID(std::string value) = 0;
	// Object descriptor
	// virtual void writeReal(float value) = 0;
	// Enumerated
	// Embedded PVD
	// Relative OID
	// TIME
	// UTC time
	// Generalized time
	// Date
	// Time of day
	// Date-time
	// Duration
	// OID-IRI
	// Relative OID-IRI

};

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 writeStreamStart() override {
		handler->writeStreamStart();
	}

	void writeStreamEnd() override {
		handler->writeStreamEnd();
	}

	void writeCollectionStart(CollectionType type) override {
		handler->writeCollectionStart(type);
	}

	void writeCollectionEnd() override {
		handler->writeCollectionEnd();
	}

	void writeBoolean(bool value) override {
		handler->writeBoolean(value);
	}

	void writeNull() override {
		handler->writeNull();
	}

	void writeInteger(int64_t value) override { // TODO: Integer class containing raw data + methods for converting them to particular numeric data types
		handler->writeInteger(value);
	}

	void writeString(StringType type, std::string value) override {
		handler->writeString(type, value);
	}

#undef handler

};


}
}
}
}