#pragma once
#include <string>
#include <iostream>
#include "DataType.h"
#include "RelpipeException.h"
using namespace std;
namespace rp_prototype {
class BooleanDataType : public DataType<bool> {
private:
const wstring TRUE = L"true";
const wstring FALSE = L"false";
public:
BooleanDataType() : DataType<bool>(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
}
bool readValue(istream &input) override {
auto value = input.get(); // TODO: check failbit
if (value == 0) return false;
else if (value == 1) return true;
else throw RelpipeException(L"Unable to convert the octet to boolean", EXIT_CODE_DATA_ERROR);
}
void writeValue(ostream &output, const bool &value) override {
output.put(value ? 1 : 0);
}
bool toValue(const wstring &stringValue) override {
if (stringValue == TRUE) return true;
else if (stringValue == FALSE) return false;
else throw RelpipeException(L"Unable to convert the string to boolean", EXIT_CODE_DATA_ERROR);
}
wstring toString(const bool &value) override {
return value ? TRUE : FALSE;
}
};
}