src/format.h

Thu, 11 Jan 2018 15:09:44 +0200

author
Santeri Piippo
date
Thu, 11 Jan 2018 15:09:44 +0200
changeset 1231
ce0c9f2e6b9c
parent 1222
34def2630300
permissions
-rw-r--r--

begin rendering rework

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2018 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  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 "basics.h"
#include "colors.h"

extern void printToLog(const QString& msg);


inline const QString& toString(const QString& text)
{
	return text;
}


inline QString toString(char character)
{
	return QString {character};
}


inline QString toString(unsigned char character)
{
	return QString {character};
}


inline QString toString(QChar character)
{
	return character;
}


inline QString toString(int value)
{
	return QString::number(value);
}


inline QString toString(short int value)
{
	return QString::number(value);
}


inline QString toString(long int value)
{
	return QString::number(value);
}


inline QString toString(unsigned int value)
{
	return QString::number(value);
}


inline QString toString(unsigned long int value)
{
	return QString::number(value);
}


inline QString toString(unsigned short int value)
{
	return QString::number(value);
}


inline QString toString(float value)
{
	return QString::number(value);
}


inline QString toString(double value)
{
	return QString::number(value);
}


inline QString toString(const Vertex& position)
{
	return position.toString();
}


inline QString toString(const Matrix& transformation)
{
	return transformation.toString();
}


inline QString toString(const char* text)
{
	return text;
}


inline QString toString(LDColor color)
{
	return color.indexString();
}


inline QString toString(const void* pointer)
{
	QString result;
	result.sprintf("%p", pointer);
	return result;
}


template<typename T, int N>
QString toString(T(&array)[N])
{
	QString rep = "{";

	for (int i = 0; i < N; ++i)
	{
		if (i > 0)
			rep += ", ";
		rep += toString(array[i]);
	}

	rep += "}";
	return rep;
}


template<typename T>
QString toString(const QList<T>& list)
{
	QString result = "{";

	for (const T& element : list)
	{
		if (&element != &list.first())
			result += ", ";

		result += toString(element);
	}

	result += "}";
	return result;
}


inline QString toString(const QSize& size)
{
	return toString(size.width()) + " × " + toString(size.height());
}


/*
 * Formats the message with the given args using QString::arg().
 */
template<typename Arg1, typename... Rest>
QString format(const QString& string, Arg1 arg1, Rest&&... rest)
{
	return format(string.arg(toString(arg1)), rest...);
}


template<typename... Rest>
const QString& format(const QString& string)
{
	return string;
}


// Format and print the given args to the message log.
template<typename... Args>
void print(const QString& formatString, Args&&... args)
{
	printToLog(format(formatString, args...));
}


template<typename... Args>
void fprint(FILE* filePointer, const QString& formatString, Args&&... args)
{
	fprintf(filePointer, "%s", qPrintable(format(formatString, args...)));
}


template<typename... Args>
void fprint(QIODevice& device, const QString& formatString, Args&&... args)
{
	device.write(format(formatString, args...).toUtf8());
}


// Exactly like print() except no-op in release builds.
#ifndef RELEASE
template<typename... Args>
void dprint(const QString& formatString, Args&&... args)
{
	print(formatString, args...);
}
#else
template<typename... Args>
void dprint(QString, Args...) {}
#endif

mercurial