src/BooleanDataType.h
branchv_0
changeset 7 489e52138771
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/BooleanDataType.h	Sat Jul 14 23:24:22 2018 +0200
@@ -0,0 +1,45 @@
+#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;
+	}
+
+};
+
+}
\ No newline at end of file