src/types/IntegerDataTypeWriter.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:47:14 +0200
branchv_0
changeset 41 744b61559eb2
parent 30 3f18cb7a6963
child 44 3f7b5f3f3f84
permissions -rw-r--r--
fix license version: GNU LGPLv3 or GPLv2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
     1
/**
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     2
 * Relational pipes (library)
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
     4
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     6
 * it under the terms of the:
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     7
 *  - GNU Lesser General Public License as published by the Free Software Foundation;
41
744b61559eb2 fix license version: GNU LGPLv3 or GPLv2
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
     8
 *    version 3 of the License or (at your option)
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     9
 *  - GNU General Public License as published by the Free Software Foundation;
41
744b61559eb2 fix license version: GNU LGPLv3 or GPLv2
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    10
 *    version 2 of the License.
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    11
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    12
 * This program is distributed in the hope that it will be useful,
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    15
 * GNU General Public License for more details.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    16
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    17
 * You should have received a copy of the GNU General Public License
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    19
 */
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#pragma once
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
#include <string>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <iostream>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <cassert>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <limits>
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    27
#include <relpipe/protocol/constants.h>
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    28
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
#include "../DataTypeWriter.h"
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    31
namespace relpipe {
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    32
namespace writer {
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    33
namespace types {
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
23
d505eaedc35e remove format.h and use constants.h from lib-protocol
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    35
using namespace relpipe::protocol;
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    36
using namespace relpipe::writer;
7
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
 * The prototype does not have various integer and other numeric data types,
01dd90eeedbb 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.
01dd90eeedbb 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).
01dd90eeedbb 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.
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
 * TODO: support also big endian architectures.
01dd90eeedbb 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)
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
 * 
01dd90eeedbb 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:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
 * -------------------------------------------------------------------------------------------------
01dd90eeedbb 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
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
 *                    0 = 00000000  00                                                |.|
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
 *                    1 = 00000000  01                                                |.|
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
 *                   10 = 00000000  0a                                                |.|
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
 *                  250 = 00000000  fa                                                |.|
01dd90eeedbb 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                                             |..|
01dd90eeedbb 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                                             |..|
01dd90eeedbb 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                                          |...|
01dd90eeedbb 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                                    |.....|
01dd90eeedbb 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                                    |.....|
01dd90eeedbb 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                       |.........|
01dd90eeedbb 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                       |.........|
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
 * -------------------------------------------------------------------------------------------------
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
 * 
01dd90eeedbb 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:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
 * -------------------------------------------------------------------------------------------------
01dd90eeedbb 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;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
 * 0
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
 * 1
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
 * 10
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
 * 250
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
 * 251
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
 * 252
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
 * 65535
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
 * 65536
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
 * 4294967295
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
 * 4294967296
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
 * 18446744073709551615
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
 * -------------------------------------------------------------------------------------------------
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
 * 
01dd90eeedbb 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
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
 * 
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
 */
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    82
class IntegerDataTypeWriter : public DataTypeWriter<integer_t> {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
private:
01dd90eeedbb 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;
01dd90eeedbb 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;
01dd90eeedbb 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;
01dd90eeedbb 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;
01dd90eeedbb 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;
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    90
	template<typename T> void write(std::ostream &output, const integer_t &value) {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		assert(sizeof (T) <= sizeof (value));
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		output.write(reinterpret_cast<const char *> (&value), sizeof (T));
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    95
	template<typename T> void write(std::ostream &output, const uint8_t type, const integer_t &value) {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
		write<uint8_t>(output, type);
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		write<T>(output, value);
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	template<typename T> bool fits(const integer_t &value) {
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		return value <= numeric_limits<T>::max();
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
public:
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   106
	IntegerDataTypeWriter() : DataTypeWriter<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   109
	void writeValue(std::ostream &output, const integer_t &value) override {
7
01dd90eeedbb 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
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
01dd90eeedbb 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);
01dd90eeedbb 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);
01dd90eeedbb 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);
01dd90eeedbb 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);
01dd90eeedbb 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);
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   117
		else throw RelpipeWriterException(L"Error while writing integer type: value too long");
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   120
	integer_t toValue(const string_t &stringValue) override {
7
01dd90eeedbb 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
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
		return stoul(stringValue);
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
	}
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
};
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
8
03750aff8619 writer only writes + refactoring
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   127
}
10
40ab091e5dfa move *DataTypeWriter.h to separate directory
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   128
}
7
01dd90eeedbb add data type and catalog classes from the prototype
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
}