src/DataTypeWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Jul 2018 17:19:25 +0200
branchv_0
changeset 16 3613617d3076
parent 15 8fd6c4d44071
child 29 142bdbba520f
permissions -rw-r--r--
writeAttribute() with raw pointer and type_info

#pragma once

#include <functional>
#include <sstream>

#include "DataTypeWriterBase.h"

namespace relpipe {
namespace writer {

template<typename T> class DataTypeWriter : public DataTypeWriterBase {
public:

	DataTypeWriter(const TypeId typeId, const string_t typeCode) : DataTypeWriterBase(typeId, typeCode) {
	}

	virtual ~DataTypeWriter() {
	};

	virtual void writeValue(std::ostream& output, const T& value) = 0;

	void writeRaw(std::ostream& output, const void * value, const std::type_info& type) override {
		if (type == typeid (T)) {
			const T v = *(static_cast<const T*>(value));
			writeValue(output, v);
		} else {
			wstringstream message;
			message << L"Data type in writeRaw() does not match – got: " << type.name() << " but expected: " << typeid (T).name();
			throw RelpipeWriterException(message.str());
		}
	}

	void writeString(std::ostream& output, const string_t &stringValue) override {
		writeValue(output, toValue(stringValue));
	}

	virtual T toValue(const string_t &stringValue) = 0;

};

}
}