refactoring, move files, use TypeId instead of integer_t v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 25 Aug 2018 18:16:53 +0200
branchv_0
changeset 14 e8de089f95dd
parent 13 543f1613c2da
child 15 09ca53671b28
refactoring, move files, use TypeId instead of integer_t
include/relpipe/reader/Factory.h
include/relpipe/reader/RelationalReader.h
nbproject/Makefile-Debug.mk
nbproject/Makefile-Release.mk
nbproject/configurations.xml
src/BooleanDataTypeReader.h
src/DataTypeReader.h
src/DataTypeReaderBase.cpp
src/DataTypeReaderBase.h
src/Factory.cpp
src/IntegerDataTypeReader.h
src/StreamRelationalReader.h
src/StringDataTypeReader.h
src/types/BooleanDataTypeReader.h
src/types/IntegerDataTypeReader.h
src/types/StringDataTypeReader.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/relpipe/reader/Factory.h	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,14 @@
+#pragma once
+
+#include <iostream>
+
+namespace relpipe {
+namespace reader {
+
+class Factory {
+public:
+	static RelationalReader* create(std::istream &input);
+};
+
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/relpipe/reader/RelationalReader.h	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <iostream>
+
+#include "typedefs.h"
+#include "TypeId.h"
+
+namespace relpipe {
+namespace reader {
+
+class RelationalReader {
+public:
+
+	virtual ~RelationalReader() = default;
+
+	/**
+	 * @param typeId numeric type id as defined in Specification
+	 * @return string code of given type
+	 * @throws RelpipeReaderException on unsupported typeId
+	 */
+	virtual string_t toTypeCode(const TypeId typeId) = 0;
+
+};
+
+}
+}
--- a/nbproject/Makefile-Debug.mk	Sat Aug 25 17:39:27 2018 +0200
+++ b/nbproject/Makefile-Debug.mk	Sat Aug 25 18:16:53 2018 +0200
@@ -36,7 +36,7 @@
 # Object Files
 OBJECTFILES= \
 	${OBJECTDIR}/src/DataTypeReader.o \
-	${OBJECTDIR}/src/DataTypeReaderBase.o
+	${OBJECTDIR}/src/Factory.o
 
 
 # C Compiler Flags
@@ -53,7 +53,7 @@
 ASFLAGS=
 
 # Link Libraries and Options
-LDLIBSOPTIONS=
+LDLIBSOPTIONS=`pkg-config --libs relpipe-lib-protocol.cpp`  
 
 # Build Targets
 .build-conf: ${BUILD_SUBPROJECTS}
@@ -66,12 +66,12 @@
 ${OBJECTDIR}/src/DataTypeReader.o: src/DataTypeReader.cpp
 	${MKDIR} -p ${OBJECTDIR}/src
 	${RM} "$@.d"
-	$(COMPILE.cc) -g -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/DataTypeReader.o src/DataTypeReader.cpp
+	$(COMPILE.cc) -g `pkg-config --cflags relpipe-lib-protocol.cpp`   -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/DataTypeReader.o src/DataTypeReader.cpp
 
-${OBJECTDIR}/src/DataTypeReaderBase.o: src/DataTypeReaderBase.cpp
+${OBJECTDIR}/src/Factory.o: src/Factory.cpp
 	${MKDIR} -p ${OBJECTDIR}/src
 	${RM} "$@.d"
-	$(COMPILE.cc) -g -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/DataTypeReaderBase.o src/DataTypeReaderBase.cpp
+	$(COMPILE.cc) -g `pkg-config --cflags relpipe-lib-protocol.cpp`   -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/Factory.o src/Factory.cpp
 
 # Subprojects
 .build-subprojects:
--- a/nbproject/Makefile-Release.mk	Sat Aug 25 17:39:27 2018 +0200
+++ b/nbproject/Makefile-Release.mk	Sat Aug 25 18:16:53 2018 +0200
@@ -36,7 +36,7 @@
 # Object Files
 OBJECTFILES= \
 	${OBJECTDIR}/src/DataTypeReader.o \
-	${OBJECTDIR}/src/DataTypeReaderBase.o
+	${OBJECTDIR}/src/Factory.o
 
 
 # C Compiler Flags
@@ -53,7 +53,7 @@
 ASFLAGS=
 
 # Link Libraries and Options
-LDLIBSOPTIONS=
+LDLIBSOPTIONS=`pkg-config --libs relpipe-lib-protocol.cpp`  
 
 # Build Targets
 .build-conf: ${BUILD_SUBPROJECTS}
@@ -66,12 +66,12 @@
 ${OBJECTDIR}/src/DataTypeReader.o: src/DataTypeReader.cpp
 	${MKDIR} -p ${OBJECTDIR}/src
 	${RM} "$@.d"
-	$(COMPILE.cc) -O2 -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/DataTypeReader.o src/DataTypeReader.cpp
+	$(COMPILE.cc) -O2 `pkg-config --cflags relpipe-lib-protocol.cpp`   -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/DataTypeReader.o src/DataTypeReader.cpp
 
-${OBJECTDIR}/src/DataTypeReaderBase.o: src/DataTypeReaderBase.cpp
+${OBJECTDIR}/src/Factory.o: src/Factory.cpp
 	${MKDIR} -p ${OBJECTDIR}/src
 	${RM} "$@.d"
-	$(COMPILE.cc) -O2 -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/DataTypeReaderBase.o src/DataTypeReaderBase.cpp
+	$(COMPILE.cc) -O2 `pkg-config --cflags relpipe-lib-protocol.cpp`   -fPIC  -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/Factory.o src/Factory.cpp
 
 # Subprojects
 .build-subprojects:
--- a/nbproject/configurations.xml	Sat Aug 25 17:39:27 2018 +0200
+++ b/nbproject/configurations.xml	Sat Aug 25 18:16:53 2018 +0200
@@ -4,13 +4,16 @@
     <logicalFolder name="HeaderFiles"
                    displayName="Header Files"
                    projectFiles="true">
-      <itemPath>src/BooleanDataTypeReader.h</itemPath>
+      <itemPath>src/types/BooleanDataTypeReader.h</itemPath>
       <itemPath>src/DataTypeReader.h</itemPath>
       <itemPath>src/DataTypeReaderBase.h</itemPath>
       <itemPath>src/DataTypeReaderCatalog.h</itemPath>
-      <itemPath>src/IntegerDataTypeReader.h</itemPath>
-      <itemPath>src/RelpipeReaderException.h</itemPath>
-      <itemPath>src/StringDataTypeReader.h</itemPath>
+      <itemPath>include/relpipe/reader/Factory.h</itemPath>
+      <itemPath>src/types/IntegerDataTypeReader.h</itemPath>
+      <itemPath>include/relpipe/reader/RelationalReader.h</itemPath>
+      <itemPath>include/relpipe/reader/RelpipeReaderException.h</itemPath>
+      <itemPath>src/StreamRelationalReader.h</itemPath>
+      <itemPath>src/types/StringDataTypeReader.h</itemPath>
       <itemPath>include/relpipe/reader/TypeId.h</itemPath>
       <itemPath>include/relpipe/reader/typedefs.h</itemPath>
     </logicalFolder>
@@ -22,7 +25,7 @@
                    displayName="Source Files"
                    projectFiles="true">
       <itemPath>src/DataTypeReader.cpp</itemPath>
-      <itemPath>src/DataTypeReaderBase.cpp</itemPath>
+      <itemPath>src/Factory.cpp</itemPath>
     </logicalFolder>
     <logicalFolder name="TestFiles"
                    displayName="Test Files"
@@ -48,28 +51,45 @@
         <ccTool>
           <commandLine>-fsanitize=address</commandLine>
         </ccTool>
+        <linkerTool>
+          <linkerLibItems>
+            <linkerOptionItem>`pkg-config --libs relpipe-lib-protocol.cpp`</linkerOptionItem>
+          </linkerLibItems>
+        </linkerTool>
       </compileType>
+      <item path="include/relpipe/reader/Factory.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="include/relpipe/reader/RelationalReader.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
+      <item path="include/relpipe/reader/RelpipeReaderException.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
       <item path="include/relpipe/reader/TypeId.h" ex="false" tool="3" flavor2="0">
       </item>
       <item path="include/relpipe/reader/typedefs.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/BooleanDataTypeReader.h" ex="false" tool="3" flavor2="0">
-      </item>
       <item path="src/DataTypeReader.cpp" ex="false" tool="1" flavor2="0">
       </item>
       <item path="src/DataTypeReader.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/DataTypeReaderBase.cpp" ex="false" tool="1" flavor2="0">
-      </item>
       <item path="src/DataTypeReaderBase.h" ex="false" tool="3" flavor2="0">
       </item>
       <item path="src/DataTypeReaderCatalog.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/IntegerDataTypeReader.h" ex="false" tool="3" flavor2="0">
+      <item path="src/Factory.cpp" ex="false" tool="1" flavor2="0">
+      </item>
+      <item path="src/StreamRelationalReader.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/RelpipeReaderException.h" ex="false" tool="3" flavor2="0">
+      <item path="src/types/BooleanDataTypeReader.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/StringDataTypeReader.h" ex="false" tool="3" flavor2="0">
+      <item path="src/types/IntegerDataTypeReader.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/types/StringDataTypeReader.h" ex="false" tool="3" flavor2="0">
       </item>
     </conf>
     <conf name="Release" type="2">
@@ -91,28 +111,45 @@
         <asmTool>
           <developmentMode>5</developmentMode>
         </asmTool>
+        <linkerTool>
+          <linkerLibItems>
+            <linkerOptionItem>`pkg-config --libs relpipe-lib-protocol.cpp`</linkerOptionItem>
+          </linkerLibItems>
+        </linkerTool>
       </compileType>
+      <item path="include/relpipe/reader/Factory.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="include/relpipe/reader/RelationalReader.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
+      <item path="include/relpipe/reader/RelpipeReaderException.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
       <item path="include/relpipe/reader/TypeId.h" ex="false" tool="3" flavor2="0">
       </item>
       <item path="include/relpipe/reader/typedefs.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/BooleanDataTypeReader.h" ex="false" tool="3" flavor2="0">
-      </item>
       <item path="src/DataTypeReader.cpp" ex="false" tool="1" flavor2="0">
       </item>
       <item path="src/DataTypeReader.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/DataTypeReaderBase.cpp" ex="false" tool="1" flavor2="0">
-      </item>
       <item path="src/DataTypeReaderBase.h" ex="false" tool="3" flavor2="0">
       </item>
       <item path="src/DataTypeReaderCatalog.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/IntegerDataTypeReader.h" ex="false" tool="3" flavor2="0">
+      <item path="src/Factory.cpp" ex="false" tool="1" flavor2="0">
+      </item>
+      <item path="src/StreamRelationalReader.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/RelpipeReaderException.h" ex="false" tool="3" flavor2="0">
+      <item path="src/types/BooleanDataTypeReader.h" ex="false" tool="3" flavor2="0">
       </item>
-      <item path="src/StringDataTypeReader.h" ex="false" tool="3" flavor2="0">
+      <item path="src/types/IntegerDataTypeReader.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/types/StringDataTypeReader.h" ex="false" tool="3" flavor2="0">
       </item>
     </conf>
   </confs>
--- a/src/BooleanDataTypeReader.h	Sat Aug 25 17:39:27 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-#pragma once
-
-#include <string>
-#include <iostream>
-
-#include <relpipe/protocol/constants.h>
-
-#include "../include/typedefs.h"
-#include "../include/DataTypeReader.h"
-
-namespace relpipe {
-namespace reader {
-
-class BooleanDataTypeReader : public DataTypeReader<boolean_t> {
-private:
-	const string_t TRUE = L"true";
-	const string_t FALSE = L"false";
-public:
-
-	BooleanDataTypeReader() : DataTypeReader<boolean_t>(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
-	}
-
-	bool readValue(std::istream &input) override {
-		auto value = input.get(); // TODO: check failbit
-		if (value == 0) return false;
-		else if (value == 1) return true;
-		else throw RelpipeReaderException(L"Unable to convert the octet to boolean");
-	}
-
-	string_t toString(const boolean_t &value) override {
-		return value ? TRUE : FALSE;
-	}
-
-};
-
-}
-}
\ No newline at end of file
--- a/src/DataTypeReader.h	Sat Aug 25 17:39:27 2018 +0200
+++ b/src/DataTypeReader.h	Sat Aug 25 18:16:53 2018 +0200
@@ -8,7 +8,7 @@
 template<typename T> class DataTypeReader : public DataTypeReaderBase {
 public:
 
-	DataTypeReader(const integer_t typeId, const string_t typeCode) : DataTypeReaderBase(typeId, typeCode) {
+	DataTypeReader(const TypeId typeId, const string_t typeCode) : DataTypeReaderBase(typeId, typeCode) {
 	}
 
 	virtual ~DataTypeReader() {
--- a/src/DataTypeReaderBase.cpp	Sat Aug 25 17:39:27 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-#include <string>
-#include "DataTypeReaderBase.h"
-
-namespace relpipe {
-namespace reader {
-
-bool DataTypeReaderBase::supports(const integer_t &dataType) {
-	return dataType == typeId;
-}
-
-bool DataTypeReaderBase::supports(const string_t &dataType) {
-	return dataType == typeCode;
-}
-
-integer_t DataTypeReaderBase::getTypeId() {
-	return typeId;
-}
-
-string_t DataTypeReaderBase::getTypeCode() {
-	return typeCode;
-}
-
-}
-}
--- a/src/DataTypeReaderBase.h	Sat Aug 25 17:39:27 2018 +0200
+++ b/src/DataTypeReaderBase.h	Sat Aug 25 18:16:53 2018 +0200
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <string>
+#include "../include/relpipe/reader/TypeId.h"
 #include "../include/relpipe/reader/typedefs.h"
 
 namespace relpipe {
@@ -11,11 +12,11 @@
  */
 class DataTypeReaderBase {
 private:
-	const integer_t typeId;
+	const TypeId typeId;
 	const string_t typeCode;
 public:
 
-	DataTypeReaderBase(const integer_t typeId, const string_t typeCode) :
+	DataTypeReaderBase(const TypeId typeId, const string_t typeCode) :
 	typeId(typeId), typeCode(typeCode) {
 	}
 
@@ -35,17 +36,25 @@
 	 * @param dataType data type code as defined in DDP L0
 	 * @return whether this class supports conversions of this type
 	 */
-	virtual bool supports(const integer_t &dataType);
+	virtual bool supports(const TypeId &dataType) {
+		return dataType == typeId;
+	}
 
 	/**
 	 * @param dataType data type name as defined in DDP L0
 	 * @return whether this class supports conversions of this type
 	 */
-	virtual bool supports(const string_t &dataType);
+	virtual bool supports(const string_t &dataType) {
+		return dataType == typeCode;
+	}
 
-	integer_t getTypeId();
+	TypeId getTypeId() {
+		return typeId;
+	}
 
-	string_t getTypeCode();
+	string_t getTypeCode() {
+		return typeCode;
+	}
 };
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Factory.cpp	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,31 @@
+#include <relpipe/protocol/constants.h>
+
+#include "../include/relpipe/reader/TypeId.h"
+#include "../include/relpipe/reader/RelationalReader.h"
+#include "../include/relpipe/reader/Factory.h"
+#include "StreamRelationalReader.h"
+
+namespace relpipe {
+namespace reader {
+
+RelationalReader* Factory::create(std::istream& input) {
+	return new StreamRelationalReader(input);
+}
+
+/**
+ * Some basic constants defined in lib-protocol are re-exported in public headers of lib-reader and lib-reader,
+ * so we should check (during compile-time) that they match.
+ */
+#define PROTOCOL_VIOLATION_TYPE_ID_ERROR "type id does not match with protocol"
+static_assert((int) TypeId::BOOLEAN == relpipe::protocol::DATA_TYPE_ID_BOOLEAN, PROTOCOL_VIOLATION_TYPE_ID_ERROR);
+static_assert((int) TypeId::STRING == relpipe::protocol::DATA_TYPE_ID_STRING, PROTOCOL_VIOLATION_TYPE_ID_ERROR);
+static_assert((int) TypeId::INTEGER == relpipe::protocol::DATA_TYPE_ID_INTEGER, PROTOCOL_VIOLATION_TYPE_ID_ERROR);
+
+#define PROTOCOL_VIOLATION_TYPE_DEF_ERROR "type definition does not match with protocol"
+static_assert(std::is_same<relpipe::reader::boolean_t, typename relpipe::protocol::boolean_t>::value, PROTOCOL_VIOLATION_TYPE_DEF_ERROR);
+static_assert(std::is_same<relpipe::reader::integer_t, typename relpipe::protocol::integer_t>::value, PROTOCOL_VIOLATION_TYPE_DEF_ERROR);
+static_assert(std::is_same<relpipe::reader::string_t, typename relpipe::protocol::string_t>::value, PROTOCOL_VIOLATION_TYPE_DEF_ERROR);
+static_assert(std::is_same<relpipe::reader::octet_t, typename relpipe::protocol::octet_t>::value, PROTOCOL_VIOLATION_TYPE_DEF_ERROR);
+
+}
+}
\ No newline at end of file
--- a/src/IntegerDataTypeReader.h	Sat Aug 25 17:39:27 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,102 +0,0 @@
-#pragma once
-
-#include <string>
-#include <iostream>
-#include <cassert>
-#include <limits>
-
-#include <relpipe/protocol/constants.h>
-
-#include "../include/typedefs.h"
-#include "../include/DataTypeReader.h"
-
-namespace relpipe {
-namespace reader {
-
-/**
- * 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 IntegerDataTypeReader : public DataTypeReader<integer_t> {
-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<typename T> integer_t read(std::istream &input) {
-		T value = 0;
-		input.read(reinterpret_cast<char *> (&value), sizeof (value));
-		return value;
-	}
-
-public:
-
-	IntegerDataTypeReader() : DataTypeReader<integer_t>(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) {
-	}
-
-	integer_t readValue(std::istream &input) override {
-		uint8_t first;
-		input.read(reinterpret_cast<char *> (&first), sizeof (first));
-		if (input.good()) {
-
-			if (first < INTEGER_TYPE_UINT8) return first;
-			else if (first == INTEGER_TYPE_UINT8) return read<uint8_t>(input);
-			else if (first == INTEGER_TYPE_UINT16) return read<uint16_t>(input);
-			else if (first == INTEGER_TYPE_UINT32) return read<uint32_t>(input);
-			else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input);
-			else throw RelpipeReaderException(L"Error while parsing integer type: unsupported type");
-		} else {
-			throw RelpipeReaderException(L"Error while reading integer from the stream.");
-		}
-	}
-
-	string_t toString(const integer_t &value) override {
-		return std::to_wstring(value);
-	}
-
-};
-
-}
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/StreamRelationalReader.h	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,58 @@
+#pragma once
+
+#include <string>
+#include <iostream>
+#include <vector>
+
+#include <relpipe/protocol/constants.h>
+
+#include "../include/relpipe/reader/typedefs.h"
+#include "../include/relpipe/reader/RelationalReader.h"
+#include "../include/relpipe/reader/TypeId.h"
+#include "DataTypeReaderBase.h"
+#include "types/BooleanDataTypeReader.h"
+#include "types/IntegerDataTypeReader.h"
+#include "types/StringDataTypeReader.h"
+
+namespace relpipe {
+namespace reader {
+
+using namespace relpipe::protocol;
+
+class StreamRelationalReader : public RelationalReader {
+private:
+	std::istream &input;
+	types::BooleanDataTypeReader booleanReader;
+	types::IntegerDataTypeReader integerReader;
+	types::StringDataTypeReader stringReader;
+	std::vector<DataTypeReaderBase*> readers = {&booleanReader, &integerReader, &stringReader};
+
+	/**
+	 * count of columns in the current table
+	 */
+	integer_t columnCount;
+	/**
+	 * number of column (0 = first) that will be written; after writing, the number is increased and prepared for next one
+	 */
+	integer_t currentColumn;
+
+	/**
+	 * types of columns in the current table
+	 */
+	std::vector<TypeId> columnTypes;
+
+public:
+
+	StreamRelationalReader(std::istream &input) :
+	input(input) {
+	}
+
+	string_t toTypeCode(const TypeId typeId) override {
+		for (DataTypeReaderBase* reader : readers) if (reader->supports(typeId)) return reader->getTypeCode();
+		throw RelpipeReaderException(L"Unsupported data type: " + static_cast<integer_t> (typeId));
+	}
+
+};
+
+}
+}
\ No newline at end of file
--- a/src/StringDataTypeReader.h	Sat Aug 25 17:39:27 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-#pragma once
-
-#include <string>
-#include <sstream>
-#include <iostream>
-#include <vector>
-#include <locale>
-#include <codecvt>
-
-#include <relpipe/protocol/constants.h>
-
-#include "../include/typedefs.h"
-#include "../include/DataTypeReader.h"
-
-namespace relpipe {
-namespace reader {
-
-/**
- * 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 StringDataTypeReader : public DataTypeReader<string_t> {
-private:
-	IntegerDataTypeReader integerType;
-	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
-public:
-
-	StringDataTypeReader() : DataTypeReader<string_t>(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) {
-	}
-
-	string_t readValue(std::istream &input) override {
-		integer_t length = integerType.readValue(input);
-		// TODO: check maximum length of single field
-		// if (length > 4000) throw RelpipeException("data too long", EXIT_CODE_DATA_ERROR);
-		std::vector<char> buf(length);
-		input.read(buf.data(), length);
-		if (input.good()) {
-			return convertor.from_bytes(std::string(buf.data(), length));
-		} else {
-			throw RelpipeReaderException(L"Error while reading string from the stream.");
-		}
-	}
-
-	string_t toString(const string_t &value) override {
-		return value;
-	}
-
-};
-
-}
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/types/BooleanDataTypeReader.h	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <string>
+#include <iostream>
+
+#include <relpipe/protocol/constants.h>
+
+#include "../../include/relpipe/reader/RelpipeReaderException.h"
+#include "../../include/relpipe/reader/typedefs.h"
+#include "../DataTypeReader.h"
+
+namespace relpipe {
+namespace reader {
+namespace types {
+
+using namespace relpipe::protocol;
+using namespace relpipe::reader;
+
+class BooleanDataTypeReader : public DataTypeReader<boolean_t> {
+private:
+	const string_t TRUE = L"true";
+	const string_t FALSE = L"false";
+public:
+
+	BooleanDataTypeReader() : DataTypeReader<boolean_t>(TypeId::BOOLEAN, DATA_TYPE_CODE_BOOLEAN) {
+	}
+
+	bool readValue(std::istream &input) override {
+		auto value = input.get(); // TODO: check failbit
+		if (value == 0) return false;
+		else if (value == 1) return true;
+		else throw RelpipeReaderException(L"Unable to convert the octet to boolean");
+	}
+
+	string_t toString(const boolean_t &value) override {
+		return value ? TRUE : FALSE;
+	}
+
+};
+
+}
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/types/IntegerDataTypeReader.h	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,108 @@
+#pragma once
+
+#include <string>
+#include <iostream>
+#include <cassert>
+#include <limits>
+
+#include <relpipe/protocol/constants.h>
+
+#include "../../include/relpipe/reader/RelpipeReaderException.h"
+#include "../../include/relpipe/reader/typedefs.h"
+#include "../DataTypeReader.h"
+
+namespace relpipe {
+namespace reader {
+namespace types {
+
+using namespace relpipe::protocol;
+using namespace relpipe::reader;
+
+/**
+ * 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 IntegerDataTypeReader : public DataTypeReader<integer_t> {
+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<typename T> integer_t read(std::istream &input) {
+		T value = 0;
+		input.read(reinterpret_cast<char *> (&value), sizeof (value));
+		return value;
+	}
+
+public:
+
+	IntegerDataTypeReader() : DataTypeReader<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
+	}
+
+	integer_t readValue(std::istream &input) override {
+		uint8_t first;
+		input.read(reinterpret_cast<char *> (&first), sizeof (first));
+		if (input.good()) {
+
+			if (first < INTEGER_TYPE_UINT8) return first;
+			else if (first == INTEGER_TYPE_UINT8) return read<uint8_t>(input);
+			else if (first == INTEGER_TYPE_UINT16) return read<uint16_t>(input);
+			else if (first == INTEGER_TYPE_UINT32) return read<uint32_t>(input);
+			else if (first == INTEGER_TYPE_UINT64) return read<uint64_t>(input);
+			else throw RelpipeReaderException(L"Error while parsing integer type: unsupported type");
+		} else {
+			throw RelpipeReaderException(L"Error while reading integer from the stream.");
+		}
+	}
+
+	string_t toString(const integer_t &value) override {
+		return std::to_wstring(value);
+	}
+
+};
+
+}
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/types/StringDataTypeReader.h	Sat Aug 25 18:16:53 2018 +0200
@@ -0,0 +1,58 @@
+#pragma once
+
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <vector>
+#include <locale>
+#include <codecvt>
+
+#include <relpipe/protocol/constants.h>
+
+#include "../../include/relpipe/reader/RelpipeReaderException.h"
+#include "../../include/relpipe/reader/typedefs.h"
+#include "../DataTypeReader.h"
+
+namespace relpipe {
+namespace reader {
+namespace types {
+
+using namespace relpipe::protocol;
+using namespace relpipe::reader;
+
+/**
+ * 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 StringDataTypeReader : public DataTypeReader<string_t> {
+private:
+	IntegerDataTypeReader integerType;
+	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+public:
+
+	StringDataTypeReader() : DataTypeReader<string_t>(TypeId::STRING, DATA_TYPE_CODE_STRING) {
+	}
+
+	string_t readValue(std::istream &input) override {
+		integer_t length = integerType.readValue(input);
+		// TODO: check maximum length of single field
+		// if (length > 4000) throw RelpipeException("data too long", EXIT_CODE_DATA_ERROR);
+		std::vector<char> buf(length);
+		input.read(buf.data(), length);
+		if (input.good()) {
+			return convertor.from_bytes(std::string(buf.data(), length));
+		} else {
+			throw RelpipeReaderException(L"Error while reading string from the stream.");
+		}
+	}
+
+	string_t toString(const string_t &value) override {
+		return value;
+	}
+
+};
+
+}
+}
+}
\ No newline at end of file