src/DataTypeCatalog.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 14 Jul 2018 23:24:22 +0200
branchv_0
changeset 7 489e52138771
permissions -rw-r--r--
add data type and catalog classes from the prototype

#pragma once

#include <string>
#include <iostream>
#include <vector>

#include "common.h"
#include "DataType.h"
#include "BooleanDataType.h"
#include "IntegerDataType.h"
#include "StringDataType.h"

using namespace std;

namespace rp_prototype {

class DataTypeCatalog {
private:
	BooleanDataType booleanType;
	IntegerDataType integerType;
	StringDataType stringType;
	vector<DataTypeBase*> types = {&booleanType, &integerType, &stringType};
public:

	integer_t toTypeId(const wstring typeCode) {
		for (DataTypeBase* dataType : types) if (dataType->supports(typeCode)) return dataType->getTypeId();
		throw RelpipeException(L"Unsupported data type: " + typeCode, EXIT_CODE_DATA_ERROR);
	}

	wstring toTypeCode(const integer_t typeId) {
		for (DataTypeBase* dataType : types) if (dataType->supports(typeId)) return dataType->getTypeCode();
		throw RelpipeException(L"Unsupported data type: " + typeId, EXIT_CODE_DATA_ERROR);
	}

	void writeString(ostream &output, const wstring &stringValue, const integer_t typeId) {
		for (DataTypeBase* dataType : types) if (dataType->supports(typeId)) return dataType->writeString(output, stringValue);
		throw RelpipeException(L"Unsupported data type: " + typeId, EXIT_CODE_DATA_ERROR);
	}

	wstring readString(istream &input, const integer_t typeId) {
		for (DataTypeBase* dataType : types) if (dataType->supports(typeId)) return dataType->readString(input);
		throw RelpipeException(L"Unsupported data type: " + typeId, EXIT_CODE_DATA_ERROR);
	}

};

}