src/BooleanDataType.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 "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;
	}

};

}