src/DataTypeWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 01 Dec 2018 15:35:05 +0100
branchv_0
changeset 30 3f18cb7a6963
parent 29 142bdbba520f
child 41 744b61559eb2
permissions -rw-r--r--
license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
     1
/**
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     2
 * Relational pipes (library)
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
     4
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
30
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     6
 * it under the terms of the:
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     7
 *  - GNU Lesser General Public License as published by the Free Software Foundation;
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     8
 *    either version 3 of the License, or (at your option) any later version;
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
     9
 *    or (at your option)
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    10
 *  - GNU General Public License as published by the Free Software Foundation;
3f18cb7a6963 license of protocol, reader and writer: GNU LGPLv3+ or GNU GPLv2+
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    11
 *    either version 2 of the License, or (at your option) any later version.
29
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    12
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    13
 * This program is distributed in the hope that it will be useful,
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    16
 * GNU General Public License for more details.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    17
 *
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    18
 * You should have received a copy of the GNU General Public License
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
142bdbba520f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    20
 */
1
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#pragma once
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    23
#include <functional>
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    24
#include <sstream>
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    25
1
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "DataTypeWriterBase.h"
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace writer {
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
template<typename T> class DataTypeWriter : public DataTypeWriterBase {
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
public:
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
15
8fd6c4d44071 use more TypeId enum
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
	DataTypeWriter(const TypeId typeId, const string_t typeCode) : DataTypeWriterBase(typeId, typeCode) {
1
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	}
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	virtual ~DataTypeWriter() {
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	};
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	virtual void writeValue(std::ostream& output, const T& value) = 0;
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
16
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    42
	void writeRaw(std::ostream& output, const void * value, const std::type_info& type) override {
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    43
		if (type == typeid (T)) {
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    44
			const T v = *(static_cast<const T*>(value));
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    45
			writeValue(output, v);
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    46
		} else {
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    47
			wstringstream message;
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    48
			message << L"Data type in writeRaw() does not match – got: " << type.name() << " but expected: " << typeid (T).name();
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    49
			throw RelpipeWriterException(message.str());
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    50
		}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    51
	}
3613617d3076 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    52
9
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    53
	void writeString(std::ostream& output, const string_t &stringValue) override {
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    54
		writeValue(output, toValue(stringValue));
0a40752e401d shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    55
	}
1
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	virtual T toValue(const string_t &stringValue) = 0;
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
};
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
}
6e3494943c91 base classes
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
}