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