src/types/BooleanDataTypeReader.h
branchv_0
changeset 14 e8de089f95dd
parent 12 2d7109286408
child 29 755978b0935c
equal deleted inserted replaced
13:543f1613c2da 14:e8de089f95dd
       
     1 #pragma once
       
     2 
       
     3 #include <string>
       
     4 #include <iostream>
       
     5 
       
     6 #include <relpipe/protocol/constants.h>
       
     7 
       
     8 #include "../../include/relpipe/reader/RelpipeReaderException.h"
       
     9 #include "../../include/relpipe/reader/typedefs.h"
       
    10 #include "../DataTypeReader.h"
       
    11 
       
    12 namespace relpipe {
       
    13 namespace reader {
       
    14 namespace types {
       
    15 
       
    16 using namespace relpipe::protocol;
       
    17 using namespace relpipe::reader;
       
    18 
       
    19 class BooleanDataTypeReader : public DataTypeReader<boolean_t> {
       
    20 private:
       
    21 	const string_t TRUE = L"true";
       
    22 	const string_t FALSE = L"false";
       
    23 public:
       
    24 
       
    25 	BooleanDataTypeReader() : DataTypeReader<boolean_t>(TypeId::BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
       
    26 	}
       
    27 
       
    28 	bool readValue(std::istream &input) override {
       
    29 		auto value = input.get(); // TODO: check failbit
       
    30 		if (value == 0) return false;
       
    31 		else if (value == 1) return true;
       
    32 		else throw RelpipeReaderException(L"Unable to convert the octet to boolean");
       
    33 	}
       
    34 
       
    35 	string_t toString(const boolean_t &value) override {
       
    36 		return value ? TRUE : FALSE;
       
    37 	}
       
    38 
       
    39 };
       
    40 
       
    41 }
       
    42 }
       
    43 }