src/types/BooleanDataTypeReader.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 25 Aug 2018 18:16:53 +0200
branchv_0
changeset 14 e8de089f95dd
parent 12 src/BooleanDataTypeReader.h@2d7109286408
child 29 755978b0935c
permissions -rw-r--r--
refactoring, move files, use TypeId instead of integer_t

#pragma once

#include <string>
#include <iostream>

#include <relpipe/protocol/constants.h>

#include "../../include/relpipe/reader/RelpipeReaderException.h"
#include "../../include/relpipe/reader/typedefs.h"
#include "../DataTypeReader.h"

namespace relpipe {
namespace reader {
namespace types {

using namespace relpipe::protocol;
using namespace relpipe::reader;

class BooleanDataTypeReader : public DataTypeReader<boolean_t> {
private:
	const string_t TRUE = L"true";
	const string_t FALSE = L"false";
public:

	BooleanDataTypeReader() : DataTypeReader<boolean_t>(TypeId::BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
	}

	bool readValue(std::istream &input) override {
		auto value = input.get(); // TODO: check failbit
		if (value == 0) return false;
		else if (value == 1) return true;
		else throw RelpipeReaderException(L"Unable to convert the octet to boolean");
	}

	string_t toString(const boolean_t &value) override {
		return value ? TRUE : FALSE;
	}

};

}
}
}