src/IntegerDataType.h
branchv_0
changeset 7 489e52138771
equal deleted inserted replaced
6:e4543a40a85f 7:489e52138771
       
     1 #pragma once
       
     2 
       
     3 #include <string>
       
     4 #include <iostream>
       
     5 #include <cassert>
       
     6 #include <limits>
       
     7 
       
     8 #include "common.h"
       
     9 #include "DataType.h"
       
    10 #include "RelpipeException.h"
       
    11 
       
    12 using namespace std;
       
    13 
       
    14 namespace rp_prototype {
       
    15 
       
    16 /**
       
    17  * The prototype does not have various integer and other numeric data types,
       
    18  * it just works with one type of integer.
       
    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).
       
    20  * In the real implementation of relational pipes, there will be DataTypes for particular numeric types.
       
    21  * 
       
    22  * TODO: support also big endian architectures.
       
    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)
       
    24  * 
       
    25  * Example of encoded values:
       
    26  * -------------------------------------------------------------------------------------------------
       
    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
       
    28  *                    0 = 00000000  00                                                |.|
       
    29  *                    1 = 00000000  01                                                |.|
       
    30  *                   10 = 00000000  0a                                                |.|
       
    31  *                  250 = 00000000  fa                                                |.|
       
    32  *                  251 = 00000000  fb fb                                             |..|
       
    33  *                  252 = 00000000  fb fc                                             |..|
       
    34  *                65535 = 00000000  fc ff ff                                          |...|
       
    35  *                65536 = 00000000  fd 00 00 01 00                                    |.....|
       
    36  *           4294967295 = 00000000  fd ff ff ff ff                                    |.....|
       
    37  *           4294967296 = 00000000  fe 00 00 00 00 01 00 00  00                       |.........|
       
    38  * 18446744073709551615 = 00000000  fe ff ff ff ff ff ff ff  ff                       |.........|
       
    39  * -------------------------------------------------------------------------------------------------
       
    40  * 
       
    41  * Example of decoded values:
       
    42  * -------------------------------------------------------------------------------------------------
       
    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;
       
    44  * 0
       
    45  * 1
       
    46  * 10
       
    47  * 250
       
    48  * 251
       
    49  * 252
       
    50  * 65535
       
    51  * 65536
       
    52  * 4294967295
       
    53  * 4294967296
       
    54  * 18446744073709551615
       
    55  * -------------------------------------------------------------------------------------------------
       
    56  * 
       
    57  * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets
       
    58  * 
       
    59  */
       
    60 class IntegerDataType : public DataType<integer_t> {
       
    61 private:
       
    62 	static const uint8_t INTEGER_TYPE_UINT8 = 251;
       
    63 	static const uint8_t INTEGER_TYPE_UINT16 = 252;
       
    64 	static const uint8_t INTEGER_TYPE_UINT32 = 253;
       
    65 	static const uint8_t INTEGER_TYPE_UINT64 = 254;
       
    66 	static const uint8_t INTEGER_TYPE_RESERVED = 255;
       
    67 
       
    68 	template<typename T> integer_t read(istream &input) {
       
    69 		T value = 0;
       
    70 		input.read(reinterpret_cast<char *> (&value), sizeof (value));
       
    71 		return value;
       
    72 	}
       
    73 
       
    74 	template<typename T> void write(ostream &output, const integer_t &value) {
       
    75 		assert(sizeof (T) <= sizeof (value));
       
    76 		output.write(reinterpret_cast<const char *> (&value), sizeof (T));
       
    77 	}
       
    78 
       
    79 	template<typename T> void write(ostream &output, const uint8_t type, const integer_t &value) {
       
    80 		write<uint8_t>(output, type);
       
    81 		write<T>(output, value);
       
    82 	}
       
    83 
       
    84 	template<typename T> bool fits(const integer_t &value) {
       
    85 		return value <= numeric_limits<T>::max();
       
    86 	}
       
    87 
       
    88 public:
       
    89 
       
    90 	IntegerDataType() : DataType<integer_t>(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) {
       
    91 	}
       
    92 
       
    93 	integer_t readValue(istream &input) override {
       
    94 		uint8_t first;
       
    95 		input.read(reinterpret_cast<char *> (&first), sizeof (first));
       
    96 		if (input.good()) {
       
    97 
       
    98 			if (first < INTEGER_TYPE_UINT8) return first;
       
    99 			else if (first == INTEGER_TYPE_UINT8) return read<uint8_t>(input);
       
   100 			else if (first == INTEGER_TYPE_UINT16) return read<uint16_t>(input);
       
   101 			else if (first == INTEGER_TYPE_UINT32) return read<uint32_t>(input);
       
   102 			else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input);
       
   103 			else throw RelpipeException(L"Error while parsing integer type: unsupported type", EXIT_CODE_DATA_ERROR);
       
   104 		} else {
       
   105 			throw RelpipeException(L"Error while reading integer from the stream.", EXIT_CODE_DATA_ERROR);
       
   106 		}
       
   107 	}
       
   108 
       
   109 	void writeValue(ostream &output, const integer_t &value) override {
       
   110 		// output << value; // by zapsalo číslo jako ASII text
       
   111 
       
   112 		if (value < INTEGER_TYPE_UINT8) write<uint8_t>(output, value);
       
   113 		else if (fits<uint8_t>(value)) write<uint8_t>(output, INTEGER_TYPE_UINT8, value);
       
   114 		else if (fits<uint16_t>(value)) write<uint16_t>(output, INTEGER_TYPE_UINT16, value);
       
   115 		else if (fits<uint32_t>(value)) write<uint32_t>(output, INTEGER_TYPE_UINT32, value);
       
   116 		else if (fits<uint64_t>(value)) write<uint64_t>(output, INTEGER_TYPE_UINT64, value);
       
   117 		else throw RelpipeException(L"Error while writing integer type: value too long", EXIT_CODE_DATA_ERROR);
       
   118 	}
       
   119 
       
   120 	integer_t toValue(const wstring &stringValue) override {
       
   121 		// throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number
       
   122 		return stoul(stringValue);
       
   123 	}
       
   124 
       
   125 	wstring toString(const integer_t &value) override {
       
   126 		return to_wstring(value);
       
   127 	}
       
   128 
       
   129 };
       
   130 
       
   131 }