src/BooleanDataType.h
branchv_0
changeset 8 c87e9c84f7aa
parent 7 489e52138771
child 9 517888868e55
equal deleted inserted replaced
7:489e52138771 8:c87e9c84f7aa
     1 #pragma once
       
     2 
       
     3 #include <string>
       
     4 #include <iostream>
       
     5 
       
     6 #include "DataType.h"
       
     7 #include "RelpipeException.h"
       
     8 
       
     9 using namespace std;
       
    10 
       
    11 namespace rp_prototype {
       
    12 
       
    13 class BooleanDataType : public DataType<bool> {
       
    14 private:
       
    15 	const wstring TRUE = L"true";
       
    16 	const wstring FALSE = L"false";
       
    17 public:
       
    18 
       
    19 	BooleanDataType() : DataType<bool>(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
       
    20 	}
       
    21 
       
    22 	bool readValue(istream &input) override {
       
    23 		auto value = input.get(); // TODO: check failbit
       
    24 		if (value == 0) return false;
       
    25 		else if (value == 1) return true;
       
    26 		else throw RelpipeException(L"Unable to convert the octet to boolean", EXIT_CODE_DATA_ERROR);
       
    27 	}
       
    28 
       
    29 	void writeValue(ostream &output, const bool &value) override {
       
    30 		output.put(value ? 1 : 0);
       
    31 	}
       
    32 
       
    33 	bool toValue(const wstring &stringValue) override {
       
    34 		if (stringValue == TRUE) return true;
       
    35 		else if (stringValue == FALSE) return false;
       
    36 		else throw RelpipeException(L"Unable to convert the string to boolean", EXIT_CODE_DATA_ERROR);
       
    37 	}
       
    38 
       
    39 	wstring toString(const bool &value) override {
       
    40 		return value ? TRUE : FALSE;
       
    41 	}
       
    42 
       
    43 };
       
    44 
       
    45 }