Plumbing: Use variadic templates for format()

- Use variadic templates, with a slight twist for properly supporting
  Taskwarrior's l10n strings.
- Adjust code that relied on implicit type conversion which led to
  problems with template instantiation.
- Move some functionality out of format() in order to keep it generic.
This commit is contained in:
Wilhelm Schuermann 2015-06-07 20:36:21 +02:00
parent f0fbdaf2c9
commit e95167a984
6 changed files with 73 additions and 218 deletions

View file

@ -20,6 +20,8 @@ Babej).
- Removed obsolete script 'context'. - Removed obsolete script 'context'.
- Fix "project" verbosity info not showing without "footnote" being manually - Fix "project" verbosity info not showing without "footnote" being manually
enabled. enabled.
- Internal plumbing: Use variadic templates for format(), reducing code bloat
and enabling more flexible use of the function.
------ current release --------------------------- ------ current release ---------------------------

View file

@ -58,7 +58,7 @@ TF2::TF2 ()
TF2::~TF2 () TF2::~TF2 ()
{ {
if (_dirty && context.verbose ("debug")) if (_dirty && context.verbose ("debug"))
std::cout << format (STRING_TDB2_DIRTY_EXIT, _file) std::cout << format (STRING_TDB2_DIRTY_EXIT, std::string (_file))
<< "\n"; << "\n";
} }

View file

@ -28,6 +28,7 @@
#include <sstream> #include <sstream>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <string>
#ifdef PRODUCT_TASKWARRIOR #ifdef PRODUCT_TASKWARRIOR
#include <math.h> #include <math.h>
#include <cfloat> #include <cfloat>
@ -1961,7 +1962,7 @@ void Task::modify (modType type, bool text_required /* = false */)
// If v is duration, add 'now' to it, else store as date. // If v is duration, add 'now' to it, else store as date.
if (v.type () == Variant::type_duration) if (v.type () == Variant::type_duration)
{ {
context.debug (label + name + " <-- '" + format ("{1}", v.get_duration ()) + "' <-- '" + (std::string) v + "' <-- '" + value + "'"); context.debug (label + name + " <-- '" + format ("{1}", std::to_string (v.get_duration ())) + "' <-- '" + (std::string) v + "' <-- '" + value + "'");
Variant now; Variant now;
if (namedDates ("now", now)) if (namedDates ("now", now))
v += now; v += now;

View file

@ -67,14 +67,14 @@ int CmdUrgency::execute (std::string& output)
if (task.id) if (task.id)
{ {
out << format (STRING_CMD_URGENCY_RESULT, out << format (STRING_CMD_URGENCY_RESULT,
task.id, task.urgency ()) task.id, trim (format (task.urgency (), 6, 3)))
<< "\n"; << "\n";
} }
else else
{ {
out << format (STRING_CMD_URGENCY_RESULT, out << format (STRING_CMD_URGENCY_RESULT,
task.get ("uuid"), task.get ("uuid"),
task.urgency ()) trim (format (task.urgency (), 6, 3)))
<< "\n"; << "\n";
} }
} }

View file

