src/IntegerDataTypeReader.h
branchv_0
changeset 8 c87e9c84f7aa
parent 7 489e52138771
child 9 517888868e55
equal deleted inserted replaced
7:489e52138771 8:c87e9c84f7aa
       
     1 #pragma once
       
     2 
       
     3 #include <string>
       
     4 #include <iostream>
       
     5 #include <cassert>
       
     6 #include <limits>
       
     7 
       
     8 #include "../include/typedefs.h"
       
     9 #include "../include/DataTypeReader.h"
       
    10 #include "../include/DataTypeReader.h"
       
    11 #include "format.h"
       
    12 
       
    13 namespace relpipe {
       
    14 namespace reader {
       
    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 IntegerDataTypeReader : public DataTypeReader<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(std::istream &input) {
       
    69 		T value = 0;
       
    70 		input.read(reinterpret_cast<char *> (&value), sizeof (value));
       
    71 		return value;
       
    72 	}
       
    73 
       
    74 public:
       
    75 
       
    76 	IntegerDataTypeReader() : DataTypeReader<integer_t>(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) {
       
    77 	}
       
    78 
       
    79 	integer_t readValue(std::istream &input) override {
       
    80 		uint8_t first;
       
    81 		input.read(reinterpret_cast<char *> (&first), sizeof (first));
       
    82 		if (input.good()) {
       
    83 
       
    84 			if (first < INTEGER_TYPE_UINT8) return first;
       
    85 			else if (first == INTEGER_TYPE_UINT8) return read<uint8_t>(input);
       
    86 			else if (first == INTEGER_TYPE_UINT16) return read<uint16_t>(input);
       
    87 			else if (first == INTEGER_TYPE_UINT32) return read<uint32_t>(input);
       
    88 			else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input);
       
    89 			else throw RelpipeReaderException(L"Error while parsing integer type: unsupported type");
       
    90 		} else {
       
    91 			throw RelpipeReaderException(L"Error while reading integer from the stream.");
       
    92 		}
       
    93 	}
       
    94 
       
    95 	string_t toString(const integer_t &value) override {
       
    96 		return std::to_wstring(value);
       
    97 	}
       
    98 
       
    99 };
       
   100 
       
   101 }
       
   102 }