# HG changeset patch # User František Kučera # Date 1532192677 -7200 # Node ID 40ab091e5dfa6113cc91effc020ed2fb74edd8da # Parent 0a40752e401d13bf3cdcccfec34caf7d05004cd4 move *DataTypeWriter.h to separate directory diff -r 0a40752e401d -r 40ab091e5dfa nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Jul 21 17:30:25 2018 +0200 +++ b/nbproject/configurations.xml Sat Jul 21 19:04:37 2018 +0200 @@ -4,14 +4,14 @@ - src/BooleanDataTypeWriter.h + src/types/BooleanDataTypeWriter.h src/DataTypeWriter.h src/DataTypeWriterBase.h - src/IntegerDataTypeWriter.h + src/types/IntegerDataTypeWriter.h include/RelationalWriter.h include/RelpipeWriterException.h src/StreamRelationalWriter.h - src/StringDataTypeWriter.h + src/types/StringDataTypeWriter.h src/format.h include/typedefs.h @@ -52,21 +52,21 @@ - - - - - + + + - + + + @@ -95,21 +95,21 @@ - - - - - + + + - + + + diff -r 0a40752e401d -r 40ab091e5dfa src/BooleanDataTypeWriter.h --- a/src/BooleanDataTypeWriter.h Sat Jul 21 17:30:25 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -#pragma once - -#include -#include - -#include "DataTypeWriter.h" -#include "../include/RelpipeWriterException.h" -#include "format.h" - -namespace relpipe { -namespace writer { - -class BooleanDataTypeWriter : public DataTypeWriter { -private: - const string_t TRUE = L"true"; - const string_t FALSE = L"false"; -public: - - BooleanDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) { - } - - void writeValue(std::ostream &output, const boolean_t &value) override { - output.put(value ? 1 : 0); - } - - bool toValue(const string_t &stringValue) override { - if (stringValue == TRUE) return true; - else if (stringValue == FALSE) return false; - else throw RelpipeWriterException(L"Unable to convert the string to boolean"); - } - -}; - -} -} \ No newline at end of file diff -r 0a40752e401d -r 40ab091e5dfa src/IntegerDataTypeWriter.h --- a/src/IntegerDataTypeWriter.h Sat Jul 21 17:30:25 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "DataTypeWriter.h" -#include "format.h" - -namespace relpipe { -namespace writer { - -/** - * The prototype does not have various integer and other numeric data types, - * it just works with one type of integer. - * 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). - * In the real implementation of relational pipes, there will be DataTypes for particular numeric types. - * - * TODO: support also big endian architectures. - * 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) - * - * Example of encoded values: - * ------------------------------------------------------------------------------------------------- - * $ 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 - * 0 = 00000000 00 |.| - * 1 = 00000000 01 |.| - * 10 = 00000000 0a |.| - * 250 = 00000000 fa |.| - * 251 = 00000000 fb fb |..| - * 252 = 00000000 fb fc |..| - * 65535 = 00000000 fc ff ff |...| - * 65536 = 00000000 fd 00 00 01 00 |.....| - * 4294967295 = 00000000 fd ff ff ff ff |.....| - * 4294967296 = 00000000 fe 00 00 00 00 01 00 00 00 |.........| - * 18446744073709551615 = 00000000 fe ff ff ff ff ff ff ff ff |.........| - * ------------------------------------------------------------------------------------------------- - * - * Example of decoded values: - * ------------------------------------------------------------------------------------------------- - * $ 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; - * 0 - * 1 - * 10 - * 250 - * 251 - * 252 - * 65535 - * 65536 - * 4294967295 - * 4294967296 - * 18446744073709551615 - * ------------------------------------------------------------------------------------------------- - * - * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets - * - */ -class IntegerDataTypeWriter : public DataTypeWriter { -private: - static const uint8_t INTEGER_TYPE_UINT8 = 251; - static const uint8_t INTEGER_TYPE_UINT16 = 252; - static const uint8_t INTEGER_TYPE_UINT32 = 253; - static const uint8_t INTEGER_TYPE_UINT64 = 254; - static const uint8_t INTEGER_TYPE_RESERVED = 255; - - template void write(std::ostream &output, const integer_t &value) { - assert(sizeof (T) <= sizeof (value)); - output.write(reinterpret_cast (&value), sizeof (T)); - } - - template void write(std::ostream &output, const uint8_t type, const integer_t &value) { - write(output, type); - write(output, value); - } - - template bool fits(const integer_t &value) { - return value <= numeric_limits::max(); - } - -public: - - IntegerDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) { - } - - void writeValue(std::ostream &output, const integer_t &value) override { - // output << value; // by zapsalo číslo jako ASII text - - if (value < INTEGER_TYPE_UINT8) write(output, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT8, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT16, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT32, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT64, value); - else throw RelpipeWriterException(L"Error while writing integer type: value too long"); - } - - integer_t toValue(const string_t &stringValue) override { - // throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number - return stoul(stringValue); - } - -}; - -} -} \ No newline at end of file diff -r 0a40752e401d -r 40ab091e5dfa src/StreamRelationalWriter.h --- a/src/StreamRelationalWriter.h Sat Jul 21 17:30:25 2018 +0200 +++ b/src/StreamRelationalWriter.h Sat Jul 21 19:04:37 2018 +0200 @@ -7,9 +7,9 @@ #include "../include/typedefs.h" #include "../include/RelationalWriter.h" #include "DataTypeWriterBase.h" -#include "BooleanDataTypeWriter.h" -#include "IntegerDataTypeWriter.h" -#include "StringDataTypeWriter.h" +#include "types/BooleanDataTypeWriter.h" +#include "types/IntegerDataTypeWriter.h" +#include "types/StringDataTypeWriter.h" namespace relpipe { namespace writer { @@ -17,9 +17,9 @@ class StreamRelationalWriter : public RelationalWriter { private: std::ostream &output; - BooleanDataTypeWriter booleanWriter; - IntegerDataTypeWriter integerWriter; - StringDataTypeWriter stringWriter; + types::BooleanDataTypeWriter booleanWriter; + types::IntegerDataTypeWriter integerWriter; + types::StringDataTypeWriter stringWriter; vector writers = {&booleanWriter, &integerWriter, &stringWriter}; void writeString(const string_t &stringValue, const integer_t typeId) { diff -r 0a40752e401d -r 40ab091e5dfa src/StringDataTypeWriter.h --- a/src/StringDataTypeWriter.h Sat Jul 21 17:30:25 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#include "DataTypeWriter.h" -#include "format.h" - -namespace relpipe { -namespace writer { - -/** - * The prototype does not recognize any encoding, - * it just works with c++ strings in encoding default to given platform. - * In the real implementation of relational pipes, there will be DataTypes for particular encodings. - */ -class StringDataTypeWriter : public DataTypeWriter { -private: - IntegerDataTypeWriter integerType; - std::wstring_convert> convertor; // TODO: support also other encodings. -public: - - StringDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) { - } - - void writeValue(std::ostream &output, const string_t &value) override { - std::string s = convertor.to_bytes(value); - integerType.writeValue(output, s.length()); - output << s.c_str(); - } - - wstring toValue(const string_t &stringValue) override { - return stringValue; - } - -}; - -} -} \ No newline at end of file diff -r 0a40752e401d -r 40ab091e5dfa src/types/BooleanDataTypeWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/types/BooleanDataTypeWriter.h Sat Jul 21 19:04:37 2018 +0200 @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +#include "../../include/RelpipeWriterException.h" +#include "../DataTypeWriter.h" +#include "../format.h" + +namespace relpipe { +namespace writer { +namespace types { + +using namespace relpipe::writer; + +class BooleanDataTypeWriter : public DataTypeWriter { +private: + const string_t TRUE = L"true"; + const string_t FALSE = L"false"; +public: + + BooleanDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) { + } + + void writeValue(std::ostream &output, const boolean_t &value) override { + output.put(value ? 1 : 0); + } + + bool toValue(const string_t &stringValue) override { + if (stringValue == TRUE) return true; + else if (stringValue == FALSE) return false; + else throw RelpipeWriterException(L"Unable to convert the string to boolean"); + } + +}; + +} +} +} \ No newline at end of file diff -r 0a40752e401d -r 40ab091e5dfa src/types/IntegerDataTypeWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/types/IntegerDataTypeWriter.h Sat Jul 21 19:04:37 2018 +0200 @@ -0,0 +1,108 @@ +#pragma once + +#include +#include +#include +#include + +#include "../DataTypeWriter.h" +#include "../format.h" + +namespace relpipe { +namespace writer { +namespace types { + +using namespace relpipe::writer; + +/** + * The prototype does not have various integer and other numeric data types, + * it just works with one type of integer. + * 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). + * In the real implementation of relational pipes, there will be DataTypes for particular numeric types. + * + * TODO: support also big endian architectures. + * 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) + * + * Example of encoded values: + * ------------------------------------------------------------------------------------------------- + * $ 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 + * 0 = 00000000 00 |.| + * 1 = 00000000 01 |.| + * 10 = 00000000 0a |.| + * 250 = 00000000 fa |.| + * 251 = 00000000 fb fb |..| + * 252 = 00000000 fb fc |..| + * 65535 = 00000000 fc ff ff |...| + * 65536 = 00000000 fd 00 00 01 00 |.....| + * 4294967295 = 00000000 fd ff ff ff ff |.....| + * 4294967296 = 00000000 fe 00 00 00 00 01 00 00 00 |.........| + * 18446744073709551615 = 00000000 fe ff ff ff ff ff ff ff ff |.........| + * ------------------------------------------------------------------------------------------------- + * + * Example of decoded values: + * ------------------------------------------------------------------------------------------------- + * $ 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; + * 0 + * 1 + * 10 + * 250 + * 251 + * 252 + * 65535 + * 65536 + * 4294967295 + * 4294967296 + * 18446744073709551615 + * ------------------------------------------------------------------------------------------------- + * + * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets + * + */ +class IntegerDataTypeWriter : public DataTypeWriter { +private: + static const uint8_t INTEGER_TYPE_UINT8 = 251; + static const uint8_t INTEGER_TYPE_UINT16 = 252; + static const uint8_t INTEGER_TYPE_UINT32 = 253; + static const uint8_t INTEGER_TYPE_UINT64 = 254; + static const uint8_t INTEGER_TYPE_RESERVED = 255; + + template void write(std::ostream &output, const integer_t &value) { + assert(sizeof (T) <= sizeof (value)); + output.write(reinterpret_cast (&value), sizeof (T)); + } + + template void write(std::ostream &output, const uint8_t type, const integer_t &value) { + write(output, type); + write(output, value); + } + + template bool fits(const integer_t &value) { + return value <= numeric_limits::max(); + } + +public: + + IntegerDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) { + } + + void writeValue(std::ostream &output, const integer_t &value) override { + // output << value; // by zapsalo číslo jako ASII text + + if (value < INTEGER_TYPE_UINT8) write(output, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT8, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT16, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT32, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT64, value); + else throw RelpipeWriterException(L"Error while writing integer type: value too long"); + } + + integer_t toValue(const string_t &stringValue) override { + // throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number + return stoul(stringValue); + } + +}; + +} +} +} \ No newline at end of file diff -r 0a40752e401d -r 40ab091e5dfa src/types/StringDataTypeWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/types/StringDataTypeWriter.h Sat Jul 21 19:04:37 2018 +0200 @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "../DataTypeWriter.h" +#include "../format.h" + +namespace relpipe { +namespace writer { +namespace types { + +using namespace relpipe::writer; +/** + * The prototype does not recognize any encoding, + * it just works with c++ strings in encoding default to given platform. + * In the real implementation of relational pipes, there will be DataTypes for particular encodings. + */ +class StringDataTypeWriter : public DataTypeWriter { +private: + IntegerDataTypeWriter integerType; + std::wstring_convert> convertor; // TODO: support also other encodings. +public: + + StringDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) { + } + + void writeValue(std::ostream &output, const string_t &value) override { + std::string s = convertor.to_bytes(value); + integerType.writeValue(output, s.length()); + output << s.c_str(); + } + + wstring toValue(const string_t &stringValue) override { + return stringValue; + } + +}; + +} +} +} \ No newline at end of file