src/DataTypeWriterCatalog.h
branchv_0
changeset 8 03750aff8619
parent 7 01dd90eeedbb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/DataTypeWriterCatalog.h	Sun Jul 15 00:45:21 2018 +0200
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <string>
+#include <iostream>
+#include <vector>
+
+#include "../include/typedefs.h"
+#include "../include/DataTypeWriterBase.h"
+#include "BooleanDataTypeWriter.h"
+#include "IntegerDataTypeWriter.h"
+#include "StringDataTypeWriter.h"
+
+namespace relpipe {
+namespace writer {
+
+class DataTypeCatalog {
+private:
+	BooleanDataTypeWriter booleanWriter;
+	IntegerDataTypeWriter integerWriter;
+	StringDataTypeWriter stringWriter;
+	vector<DataTypeWriterBase*> writers = {&booleanWriter, &integerWriter, &stringWriter};
+public:
+
+	integer_t toTypeId(const string_t typeCode) {
+		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeCode)) return writer->getTypeId();
+		throw RelpipeWriterException(L"Unsupported data type: " + typeCode);
+	}
+
+	string_t toTypeCode(const integer_t typeId) {
+		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->getTypeCode();
+		throw RelpipeWriterException(L"Unsupported data type: " + typeId);
+	}
+
+	void writeString(std::ostream &output, const string_t &stringValue, const integer_t typeId) {
+		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue);
+		throw RelpipeWriterException(L"Unsupported data type: " + typeId);
+	}
+
+};
+
+}
+}
\ No newline at end of file