src/types/StringDataTypeWriter.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 25 Aug 2018 17:30:19 +0200
branchv_0
changeset 23 d505eaedc35e
parent 15 8fd6c4d44071
child 29 142bdbba520f
permissions -rw-r--r--
remove format.h and use constants.h from lib-protocol

#pragma once

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <locale>
#include <codecvt>

#include <relpipe/protocol/constants.h>

#include "../DataTypeWriter.h"

namespace relpipe {
namespace writer {
namespace types {

using namespace relpipe::protocol;
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<string_t> {
private:
	IntegerDataTypeWriter integerType;
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
public:

	StringDataTypeWriter() : DataTypeWriter<string_t>(TypeId::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;
	}

};

}
}
}