src/types/IntegerDataTypeReader.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/IntegerDataTypeReader.h@2d7109286408
child 29 755978b0935c
permissions -rw-r--r--
refactoring, move files, use TypeId instead of integer_t
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
12
2d7109286408 remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     8
#include <relpipe/protocol/constants.h>
2d7109286408 remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     9
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    10
#include "../../include/relpipe/reader/RelpipeReaderException.h"
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    11
#include "../../include/relpipe/reader/typedefs.h"
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    12
#include "../DataTypeReader.h"
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    14
namespace relpipe {
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    15
namespace reader {
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    16
namespace types {
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    17
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    18
using namespace relpipe::protocol;
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    19
using namespace relpipe::reader;
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
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
 * 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
    23
 * 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
    24
 * 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
    25
 * 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
    26
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
 * 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
    28
 * 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
    29
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 * 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
    31
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 * $ 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
    33
 *                    0 = 00000000  00                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 *                    1 = 00000000  01                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
 *                   10 = 00000000  0a                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
 *                  250 = 00000000  fa                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
 *                  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
    38
 *                  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
    39
 *                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
    40
 *                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
    41
 *           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
    42
 *           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
    43
 * 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
    44
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
 * 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
    47
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
 * $ 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
    49
 * 0
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
 * 1
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
 * 10
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
 * 250
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
 * 251
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
 * 252
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
 * 65535
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
 * 65536
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
 * 4294967295
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
 * 4294967296
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
 * 18446744073709551615
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
 * 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
    63
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
 */
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    65
class IntegerDataTypeReader : public DataTypeReader<integer_t> {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
private:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	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
    68
	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
    69
	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
    70
	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
    71
	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
    72
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    73
	template<typename T> integer_t read(std::istream &input) {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		T value = 0;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		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
    76
		return value;
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
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    79
public:
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    81
	IntegerDataTypeReader() : DataTypeReader<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
7
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
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    84
	integer_t readValue(std::istream &input) override {
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		uint8_t first;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		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
    87
		if (input.good()) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
			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
    90
			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
    91
			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
    92
			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
    93
			else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input);
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    94
			else throw RelpipeReaderException(L"Error while parsing integer type: unsupported type");
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		} else {
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    96
			throw RelpipeReaderException(L"Error while reading integer from the stream.");
7
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
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   100
	string_t toString(const integer_t &value) override {
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   101
		return std::to_wstring(value);
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
};
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   106
}
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   107
}
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
}