@ -45,8 +45,6 @@ extern Context context;
static const char* newline = "\n"; static const char* newline = "\n";
static const char* noline = ""; static const char* noline = "";
static void replace_positional (std::string&, const std::string&, const std::string&);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void wrapText ( void wrapText (
std::vector <std::string>& lines, std::vector <std::string>& lines,
@ -675,6 +673,19 @@ const std::string obfuscateText (const std::string& input)
return output.str (); return output.str ();
} }
////////////////////////////////////////////////////////////////////////////////
const std::string format (std::string& value)
{
return value;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (const char* value)
{
std::string s (value);
return s;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
const std::string format (char value) const std::string format (char value)
{ {
@ -691,6 +702,30 @@ const std::string format (int value)
return s.str (); return s.str ();
} }
////////////////////////////////////////////////////////////////////////////////
const std::string format (unsigned int value)
{
std::stringstream s;
s << value;
return s.str ();
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (unsigned long value)
{
std::stringstream s;
s << value;
return s.str ();
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (long value)
{
std::stringstream s;
s << value;
return s.str ();
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
const std::string formatHex (int value) const std::string formatHex (int value)
{ {
@ -747,7 +782,7 @@ const std::string format (double value)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static void replace_positional ( void replace_positional (
std::string& fmt, std::string& fmt,
const std::string& from, const std::string& from,
const std::string& to) const std::string& to)
@ -760,202 +795,6 @@ static void replace_positional (
} }
} }
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
const std::string& arg1)
{
std::string output = fmt;
replace_positional (output, "{1}", arg1);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
const std::string& arg1,
const std::string& arg2)
{
std::string output = fmt;
replace_positional (output, "{1}", arg1);
replace_positional (output, "{2}", arg2);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
const std::string& arg1,
int arg2)
{
std::string output = fmt;
replace_positional (output, "{1}", arg1);
replace_positional (output, "{2}", format (arg2));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
const std::string& arg1,
double arg2)
{
std::string output = fmt;
replace_positional (output, "{1}", arg1);
replace_positional (output, "{2}", trim (format (arg2, 6, 3)));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
int arg2,
const std::string& arg3)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", format (arg2));
replace_positional (output, "{3}", arg3);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
const std::string& arg2)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", arg2);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
const std::string& arg2,
const std::string& arg3)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", arg2);
replace_positional (output, "{3}", arg3);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
const std::string& arg2,
const std::string& arg3,
const std::string& arg4)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", arg2);
replace_positional (output, "{3}", arg3);
replace_positional (output, "{4}", arg4);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
int arg2)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", format (arg2));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
int arg2,
int arg3)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", format (arg2));
replace_positional (output, "{3}", format (arg3));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
int arg2,
int arg3,
int arg4)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", format (arg2));
replace_positional (output, "{3}", format (arg3));
replace_positional (output, "{4}", format (arg4));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
int arg1,
double arg2)
{
std::string output = fmt;
replace_positional (output, "{1}", format (arg1));
replace_positional (output, "{2}", trim (format (arg2, 6, 3)));
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
const std::string& arg1,
const std::string& arg2,
const std::string& arg3)
{
std::string output = fmt;
replace_positional (output, "{1}", arg1);
replace_positional (output, "{2}", arg2);
replace_positional (output, "{3}", arg3);
return output;
}
////////////////////////////////////////////////////////////////////////////////
const std::string format (
const std::string& fmt,
const std::string& arg1,
const std::string& arg2,
const std::string& arg3,
const std::string& arg4)
{
std::string output = fmt;
replace_positional (output, "{1}", arg1);
replace_positional (output, "{2}", arg2);
replace_positional (output, "{3}", arg3);
replace_positional (output, "{4}", arg4);
return output;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string leftJustify (const int input, const int width) std::string leftJustify (const int input, const int width)
{ {

View file

@ -28,6 +28,7 @@
#define INCLUDED_TEXT #define INCLUDED_TEXT
#include <set> #include <set>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -58,27 +59,39 @@ std::string::size_type find (const std::string&, const std::string&, bool sensit
std::string::size_type find (const std::string&, const std::string&, std::string::size_type, bool sensitive = true); std::string::size_type find (const std::string&, const std::string&, std::string::size_type, bool sensitive = true);
int strippedLength (const std::string&); int strippedLength (const std::string&);
const std::string obfuscateText (const std::string&); const std::string obfuscateText (const std::string&);
const std::string format (std::string&);
const std::string format (const char*);
const std::string format (char); const std::string format (char);
const std::string format (int); const std::string format (int);
const std::string format (unsigned int);
const std::string format (long);
const std::string format (unsigned long);
const std::string formatHex (int); const std::string formatHex (int);
const std::string format (float, int, int); const std::string format (float, int, int);
const std::string format (double, int, int); const std::string format (double, int, int);
const std::string format (double); const std::string format (double);
const std::string format (const std::string&, const std::string&); void replace_positional (std::string&, const std::string&, const std::string&);
const std::string format (const std::string&, int);
const std::string format (const std::string&, const std::string&, const std::string&); template<typename T>
const std::string format (const std::string&, const std::string&, int); const std::string format (int fmt_num, const std::string& fmt, T arg)
const std::string format (const std::string&, const std::string&, double); {
const std::string format (const std::string&, int, int, const std::string&); std::string output = fmt;
const std::string format (const std::string&, int, const std::string&); replace_positional (output, "{" + std::to_string(fmt_num) + "}", format (arg));
const std::string format (const std::string&, int, const std::string&, const std::string&); return output;
const std::string format (const std::string&, int, const std::string&, const std::string&, const std::string&); }
const std::string format (const std::string&, int, int);
const std::string format (const std::string&, int, int, int); template<typename T, typename... Args>
const std::string format (const std::string&, int, int, int, int); const std::string format (int fmt_num, const std::string& fmt, T arg, Args... args)
const std::string format (const std::string&, int, double); {
const std::string format (const std::string&, const std::string&, const std::string&, const std::string&); const std::string fmt_replaced (format (fmt_num, fmt, arg));
const std::string format (const std::string&, const std::string&, const std::string&, const std::string&, const std::string&); return format (fmt_num+1, fmt_replaced, args...);
}
template<typename... Args>
const std::string format (const std::string& fmt, Args... args)
{
return format (1, fmt, args...);
}
std::string leftJustify (const int, const int); std::string leftJustify (const int, const int);
std::string leftJustify (const std::string&, const int); std::string leftJustify (const std::string&, const int);