src/DataTypeWriterBase.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:47:14 +0200
branchv_0
changeset 41 744b61559eb2
parent 30 3f18cb7a6963
permissions -rw-r--r--
fix license version: GNU LGPLv3 or GPLv2

/**
 * Relational pipes (library)
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the:
 *  - GNU Lesser General Public License as published by the Free Software Foundation;
 *    version 3 of the License or (at your option)
 *  - GNU General Public License as published by the Free Software Foundation;
 *    version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <string>
#include "../include/relpipe/writer/TypeId.h"
#include "../include/relpipe/writer/typedefs.h"

namespace relpipe {
namespace writer {

/**
 * This class contains common features that are independent from particular data type (generic/template type)
 */
class DataTypeWriterBase {
private:
	const TypeId typeId;
	const string_t typeCode;
public:

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

	virtual ~DataTypeWriterBase() {
	};

	/**
	 * @param output output stream, should be at position where the value is to be written; the stream will not be closed not flushed after writing
	 * @param stringValue write value as given data type (e.g. integer or boolean); stringValue parameter contains given value in string representation of given data type
	 * E.g. integer 123 is passed as a character string "123"
	 * boolean true is passed as a character string "true".
	 * See Relational pipes format specification for details.
	 */
	virtual void writeString(std::ostream& output, const string_t &stringValue) = 0;

	/**
	 * @param output output stream, should be at position where the value is to be written; the stream will not be closed not flushed after writing
	 * @param value raw pointer to the value, must exactly match data type of this writer
	 * @param type used as a safety mechanism to avoid wrong pointer interpretation;
	 * should be called in this way: writeRaw(output, &value, typeid(value));
	 * if the type does not match, the RelpipeWriterException is thrown
	 * 
	 * TODO: typeid() / type_info seems working but consider also sizeof() / size_t and teplates
	 */
	virtual void writeRaw(std::ostream& output, const void * value, const std::type_info& type) = 0;

	/**
	 * @param dataType data type code as defined in DDP L0
	 * @return whether this class supports conversions of this type
	 */
	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) {
		return dataType == typeCode;
	}

	TypeId getTypeId() {
		return typeId;
	}

	string_t getTypeCode() {
		return typeCode;
	}
};

}
}