src/types/IntegerDataTypeWriter.h
branchv_0
changeset 44 3f7b5f3f3f84
parent 41 744b61559eb2
child 45 27d5335cd924
equal deleted inserted replaced
43:5dd0bfb430a1 44:3f7b5f3f3f84
    34 
    34 
    35 using namespace relpipe::protocol;
    35 using namespace relpipe::protocol;
    36 using namespace relpipe::writer;
    36 using namespace relpipe::writer;
    37 
    37 
    38 /**
    38 /**
    39  * The prototype does not have various integer and other numeric data types,
    39  * Unsigned variable-length integer.
    40  * it just works with one type of integer.
    40  * ULEB128
    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).
       
    42  * In the real implementation of relational pipes, there will be DataTypes for particular numeric types.
       
    43  * 
       
    44  * TODO: support also big endian architectures.
       
    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)
       
    46  * 
       
    47  * Example of encoded values:
       
    48  * -------------------------------------------------------------------------------------------------
       
    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
       
    50  *                    0 = 00000000  00                                                |.|
       
    51  *                    1 = 00000000  01                                                |.|
       
    52  *                   10 = 00000000  0a                                                |.|
       
    53  *                  250 = 00000000  fa                                                |.|
       
    54  *                  251 = 00000000  fb fb                                             |..|
       
    55  *                  252 = 00000000  fb fc                                             |..|
       
    56  *                65535 = 00000000  fc ff ff                                          |...|
       
    57  *                65536 = 00000000  fd 00 00 01 00                                    |.....|
       
    58  *           4294967295 = 00000000  fd ff ff ff ff                                    |.....|
       
    59  *           4294967296 = 00000000  fe 00 00 00 00 01 00 00  00                       |.........|
       
    60  * 18446744073709551615 = 00000000  fe ff ff ff ff ff ff ff  ff                       |.........|
       
    61  * -------------------------------------------------------------------------------------------------
       
    62  * 
       
    63  * Example of decoded values:
       
    64  * -------------------------------------------------------------------------------------------------
       
    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;
       
    66  * 0
       
    67  * 1
       
    68  * 10
       
    69  * 250
       
    70  * 251
       
    71  * 252
       
    72  * 65535
       
    73  * 65536
       
    74  * 4294967295
       
    75  * 4294967296
       
    76  * 18446744073709551615
       
    77  * -------------------------------------------------------------------------------------------------
       
    78  * 
       
    79  * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets
       
    80  * 
       
    81  */
    41  */
    82 class IntegerDataTypeWriter : public DataTypeWriter<integer_t> {
    42 class IntegerDataTypeWriter : public DataTypeWriter<integer_t> {
    83 private:
       
    84 	static const uint8_t INTEGER_TYPE_UINT8 = 251;
       
    85 	static const uint8_t INTEGER_TYPE_UINT16 = 252;
       
    86 	static const uint8_t INTEGER_TYPE_UINT32 = 253;
       
    87 	static const uint8_t INTEGER_TYPE_UINT64 = 254;
       
    88 	static const uint8_t INTEGER_TYPE_RESERVED = 255;
       
    89 
       
    90 	template<typename T> void write(std::ostream &output, const integer_t &value) {
       
    91 		assert(sizeof (T) <= sizeof (value));
       
    92 		output.write(reinterpret_cast<const char *> (&value), sizeof (T));
       
    93 	}
       
    94 
       
    95 	template<typename T> void write(std::ostream &output, const uint8_t type, const integer_t &value) {
       
    96 		write<uint8_t>(output, type);
       
    97 		write<T>(output, value);
       
    98 	}
       
    99 
       
   100 	template<typename T> bool fits(const integer_t &value) {
       
   101 		return value <= numeric_limits<T>::max();
       
   102 	}
       
   103 
       
   104 public:
    43 public:
   105 
    44 
   106 	IntegerDataTypeWriter() : DataTypeWriter<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
    45 	IntegerDataTypeWriter() : DataTypeWriter<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
   107 	}
    46 	}
   108 
    47 
   109 	void writeValue(std::ostream &output, const integer_t &value) override {
    48 	void writeValue(std::ostream &output, const integer_t &value) override {
   110 		// output << value; // by zapsalo číslo jako ASII text
    49 		integer_t v = value;
   111 
    50 		do {
   112 		if (value < INTEGER_TYPE_UINT8) write<uint8_t>(output, value);
    51 			octet_t octet = v & 0x7F;
   113 		else if (fits<uint8_t>(value)) write<uint8_t>(output, INTEGER_TYPE_UINT8, value);
    52 			v >>= 7;
   114 		else if (fits<uint16_t>(value)) write<uint16_t>(output, INTEGER_TYPE_UINT16, value);
    53 			if (v) octet |= 0x80; // more bytes follow
   115 		else if (fits<uint32_t>(value)) write<uint32_t>(output, INTEGER_TYPE_UINT32, value);
    54 			output << char(octet);
   116 		else if (fits<uint64_t>(value)) write<uint64_t>(output, INTEGER_TYPE_UINT64, value);
    55 		} while (v);
   117 		else throw RelpipeWriterException(L"Error while writing integer type: value too long");
       
   118 	}
    56 	}
   119 
    57 
   120 	integer_t toValue(const string_t &stringValue) override {
    58 	integer_t toValue(const string_t &stringValue) override {
   121 		// throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number
    59 		// throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number
   122 		return stoul(stringValue);
    60 		return stoul(stringValue);