src/IntegerDataType.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 14 Jul 2018 23:24:22 +0200
branchv_0
changeset 7 489e52138771
permissions -rw-r--r--
add data type and catalog classes from the prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#pragma once
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <string>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
#include <iostream>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#include <cassert>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
#include <limits>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
#include "common.h"
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#include "DataType.h"
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
#include "RelpipeException.h"
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
using namespace std;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
namespace rp_prototype {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
/**
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 * The prototype does not have various integer and other numeric data types,
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
 * it just works with one type of integer.
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
 * But this integer has variable length -- smaller values occupy only one byte, bigger ones, more bytes 1,2,4,8 + first byte (contains length signalization).
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
 * In the real implementation of relational pipes, there will be DataTypes for particular numeric types.
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
 * TODO: support also big endian architectures.
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
 * TODO: throw exception if a value was stored in bigger type than needed (while reading – there should be only one supported way how to encode a single value)
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
 * Example of encoded values:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do printf '%20s = ' $n; dist/Debug/GNU-Linux/rp-prototype write integer $n | hd | head -n 1; done
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 *                    0 = 00000000  00                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 *                    1 = 00000000  01                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 *                   10 = 00000000  0a                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 *                  250 = 00000000  fa                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 *                  251 = 00000000  fb fb                                             |..|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 *                  252 = 00000000  fb fc                                             |..|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 *                65535 = 00000000  fc ff ff                                          |...|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
 *                65536 = 00000000  fd 00 00 01 00                                    |.....|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
 *           4294967295 = 00000000  fd ff ff ff ff                                    |.....|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
 *           4294967296 = 00000000  fe 00 00 00 00 01 00 00  00                       |.........|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
 * 18446744073709551615 = 00000000  fe ff ff ff ff ff ff ff  ff                       |.........|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
 * Example of decoded values:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
 * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do dist/Debug/GNU-Linux/rp-prototype write integer $n | dist/Debug/GNU-Linux/rp-prototype read integer; done;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
 * 0
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
 * 1
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
 * 10
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
 * 250
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
 * 251
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
 * 252
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
 * 65535
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
 * 65536
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
 * 4294967295
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
 * 4294967296
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
 * 18446744073709551615
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
 * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
 */
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
class IntegerDataType : public DataType<integer_t> {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
private:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	static const uint8_t INTEGER_TYPE_UINT8 = 251;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	static const uint8_t INTEGER_TYPE_UINT16 = 252;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	static const uint8_t INTEGER_TYPE_UINT32 = 253;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	static const uint8_t INTEGER_TYPE_UINT64 = 254;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	static const uint8_t INTEGER_TYPE_RESERVED = 255;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	template<typename T> integer_t read(istream &input) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		T value = 0;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		input.read(reinterpret_cast<char *> (&value), sizeof (value));
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		return value;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	template<typename T> void write(ostream &output, const integer_t &value) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		assert(sizeof (T) <= sizeof (value));
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		output.write(reinterpret_cast<const char *> (&value), sizeof (T));
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	template<typename T> void write(ostream &output, const uint8_t type, const integer_t &value) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		write<uint8_t>(output, type);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		write<T>(output, value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	template<typename T> bool fits(const integer_t &value) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		return value <= numeric_limits<T>::max();
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
public:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	IntegerDataType() : DataType<integer_t>(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	integer_t readValue(istream &input) override {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		uint8_t first;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		input.read(reinterpret_cast<char *> (&first), sizeof (first));
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
		if (input.good()) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
			if (first < INTEGER_TYPE_UINT8) return first;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
			else if (first == INTEGER_TYPE_UINT8) return read<uint8_t>(input);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
			else if (first == INTEGER_TYPE_UINT16) return read<uint16_t>(input);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
			else if (first == INTEGER_TYPE_UINT32) return read<uint32_t>(input);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
			else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
			else throw RelpipeException(L"Error while parsing integer type: unsupported type", EXIT_CODE_DATA_ERROR);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		} else {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
			throw RelpipeException(L"Error while reading integer from the stream.", EXIT_CODE_DATA_ERROR);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	void writeValue(ostream &output, const integer_t &value) override {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		// output << value; // by zapsalo číslo jako ASII text
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
		if (value < INTEGER_TYPE_UINT8) write<uint8_t>(output, value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
		else if (fits<uint8_t>(value)) write<uint8_t>(output, INTEGER_TYPE_UINT8, value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
		else if (fits<uint16_t>(value)) write<uint16_t>(output, INTEGER_TYPE_UINT16, value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
		else if (fits<uint32_t>(value)) write<uint32_t>(output, INTEGER_TYPE_UINT32, value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
		else if (fits<uint64_t>(value)) write<uint64_t>(output, INTEGER_TYPE_UINT64, value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		else throw RelpipeException(L"Error while writing integer type: value too long", EXIT_CODE_DATA_ERROR);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
	integer_t toValue(const wstring &stringValue) override {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
		// throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
		return stoul(stringValue);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
	wstring toString(const integer_t &value) override {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
		return to_wstring(value);
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
};
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
}