src/types/IntegerDataTypeReader.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 29 Nov 2018 22:30:12 +0100
branchv_0
changeset 29 755978b0935c
parent 14 e8de089f95dd
child 30 ef99f5a6c9f6
permissions -rw-r--r--
license: GNU GPLv3+
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     1
/**
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     2
 * Relational pipes
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     4
 *
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     8
 * (at your option) any later version.
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
     9
 *
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    13
 * GNU General Public License for more details.
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    14
 *
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    15
 * You should have received a copy of the GNU General Public License
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
755978b0935c license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    17
 */
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <cassert>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <limits>
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
12
2d7109286408 remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    25
#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
    26
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    27
#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
    28
#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
    29
#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
    30
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    31
namespace relpipe {
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    32
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
    33
namespace types {
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    34
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    35
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
    36
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
    37
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
/**
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
 * 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
    40
 * 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
    41
 * 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
    42
 * 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
    43
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
 * 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
    45
 * 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
    46
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
 * 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
    48
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
 * $ 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
    50
 *                    0 = 00000000  00                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
 *                    1 = 00000000  01                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
 *                   10 = 00000000  0a                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
 *                  250 = 00000000  fa                                                |.|
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
 *                  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
    55
 *                  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
    56
 *                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
    57
 *                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
    58
 *           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
    59
 *           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
    60
 * 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
    61
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
 * 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
    64
 * -------------------------------------------------------------------------------------------------
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
 * $ 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
    66
 * 0
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
 * 1
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
 * 10
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
 * 250
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
 * 251
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
 * 252
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
 * 65535
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
 * 65536
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
 * 4294967295
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
 * 4294967296
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
 * 18446744073709551615
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
 * 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
    80
 * 
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
 */
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    82
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
    83
private:
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	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
    85
	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
    86
	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
    87
	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
    88
	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
    89
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    90
	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
    91
		T value = 0;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		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
    93
		return value;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    96
public:
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
    98
	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
    99
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   101
	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
   102
		uint8_t first;
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		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
   104
		if (input.good()) {
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
			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
   107
			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
   108
			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
   109
			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
   110
			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
   111
			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
   112
		} else {
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   113
			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
   114
		}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   117
	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
   118
		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
   119
	}
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
};
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
8
c87e9c84f7aa reader only reads + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   123
}
14
e8de089f95dd refactoring, move files, use TypeId instead of integer_t
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   124
}
7
489e52138771 add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
}