[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [glob2-devel] Refactoring
From: |
Sébastien |
Subject: |
Re: [glob2-devel] Refactoring |
Date: |
Wed, 3 May 2006 17:17:39 +0200 |
User-agent: |
KMail/1.9.1 |
Le Mercredi 3 Mai 2006 13:46, Stéphane Magnenat a écrit :
> On Wednesday 03 May 2006 13:21, Cyrille Dunant wrote:
> > > > FormatableString("Hello %s %s").arg("to").arg("you")
> > >
> > > I'll take this one ^ since I find it more readable...
> > >
> > > > FormatableString("Hello %s %s")("to")("you")
> >
> > You could have both :)
> >
> > I wonder, on a general note, is having alternative syntaxes available a
> > good or bad thing ?
>
> I would say slightly bad, as it increase the amount of known thing required
> to understand the code.
>
> I prefer the .arg() version, using %0, %1 (not %s but I think Nuage just
> wrote this by mistake) as argument.
>
> Steph
>
>
> _______________________________________________
> glob2-devel mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/glob2-devel
For the moment, I've implemented the following class :
// --- FormatableString.h --- //
#ifndef FORMATABLESTRING_H
#define FORMATABLESTRING_H
#include <string>
/*!
* string that can be used for argument substitution.
* Example :
* FormatableString fs("Hello %0");
* cout << fs.arg("World");
*/
class FormatableString : public std::string
{
private:
/*!
* Next argument to be replaced.
*/
int argLevel;
/*!
* Replace the next argument by replacement.
*/
void proceedReplace(const std::string &replacement);
public:
/*!
* Creates a new FormatableString with format string set to s.
* \param s A string with indicators for argument substitution.
* Each indicator is the % symbol followed by a number. The
number
* is the index of the corresponding argument (starting at %0).
*/
FormatableString(const std::string &s)
: std::string(s), argLevel(0) { }
/*!
* Replace the next arg by an int value.
* \param value Value used to replace the current argument.
* \param fieldWidth min width of the displayed number
* \param base Radix of the number (8, 10 or 16)
* \param fillChar Character used to pad the number to reach
fieldWidth
* \see arg(const T& value)
*/
FormatableString &arg(int value, int fieldWidth = 0, int base =
10, char
fillChar = ' ');
/*!
* Replace the next arg by a float value.
* \param value Value used to replace the current argument.
* \param fieldWidth min width of the displayed number.
* \param precision Number of digits displayed.
* \param fillChar Character used to pad the number to reach
fieldWidth.
* \see arg(const T& value)
*/
FormatableString &arg(float value, int fieldWidth = 0, int
precision = 6,
char fillChar = ' ');
/*!
* Replace the next arg by a value that can be passed to an
ostringstream.
* The first call to arg replace %0, the second %1, and so on.
* \param value Value used to replace the current argument.
*/
template <typename T> FormatableString &arg(const T& value)
{
// transform value into std::string
std::ostringstream oss;
oss << value;
proceedReplace(oss.str());
// return reference to this so that .arg can proceed
further
return *this;
}
};
#endif // FORMATABLESTRING_H //
// --- FormatableString.cpp --- //
#include <cassert>
#include <string>
#include <sstream>
#include <iomanip>
#include "FormatableString.h"
void FormatableString::proceedReplace(const std::string &replacement)
{
std::ostringstream search;
search << "%" << this->argLevel;
std::string::size_type pos = this->find(search.str(), 0);
assert(pos != std::string::npos);
this->replace(pos, search.str().length(), replacement);
++argLevel;
}
FormatableString &FormatableString::arg(int value, int fieldWidth, int base,
char fillChar)
{
std::ostringstream oss;
oss << std::setbase(base);
oss.width(fieldWidth);
oss.fill(fillChar);
// transform value into std::string
oss << value;
proceedReplace(oss.str());
// return reference to this so that .arg can proceed further
return *this;
}
FormatableString &FormatableString::arg(float value, int fieldWidth, int
precision, char fillChar)
{
std::ostringstream oss;
oss.precision(precision);
oss.width(fieldWidth);
oss.fill(fillChar);
oss.setf(oss.fixed, oss.floatfield);
// transform value into std::string
oss << value;
proceedReplace(oss.str());
// return reference to this so that .arg can proceed further
return *this;
}
- Re: [glob2-devel] Refactoring, (continued)
- Re: [glob2-devel] Refactoring, Cyrille Dunant, 2006/05/03
- Re: [glob2-devel] Refactoring, Bradley Arsenault, 2006/05/03
- Re: [glob2-devel] Refactoring, Nuage, 2006/05/03
- Re: [glob2-devel] Refactoring, Stéphane Magnenat, 2006/05/04
- Re: [glob2-devel] Refactoring, Sébastien, 2006/05/06
- Re: [glob2-devel] Refactoring, Stéphane Magnenat, 2006/05/06
- Re: [glob2-devel] Refactoring, Nuage, 2006/05/03
- Re: [glob2-devel] Refactoring, Sébastien, 2006/05/03
- Re: [glob2-devel] Refactoring, Cyrille Dunant, 2006/05/03
- Re: [glob2-devel] Refactoring, Stéphane Magnenat, 2006/05/03
- Re: [glob2-devel] Refactoring,
Sébastien <=
- Re: [glob2-devel] Refactoring, Stéphane Magnenat, 2006/05/03
- Re: [glob2-devel] Refactoring, Sébastien, 2006/05/03
- Re: [glob2-devel] Refactoring, Bradley Arsenault, 2006/05/03
- Re: [glob2-devel] Refactoring, Bradley Arsenault, 2006/05/01
- Re: [glob2-devel] Refactoring, Bradley Arsenault, 2006/05/01
- Re: [glob2-devel] Refactoring, Kai Antweiler, 2006/05/02
- Re: [glob2-devel] Refactoring, Stéphane Magnenat, 2006/05/03
- Re: [glob2-devel] Refactoring, Kai Antweiler, 2006/05/03