src/BooleanDataType.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 14 Jul 2018 23:24:10 +0200
branchv_0
changeset 7 01dd90eeedbb
permissions -rw-r--r--
add data type and catalog classes from the prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#pragma once
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <string>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
#include <iostream>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
#include "DataType.h"
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
#include "RelpipeException.h"
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
using namespace std;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
namespace rp_prototype {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
class BooleanDataType : public DataType<bool> {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
private:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
	const wstring TRUE = L"true";
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
	const wstring FALSE = L"false";
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
public:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
	BooleanDataType() : DataType<bool>(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
	bool readValue(istream &input) override {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
		auto value = input.get(); // TODO: check failbit
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
		if (value == 0) return false;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
		else if (value == 1) return true;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
		else throw RelpipeException(L"Unable to convert the octet to boolean", EXIT_CODE_DATA_ERROR);
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	void writeValue(ostream &output, const bool &value) override {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
		output.put(value ? 1 : 0);
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	bool toValue(const wstring &stringValue) override {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		if (stringValue == TRUE) return true;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		else if (stringValue == FALSE) return false;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
		else throw RelpipeException(L"Unable to convert the string to boolean", EXIT_CODE_DATA_ERROR);
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	wstring toString(const bool &value) override {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		return value ? TRUE : FALSE;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
};
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
}