diff -r 64f382e63e01 -r 01dd90eeedbb src/BooleanDataType.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/BooleanDataType.h Sat Jul 14 23:24:10 2018 +0200 @@ -0,0 +1,45 @@ +#pragma once + +#include +#include + +#include "DataType.h" +#include "RelpipeException.h" + +using namespace std; + +namespace rp_prototype { + +class BooleanDataType : public DataType { +private: + const wstring TRUE = L"true"; + const wstring FALSE = L"false"; +public: + + BooleanDataType() : DataType(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