Thu, 04 Jan 2018 21:40:45 +0200
revamped format.h
--- a/src/format.h Thu Jan 04 20:23:21 2018 +0200 +++ b/src/format.h Thu Jan 04 21:40:45 2018 +0200 @@ -20,121 +20,195 @@ #include "basics.h" #include "colors.h" +extern void printToLog(const QString& msg); -// Converts a given value into a string that can be retrieved with text(). -// Used as the argument type to the formatting functions, hence its name. -class StringFormatArg + +inline const QString& toString(const QString& text) { -public: - StringFormatArg(const QString& a) : m_text(a) {} - StringFormatArg(char a) : m_text(a) {} - StringFormatArg(uchar a) : m_text(a) {} - StringFormatArg(QChar a) : m_text(a) {} - StringFormatArg(int a) : m_text(QString::number(a)) {} - StringFormatArg(long a) : m_text(QString::number(a)) {} - StringFormatArg(float a) : m_text(QString::number(a)) {} - StringFormatArg(double a) : m_text(QString::number(a)) {} - StringFormatArg(const Vertex& a) : m_text(a.toString()) {} - StringFormatArg(const Matrix& a) : m_text(a.toString()) {} - StringFormatArg(const char* a) : m_text(a) {} - StringFormatArg(LDColor a) : m_text(a.indexString()) {} + return text; +} + - StringFormatArg(const void* a) - { - m_text.sprintf("%p", a); - } +inline QString toString(char character) +{ + return QString {character}; +} + - template<typename T> - StringFormatArg(const QList<T>& a) - { - m_text = "{"; +inline QString toString(unsigned char character) +{ + return QString {character}; +} - for (const T& it : a) - { - if (&it != &a.first()) - m_text += ", "; - StringFormatArg arg(it); - m_text += arg.text(); - } - - m_text += "}"; - } +inline QString toString(QChar character) +{ + return character; +} - inline QString text() const - { - return m_text; - } -private: - QString m_text; -}; +inline QString toString(int value) +{ + return QString::number(value); +} -// Helper function for format() -template<typename Arg1, typename... Rest> -void formatHelper(QString& str, Arg1 arg1, Rest... rest) +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) { - str = str.arg(StringFormatArg(arg1).text()); - formatHelper(str, rest...); + 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); } -static void formatHelper(QString& str) __attribute__((unused)); -static void formatHelper(QString& str) +inline QString toString(const Vertex& position) +{ + return position.toString(); +} + + +inline QString toString(const Matrix& transformation) +{ + return transformation.toString(); +} + + +inline QString toString(const char* text) { - (void) str; + return text; +} + + +inline QString toString(LDColor color) +{ + return color.indexString(); +} + + +inline QString toString(const void* pointer) +{ + QString result; + result.sprintf("%p", pointer); + return result; } -// Format the message with the given args. -// -// The formatting ultimately uses String's arg() method to actually format the args so the format string should be -// prepared accordingly, with %1 referring to the first arg, %2 to the second, etc. -template<typename... Args> -QString format(QString fmtstr, Args... args) +template<typename T, int N> +QString toString(T(&array)[N]) { - formatHelper(fmtstr, args...); - return fmtstr; + QString rep = "{"; + + for (int i = 0; i < N; ++i) + { + if (i > 0) + rep += ", "; + rep += toString(array[i]); + } + + rep += "}"; + return rep; } -// From messageLog.cc - declared here so that I don't need to include messageLog.h here. -void printToLog(const QString& msg); +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; +} + + +/* + * 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(QString fmtstr, Args... args) +void print(const QString& formatString, Args&&... args) { - formatHelper(fmtstr, args...); - printToLog(fmtstr); + printToLog(format(formatString, args...)); } + template<typename... Args> -void fprint(FILE* fp, QString fmtstr, Args... args) +void fprint(FILE* filePointer, const QString& formatString, Args&&... args) { - formatHelper(fmtstr, args...); - fprintf(fp, "%s", qPrintable(fmtstr)); + fprintf(filePointer, "%s", qPrintable(format(formatString, args...))); } template<typename... Args> -void fprint(QIODevice& dev, QString fmtstr, Args... args) +void fprint(QIODevice& device, const QString& formatString, Args&&... args) { - formatHelper(fmtstr, args...); - dev.write(fmtstr.toUtf8()); + device.write(format(formatString, args...).toUtf8()); } // Exactly like print() except no-op in release builds. +#ifndef RELEASE template<typename... Args> -#ifndef RELEASE -void dprint(QString fmtstr, Args... args) +void dprint(const QString& formatString, Args&&... args) { - formatHelper(fmtstr, args...); - printToLog(fmtstr); + print(formatString, args...); } #else +template<typename... Args> void dprint(QString, Args...) {} #endif
--- a/src/miscallenous.cpp Thu Jan 04 20:23:21 2018 +0200 +++ b/src/miscallenous.cpp Thu Jan 04 21:40:45 2018 +0200 @@ -262,18 +262,6 @@ // ============================================================================= // -QString Join(QList<StringFormatArg> vals, QString delim) -{ - QStringList list; - - for (const StringFormatArg& arg : vals) - list << arg.text(); - - return list.join(delim); -} - -// ============================================================================= -// void RoundToDecimals(double& a, int decimals) { static const long e10[] =
--- a/src/miscallenous.h Thu Jan 04 20:23:21 2018 +0200 +++ b/src/miscallenous.h Thu Jan 04 21:40:45 2018 +0200 @@ -39,7 +39,28 @@ double GetCoordinateOf(const Vertex& a, Axis ax); QString MakePrettyFileSize(qint64 size); -QString Join(QList<StringFormatArg> vals, QString delim = " "); +template<typename Arg1, typename... Rest> +void argumentsToStringsHelper(QStringList& list, Arg1&& arg, Rest&&... rest) +{ + list << toString(arg); + argumentsToStringsHelper(list, rest...); +} + +inline void argumentsToStringsHelper(QStringList&) {} + +template<typename... Args> +QStringList argumentsToStrings(Args&&... args) +{ + QStringList result; + argumentsToStringsHelper(args...); + return result; +} + +template<typename... Args> +QString Join(QString delimeter, Args&&... args) +{ + return argumentsToStrings(args...).join(delimeter); +} // Grid stuff float gridCoordinateSnap();
--- a/src/toolsets/extprogramtoolset.cpp Thu Jan 04 20:23:21 2018 +0200 +++ b/src/toolsets/extprogramtoolset.cpp Thu Jan 04 21:40:45 2018 +0200 @@ -236,11 +236,10 @@ // ============================================================================= // -bool ExtProgramToolset::runExtProgram(ExtProgramType program, QString argvstr) +bool ExtProgramToolset::runExtProgram(ExtProgramType program, QStringList argv) { QString path = getPathSetting(program); QTemporaryFile input; - QStringList argv = argvstr.split(" ", QString::SkipEmptyParts); #ifndef Q_OS_WIN32 if (programUsesWine(program)) @@ -391,16 +390,20 @@ return; // Compose the command-line arguments - QString argv = Join( - { - (axis == X) ? "-x" :(axis == Y) ? "-y" : "-z", - (mode == Distance) ? "-d" :(mode == Symmetry) ? "-s" :(mode == Projection) ? "-p" : "-r", - depth, + QStringList argv { + (axis == X) ? "-x" : + (axis == Y) ? "-y" : + "-z", + (mode == Distance) ? "-d" : + (mode == Symmetry) ? "-s" : + (mode == Projection) ? "-p" : + "-r", + toString(depth), "-a", - condAngle, + toString(condAngle), inDATName, - outDATName - }); + outDATName, + }; writeSelection(inDATName); @@ -435,17 +438,16 @@ return; // Compose arguments - QString argv = Join( - { + QStringList argv = { (not ui.cb_condense->isChecked()) ? "-q" : "", (not ui.cb_subst->isChecked()) ? "-r" : "", (ui.cb_condlineCheck->isChecked()) ? "-a" : "", (ui.cb_colorize->isChecked()) ? "-c" : "", "-t", - ui.dsb_coplthres->value(), + toString(ui.dsb_coplthres->value()), inDATName, outDATName - }); + }; writeSelection(inDATName); @@ -512,29 +514,24 @@ return; } - QString parms = Join( - { + QStringList parms { (ui.cb_colorize->isChecked()) ? "-c" : "", (ui.cb_nocondense->isChecked()) ? "-t" : "", "-s", - ui.dsb_prescale->value() - }); + toString(ui.dsb_prescale->value()), + }; - QString argv_normal = Join( - { - parms, + QStringList argv_normal = parms + QStringList { inDATName, cutDATName, - outDATName - }); + outDATName, + }; - QString argv_inverse = Join( - { - parms, + QStringList argv_inverse = parms + QStringList { cutDATName, inDATName, - outDAT2Name - }); + outDAT2Name, + }; writeColorGroup(inCol, inDATName); writeColorGroup(cutCol, cutDATName); @@ -549,7 +546,7 @@ if (ui.cb_edges->isChecked() and checkExtProgramPath(Isecalc) - and runExtProgram(Isecalc, Join({inDATName, cutDATName, edgesDATName}))) + and runExtProgram(Isecalc, {inDATName, cutDATName, edgesDATName})) { insertOutput(edgesDATName, false, {}); } @@ -599,8 +596,7 @@ return; } - QString argv = Join( - { + QStringList argv = { (ui.cb_oldsweep->isChecked() ? "-s" : ""), (ui.cb_reverse->isChecked() ? "-r" : ""), (ui.dsb_segsplit->value() != 0 ? format("-l %1", ui.dsb_segsplit->value()) : ""), @@ -608,7 +604,7 @@ in1DATName, in2DATName, outDATName - }); + }; writeColorGroup(in1Col, in1DATName); writeColorGroup(in2Col, in2DATName); @@ -665,16 +661,9 @@ return; } - QString argv = Join( - { - in1DATName, - in2DATName, - outDATName - }); - writeColorGroup(in1Col, in1DATName); writeColorGroup(in2Col, in2DATName); - runExtProgram(Isecalc, argv); + runExtProgram(Isecalc, {in1DATName, in2DATName, outDATName}); insertOutput(outDATName, false, {}); } @@ -702,8 +691,7 @@ int unmatched = ui.unmatched->currentIndex(); - QString argv = Join( - { + QStringList argv = { format("-p %1", ui.precision->value()), format("-af %1", ui.flatAngle->value()), format("-ac %1", ui.condAngle->value()), @@ -717,7 +705,7 @@ unmatched == 0 ? "-u+" :(unmatched == 2 ? "-u-" : ""), inName, outName, - }); + }; writeSelection(inName);
--- a/src/toolsets/extprogramtoolset.h Thu Jan 04 20:23:21 2018 +0200 +++ b/src/toolsets/extprogramtoolset.h Thu Jan 04 21:40:45 2018 +0200 @@ -60,7 +60,7 @@ private: bool checkExtProgramPath(ExtProgramType program); bool makeTempFile(class QTemporaryFile& tmp, QString& fname); - bool runExtProgram(ExtProgramType prog, QString argvstr); + bool runExtProgram(ExtProgramType prog, QStringList argv); QString errorCodeString(ExtProgramType program, class QProcess& process); void insertOutput(QString fname, bool replace, QList<LDColor> colorsToReplace); void writeColorGroup(LDColor color, QString fname);