#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);
}
};
}